[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 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 | |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |
| 6 | #define CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 7 | |
| 8 | #include <set> |
[email protected] | ab01dd75 | 2013-06-08 05:13:36 | [diff] [blame] | 9 | #include <vector> |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 10 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 11 | #include "base/gtest_prod_util.h" |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 13 | #include "base/observer_list.h" |
sdefresne | bc766ef | 2014-09-25 09:28:13 | [diff] [blame] | 14 | #include "components/history/core/browser/history_types.h" |
sdefresne | edf9e01f | 2015-01-13 19:45:41 | [diff] [blame] | 15 | #include "components/history/core/browser/top_sites_observer.h" |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame] | 16 | #include "components/keyed_service/core/keyed_service.h" |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 17 | #include "components/search_engines/template_url_service_observer.h" |
fserb | a485e91 | 2015-08-05 20:26:08 | [diff] [blame^] | 18 | #include "components/suggestions/proto/suggestions.pb.h" |
| 19 | #include "components/suggestions/suggestions_service.h" |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 20 | #include "content/public/browser/notification_observer.h" |
| 21 | #include "content/public/browser/notification_registrar.h" |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 22 | #include "url/gurl.h" |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 23 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 24 | class InstantIOContext; |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 25 | struct InstantMostVisitedItem; |
| 26 | class InstantSearchPrerenderer; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 27 | class InstantServiceObserver; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 28 | class Profile; |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 29 | struct TemplateURLData; |
| 30 | class TemplateURLService; |
| 31 | struct ThemeBackgroundInfo; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 32 | class ThemeService; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 33 | |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 34 | namespace content { |
[email protected] | 77759005 | 2014-01-17 22:11:54 | [diff] [blame] | 35 | class RenderProcessHost; |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 36 | } |
| 37 | |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 38 | // Tracks render process host IDs that are associated with Instant. |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame] | 39 | class InstantService : public KeyedService, |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 40 | public content::NotificationObserver, |
sdefresne | edf9e01f | 2015-01-13 19:45:41 | [diff] [blame] | 41 | public TemplateURLServiceObserver, |
| 42 | public history::TopSitesObserver { |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 43 | public: |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 44 | explicit InstantService(Profile* profile); |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 45 | ~InstantService() override; |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 46 | |
| 47 | // Add, remove, and query RenderProcessHost IDs that are associated with |
| 48 | // Instant processes. |
| 49 | void AddInstantProcess(int process_id); |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 50 | bool IsInstantProcess(int process_id) const; |
| 51 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 52 | // Adds/Removes InstantService observers. |
| 53 | void AddObserver(InstantServiceObserver* observer); |
| 54 | void RemoveObserver(InstantServiceObserver* observer); |
| 55 | |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 56 | #if defined(UNIT_TEST) |
| 57 | int GetInstantProcessCount() const { |
| 58 | return process_ids_.size(); |
| 59 | } |
| 60 | #endif |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 61 | |
[email protected] | 8b169b4b | 2013-03-23 18:24:46 | [diff] [blame] | 62 | // Most visited item API. |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 63 | |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 64 | // Invoked by the InstantController when the Instant page wants to delete a |
| 65 | // Most Visited item. |
| 66 | void DeleteMostVisitedItem(const GURL& url); |
| 67 | |
| 68 | // Invoked by the InstantController when the Instant page wants to undo the |
| 69 | // blacklist action. |
| 70 | void UndoMostVisitedDeletion(const GURL& url); |
| 71 | |
| 72 | // Invoked by the InstantController when the Instant page wants to undo all |
| 73 | // Most Visited deletions. |
| 74 | void UndoAllMostVisitedDeletions(); |
| 75 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 76 | // Invoked by the InstantController to update theme information for NTP. |
| 77 | // |
| 78 | // TODO(kmadhusu): Invoking this from InstantController shouldn't be |
| 79 | // necessary. Investigate more and remove this from here. |
| 80 | void UpdateThemeInfo(); |
| 81 | |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 82 | // Invoked by the InstantController to update most visited items details for |
| 83 | // NTP. |
| 84 | void UpdateMostVisitedItemsInfo(); |
| 85 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 86 | // Sends the current set of search URLs to a renderer process. |
| 87 | void SendSearchURLsToRenderer(content::RenderProcessHost* rph); |
| 88 | |
[email protected] | 553c7af | 2014-02-06 17:58:58 | [diff] [blame] | 89 | // Invoked to notify the Instant page that the omnibox start margin has |
| 90 | // changed. |
| 91 | void OnOmniboxStartMarginChanged(int start_margin); |
| 92 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 93 | InstantSearchPrerenderer* instant_search_prerenderer() { |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 94 | return instant_prerenderer_.get(); |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | 553c7af | 2014-02-06 17:58:58 | [diff] [blame] | 97 | int omnibox_start_margin() const { return omnibox_start_margin_; } |
| 98 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 99 | private: |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 100 | friend class InstantExtendedTest; |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 101 | friend class InstantServiceTest; |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 102 | friend class InstantTestBase; |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 103 | friend class InstantUnitTestBase; |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 104 | |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 105 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest, |
| 106 | MANUAL_SearchesFromFakebox); |
[email protected] | df113a1 | 2013-07-24 04:56:08 | [diff] [blame] | 107 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation); |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 108 | FRIEND_TEST_ALL_PREFIXES(InstantServiceEnabledTest, |
| 109 | SendsSearchURLsToRenderer); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 110 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 111 | // KeyedService: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 112 | void Shutdown() override; |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 113 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 114 | // content::NotificationObserver: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 115 | void Observe(int type, |
| 116 | const content::NotificationSource& source, |
| 117 | const content::NotificationDetails& details) override; |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 118 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 119 | // TemplateURLServiceObserver: |
| 120 | // Caches the previous value of the Default Search Provider and the Google |
| 121 | // base URL to filter out changes other than those affecting the Default |
| 122 | // Search Provider. |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 123 | void OnTemplateURLServiceChanged() override; |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 124 | |
sdefresne | edf9e01f | 2015-01-13 19:45:41 | [diff] [blame] | 125 | // TopSitesObserver: |
| 126 | void TopSitesLoaded(history::TopSites* top_sites) override; |
fserb | db57511 | 2015-06-29 21:31:59 | [diff] [blame] | 127 | void TopSitesChanged(history::TopSites* top_sites, |
| 128 | ChangeReason change_reason) override; |
sdefresne | edf9e01f | 2015-01-13 19:45:41 | [diff] [blame] | 129 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 130 | // Called when a renderer process is terminated. |
| 131 | void OnRendererProcessTerminated(int process_id); |
| 132 | |
fserb | a485e91 | 2015-08-05 20:26:08 | [diff] [blame^] | 133 | // Called when SuggestionsService has a new suggestions profile available. |
| 134 | void OnSuggestionsAvailable(const suggestions::SuggestionsProfile& profile); |
| 135 | |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 136 | // Called when we get new most visited items from TopSites, registered as an |
| 137 | // async callback. Parses them and sends them to the renderer via |
| 138 | // SendMostVisitedItems. |
| 139 | void OnMostVisitedItemsReceived(const history::MostVisitedURLList& data); |
| 140 | |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 141 | // Notifies the observer about the last known most visited items. |
| 142 | void NotifyAboutMostVisitedItems(); |
| 143 | |
pcc | 3a725956 | 2015-04-15 22:21:07 | [diff] [blame] | 144 | #if defined(ENABLE_THEMES) |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 145 | // Theme changed notification handler. |
| 146 | void OnThemeChanged(ThemeService* theme_service); |
pcc | 3a725956 | 2015-04-15 22:21:07 | [diff] [blame] | 147 | #endif |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 148 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 149 | void ResetInstantSearchPrerenderer(); |
| 150 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 151 | Profile* const profile_; |
| 152 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 153 | // The TemplateURLService that we are observing. It will outlive this |
| 154 | // InstantService due to the dependency declared in InstantServiceFactory. |
| 155 | TemplateURLService* template_url_service_; |
| 156 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 157 | // The process ids associated with Instant processes. |
| 158 | std::set<int> process_ids_; |
| 159 | |
fserb | a485e91 | 2015-08-05 20:26:08 | [diff] [blame^] | 160 | // InstantMostVisitedItems from TopSites. |
[email protected] | ab01dd75 | 2013-06-08 05:13:36 | [diff] [blame] | 161 | std::vector<InstantMostVisitedItem> most_visited_items_; |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 162 | |
fserb | a485e91 | 2015-08-05 20:26:08 | [diff] [blame^] | 163 | // InstantMostVisitedItems from SuggestionService. |
| 164 | std::vector<InstantMostVisitedItem> suggestions_items_; |
| 165 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 166 | // Theme-related data for NTP overlay to adopt themes. |
| 167 | scoped_ptr<ThemeBackgroundInfo> theme_info_; |
| 168 | |
[email protected] | 553c7af | 2014-02-06 17:58:58 | [diff] [blame] | 169 | // The start-edge margin of the omnibox, used by the Instant page to align |
| 170 | // text or assets properly with the omnibox. |
| 171 | int omnibox_start_margin_; |
| 172 | |
brettw | d195c95 | 2015-06-02 17:31:12 | [diff] [blame] | 173 | base::ObserverList<InstantServiceObserver> observers_; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 174 | |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 175 | content::NotificationRegistrar registrar_; |
| 176 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 177 | scoped_refptr<InstantIOContext> instant_io_context_; |
| 178 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 179 | // Set to NULL if the default search provider does not support Instant. |
| 180 | scoped_ptr<InstantSearchPrerenderer> instant_prerenderer_; |
| 181 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 182 | // Used to check whether notifications from TemplateURLService indicate a |
| 183 | // change that affects the default search provider. |
| 184 | scoped_ptr<TemplateURLData> previous_default_search_provider_; |
| 185 | GURL previous_google_base_url_; |
| 186 | |
fserb | a485e91 | 2015-08-05 20:26:08 | [diff] [blame^] | 187 | // Suggestions Service to fetch server suggestions. |
| 188 | suggestions::SuggestionsService* suggestions_service_; |
| 189 | |
mohan.reddy | 7459536 | 2014-09-23 20:35:34 | [diff] [blame] | 190 | // Used for Top Sites async retrieval. |
| 191 | base::WeakPtrFactory<InstantService> weak_ptr_factory_; |
| 192 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 193 | DISALLOW_COPY_AND_ASSIGN(InstantService); |
| 194 | }; |
| 195 | |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 196 | #endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |