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 <set> 9 #include <vector> 10 11 #include "base/gtest_prod_util.h" 12 #include "base/memory/weak_ptr.h" 13 #include "base/observer_list.h" 14 #include "chrome/browser/history/history_types.h" 15 #include "chrome/browser/search_engines/template_url_service_observer.h" 16 #include "components/keyed_service/core/keyed_service.h" 17 #include "content/public/browser/notification_observer.h" 18 #include "content/public/browser/notification_registrar.h" 19 #include "url/gurl.h" 20 21 class InstantIOContext; 22 struct InstantMostVisitedItem; 23 class InstantSearchPrerenderer; 24 class InstantServiceObserver; 25 class Profile; 26 struct TemplateURLData; 27 class TemplateURLService; 28 struct ThemeBackgroundInfo; 29 class ThemeService; 30 31 namespace content { 32 class RenderProcessHost; 33 } 34 35 // Tracks render process host IDs that are associated with Instant. 36 class InstantService : public KeyedService, 37 public content::NotificationObserver, 38 public TemplateURLServiceObserver { 39 public: 40 explicit InstantService(Profile* profile); 41 virtual ~InstantService(); 42 43 // Add, remove, and query RenderProcessHost IDs that are associated with 44 // Instant processes. 45 void AddInstantProcess(int process_id); 46 bool IsInstantProcess(int process_id) const; 47 48 // Adds/Removes InstantService observers. 49 void AddObserver(InstantServiceObserver* observer); 50 void RemoveObserver(InstantServiceObserver* observer); 51 52 #if defined(UNIT_TEST) 53 int GetInstantProcessCount() const { 54 return process_ids_.size(); 55 } 56 #endif 57 58 // Most visited item API. 59 60 // Invoked by the InstantController when the Instant page wants to delete a 61 // Most Visited item. 62 void DeleteMostVisitedItem(const GURL& url); 63 64 // Invoked by the InstantController when the Instant page wants to undo the 65 // blacklist action. 66 void UndoMostVisitedDeletion(const GURL& url); 67 68 // Invoked by the InstantController when the Instant page wants to undo all 69 // Most Visited deletions. 70 void UndoAllMostVisitedDeletions(); 71 72 // Invoked by the InstantController to update theme information for NTP. 73 // 74 // TODO(kmadhusu): Invoking this from InstantController shouldn't be 75 // necessary. Investigate more and remove this from here. 76 void UpdateThemeInfo(); 77 78 // Invoked by the InstantController to update most visited items details for 79 // NTP. 80 void UpdateMostVisitedItemsInfo(); 81 82 // Sends the current set of search URLs to a renderer process. 83 void SendSearchURLsToRenderer(content::RenderProcessHost* rph); 84 85 // Invoked to notify the Instant page that the omnibox start margin has 86 // changed. 87 void OnOmniboxStartMarginChanged(int start_margin); 88 89 InstantSearchPrerenderer* instant_search_prerenderer() { 90 return instant_prerenderer_.get(); 91 } 92 93 int omnibox_start_margin() const { return omnibox_start_margin_; } 94 95 private: 96 friend class InstantExtendedTest; 97 friend class InstantServiceTest; 98 friend class InstantTestBase; 99 friend class InstantUnitTestBase; 100 101 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest, 102 MANUAL_SearchesFromFakebox); 103 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation); 104 FRIEND_TEST_ALL_PREFIXES(InstantServiceEnabledTest, 105 SendsSearchURLsToRenderer); 106 107 // KeyedService: 108 virtual void Shutdown() OVERRIDE; 109 110 // content::NotificationObserver: 111 virtual void Observe(int type, 112 const content::NotificationSource& source, 113 const content::NotificationDetails& details) OVERRIDE; 114 115 // TemplateURLServiceObserver: 116 // Caches the previous value of the Default Search Provider and the Google 117 // base URL to filter out changes other than those affecting the Default 118 // Search Provider. 119 virtual void OnTemplateURLServiceChanged() OVERRIDE; 120 121 // Called when a renderer process is terminated. 122 void OnRendererProcessTerminated(int process_id); 123 124 // Called when we get new most visited items from TopSites, registered as an 125 // async callback. Parses them and sends them to the renderer via 126 // SendMostVisitedItems. 127 void OnMostVisitedItemsReceived(const history::MostVisitedURLList& data); 128 129 // Notifies the observer about the last known most visited items. 130 void NotifyAboutMostVisitedItems(); 131 132 // Theme changed notification handler. 133 void OnThemeChanged(ThemeService* theme_service); 134 135 void ResetInstantSearchPrerenderer(); 136 137 Profile* const profile_; 138 139 // The TemplateURLService that we are observing. It will outlive this 140 // InstantService due to the dependency declared in InstantServiceFactory. 141 TemplateURLService* template_url_service_; 142 143 // The process ids associated with Instant processes. 144 std::set<int> process_ids_; 145 146 // InstantMostVisitedItems sent to the Instant Pages. 147 std::vector<InstantMostVisitedItem> most_visited_items_; 148 149 // Theme-related data for NTP overlay to adopt themes. 150 scoped_ptr<ThemeBackgroundInfo> theme_info_; 151 152 // The start-edge margin of the omnibox, used by the Instant page to align 153 // text or assets properly with the omnibox. 154 int omnibox_start_margin_; 155 156 ObserverList<InstantServiceObserver> observers_; 157 158 content::NotificationRegistrar registrar_; 159 160 scoped_refptr<InstantIOContext> instant_io_context_; 161 162 // Set to NULL if the default search provider does not support Instant. 163 scoped_ptr<InstantSearchPrerenderer> instant_prerenderer_; 164 165 // Used for Top Sites async retrieval. 166 base::WeakPtrFactory<InstantService> weak_ptr_factory_; 167 168 // Used to check whether notifications from TemplateURLService indicate a 169 // change that affects the default search provider. 170 scoped_ptr<TemplateURLData> previous_default_search_provider_; 171 GURL previous_google_base_url_; 172 173 DISALLOW_COPY_AND_ASSIGN(InstantService); 174 }; 175 176 #endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ 177