[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 | |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 8 | #include <map> |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 9 | #include <memory> |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 10 | #include <set> |
[email protected] | ab01dd75 | 2013-06-08 05:13:36 | [diff] [blame] | 11 | #include <vector> |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 12 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 13 | #include "base/gtest_prod_util.h" |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 14 | #include "base/macros.h" |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 17 | #include "base/observer_list.h" |
brettw | 9b0866f | 2016-12-11 02:34:06 | [diff] [blame] | 18 | #include "build/build_config.h" |
treib | c689219 | 2017-02-15 13:03:37 | [diff] [blame] | 19 | #include "chrome/browser/search/search_engine_base_url_tracker.h" |
sdefresne | bc766ef | 2014-09-25 09:28:13 | [diff] [blame] | 20 | #include "components/history/core/browser/history_types.h" |
sdefresne | edf9e01f | 2015-01-13 19:45:41 | [diff] [blame] | 21 | #include "components/history/core/browser/top_sites_observer.h" |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame] | 22 | #include "components/keyed_service/core/keyed_service.h" |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 23 | #include "components/ntp_tiles/most_visited_sites.h" |
| 24 | #include "components/ntp_tiles/ntp_tile.h" |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 25 | #include "content/public/browser/notification_observer.h" |
| 26 | #include "content/public/browser/notification_registrar.h" |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 27 | #include "url/gurl.h" |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 28 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 29 | class InstantIOContext; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 30 | class InstantServiceObserver; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 31 | class Profile; |
treib | c689219 | 2017-02-15 13:03:37 | [diff] [blame] | 32 | struct InstantMostVisitedItem; |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 33 | struct ThemeBackgroundInfo; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 34 | |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 35 | namespace content { |
[email protected] | 77759005 | 2014-01-17 22:11:54 | [diff] [blame] | 36 | class RenderProcessHost; |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 37 | } |
| 38 | |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 39 | namespace history { |
| 40 | class TopSites; |
| 41 | } |
| 42 | |
Marc Treib | be5f3feb | 2017-09-04 09:53:16 | [diff] [blame^] | 43 | // Tracks render process host IDs that are associated with Instant, i.e. |
| 44 | // processes that are used to render an NTP. Also responsible for keeping |
| 45 | // necessary information (most visited tiles and theme info) updated in those |
| 46 | // renderer processes. |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame] | 47 | class InstantService : public KeyedService, |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 48 | public content::NotificationObserver, |
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 | |
Xi Cheng | 72db9c0c | 2017-08-30 17:05:40 | [diff] [blame] | 74 | // Most visited item APIs. |
| 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); |
treib | 2df1b7d | 2016-11-23 09:16:32 | [diff] [blame] | 78 | // Invoked when the Instant page wants to undo the deletion. |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 79 | void UndoMostVisitedDeletion(const GURL& url); |
treib | 2df1b7d | 2016-11-23 09:16:32 | [diff] [blame] | 80 | // Invoked when the Instant page wants to undo all Most Visited deletions. |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 81 | void UndoAllMostVisitedDeletions(); |
| 82 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 83 | // Invoked by the InstantController to update theme information for NTP. |
| 84 | // |
| 85 | // TODO(kmadhusu): Invoking this from InstantController shouldn't be |
| 86 | // necessary. Investigate more and remove this from here. |
| 87 | void UpdateThemeInfo(); |
| 88 | |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 89 | // Invoked by the InstantController to update most visited items details for |
| 90 | // NTP. |
| 91 | void UpdateMostVisitedItemsInfo(); |
| 92 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 93 | // Sends the current set of search URLs to a renderer process. |
| 94 | void SendSearchURLsToRenderer(content::RenderProcessHost* rph); |
| 95 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 96 | private: |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 97 | friend class InstantExtendedTest; |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 98 | friend class InstantServiceTest; |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 99 | friend class InstantUnitTestBase; |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 100 | |
[email protected] | df113a1 | 2013-07-24 04:56:08 | [diff] [blame] | 101 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation); |
zmin | fddc3e3 | 2015-12-09 22:59:08 | [diff] [blame] | 102 | FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetSuggestionFromClientSide); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 103 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 104 | // KeyedService: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 105 | void Shutdown() override; |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 106 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 107 | // content::NotificationObserver: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 108 | void Observe(int type, |
| 109 | const content::NotificationSource& source, |
| 110 | const content::NotificationDetails& details) override; |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 111 | |
sdefresne | edf9e01f | 2015-01-13 19:45:41 | [diff] [blame] | 112 | // TopSitesObserver: |
| 113 | void TopSitesLoaded(history::TopSites* top_sites) override; |
fserb | db57511 | 2015-06-29 21:31:59 | [diff] [blame] | 114 | void TopSitesChanged(history::TopSites* top_sites, |
| 115 | ChangeReason change_reason) override; |
sdefresne | edf9e01f | 2015-01-13 19:45:41 | [diff] [blame] | 116 | |
treib | c689219 | 2017-02-15 13:03:37 | [diff] [blame] | 117 | void OnSearchEngineBaseURLChanged( |
| 118 | SearchEngineBaseURLTracker::ChangeReason change_reason); |
| 119 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 120 | // Called when a renderer process is terminated. |
| 121 | void OnRendererProcessTerminated(int process_id); |
| 122 | |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 123 | // Called when we get new most visited items from TopSites, registered as an |
| 124 | // async callback. Parses them and sends them to the renderer via |
treib | 94638f3 | 2016-07-14 10:39:11 | [diff] [blame] | 125 | // NotifyAboutMostVisitedItems. |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 126 | void OnTopSitesReceived(const history::MostVisitedURLList& data); |
| 127 | |
| 128 | // ntp_tiles::MostVisitedSites::Observer implementation. |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 129 | void OnURLsAvailable( |
| 130 | const std::map<ntp_tiles::SectionType, ntp_tiles::NTPTilesVector>& |
| 131 | sections) override; |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 132 | void OnIconMadeAvailable(const GURL& site_url) override; |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 133 | |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 134 | void NotifyAboutMostVisitedItems(); |
Marc Treib | 5891b28 | 2017-08-22 09:44:30 | [diff] [blame] | 135 | void NotifyAboutThemeInfo(); |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 136 | |
brettw | 9b0866f | 2016-12-11 02:34:06 | [diff] [blame] | 137 | #if !defined(OS_ANDROID) |
Marc Treib | 5891b28 | 2017-08-22 09:44:30 | [diff] [blame] | 138 | void BuildThemeInfo(); |
pcc | 3a725956 | 2015-04-15 22:21:07 | [diff] [blame] | 139 | #endif |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 140 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 141 | Profile* const profile_; |
| 142 | |
treib | c689219 | 2017-02-15 13:03:37 | [diff] [blame] | 143 | std::unique_ptr<SearchEngineBaseURLTracker> search_engine_base_url_tracker_; |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 144 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 145 | // The process ids associated with Instant processes. |
| 146 | std::set<int> process_ids_; |
| 147 | |
fserb | a485e91 | 2015-08-05 20:26:08 | [diff] [blame] | 148 | // InstantMostVisitedItems from TopSites. |
[email protected] | ab01dd75 | 2013-06-08 05:13:36 | [diff] [blame] | 149 | std::vector<InstantMostVisitedItem> most_visited_items_; |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 150 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 151 | // Theme-related data for NTP overlay to adopt themes. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 152 | std::unique_ptr<ThemeBackgroundInfo> theme_info_; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 153 | |
brettw | d195c95 | 2015-06-02 17:31:12 | [diff] [blame] | 154 | base::ObserverList<InstantServiceObserver> observers_; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 155 | |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 156 | content::NotificationRegistrar registrar_; |
| 157 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 158 | scoped_refptr<InstantIOContext> instant_io_context_; |
| 159 | |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 160 | // Data sources for NTP tiles (aka Most Visited tiles). Only one of these will |
| 161 | // be non-null. |
| 162 | std::unique_ptr<ntp_tiles::MostVisitedSites> most_visited_sites_; |
| 163 | scoped_refptr<history::TopSites> top_sites_; |
| 164 | |
mohan.reddy | 7459536 | 2014-09-23 20:35:34 | [diff] [blame] | 165 | // Used for Top Sites async retrieval. |
| 166 | base::WeakPtrFactory<InstantService> weak_ptr_factory_; |
| 167 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 168 | DISALLOW_COPY_AND_ASSIGN(InstantService); |
| 169 | }; |
| 170 | |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 171 | #endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |