blob: 1b337b1799786c64d03d680b0b859965275b403f [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"
[email protected]c4b2af22014-05-11 19:48:5330#include "url/gurl.h"
[email protected]c5dec6292013-01-25 04:54:5231
Marc Treib9028a6a2017-12-06 16:21:5132#if defined(OS_ANDROID)
33#error "Instant is only used on desktop";
34#endif
35
[email protected]672c8c12013-03-07 12:30:0636class InstantIOContext;
[email protected]6af41782013-06-22 13:49:1137class InstantServiceObserver;
Kyle Milka31a76c6d2018-10-19 17:37:2738class NtpBackgroundService;
[email protected]672c8c12013-03-07 12:30:0639class Profile;
Kristi Parke68931522019-06-08 00:17:1840struct InstantMostVisitedInfo;
[email protected]c4b2af22014-05-11 19:48:5341struct ThemeBackgroundInfo;
[email protected]672c8c12013-03-07 12:30:0642
[email protected]4ff347e2013-07-22 19:39:0043namespace content {
[email protected]777590052014-01-17 22:11:5444class RenderProcessHost;
Kristi Parka5141b92018-10-10 01:23:5745} // namespace content
[email protected]4ff347e2013-07-22 19:39:0046
Rune Lillesveen862caf22019-04-24 17:21:5747namespace ui {
48class DarkModeObserver;
49} // namespace ui
50
Gayane Petrosyand257e7202019-05-21 03:43:3851extern const char kNtpCustomBackgroundMainColor[];
52
Marc Treibbe5f3feb2017-09-04 09:53:1653// Tracks render process host IDs that are associated with Instant, i.e.
54// processes that are used to render an NTP. Also responsible for keeping
55// necessary information (most visited tiles and theme info) updated in those
56// renderer processes.
[email protected]540380fc2014-03-14 10:10:3457class InstantService : public KeyedService,
[email protected]c4b2af22014-05-11 19:48:5358 public content::NotificationObserver,
treibbb0c5af52016-12-09 17:34:1559 public ntp_tiles::MostVisitedSites::Observer {
[email protected]c5dec6292013-01-25 04:54:5260 public:
[email protected]672c8c12013-03-07 12:30:0661 explicit InstantService(Profile* profile);
Daniel Chenga542fca2014-10-21 09:51:2962 ~InstantService() override;
[email protected]c5dec6292013-01-25 04:54:5263
64 // Add, remove, and query RenderProcessHost IDs that are associated with
65 // Instant processes.
66 void AddInstantProcess(int process_id);
[email protected]c5dec6292013-01-25 04:54:5267 bool IsInstantProcess(int process_id) const;
68
[email protected]6af41782013-06-22 13:49:1169 // Adds/Removes InstantService observers.
70 void AddObserver(InstantServiceObserver* observer);
71 void RemoveObserver(InstantServiceObserver* observer);
72
Kyle Milka64e205c2018-06-07 17:27:3173 // Register prefs associated with the NTP.
74 static void RegisterProfilePrefs(PrefRegistrySimple* registry);
75
[email protected]d572bfd2013-02-14 06:14:2076#if defined(UNIT_TEST)
77 int GetInstantProcessCount() const {
78 return process_ids_.size();
79 }
80#endif
[email protected]c5dec6292013-01-25 04:54:5281
treib2df1b7d2016-11-23 09:16:3282 // Invoked whenever an NTP is opened. Causes an async refresh of Most Visited
83 // items.
84 void OnNewTabPageOpened();
85
Xi Cheng72db9c0c2017-08-30 17:05:4086 // Most visited item APIs.
87 //
treib2df1b7d2016-11-23 09:16:3288 // Invoked when the Instant page wants to delete a Most Visited item.
[email protected]280be992013-06-04 03:19:5189 void DeleteMostVisitedItem(const GURL& url);
treib2df1b7d2016-11-23 09:16:3290 // Invoked when the Instant page wants to undo the deletion.
[email protected]280be992013-06-04 03:19:5191 void UndoMostVisitedDeletion(const GURL& url);
treib2df1b7d2016-11-23 09:16:3292 // Invoked when the Instant page wants to undo all Most Visited deletions.
[email protected]280be992013-06-04 03:19:5193 void UndoAllMostVisitedDeletions();
Kristi Parkb8aec552019-04-18 21:08:5694 // Invoked when the Instant page wants to switch between custom links and Most
95 // Visited. Toggles between the two options each time it's called. Returns
96 // false and does nothing if the profile is using a third-party NTP.
97 bool ToggleMostVisitedOrCustomLinks();
kristiparka52a3892018-07-24 18:46:4498 // Invoked when the Instant page wants to add a custom link.
kristiparkbe00c6562018-08-01 21:30:2799 bool AddCustomLink(const GURL& url, const std::string& title);
kristipark940dc202018-07-30 18:29:55100 // Invoked when the Instant page wants to update a custom link.
kristiparkbe00c6562018-08-01 21:30:27101 bool UpdateCustomLink(const GURL& url,
kristipark940dc202018-07-30 18:29:55102 const GURL& new_url,
103 const std::string& new_title);
Kristi Parka49acec2018-11-07 22:56:50104 // Invoked when the Instant page wants to reorder a custom link.
105 bool ReorderCustomLink(const GURL& url, int new_pos);
kristiparka52a3892018-07-24 18:46:44106 // Invoked when the Instant page wants to delete a custom link.
kristiparkbe00c6562018-08-01 21:30:27107 bool DeleteCustomLink(const GURL& url);
108 // Invoked when the Instant page wants to undo the previous custom link
Kristi Parkb8aec552019-04-18 21:08:56109 // action. Returns false and does nothing if the profile is using a third-
110 // party NTP.
Ramya Nagarajan7673f052018-08-13 22:53:29111 bool UndoCustomLinkAction();
kristiparka52a3892018-07-24 18:46:44112 // Invoked when the Instant page wants to delete all custom links and use Most
Ramya Nagarajan7673f052018-08-13 22:53:29113 // Visited sites instead. Returns false and does nothing if the profile is
Kristi Parkb8aec552019-04-18 21:08:56114 // using a third-party NTP. Marked virtual for mocking in tests.
Gayane Petrosyan99296102019-03-06 19:07:49115 virtual bool ResetCustomLinks();
[email protected]280be992013-06-04 03:19:51116
Kyle Milka5c0166d2019-04-03 23:57:14117 // Invoked to update theme information for the NTP.
Kyle Milkafb37a772018-06-25 16:09:19118 void UpdateThemeInfo();
[email protected]6af41782013-06-22 13:49:11119
Kyle Milka0d1b376e2019-01-28 19:34:10120 // Invoked when a background pref update is received via sync, triggering
121 // an update of theme info.
122 void UpdateBackgroundFromSync();
123
[email protected]ed68ae32013-06-29 20:46:48124 // Invoked by the InstantController to update most visited items details for
125 // NTP.
126 void UpdateMostVisitedItemsInfo();
127
Marc Treibc9c35002017-10-16 10:09:33128 // Sends the current NTP URL to a renderer process.
129 void SendNewTabPageURLToRenderer(content::RenderProcessHost* rph);
[email protected]2309e912013-10-01 01:33:30130
Kyle Milka64e205c2018-06-07 17:27:31131 // Invoked when a custom background is selected on the NTP.
132 void SetCustomBackgroundURL(const GURL& url);
133
Kyle Milka6986ede2018-07-19 02:16:24134 // Invoked when a custom background with attributions is selected on the NTP.
135 void SetCustomBackgroundURLWithAttributions(
136 const GURL& background_url,
137 const std::string& attribution_line_1,
138 const std::string& attribution_line_2,
139 const GURL& action_url);
140
Kyle Milka3626a7d2018-07-03 06:23:55141 // Invoked when a user selected the "Upload an image" option on the NTP.
142 void SelectLocalBackgroundImage(const base::FilePath& path);
143
Gayane Petrosyane85debf2019-03-06 06:19:13144 // Getter for |theme_info_| that will also initialize it if necessary.
145 ThemeBackgroundInfo* GetInitializedThemeInfo();
Kyle Milka6986ede2018-07-19 02:16:24146
Kristi Parkff3cf952019-01-28 23:00:59147 // Used for testing.
148 void SetDarkModeThemeForTesting(ui::NativeTheme* theme);
149
150 // Used for testing.
Kyle Milka31a76c6d2018-10-19 17:37:27151 void AddValidBackdropUrlForTesting(const GURL& url) const;
152
Kyle Milka427c8da2018-11-26 21:59:05153 // Check if a custom background has been set by the user.
154 bool IsCustomBackgroundSet();
155
Gayane Petrosyan99296102019-03-06 19:07:49156 // Reset all NTP customizations to default. Marked virtual for mocking in
157 // tests.
158 virtual void ResetToDefault();
159
Gayane Petrosyand257e7202019-05-21 03:43:38160 // Calculates the most frequent color of the image and stores it in prefs.
161 void UpdateCustomBackgroundColorAsync(
162 const GURL& image_url,
163 const gfx::Image& fetched_image,
164 const image_fetcher::RequestMetadata& metadata);
165
166 // Fetches the image for the given |fetch_url|.
167 void FetchCustomBackground(const GURL& image_url, const GURL& fetch_url);
168
[email protected]c5dec6292013-01-25 04:54:52169 private:
kristiparkf8fa4132018-09-13 18:07:29170 class SearchProviderObserver;
171
[email protected]4ff347e2013-07-22 19:39:00172 friend class InstantExtendedTest;
[email protected]c8a118e2013-09-24 21:33:40173 friend class InstantUnitTestBase;
Gayane Petrosyand257e7202019-05-21 03:43:38174 friend class LocalNTPBackgroundsAndDarkModeTest;
175 friend class TestInstantService;
[email protected]4ff347e2013-07-22 19:39:00176
[email protected]df113a12013-07-24 04:56:08177 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
Kristi Park999db912018-12-05 23:46:42178 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, DeleteThumbnailDataIfExists);
Marc Treib7b3985e2017-09-18 11:22:16179 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetNTPTileSuggestion);
Kristi Parke68931522019-06-08 00:17:18180 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest,
181 DoesToggleMostVisitedOrCustomLinks);
Kristi Parkb8aec552019-04-18 21:08:56182 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, IsCustomLinksEnabled);
Gayane Petrosyane85debf2019-03-06 06:19:13183 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, TestNoThemeInfo);
[email protected]4ff347e2013-07-22 19:39:00184
[email protected]c4b2af22014-05-11 19:48:53185 // KeyedService:
Daniel Chenga542fca2014-10-21 09:51:29186 void Shutdown() override;
[email protected]c5dec6292013-01-25 04:54:52187
[email protected]c4b2af22014-05-11 19:48:53188 // content::NotificationObserver:
Daniel Chenga542fca2014-10-21 09:51:29189 void Observe(int type,
190 const content::NotificationSource& source,
191 const content::NotificationDetails& details) override;
[email protected]54e0ee62013-02-01 17:25:03192
[email protected]2309e912013-10-01 01:33:30193 // Called when a renderer process is terminated.
194 void OnRendererProcessTerminated(int process_id);
195
kristiparkf8fa4132018-09-13 18:07:29196 // Called when the search provider changes. Disables custom links if the
197 // search provider is not Google.
Kristi Parkb8aec552019-04-18 21:08:56198 void OnSearchProviderChanged();
kristiparkf8fa4132018-09-13 18:07:29199
Kristi Parkff3cf952019-01-28 23:00:59200 // Called when dark mode changes. Updates current theme info as necessary and
201 // notifies that the theme has changed.
202 void OnDarkModeChanged(bool dark_mode);
203
treibbb0c5af52016-12-09 17:34:15204 // ntp_tiles::MostVisitedSites::Observer implementation.
Friedrich Horschig9e8749f02017-08-10 09:17:31205 void OnURLsAvailable(
206 const std::map<ntp_tiles::SectionType, ntp_tiles::NTPTilesVector>&
207 sections) override;
treibbb0c5af52016-12-09 17:34:15208 void OnIconMadeAvailable(const GURL& site_url) override;
[email protected]280be992013-06-04 03:19:51209
[email protected]ed68ae32013-06-29 20:46:48210 void NotifyAboutMostVisitedItems();
Marc Treib5891b282017-08-22 09:44:30211 void NotifyAboutThemeInfo();
[email protected]ed68ae32013-06-29 20:46:48212
Kristi Parkb8aec552019-04-18 21:08:56213 // Returns true if this is a Google NTP and the user has chosen to show custom
214 // links.
215 bool IsCustomLinksEnabled();
216
Marc Treib5891b282017-08-22 09:44:30217 void BuildThemeInfo();
[email protected]6af41782013-06-22 13:49:11218
Kyle Milka6986ede2018-07-19 02:16:24219 void ApplyOrResetCustomBackgroundThemeInfo();
220
Kyle Milka66bb07a2018-09-17 17:55:56221 void ApplyCustomBackgroundThemeInfo();
Kyle Milka66bb07a2018-09-17 17:55:56222
Gayane Petrosyan99296102019-03-06 19:07:49223 // Marked virtual for mocking in tests.
224 virtual void ResetCustomBackgroundThemeInfo();
Kyle Milkafb37a772018-06-25 16:09:19225
Kyle Milka66bb07a2018-09-17 17:55:56226 void FallbackToDefaultThemeInfo();
227
Kyle Milka31a76c6d2018-10-19 17:37:27228 void RemoveLocalBackgroundImageCopy();
229
Kristi Park999db912018-12-05 23:46:42230 // Remove old user thumbnail data if it exists. If |callback| is provided,
231 // calls back true if the thumbnail data was deleted. Thumbnails have been
232 // deprecated as of M69.
233 // TODO(crbug.com/893362): Remove after M75.
234 void DeleteThumbnailDataIfExists(
235 const base::FilePath& profile_path,
236 base::Optional<base::OnceCallback<void(bool)>> callback);
237
Kyle Milka427c8da2018-11-26 21:59:05238 // Returns false if the custom background pref cannot be parsed, otherwise
239 // returns true and sets custom_background_url to the value in the pref.
240 bool IsCustomBackgroundPrefValid(GURL& custom_background_url);
241
Kyle Milka3626a7d2018-07-03 06:23:55242 // Update the background pref to point to
243 // chrome-search://local-ntp/background.jpg
244 void SetBackgroundToLocalResource();
245
Dan Beam6877cef42019-01-30 21:19:03246 void CreateDarkModeObserver(ui::NativeTheme* theme);
247
Gayane Petrosyand257e7202019-05-21 03:43:38248 // Updates custom background prefs with color for the given |image_url|.
249 void UpdateCustomBackgroundPrefsWithColor(const GURL& image_url,
250 SkColor color);
251
252 void SetImageFetcherForTesting(image_fetcher::ImageFetcher* image_fetcher);
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
Dan Beam6877cef42019-01-30 21:19:03282 // Keeps track of any changes to system dark mode.
Rune Lillesveen862caf22019-04-24 17:21:57283 std::unique_ptr<ui::DarkModeObserver> dark_mode_observer_;
Dan Beam6877cef42019-01-30 21:19:03284
Kyle Milka31a76c6d2018-10-19 17:37:27285 NtpBackgroundService* background_service_;
286
Gayane Petrosyand257e7202019-05-21 03:43:38287 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_;
288
Kyle Milka3626a7d2018-07-03 06:23:55289 base::WeakPtrFactory<InstantService> weak_ptr_factory_;
290
[email protected]c5dec6292013-01-25 04:54:52291 DISALLOW_COPY_AND_ASSIGN(InstantService);
292};
293
[email protected]a7b8e43d2013-03-18 18:52:43294#endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_