[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 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 8 | #include <memory> |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 9 | #include <set> |
[email protected] | ab01dd75 | 2013-06-08 05:13:36 | [diff] [blame] | 10 | #include <vector> |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 11 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 13 | #include "base/macros.h" |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 16 | #include "base/observer_list.h" |
brettw | 9b0866f | 2016-12-11 02:34:06 | [diff] [blame^] | 17 | #include "build/build_config.h" |
sdefresne | bc766ef | 2014-09-25 09:28:13 | [diff] [blame] | 18 | #include "components/history/core/browser/history_types.h" |
sdefresne | edf9e01f | 2015-01-13 19:45:41 | [diff] [blame] | 19 | #include "components/history/core/browser/top_sites_observer.h" |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame] | 20 | #include "components/keyed_service/core/keyed_service.h" |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 21 | #include "components/ntp_tiles/most_visited_sites.h" |
| 22 | #include "components/ntp_tiles/ntp_tile.h" |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 23 | #include "components/search_engines/template_url_service_observer.h" |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 24 | #include "content/public/browser/notification_observer.h" |
| 25 | #include "content/public/browser/notification_registrar.h" |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 26 | #include "url/gurl.h" |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 27 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 28 | class InstantIOContext; |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 29 | struct InstantMostVisitedItem; |
| 30 | class InstantSearchPrerenderer; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 31 | class InstantServiceObserver; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 32 | class Profile; |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 33 | struct TemplateURLData; |
| 34 | class TemplateURLService; |
| 35 | struct ThemeBackgroundInfo; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 36 | |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 37 | namespace content { |
[email protected] | 77759005 | 2014-01-17 22:11:54 | [diff] [blame] | 38 | class RenderProcessHost; |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 39 | } |
| 40 | |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 41 | namespace history { |
| 42 | class TopSites; |
| 43 | } |
| 44 | |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 45 | // Tracks render process host IDs that are associated with Instant. |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame] | 46 | class InstantService : public KeyedService, |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 47 | public content::NotificationObserver, |
sdefresne | edf9e01f | 2015-01-13 19:45:41 | [diff] [blame] | 48 | public TemplateURLServiceObserver, |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 49 | public history::TopSitesObserver, |
| 50 | public ntp_tiles::MostVisitedSites::Observer { |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 51 | public: |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 52 | explicit InstantService(Profile* profile); |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 53 | ~InstantService() override; |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 54 | |
| 55 | // Add, remove, and query RenderProcessHost IDs that are associated with |
| 56 | // Instant processes. |
| 57 | void AddInstantProcess(int process_id); |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 58 | bool IsInstantProcess(int process_id) const; |
| 59 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 60 | // Adds/Removes InstantService observers. |
| 61 | void AddObserver(InstantServiceObserver* observer); |
| 62 | void RemoveObserver(InstantServiceObserver* observer); |
| 63 | |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 64 | #if defined(UNIT_TEST) |
| 65 | int GetInstantProcessCount() const { |
| 66 | return process_ids_.size(); |
| 67 | } |
| 68 | #endif |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 69 | |
treib | 2df1b7d | 2016-11-23 09:16:32 | [diff] [blame] | 70 | // Invoked whenever an NTP is opened. Causes an async refresh of Most Visited |
| 71 | // items. |
| 72 | void OnNewTabPageOpened(); |
| 73 | |
[email protected] | 8b169b4b | 2013-03-23 18:24:46 | [diff] [blame] | 74 | // Most visited item API. |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 75 | |
treib | 2df1b7d | 2016-11-23 09:16:32 | [diff] [blame] | 76 | // Invoked when the Instant page wants to delete a Most Visited item. |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 77 | void DeleteMostVisitedItem(const GURL& url); |
| 78 | |
treib | 2df1b7d | 2016-11-23 09:16:32 | [diff] [blame] | 79 | // Invoked when the Instant page wants to undo the deletion. |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 80 | void UndoMostVisitedDeletion(const GURL& url); |
| 81 | |
treib | 2df1b7d | 2016-11-23 09:16:32 | [diff] [blame] | 82 | // Invoked when the Instant page wants to undo all Most Visited deletions. |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 83 | void UndoAllMostVisitedDeletions(); |
| 84 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 85 | // Invoked by the InstantController to update theme information for NTP. |
| 86 | // |
| 87 | // TODO(kmadhusu): Invoking this from InstantController shouldn't be |
| 88 | // necessary. Investigate more and remove this from here. |
| 89 | void UpdateThemeInfo(); |
| 90 | |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 91 | // Invoked by the InstantController to update most visited items details for |
| 92 | // NTP. |
| 93 | void UpdateMostVisitedItemsInfo(); |
| 94 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 95 | // Sends the current set of search URLs to a renderer process. |
| 96 | void SendSearchURLsToRenderer(content::RenderProcessHost* rph); |
| 97 | |
treib | 80920653 | 2016-11-10 15:48:33 | [diff] [blame] | 98 | InstantSearchPrerenderer* GetInstantSearchPrerenderer(); |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 99 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 100 | private: |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 101 | friend class InstantExtendedTest; |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 102 | friend class InstantServiceTest; |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 103 | friend class InstantTestBase; |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 104 | friend class InstantUnitTestBase; |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 105 | |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 106 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest, |
| 107 | MANUAL_SearchesFromFakebox); |
[email protected] | df113a1 | 2013-07-24 04:56:08 | [diff] [blame] | 108 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation); |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 109 | FRIEND_TEST_ALL_PREFIXES(InstantServiceEnabledTest, |
| 110 | SendsSearchURLsToRenderer); |
zmin | fddc3e3 | 2015-12-09 22:59:08 | [diff] [blame] | 111 | FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetSuggestionFromServiceSide); |
| 112 | FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetSuggestionFromClientSide); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 113 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 114 | // KeyedService: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 115 | void Shutdown() override; |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 116 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 117 | // content::NotificationObserver: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 118 | void Observe(int type, |
| 119 | const content::NotificationSource& source, |
| 120 | const content::NotificationDetails& details) override; |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 121 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 122 | // TemplateURLServiceObserver: |
| 123 | // Caches the previous value of the Default Search Provider and the Google |
| 124 | // base URL to filter out changes other than those affecting the Default |
| 125 | // Search Provider. |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 126 | void OnTemplateURLServiceChanged() override; |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 127 | |
sdefresne | edf9e01f | 2015-01-13 19:45:41 | [diff] [blame] | 128 | // TopSitesObserver: |
| 129 | void TopSitesLoaded(history::TopSites* top_sites) override; |
fserb | db57511 | 2015-06-29 21:31:59 | [diff] [blame] | 130 | void TopSitesChanged(history::TopSites* top_sites, |
| 131 | ChangeReason change_reason) override; |
sdefresne | edf9e01f | 2015-01-13 19:45:41 | [diff] [blame] | 132 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 133 | // Called when a renderer process is terminated. |
| 134 | void OnRendererProcessTerminated(int process_id); |
| 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 |
treib | 94638f3 | 2016-07-14 10:39:11 | [diff] [blame] | 138 | // NotifyAboutMostVisitedItems. |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 139 | void OnTopSitesReceived(const history::MostVisitedURLList& data); |
| 140 | |
| 141 | // ntp_tiles::MostVisitedSites::Observer implementation. |
| 142 | void OnMostVisitedURLsAvailable( |
| 143 | const ntp_tiles::NTPTilesVector& tiles) override; |
| 144 | void OnIconMadeAvailable(const GURL& site_url) override; |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 145 | |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 146 | // Notifies the observer about the last known most visited items. |
| 147 | void NotifyAboutMostVisitedItems(); |
| 148 | |
brettw | 9b0866f | 2016-12-11 02:34:06 | [diff] [blame^] | 149 | #if !defined(OS_ANDROID) |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 150 | // Theme changed notification handler. |
estade | 68691b28 | 2015-12-11 21:50:40 | [diff] [blame] | 151 | void OnThemeChanged(); |
pcc | 3a725956 | 2015-04-15 22:21:07 | [diff] [blame] | 152 | #endif |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 153 | |
treib | 80920653 | 2016-11-10 15:48:33 | [diff] [blame] | 154 | void ResetInstantSearchPrerendererIfNecessary(); |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 155 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 156 | Profile* const profile_; |
| 157 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 158 | // The TemplateURLService that we are observing. It will outlive this |
| 159 | // InstantService due to the dependency declared in InstantServiceFactory. |
| 160 | TemplateURLService* template_url_service_; |
| 161 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 162 | // The process ids associated with Instant processes. |
| 163 | std::set<int> process_ids_; |
| 164 | |
fserb | a485e91 | 2015-08-05 20:26:08 | [diff] [blame] | 165 | // InstantMostVisitedItems from TopSites. |
[email protected] | ab01dd75 | 2013-06-08 05:13:36 | [diff] [blame] | 166 | std::vector<InstantMostVisitedItem> most_visited_items_; |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 167 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 168 | // Theme-related data for NTP overlay to adopt themes. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 169 | std::unique_ptr<ThemeBackgroundInfo> theme_info_; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 170 | |
brettw | d195c95 | 2015-06-02 17:31:12 | [diff] [blame] | 171 | base::ObserverList<InstantServiceObserver> observers_; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 172 | |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 173 | content::NotificationRegistrar registrar_; |
| 174 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 175 | scoped_refptr<InstantIOContext> instant_io_context_; |
| 176 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 177 | // Set to NULL if the default search provider does not support Instant. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 178 | std::unique_ptr<InstantSearchPrerenderer> instant_prerenderer_; |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 179 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 180 | // Used to check whether notifications from TemplateURLService indicate a |
| 181 | // change that affects the default search provider. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 182 | std::unique_ptr<TemplateURLData> previous_default_search_provider_; |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 183 | GURL previous_google_base_url_; |
| 184 | |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 185 | // Data sources for NTP tiles (aka Most Visited tiles). Only one of these will |
| 186 | // be non-null. |
| 187 | std::unique_ptr<ntp_tiles::MostVisitedSites> most_visited_sites_; |
| 188 | scoped_refptr<history::TopSites> top_sites_; |
| 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_ |