1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ 6 #define CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 #include <vector> 12 13 #include "base/basictypes.h" 14 #include "base/compiler_specific.h" 15 #include "base/gtest_prod_util.h" 16 #include "base/memory/ref_counted.h" 17 #include "base/memory/scoped_ptr.h" 18 #include "base/memory/weak_ptr.h" 19 #include "base/observer_list.h" 20 #include "chrome/browser/history/history_types.h" 21 #include "chrome/browser/ui/search/instant_ntp_prerenderer.h" 22 #include "chrome/common/instant_types.h" 23 #include "components/browser_context_keyed_service/browser_context_keyed_service.h" 24 #include "content/public/browser/notification_observer.h" 25 #include "content/public/browser/notification_registrar.h" 26 27 class GURL; 28 class InstantExtendedTest; 29 class InstantIOContext; 30 class InstantServiceObserver; 31 class InstantTestBase; 32 class Profile; 33 class ThemeService; 34 35 namespace content { 36 class WebContents; 37 } 38 39 namespace net { 40 class URLRequest; 41 } 42 43 // Tracks render process host IDs that are associated with Instant. 44 class InstantService : public BrowserContextKeyedService, 45 public content::NotificationObserver { 46 public: 47 explicit InstantService(Profile* profile); 48 virtual ~InstantService(); 49 50 // Add, remove, and query RenderProcessHost IDs that are associated with 51 // Instant processes. 52 void AddInstantProcess(int process_id); 53 bool IsInstantProcess(int process_id) const; 54 55 // Adds/Removes InstantService observers. 56 void AddObserver(InstantServiceObserver* observer); 57 void RemoveObserver(InstantServiceObserver* observer); 58 59 #if defined(UNIT_TEST) 60 int GetInstantProcessCount() const { 61 return process_ids_.size(); 62 } 63 #endif 64 65 // Most visited item API. 66 67 // Invoked by the InstantController when the Instant page wants to delete a 68 // Most Visited item. 69 void DeleteMostVisitedItem(const GURL& url); 70 71 // Invoked by the InstantController when the Instant page wants to undo the 72 // blacklist action. 73 void UndoMostVisitedDeletion(const GURL& url); 74 75 // Invoked by the InstantController when the Instant page wants to undo all 76 // Most Visited deletions. 77 void UndoAllMostVisitedDeletions(); 78 79 // Invoked by the InstantController to update theme information for NTP. 80 // 81 // TODO(kmadhusu): Invoking this from InstantController shouldn't be 82 // necessary. Investigate more and remove this from here. 83 void UpdateThemeInfo(); 84 85 // Invoked by the InstantController to update most visited items details for 86 // NTP. 87 void UpdateMostVisitedItemsInfo(); 88 89 // Forwards the request to InstantNTPPrerenderer to release and return the 90 // preloaded InstantNTP WebContents. May be NULL. InstantNTPPrerenderer will 91 // load a new InstantNTP after releasing the preloaded contents. 92 scoped_ptr<content::WebContents> ReleaseNTPContents() WARN_UNUSED_RESULT; 93 94 // The NTP WebContents. May be NULL. InstantNTPPrerenderer retains ownership. 95 content::WebContents* GetNTPContents() const; 96 97 // Notifies InstantService about the creation of a BrowserInstantController 98 // object. Used to preload InstantNTP. 99 void OnBrowserInstantControllerCreated(); 100 101 // Notifies InstantService about the destruction of a BrowserInstantController 102 // object. Used to destroy the preloaded InstantNTP. 103 void OnBrowserInstantControllerDestroyed(); 104 105 private: 106 friend class InstantExtendedTest; 107 friend class InstantTestBase; 108 109 FRIEND_TEST_ALL_PREFIXES(InstantExtendedNetworkTest, 110 NTPReactsToNetworkChanges); 111 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest, 112 MANUAL_ShowsGoogleNTP); 113 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest, 114 MANUAL_SearchesFromFakebox); 115 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation); 116 117 // Overridden from BrowserContextKeyedService: 118 virtual void Shutdown() OVERRIDE; 119 120 // Overridden from content::NotificationObserver: 121 virtual void Observe(int type, 122 const content::NotificationSource& source, 123 const content::NotificationDetails& details) OVERRIDE; 124 125 // Called when we get new most visited items from TopSites, registered as an 126 // async callback. Parses them and sends them to the renderer via 127 // SendMostVisitedItems. 128 void OnMostVisitedItemsReceived(const history::MostVisitedURLList& data); 129 130 // Notifies the observer about the last known most visited items. 131 void NotifyAboutMostVisitedItems(); 132 133 // Theme changed notification handler. 134 void OnThemeChanged(ThemeService* theme_service); 135 136 // Used by tests. 137 InstantNTPPrerenderer* ntp_prerenderer(); 138 139 Profile* const profile_; 140 141 // The process ids associated with Instant processes. 142 std::set<int> process_ids_; 143 144 // InstantMostVisitedItems sent to the Instant Pages. 145 std::vector<InstantMostVisitedItem> most_visited_items_; 146 147 // Theme-related data for NTP overlay to adopt themes. 148 scoped_ptr<ThemeBackgroundInfo> theme_info_; 149 150 ObserverList<InstantServiceObserver> observers_; 151 152 content::NotificationRegistrar registrar_; 153 154 scoped_refptr<InstantIOContext> instant_io_context_; 155 156 InstantNTPPrerenderer ntp_prerenderer_; 157 158 // Total number of BrowserInstantController objects (does not include objects 159 // created for OTR browser windows). Used to preload and delete InstantNTP. 160 size_t browser_instant_controller_object_count_; 161 162 // Used for Top Sites async retrieval. 163 base::WeakPtrFactory<InstantService> weak_ptr_factory_; 164 165 DISALLOW_COPY_AND_ASSIGN(InstantService); 166 }; 167 168 #endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ 169