blob: 35e61323399a80df15d5d9ac362b53fe7610ee04 [file] [log] [blame]
[email protected]c5dec6292013-01-25 04:54:521// 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]a7b8e43d2013-03-18 18:52:435#ifndef CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_
6#define CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_
[email protected]c5dec6292013-01-25 04:54:527
Friedrich Horschig9e8749f02017-08-10 09:17:318#include <map>
dcheng4af48582016-04-19 00:29:359#include <memory>
[email protected]c5dec6292013-01-25 04:54:5210#include <set>
[email protected]ab01dd752013-06-08 05:13:3611#include <vector>
[email protected]c5dec6292013-01-25 04:54:5212
[email protected]c4b2af22014-05-11 19:48:5313#include "base/gtest_prod_util.h"
avib896c712015-12-26 02:10:4314#include "base/macros.h"
treibbb0c5af52016-12-09 17:34:1515#include "base/memory/ref_counted.h"
[email protected]280be992013-06-04 03:19:5116#include "base/memory/weak_ptr.h"
[email protected]6af41782013-06-22 13:49:1117#include "base/observer_list.h"
Kristi Park999db912018-12-05 23:46:4218#include "base/optional.h"
brettw9b0866f2016-12-11 02:34:0619#include "build/build_config.h"
sdefresnebc766ef2014-09-25 09:28:1320#include "components/history/core/browser/history_types.h"
Gayane Petrosyand257e7202019-05-21 03:43:3821#include "components/image_fetcher/core/image_fetcher_impl.h"
[email protected]540380fc2014-03-14 10:10:3422#include "components/keyed_service/core/keyed_service.h"
treibbb0c5af52016-12-09 17:34:1523#include "components/ntp_tiles/most_visited_sites.h"
24#include "components/ntp_tiles/ntp_tile.h"
Kyle Milka66bb07a2018-09-17 17:55:5625#include "components/prefs/pref_change_registrar.h"
Kyle Milka64e205c2018-06-07 17:27:3126#include "components/prefs/pref_registry_simple.h"
[email protected]54e0ee62013-02-01 17:25:0327#include "content/public/browser/notification_observer.h"
28#include "content/public/browser/notification_registrar.h"
Kristi Parkff3cf952019-01-28 23:00:5929#include "ui/native_theme/native_theme.h"
Kyle Milka35af9bd42019-06-28 21:57:4630#include "ui/native_theme/native_theme_observer.h"
[email protected]c4b2af22014-05-11 19:48:5331#include "url/gurl.h"
[email protected]c5dec6292013-01-25 04:54:5232
Marc Treib9028a6a2017-12-06 16:21:5133#if defined(OS_ANDROID)
34#error "Instant is only used on desktop";
35#endif
36
[email protected]672c8c12013-03-07 12:30:0637class InstantIOContext;
[email protected]6af41782013-06-22 13:49:1138class InstantServiceObserver;
Kyle Milka31a76c6d2018-10-19 17:37:2739class NtpBackgroundService;
[email protected]672c8c12013-03-07 12:30:0640class Profile;
Kristi Parke68931522019-06-08 00:17:1841struct InstantMostVisitedInfo;
[email protected]c4b2af22014-05-11 19:48:5342struct ThemeBackgroundInfo;
[email protected]672c8c12013-03-07 12:30:0643
[email protected]4ff347e2013-07-22 19:39:0044namespace content {
[email protected]777590052014-01-17 22:11:5445class RenderProcessHost;
Kristi Parka5141b92018-10-10 01:23:5746} // namespace content
[email protected]4ff347e2013-07-22 19:39:0047
Gayane Petrosyand257e7202019-05-21 03:43:3848extern const char kNtpCustomBackgroundMainColor[];
49
Marc Treibbe5f3feb2017-09-04 09:53:1650// Tracks render process host IDs that are associated with Instant, i.e.
51// processes that are used to render an NTP. Also responsible for keeping
52// necessary information (most visited tiles and theme info) updated in those
53// renderer processes.
[email protected]540380fc2014-03-14 10:10:3454class InstantService : public KeyedService,
[email protected]c4b2af22014-05-11 19:48:5355 public content::NotificationObserver,
Kyle Milka35af9bd42019-06-28 21:57:4656 public ntp_tiles::MostVisitedSites::Observer,
57 public ui::NativeThemeObserver {
[email protected]c5dec6292013-01-25 04:54:5258 public:
[email protected]672c8c12013-03-07 12:30:0659 explicit InstantService(Profile* profile);
Daniel Chenga542fca2014-10-21 09:51:2960 ~InstantService() override;
[email protected]c5dec6292013-01-25 04:54:5261
62 // Add, remove, and query RenderProcessHost IDs that are associated with
63 // Instant processes.
64 void AddInstantProcess(int process_id);
[email protected]c5dec6292013-01-25 04:54:5265 bool IsInstantProcess(int process_id) const;
66
[email protected]6af41782013-06-22 13:49:1167 // Adds/Removes InstantService observers.
68 void AddObserver(InstantServiceObserver* observer);
69 void RemoveObserver(InstantServiceObserver* observer);
70
Kyle Milka64e205c2018-06-07 17:27:3171 // Register prefs associated with the NTP.
72 static void RegisterProfilePrefs(PrefRegistrySimple* registry);
73
[email protected]d572bfd2013-02-14 06:14:2074#if defined(UNIT_TEST)
75 int GetInstantProcessCount() const {
76 return process_ids_.size();
77 }
78#endif
[email protected]c5dec6292013-01-25 04:54:5279
treib2df1b7d2016-11-23 09:16:3280 // Invoked whenever an NTP is opened. Causes an async refresh of Most Visited
81 // items.
82 void OnNewTabPageOpened();
83
Xi Cheng72db9c0c2017-08-30 17:05:4084 // Most visited item APIs.
85 //
treib2df1b7d2016-11-23 09:16:3286 // Invoked when the Instant page wants to delete a Most Visited item.
[email protected]280be992013-06-04 03:19:5187 void DeleteMostVisitedItem(const GURL& url);
treib2df1b7d2016-11-23 09:16:3288 // Invoked when the Instant page wants to undo the deletion.
[email protected]280be992013-06-04 03:19:5189 void UndoMostVisitedDeletion(const GURL& url);
treib2df1b7d2016-11-23 09:16:3290 // Invoked when the Instant page wants to undo all Most Visited deletions.
[email protected]280be992013-06-04 03:19:5191 void UndoAllMostVisitedDeletions();
kristiparka52a3892018-07-24 18:46:4492 // Invoked when the Instant page wants to add a custom link.
kristiparkbe00c6562018-08-01 21:30:2793 bool AddCustomLink(const GURL& url, const std::string& title);
kristipark940dc202018-07-30 18:29:5594 // Invoked when the Instant page wants to update a custom link.
kristiparkbe00c6562018-08-01 21:30:2795 bool UpdateCustomLink(const GURL& url,
kristipark940dc202018-07-30 18:29:5596 const GURL& new_url,
97 const std::string& new_title);
Kristi Parka49acec2018-11-07 22:56:5098 // Invoked when the Instant page wants to reorder a custom link.
99 bool ReorderCustomLink(const GURL& url, int new_pos);
kristiparka52a3892018-07-24 18:46:44100 // Invoked when the Instant page wants to delete a custom link.
kristiparkbe00c6562018-08-01 21:30:27101 bool DeleteCustomLink(const GURL& url);
102 // Invoked when the Instant page wants to undo the previous custom link
Kristi Parkb8aec552019-04-18 21:08:56103 // action. Returns false and does nothing if the profile is using a third-
104 // party NTP.
Ramya Nagarajan7673f052018-08-13 22:53:29105 bool UndoCustomLinkAction();
kristiparka52a3892018-07-24 18:46:44106 // Invoked when the Instant page wants to delete all custom links and use Most
Ramya Nagarajan7673f052018-08-13 22:53:29107 // Visited sites instead. Returns false and does nothing if the profile is
Kristi Parkb8aec552019-04-18 21:08:56108 // using a third-party NTP. Marked virtual for mocking in tests.
Gayane Petrosyan99296102019-03-06 19:07:49109 virtual bool ResetCustomLinks();
Kristi Park30aa6342019-06-11 22:43:27110 // Invoked when the Instant page wants to switch between custom links and Most
111 // Visited. Toggles between the two options each time it's called. Returns
112 // false and does nothing if the profile is using a third-party NTP.
113 bool ToggleMostVisitedOrCustomLinks();
114 // Invoked when the Instant page wants to toggle visibility of the tiles.
Kristi Park955ad552019-07-02 21:50:33115 // Notifies observers only if |do_notify| is true, which is usually the case
116 // if |ToggleMostVisitedOrCustomLinks| will not be called immediately after.
Kristi Park30aa6342019-06-11 22:43:27117 // Returns false and does nothing if the profile is using a third-party NTP.
Kristi Park955ad552019-07-02 21:50:33118 bool ToggleShortcutsVisibility(bool do_notify);
[email protected]280be992013-06-04 03:19:51119
Kyle Milka5c0166d2019-04-03 23:57:14120 // Invoked to update theme information for the NTP.
Kyle Milkafb37a772018-06-25 16:09:19121 void UpdateThemeInfo();
[email protected]6af41782013-06-22 13:49:11122
Kyle Milka0d1b376e2019-01-28 19:34:10123 // Invoked when a background pref update is received via sync, triggering
124 // an update of theme info.
125 void UpdateBackgroundFromSync();
126
[email protected]ed68ae32013-06-29 20:46:48127 // Invoked by the InstantController to update most visited items details for
128 // NTP.
Kristi Park6424e602019-06-11 18:01:43129 void UpdateMostVisitedInfo();
[email protected]ed68ae32013-06-29 20:46:48130
Marc Treibc9c35002017-10-16 10:09:33131 // Sends the current NTP URL to a renderer process.
132 void SendNewTabPageURLToRenderer(content::RenderProcessHost* rph);
[email protected]2309e912013-10-01 01:33:30133
Kyle Milka64e205c2018-06-07 17:27:31134 // Invoked when a custom background is selected on the NTP.
135 void SetCustomBackgroundURL(const GURL& url);
136
Kyle Milka6986ede2018-07-19 02:16:24137 // Invoked when a custom background with attributions is selected on the NTP.
138 void SetCustomBackgroundURLWithAttributions(
139 const GURL& background_url,
140 const std::string& attribution_line_1,
141 const std::string& attribution_line_2,
142 const GURL& action_url);
143
Kyle Milka3626a7d2018-07-03 06:23:55144 // Invoked when a user selected the "Upload an image" option on the NTP.
145 void SelectLocalBackgroundImage(const base::FilePath& path);
146
Gayane Petrosyane85debf2019-03-06 06:19:13147 // Getter for |theme_info_| that will also initialize it if necessary.
148 ThemeBackgroundInfo* GetInitializedThemeInfo();
Kyle Milka6986ede2018-07-19 02:16:24149
Kristi Parkff3cf952019-01-28 23:00:59150 // Used for testing.
Kyle Milka35af9bd42019-06-28 21:57:46151 void SetNativeThemeForTesting(ui::NativeTheme* theme);
Kristi Parkff3cf952019-01-28 23:00:59152
153 // Used for testing.
Kyle Milka31a76c6d2018-10-19 17:37:27154 void AddValidBackdropUrlForTesting(const GURL& url) const;
155
Kyle Milka427c8da2018-11-26 21:59:05156 // Check if a custom background has been set by the user.
157 bool IsCustomBackgroundSet();
158
Gayane Petrosyan99296102019-03-06 19:07:49159 // Reset all NTP customizations to default. Marked virtual for mocking in
160 // tests.
161 virtual void ResetToDefault();
162
Gayane Petrosyand257e7202019-05-21 03:43:38163 // Calculates the most frequent color of the image and stores it in prefs.
164 void UpdateCustomBackgroundColorAsync(
Kyle Milkac3d1a5302019-07-10 19:04:02165 base::TimeTicks timestamp,
Gayane Petrosyand257e7202019-05-21 03:43:38166 const gfx::Image& fetched_image,
167 const image_fetcher::RequestMetadata& metadata);
168
169 // Fetches the image for the given |fetch_url|.
Kyle Milkac3d1a5302019-07-10 19:04:02170 void FetchCustomBackground(base::TimeTicks timestamp, const GURL& fetch_url);
Gayane Petrosyand257e7202019-05-21 03:43:38171
[email protected]c5dec6292013-01-25 04:54:52172 private:
kristiparkf8fa4132018-09-13 18:07:29173 class SearchProviderObserver;
174
[email protected]4ff347e2013-07-22 19:39:00175 friend class InstantExtendedTest;
[email protected]c8a118e2013-09-24 21:33:40176 friend class InstantUnitTestBase;
Gayane Petrosyand257e7202019-05-21 03:43:38177 friend class LocalNTPBackgroundsAndDarkModeTest;
178 friend class TestInstantService;
[email protected]4ff347e2013-07-22 19:39:00179
[email protected]df113a12013-07-24 04:56:08180 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
Marc Treib7b3985e2017-09-18 11:22:16181 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetNTPTileSuggestion);
Kristi Parke68931522019-06-08 00:17:18182 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest,
183 DoesToggleMostVisitedOrCustomLinks);
Kristi Park30aa6342019-06-11 22:43:27184 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, DoesToggleShortcutsVisibility);
Kristi Parkb8aec552019-04-18 21:08:56185 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, IsCustomLinksEnabled);
Gayane Petrosyane85debf2019-03-06 06:19:13186 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, TestNoThemeInfo);
Kyle Milkac3d1a5302019-07-10 19:04:02187 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, TestUpdateCustomBackgroundColor);
188 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest,
189 LocalImageDoesNotUpdateCustomBackgroundColor);
[email protected]4ff347e2013-07-22 19:39:00190
[email protected]c4b2af22014-05-11 19:48:53191 // KeyedService:
Daniel Chenga542fca2014-10-21 09:51:29192 void Shutdown() override;
[email protected]c5dec6292013-01-25 04:54:52193
[email protected]c4b2af22014-05-11 19:48:53194 // content::NotificationObserver:
Daniel Chenga542fca2014-10-21 09:51:29195 void Observe(int type,
196 const content::NotificationSource& source,
197 const content::NotificationDetails& details) override;
[email protected]54e0ee62013-02-01 17:25:03198
[email protected]2309e912013-10-01 01:33:30199 // Called when a renderer process is terminated.
200 void OnRendererProcessTerminated(int process_id);
201
Kyle Milka35af9bd42019-06-28 21:57:46202 // ui::NativeThemeObserver:
203 void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
204
kristiparkf8fa4132018-09-13 18:07:29205 // Called when the search provider changes. Disables custom links if the
206 // search provider is not Google.
Kristi Parkb8aec552019-04-18 21:08:56207 void OnSearchProviderChanged();
kristiparkf8fa4132018-09-13 18:07:29208
treibbb0c5af52016-12-09 17:34:15209 // ntp_tiles::MostVisitedSites::Observer implementation.
Friedrich Horschig9e8749f02017-08-10 09:17:31210 void OnURLsAvailable(
211 const std::map<ntp_tiles::SectionType, ntp_tiles::NTPTilesVector>&
212 sections) override;
treibbb0c5af52016-12-09 17:34:15213 void OnIconMadeAvailable(const GURL& site_url) override;
[email protected]280be992013-06-04 03:19:51214
Kristi Park6424e602019-06-11 18:01:43215 void NotifyAboutMostVisitedInfo();
Marc Treib5891b282017-08-22 09:44:30216 void NotifyAboutThemeInfo();
[email protected]ed68ae32013-06-29 20:46:48217
Kristi Parkb8aec552019-04-18 21:08:56218 // Returns true if this is a Google NTP and the user has chosen to show custom
219 // links.
220 bool IsCustomLinksEnabled();
221
Marc Treib5891b282017-08-22 09:44:30222 void BuildThemeInfo();
[email protected]6af41782013-06-22 13:49:11223
Kyle Milka6986ede2018-07-19 02:16:24224 void ApplyOrResetCustomBackgroundThemeInfo();
225
Kyle Milka66bb07a2018-09-17 17:55:56226 void ApplyCustomBackgroundThemeInfo();
Kyle Milka66bb07a2018-09-17 17:55:56227
Gayane Petrosyan99296102019-03-06 19:07:49228 // Marked virtual for mocking in tests.
229 virtual void ResetCustomBackgroundThemeInfo();
Kyle Milkafb37a772018-06-25 16:09:19230
Kyle Milka66bb07a2018-09-17 17:55:56231 void FallbackToDefaultThemeInfo();
232
Kyle Milka31a76c6d2018-10-19 17:37:27233 void RemoveLocalBackgroundImageCopy();
234
Kyle Milka427c8da2018-11-26 21:59:05235 // Returns false if the custom background pref cannot be parsed, otherwise
236 // returns true and sets custom_background_url to the value in the pref.
237 bool IsCustomBackgroundPrefValid(GURL& custom_background_url);
238
Kyle Milka3626a7d2018-07-03 06:23:55239 // Update the background pref to point to
240 // chrome-search://local-ntp/background.jpg
241 void SetBackgroundToLocalResource();
242
Kyle Milkac3d1a5302019-07-10 19:04:02243 // Updates custom background prefs with color if the background hasn't changed
244 // since the calculation started.
245 void UpdateCustomBackgroundPrefsWithColor(base::TimeTicks timestamp,
Gayane Petrosyand257e7202019-05-21 03:43:38246 SkColor color);
247
248 void SetImageFetcherForTesting(image_fetcher::ImageFetcher* image_fetcher);
249
Kyle Milkac3d1a5302019-07-10 19:04:02250 base::TimeTicks GetBackgroundUpdatedTimestampForTesting() {
251 return background_updated_timestamp_;
252 }
253
[email protected]672c8c12013-03-07 12:30:06254 Profile* const profile_;
255
[email protected]c5dec6292013-01-25 04:54:52256 // The process ids associated with Instant processes.
257 std::set<int> process_ids_;
258
Kristi Parke68931522019-06-08 00:17:18259 // Contains InstantMostVisitedItems received from |most_visited_sites_| and
260 // information required to display NTP tiles.
261 std::unique_ptr<InstantMostVisitedInfo> most_visited_info_;
[email protected]e7868c82013-03-12 03:55:56262
[email protected]6af41782013-06-22 13:49:11263 // Theme-related data for NTP overlay to adopt themes.
dcheng4af48582016-04-19 00:29:35264 std::unique_ptr<ThemeBackgroundInfo> theme_info_;
[email protected]6af41782013-06-22 13:49:11265
Trent Apteda250ec3ab2018-08-19 08:52:19266 base::ObserverList<InstantServiceObserver>::Unchecked observers_;
[email protected]6af41782013-06-22 13:49:11267
[email protected]54e0ee62013-02-01 17:25:03268 content::NotificationRegistrar registrar_;
269
[email protected]672c8c12013-03-07 12:30:06270 scoped_refptr<InstantIOContext> instant_io_context_;
271
Marc Treib7b3985e2017-09-18 11:22:16272 // Data source for NTP tiles (aka Most Visited tiles). May be null.
treibbb0c5af52016-12-09 17:34:15273 std::unique_ptr<ntp_tiles::MostVisitedSites> most_visited_sites_;
mohan.reddy74595362014-09-23 20:35:34274
kristiparkf8fa4132018-09-13 18:07:29275 // Keeps track of any changes in search engine provider. May be null.
276 std::unique_ptr<SearchProviderObserver> search_provider_observer_;
277
Kyle Milka66bb07a2018-09-17 17:55:56278 PrefChangeRegistrar pref_change_registrar_;
279
280 PrefService* pref_service_;
281
Kyle Milka35af9bd42019-06-28 21:57:46282 ScopedObserver<ui::NativeTheme, InstantService> theme_observer_;
283
284 ui::NativeTheme* native_theme_;
Dan Beam6877cef42019-01-30 21:19:03285
Kyle Milka31a76c6d2018-10-19 17:37:27286 NtpBackgroundService* background_service_;
287
Gayane Petrosyand257e7202019-05-21 03:43:38288 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_;
289
Kyle Milkac3d1a5302019-07-10 19:04:02290 base::TimeTicks background_updated_timestamp_;
291
Kyle Milka3626a7d2018-07-03 06:23:55292 base::WeakPtrFactory<InstantService> weak_ptr_factory_;
293
[email protected]c5dec6292013-01-25 04:54:52294 DISALLOW_COPY_AND_ASSIGN(InstantService);
295};
296
[email protected]a7b8e43d2013-03-18 18:52:43297#endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_