blob: 319bab8191037036da9a99bf4dbabc1d198226ae [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"
[email protected]540380fc2014-03-14 10:10:3421#include "components/keyed_service/core/keyed_service.h"
treibbb0c5af52016-12-09 17:34:1522#include "components/ntp_tiles/most_visited_sites.h"
23#include "components/ntp_tiles/ntp_tile.h"
Kyle Milka66bb07a2018-09-17 17:55:5624#include "components/prefs/pref_change_registrar.h"
Kyle Milka64e205c2018-06-07 17:27:3125#include "components/prefs/pref_registry_simple.h"
[email protected]54e0ee62013-02-01 17:25:0326#include "content/public/browser/notification_observer.h"
27#include "content/public/browser/notification_registrar.h"
Kristi Parkff3cf952019-01-28 23:00:5928#include "ui/native_theme/native_theme.h"
[email protected]c4b2af22014-05-11 19:48:5329#include "url/gurl.h"
[email protected]c5dec6292013-01-25 04:54:5230
Marc Treib9028a6a2017-12-06 16:21:5131#if defined(OS_ANDROID)
32#error "Instant is only used on desktop";
33#endif
34
[email protected]672c8c12013-03-07 12:30:0635class InstantIOContext;
[email protected]6af41782013-06-22 13:49:1136class InstantServiceObserver;
Kyle Milka31a76c6d2018-10-19 17:37:2737class NtpBackgroundService;
[email protected]672c8c12013-03-07 12:30:0638class Profile;
treibc6892192017-02-15 13:03:3739struct InstantMostVisitedItem;
[email protected]c4b2af22014-05-11 19:48:5340struct ThemeBackgroundInfo;
[email protected]672c8c12013-03-07 12:30:0641
[email protected]4ff347e2013-07-22 19:39:0042namespace content {
[email protected]777590052014-01-17 22:11:5443class RenderProcessHost;
Kristi Parka5141b92018-10-10 01:23:5744} // namespace content
[email protected]4ff347e2013-07-22 19:39:0045
Rune Lillesveen862caf22019-04-24 17:21:5746namespace ui {
47class DarkModeObserver;
48} // namespace ui
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,
treibbb0c5af52016-12-09 17:34:1556 public ntp_tiles::MostVisitedSites::Observer {
[email protected]c5dec6292013-01-25 04:54:5257 public:
[email protected]672c8c12013-03-07 12:30:0658 explicit InstantService(Profile* profile);
Daniel Chenga542fca2014-10-21 09:51:2959 ~InstantService() override;
[email protected]c5dec6292013-01-25 04:54:5260
61 // Add, remove, and query RenderProcessHost IDs that are associated with
62 // Instant processes.
63 void AddInstantProcess(int process_id);
[email protected]c5dec6292013-01-25 04:54:5264 bool IsInstantProcess(int process_id) const;
65
[email protected]6af41782013-06-22 13:49:1166 // Adds/Removes InstantService observers.
67 void AddObserver(InstantServiceObserver* observer);
68 void RemoveObserver(InstantServiceObserver* observer);
69
Kyle Milka64e205c2018-06-07 17:27:3170 // Register prefs associated with the NTP.
71 static void RegisterProfilePrefs(PrefRegistrySimple* registry);
72
[email protected]d572bfd2013-02-14 06:14:2073#if defined(UNIT_TEST)
74 int GetInstantProcessCount() const {
75 return process_ids_.size();
76 }
77#endif
[email protected]c5dec6292013-01-25 04:54:5278
treib2df1b7d2016-11-23 09:16:3279 // Invoked whenever an NTP is opened. Causes an async refresh of Most Visited
80 // items.
81 void OnNewTabPageOpened();
82
Xi Cheng72db9c0c2017-08-30 17:05:4083 // Most visited item APIs.
84 //
treib2df1b7d2016-11-23 09:16:3285 // Invoked when the Instant page wants to delete a Most Visited item.
[email protected]280be992013-06-04 03:19:5186 void DeleteMostVisitedItem(const GURL& url);
treib2df1b7d2016-11-23 09:16:3287 // Invoked when the Instant page wants to undo the deletion.
[email protected]280be992013-06-04 03:19:5188 void UndoMostVisitedDeletion(const GURL& url);
treib2df1b7d2016-11-23 09:16:3289 // Invoked when the Instant page wants to undo all Most Visited deletions.
[email protected]280be992013-06-04 03:19:5190 void UndoAllMostVisitedDeletions();
Kristi Parkb8aec552019-04-18 21:08:5691 // Invoked when the Instant page wants to switch between custom links and Most
92 // Visited. Toggles between the two options each time it's called. Returns
93 // false and does nothing if the profile is using a third-party NTP.
94 bool ToggleMostVisitedOrCustomLinks();
kristiparka52a3892018-07-24 18:46:4495 // Invoked when the Instant page wants to add a custom link.
kristiparkbe00c6562018-08-01 21:30:2796 bool AddCustomLink(const GURL& url, const std::string& title);
kristipark940dc202018-07-30 18:29:5597 // Invoked when the Instant page wants to update a custom link.
kristiparkbe00c6562018-08-01 21:30:2798 bool UpdateCustomLink(const GURL& url,
kristipark940dc202018-07-30 18:29:5599 const GURL& new_url,
100 const std::string& new_title);
Kristi Parka49acec2018-11-07 22:56:50101 // Invoked when the Instant page wants to reorder a custom link.
102 bool ReorderCustomLink(const GURL& url, int new_pos);
kristiparka52a3892018-07-24 18:46:44103 // Invoked when the Instant page wants to delete a custom link.
kristiparkbe00c6562018-08-01 21:30:27104 bool DeleteCustomLink(const GURL& url);
105 // Invoked when the Instant page wants to undo the previous custom link
Kristi Parkb8aec552019-04-18 21:08:56106 // action. Returns false and does nothing if the profile is using a third-
107 // party NTP.
Ramya Nagarajan7673f052018-08-13 22:53:29108 bool UndoCustomLinkAction();
kristiparka52a3892018-07-24 18:46:44109 // Invoked when the Instant page wants to delete all custom links and use Most
Ramya Nagarajan7673f052018-08-13 22:53:29110 // Visited sites instead. Returns false and does nothing if the profile is
Kristi Parkb8aec552019-04-18 21:08:56111 // using a third-party NTP. Marked virtual for mocking in tests.
Gayane Petrosyan99296102019-03-06 19:07:49112 virtual bool ResetCustomLinks();
[email protected]280be992013-06-04 03:19:51113
Kyle Milka5c0166d2019-04-03 23:57:14114 // Invoked to update theme information for the NTP.
Kyle Milkafb37a772018-06-25 16:09:19115 void UpdateThemeInfo();
[email protected]6af41782013-06-22 13:49:11116
Kyle Milka0d1b376e2019-01-28 19:34:10117 // Invoked when a background pref update is received via sync, triggering
118 // an update of theme info.
119 void UpdateBackgroundFromSync();
120
[email protected]ed68ae32013-06-29 20:46:48121 // Invoked by the InstantController to update most visited items details for
122 // NTP.
123 void UpdateMostVisitedItemsInfo();
124
Marc Treibc9c35002017-10-16 10:09:33125 // Sends the current NTP URL to a renderer process.
126 void SendNewTabPageURLToRenderer(content::RenderProcessHost* rph);
[email protected]2309e912013-10-01 01:33:30127
Kyle Milka64e205c2018-06-07 17:27:31128 // Invoked when a custom background is selected on the NTP.
129 void SetCustomBackgroundURL(const GURL& url);
130
Kyle Milka6986ede2018-07-19 02:16:24131 // Invoked when a custom background with attributions is selected on the NTP.
132 void SetCustomBackgroundURLWithAttributions(
133 const GURL& background_url,
134 const std::string& attribution_line_1,
135 const std::string& attribution_line_2,
136 const GURL& action_url);
137
Kyle Milka3626a7d2018-07-03 06:23:55138 // Invoked when a user selected the "Upload an image" option on the NTP.
139 void SelectLocalBackgroundImage(const base::FilePath& path);
140
Gayane Petrosyane85debf2019-03-06 06:19:13141 // Getter for |theme_info_| that will also initialize it if necessary.
142 ThemeBackgroundInfo* GetInitializedThemeInfo();
Kyle Milka6986ede2018-07-19 02:16:24143
Kristi Parkff3cf952019-01-28 23:00:59144 // Used for testing.
145 void SetDarkModeThemeForTesting(ui::NativeTheme* theme);
146
147 // Used for testing.
Kyle Milka31a76c6d2018-10-19 17:37:27148 void AddValidBackdropUrlForTesting(const GURL& url) const;
149
Kyle Milka427c8da2018-11-26 21:59:05150 // Check if a custom background has been set by the user.
151 bool IsCustomBackgroundSet();
152
Gayane Petrosyan99296102019-03-06 19:07:49153 // Reset all NTP customizations to default. Marked virtual for mocking in
154 // tests.
155 virtual void ResetToDefault();
156
[email protected]c5dec6292013-01-25 04:54:52157 private:
kristiparkf8fa4132018-09-13 18:07:29158 class SearchProviderObserver;
159
[email protected]4ff347e2013-07-22 19:39:00160 friend class InstantExtendedTest;
[email protected]c8a118e2013-09-24 21:33:40161 friend class InstantUnitTestBase;
[email protected]4ff347e2013-07-22 19:39:00162
[email protected]df113a12013-07-24 04:56:08163 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
Kristi Park999db912018-12-05 23:46:42164 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, DeleteThumbnailDataIfExists);
Marc Treib7b3985e2017-09-18 11:22:16165 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetNTPTileSuggestion);
Kristi Parkb8aec552019-04-18 21:08:56166 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, IsCustomLinksEnabled);
Gayane Petrosyane85debf2019-03-06 06:19:13167 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, TestNoThemeInfo);
[email protected]4ff347e2013-07-22 19:39:00168
[email protected]c4b2af22014-05-11 19:48:53169 // KeyedService:
Daniel Chenga542fca2014-10-21 09:51:29170 void Shutdown() override;
[email protected]c5dec6292013-01-25 04:54:52171
[email protected]c4b2af22014-05-11 19:48:53172 // content::NotificationObserver:
Daniel Chenga542fca2014-10-21 09:51:29173 void Observe(int type,
174 const content::NotificationSource& source,
175 const content::NotificationDetails& details) override;
[email protected]54e0ee62013-02-01 17:25:03176
[email protected]2309e912013-10-01 01:33:30177 // Called when a renderer process is terminated.
178 void OnRendererProcessTerminated(int process_id);
179
kristiparkf8fa4132018-09-13 18:07:29180 // Called when the search provider changes. Disables custom links if the
181 // search provider is not Google.
Kristi Parkb8aec552019-04-18 21:08:56182 void OnSearchProviderChanged();
kristiparkf8fa4132018-09-13 18:07:29183
Kristi Parkff3cf952019-01-28 23:00:59184 // Called when dark mode changes. Updates current theme info as necessary and
185 // notifies that the theme has changed.
186 void OnDarkModeChanged(bool dark_mode);
187
treibbb0c5af52016-12-09 17:34:15188 // ntp_tiles::MostVisitedSites::Observer implementation.
Friedrich Horschig9e8749f02017-08-10 09:17:31189 void OnURLsAvailable(
190 const std::map<ntp_tiles::SectionType, ntp_tiles::NTPTilesVector>&
191 sections) override;
treibbb0c5af52016-12-09 17:34:15192 void OnIconMadeAvailable(const GURL& site_url) override;
[email protected]280be992013-06-04 03:19:51193
[email protected]ed68ae32013-06-29 20:46:48194 void NotifyAboutMostVisitedItems();
Marc Treib5891b282017-08-22 09:44:30195 void NotifyAboutThemeInfo();
[email protected]ed68ae32013-06-29 20:46:48196
Kristi Parkb8aec552019-04-18 21:08:56197 // Returns true if this is a Google NTP and the user has chosen to show custom
198 // links.
199 bool IsCustomLinksEnabled();
200
Marc Treib5891b282017-08-22 09:44:30201 void BuildThemeInfo();
[email protected]6af41782013-06-22 13:49:11202
Kyle Milka6986ede2018-07-19 02:16:24203 void ApplyOrResetCustomBackgroundThemeInfo();
204
Kyle Milka66bb07a2018-09-17 17:55:56205 void ApplyCustomBackgroundThemeInfo();
Kyle Milka66bb07a2018-09-17 17:55:56206
Gayane Petrosyan99296102019-03-06 19:07:49207 // Marked virtual for mocking in tests.
208 virtual void ResetCustomBackgroundThemeInfo();
Kyle Milkafb37a772018-06-25 16:09:19209
Kyle Milka66bb07a2018-09-17 17:55:56210 void FallbackToDefaultThemeInfo();
211
Kyle Milka31a76c6d2018-10-19 17:37:27212 void RemoveLocalBackgroundImageCopy();
213
Kristi Park999db912018-12-05 23:46:42214 // Remove old user thumbnail data if it exists. If |callback| is provided,
215 // calls back true if the thumbnail data was deleted. Thumbnails have been
216 // deprecated as of M69.
217 // TODO(crbug.com/893362): Remove after M75.
218 void DeleteThumbnailDataIfExists(
219 const base::FilePath& profile_path,
220 base::Optional<base::OnceCallback<void(bool)>> callback);
221
Kyle Milka427c8da2018-11-26 21:59:05222 // Returns false if the custom background pref cannot be parsed, otherwise
223 // returns true and sets custom_background_url to the value in the pref.
224 bool IsCustomBackgroundPrefValid(GURL& custom_background_url);
225
Kyle Milka3626a7d2018-07-03 06:23:55226 // Update the background pref to point to
227 // chrome-search://local-ntp/background.jpg
228 void SetBackgroundToLocalResource();
229
Dan Beam6877cef42019-01-30 21:19:03230 void CreateDarkModeObserver(ui::NativeTheme* theme);
231
[email protected]672c8c12013-03-07 12:30:06232 Profile* const profile_;
233
[email protected]c5dec6292013-01-25 04:54:52234 // The process ids associated with Instant processes.
235 std::set<int> process_ids_;
236
Marc Treib7b3985e2017-09-18 11:22:16237 // InstantMostVisitedItems for NTP tiles, received from |most_visited_sites_|.
[email protected]ab01dd752013-06-08 05:13:36238 std::vector<InstantMostVisitedItem> most_visited_items_;
[email protected]e7868c82013-03-12 03:55:56239
[email protected]6af41782013-06-22 13:49:11240 // Theme-related data for NTP overlay to adopt themes.
dcheng4af48582016-04-19 00:29:35241 std::unique_ptr<ThemeBackgroundInfo> theme_info_;
[email protected]6af41782013-06-22 13:49:11242
Trent Apteda250ec3ab2018-08-19 08:52:19243 base::ObserverList<InstantServiceObserver>::Unchecked observers_;
[email protected]6af41782013-06-22 13:49:11244
[email protected]54e0ee62013-02-01 17:25:03245 content::NotificationRegistrar registrar_;
246
[email protected]672c8c12013-03-07 12:30:06247 scoped_refptr<InstantIOContext> instant_io_context_;
248
Marc Treib7b3985e2017-09-18 11:22:16249 // Data source for NTP tiles (aka Most Visited tiles). May be null.
treibbb0c5af52016-12-09 17:34:15250 std::unique_ptr<ntp_tiles::MostVisitedSites> most_visited_sites_;
mohan.reddy74595362014-09-23 20:35:34251
kristiparkf8fa4132018-09-13 18:07:29252 // Keeps track of any changes in search engine provider. May be null.
253 std::unique_ptr<SearchProviderObserver> search_provider_observer_;
254
Kyle Milka66bb07a2018-09-17 17:55:56255 PrefChangeRegistrar pref_change_registrar_;
256
257 PrefService* pref_service_;
258
Dan Beam6877cef42019-01-30 21:19:03259 // Keeps track of any changes to system dark mode.
Rune Lillesveen862caf22019-04-24 17:21:57260 std::unique_ptr<ui::DarkModeObserver> dark_mode_observer_;
Dan Beam6877cef42019-01-30 21:19:03261
Kyle Milka31a76c6d2018-10-19 17:37:27262 NtpBackgroundService* background_service_;
263
Kyle Milka3626a7d2018-07-03 06:23:55264 base::WeakPtrFactory<InstantService> weak_ptr_factory_;
265
[email protected]c5dec6292013-01-25 04:54:52266 DISALLOW_COPY_AND_ASSIGN(InstantService);
267};
268
[email protected]a7b8e43d2013-03-18 18:52:43269#endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_