[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" |
sdefresne | bc766ef | 2014-09-25 09:28:13 | [diff] [blame] | 19 | #include "components/history/core/browser/history_types.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" |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 23 | #include "components/prefs/pref_change_registrar.h" |
Kyle Milka | 64e205c | 2018-06-07 17:27:31 | [diff] [blame] | 24 | #include "components/prefs/pref_registry_simple.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 | |
Marc Treib | 9028a6a | 2017-12-06 16:21:51 | [diff] [blame] | 29 | #if defined(OS_ANDROID) |
| 30 | #error "Instant is only used on desktop"; |
| 31 | #endif |
| 32 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 33 | class InstantIOContext; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 34 | class InstantServiceObserver; |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 35 | class NtpBackgroundService; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 36 | class Profile; |
treib | c689219 | 2017-02-15 13:03:37 | [diff] [blame] | 37 | struct InstantMostVisitedItem; |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 38 | struct ThemeBackgroundInfo; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 39 | |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 40 | namespace content { |
[email protected] | 77759005 | 2014-01-17 22:11:54 | [diff] [blame] | 41 | class RenderProcessHost; |
Kristi Park | a5141b9 | 2018-10-10 01:23:57 | [diff] [blame] | 42 | } // namespace content |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 43 | |
Marc Treib | be5f3feb | 2017-09-04 09:53:16 | [diff] [blame] | 44 | // Tracks render process host IDs that are associated with Instant, i.e. |
| 45 | // processes that are used to render an NTP. Also responsible for keeping |
| 46 | // necessary information (most visited tiles and theme info) updated in those |
| 47 | // renderer processes. |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame] | 48 | class InstantService : public KeyedService, |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 49 | public content::NotificationObserver, |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 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 | |
Kyle Milka | 64e205c | 2018-06-07 17:27:31 | [diff] [blame] | 64 | // Register prefs associated with the NTP. |
| 65 | static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 66 | |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 67 | #if defined(UNIT_TEST) |
| 68 | int GetInstantProcessCount() const { |
| 69 | return process_ids_.size(); |
| 70 | } |
| 71 | #endif |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 72 | |
treib | 2df1b7d | 2016-11-23 09:16:32 | [diff] [blame] | 73 | // Invoked whenever an NTP is opened. Causes an async refresh of Most Visited |
| 74 | // items. |
| 75 | void OnNewTabPageOpened(); |
| 76 | |
Xi Cheng | 72db9c0c | 2017-08-30 17:05:40 | [diff] [blame] | 77 | // Most visited item APIs. |
| 78 | // |
treib | 2df1b7d | 2016-11-23 09:16:32 | [diff] [blame] | 79 | // Invoked when the Instant page wants to delete a Most Visited item. |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 80 | void DeleteMostVisitedItem(const GURL& url); |
treib | 2df1b7d | 2016-11-23 09:16:32 | [diff] [blame] | 81 | // Invoked when the Instant page wants to undo the deletion. |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 82 | void UndoMostVisitedDeletion(const GURL& url); |
treib | 2df1b7d | 2016-11-23 09:16:32 | [diff] [blame] | 83 | // Invoked when the Instant page wants to undo all Most Visited deletions. |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 84 | void UndoAllMostVisitedDeletions(); |
kristipark | a52a389 | 2018-07-24 18:46:44 | [diff] [blame] | 85 | // Invoked when the Instant page wants to add a custom link. |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 86 | bool AddCustomLink(const GURL& url, const std::string& title); |
kristipark | 940dc20 | 2018-07-30 18:29:55 | [diff] [blame] | 87 | // Invoked when the Instant page wants to update a custom link. |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 88 | bool UpdateCustomLink(const GURL& url, |
kristipark | 940dc20 | 2018-07-30 18:29:55 | [diff] [blame] | 89 | const GURL& new_url, |
| 90 | const std::string& new_title); |
Kristi Park | a49acec | 2018-11-07 22:56:50 | [diff] [blame^] | 91 | // Invoked when the Instant page wants to reorder a custom link. |
| 92 | bool ReorderCustomLink(const GURL& url, int new_pos); |
kristipark | a52a389 | 2018-07-24 18:46:44 | [diff] [blame] | 93 | // Invoked when the Instant page wants to delete a custom link. |
kristipark | be00c656 | 2018-08-01 21:30:27 | [diff] [blame] | 94 | bool DeleteCustomLink(const GURL& url); |
| 95 | // Invoked when the Instant page wants to undo the previous custom link |
Ramya Nagarajan | 7673f05 | 2018-08-13 22:53:29 | [diff] [blame] | 96 | // action. Returns false and does nothing if the profile is using a non-Google |
| 97 | // search provider. |
| 98 | bool UndoCustomLinkAction(); |
kristipark | a52a389 | 2018-07-24 18:46:44 | [diff] [blame] | 99 | // Invoked when the Instant page wants to delete all custom links and use Most |
Ramya Nagarajan | 7673f05 | 2018-08-13 22:53:29 | [diff] [blame] | 100 | // Visited sites instead. Returns false and does nothing if the profile is |
| 101 | // using a non-Google search provider. |
| 102 | bool ResetCustomLinks(); |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 103 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 104 | // Invoked by the InstantController to update theme information for NTP. |
| 105 | // |
| 106 | // TODO(kmadhusu): Invoking this from InstantController shouldn't be |
| 107 | // necessary. Investigate more and remove this from here. |
Kyle Milka | fb37a77 | 2018-06-25 16:09:19 | [diff] [blame] | 108 | void UpdateThemeInfo(); |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 109 | |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 110 | // Invoked by the InstantController to update most visited items details for |
| 111 | // NTP. |
| 112 | void UpdateMostVisitedItemsInfo(); |
| 113 | |
Marc Treib | c9c3500 | 2017-10-16 10:09:33 | [diff] [blame] | 114 | // Sends the current NTP URL to a renderer process. |
| 115 | void SendNewTabPageURLToRenderer(content::RenderProcessHost* rph); |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 116 | |
Kyle Milka | 64e205c | 2018-06-07 17:27:31 | [diff] [blame] | 117 | // Invoked when a custom background is selected on the NTP. |
| 118 | void SetCustomBackgroundURL(const GURL& url); |
| 119 | |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 120 | // Invoked when a custom background with attributions is selected on the NTP. |
| 121 | void SetCustomBackgroundURLWithAttributions( |
| 122 | const GURL& background_url, |
| 123 | const std::string& attribution_line_1, |
| 124 | const std::string& attribution_line_2, |
| 125 | const GURL& action_url); |
| 126 | |
Kyle Milka | 3626a7d | 2018-07-03 06:23:55 | [diff] [blame] | 127 | // Invoked when a user selected the "Upload an image" option on the NTP. |
| 128 | void SelectLocalBackgroundImage(const base::FilePath& path); |
| 129 | |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 130 | // Used for testing. |
| 131 | ThemeBackgroundInfo* GetThemeInfoForTesting() { return theme_info_.get(); } |
| 132 | |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 133 | void AddValidBackdropUrlForTesting(const GURL& url) const; |
| 134 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 135 | private: |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 136 | class SearchProviderObserver; |
| 137 | |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 138 | friend class InstantExtendedTest; |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 139 | friend class InstantUnitTestBase; |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 140 | |
[email protected] | df113a1 | 2013-07-24 04:56:08 | [diff] [blame] | 141 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation); |
Marc Treib | 7b3985e | 2017-09-18 11:22:16 | [diff] [blame] | 142 | FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetNTPTileSuggestion); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 143 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 144 | // KeyedService: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 145 | void Shutdown() override; |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 146 | |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 147 | // content::NotificationObserver: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 148 | void Observe(int type, |
| 149 | const content::NotificationSource& source, |
| 150 | const content::NotificationDetails& details) override; |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 151 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 152 | // Called when a renderer process is terminated. |
| 153 | void OnRendererProcessTerminated(int process_id); |
| 154 | |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 155 | // Called when the search provider changes. Disables custom links if the |
| 156 | // search provider is not Google. |
| 157 | void OnSearchProviderChanged(bool is_google); |
| 158 | |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 159 | // ntp_tiles::MostVisitedSites::Observer implementation. |
Friedrich Horschig | 9e8749f0 | 2017-08-10 09:17:31 | [diff] [blame] | 160 | void OnURLsAvailable( |
| 161 | const std::map<ntp_tiles::SectionType, ntp_tiles::NTPTilesVector>& |
| 162 | sections) override; |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 163 | void OnIconMadeAvailable(const GURL& site_url) override; |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 164 | |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 165 | void NotifyAboutMostVisitedItems(); |
Marc Treib | 5891b28 | 2017-08-22 09:44:30 | [diff] [blame] | 166 | void NotifyAboutThemeInfo(); |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 167 | |
Marc Treib | 5891b28 | 2017-08-22 09:44:30 | [diff] [blame] | 168 | void BuildThemeInfo(); |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 169 | |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 170 | void ApplyOrResetCustomBackgroundThemeInfo(); |
| 171 | |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 172 | void ApplyCustomBackgroundThemeInfo(); |
| 173 | void ApplyCustomBackgroundThemeInfoFromLocalFile(bool file_exists); |
| 174 | |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 175 | void ResetCustomBackgroundThemeInfo(); |
Kyle Milka | fb37a77 | 2018-06-25 16:09:19 | [diff] [blame] | 176 | |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 177 | void FallbackToDefaultThemeInfo(); |
| 178 | |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 179 | void RemoveLocalBackgroundImageCopy(); |
| 180 | |
Kyle Milka | 3626a7d | 2018-07-03 06:23:55 | [diff] [blame] | 181 | // Update the background pref to point to |
| 182 | // chrome-search://local-ntp/background.jpg |
| 183 | void SetBackgroundToLocalResource(); |
| 184 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 185 | Profile* const profile_; |
| 186 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 187 | // The process ids associated with Instant processes. |
| 188 | std::set<int> process_ids_; |
| 189 | |
Marc Treib | 7b3985e | 2017-09-18 11:22:16 | [diff] [blame] | 190 | // InstantMostVisitedItems for NTP tiles, received from |most_visited_sites_|. |
[email protected] | ab01dd75 | 2013-06-08 05:13:36 | [diff] [blame] | 191 | std::vector<InstantMostVisitedItem> most_visited_items_; |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 192 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 193 | // Theme-related data for NTP overlay to adopt themes. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 194 | std::unique_ptr<ThemeBackgroundInfo> theme_info_; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 195 | |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 196 | base::ObserverList<InstantServiceObserver>::Unchecked observers_; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 197 | |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 198 | content::NotificationRegistrar registrar_; |
| 199 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 200 | scoped_refptr<InstantIOContext> instant_io_context_; |
| 201 | |
Marc Treib | 7b3985e | 2017-09-18 11:22:16 | [diff] [blame] | 202 | // Data source for NTP tiles (aka Most Visited tiles). May be null. |
treib | bb0c5af5 | 2016-12-09 17:34:15 | [diff] [blame] | 203 | std::unique_ptr<ntp_tiles::MostVisitedSites> most_visited_sites_; |
mohan.reddy | 7459536 | 2014-09-23 20:35:34 | [diff] [blame] | 204 | |
kristipark | f8fa413 | 2018-09-13 18:07:29 | [diff] [blame] | 205 | // Keeps track of any changes in search engine provider. May be null. |
| 206 | std::unique_ptr<SearchProviderObserver> search_provider_observer_; |
| 207 | |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 208 | PrefChangeRegistrar pref_change_registrar_; |
| 209 | |
| 210 | PrefService* pref_service_; |
| 211 | |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 212 | NtpBackgroundService* background_service_; |
| 213 | |
Kyle Milka | 3626a7d | 2018-07-03 06:23:55 | [diff] [blame] | 214 | base::WeakPtrFactory<InstantService> weak_ptr_factory_; |
| 215 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 216 | DISALLOW_COPY_AND_ASSIGN(InstantService); |
| 217 | }; |
| 218 | |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 219 | #endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |