blob: 89752c188921d6f4a8252fdb7ca958469b39fd03 [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"
brettw9b0866f2016-12-11 02:34:0618#include "build/build_config.h"
sdefresnebc766ef2014-09-25 09:28:1319#include "components/history/core/browser/history_types.h"
[email protected]540380fc2014-03-14 10:10:3420#include "components/keyed_service/core/keyed_service.h"
treibbb0c5af52016-12-09 17:34:1521#include "components/ntp_tiles/most_visited_sites.h"
22#include "components/ntp_tiles/ntp_tile.h"
Kyle Milka66bb07a2018-09-17 17:55:5623#include "components/prefs/pref_change_registrar.h"
Kyle Milka64e205c2018-06-07 17:27:3124#include "components/prefs/pref_registry_simple.h"
[email protected]54e0ee62013-02-01 17:25:0325#include "content/public/browser/notification_observer.h"
26#include "content/public/browser/notification_registrar.h"
[email protected]c4b2af22014-05-11 19:48:5327#include "url/gurl.h"
[email protected]c5dec6292013-01-25 04:54:5228
Marc Treib9028a6a2017-12-06 16:21:5129#if defined(OS_ANDROID)
30#error "Instant is only used on desktop";
31#endif
32
[email protected]672c8c12013-03-07 12:30:0633class InstantIOContext;
[email protected]6af41782013-06-22 13:49:1134class InstantServiceObserver;
Kyle Milka31a76c6d2018-10-19 17:37:2735class NtpBackgroundService;
[email protected]672c8c12013-03-07 12:30:0636class Profile;
treibc6892192017-02-15 13:03:3737struct InstantMostVisitedItem;
[email protected]c4b2af22014-05-11 19:48:5338struct ThemeBackgroundInfo;
[email protected]672c8c12013-03-07 12:30:0639
[email protected]4ff347e2013-07-22 19:39:0040namespace content {
[email protected]777590052014-01-17 22:11:5441class RenderProcessHost;
Kristi Parka5141b92018-10-10 01:23:5742} // namespace content
[email protected]4ff347e2013-07-22 19:39:0043
Marc Treibbe5f3feb2017-09-04 09:53:1644// 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]540380fc2014-03-14 10:10:3448class InstantService : public KeyedService,
[email protected]c4b2af22014-05-11 19:48:5349 public content::NotificationObserver,
treibbb0c5af52016-12-09 17:34:1550 public ntp_tiles::MostVisitedSites::Observer {
[email protected]c5dec6292013-01-25 04:54:5251 public:
[email protected]672c8c12013-03-07 12:30:0652 explicit InstantService(Profile* profile);
Daniel Chenga542fca2014-10-21 09:51:2953 ~InstantService() override;
[email protected]c5dec6292013-01-25 04:54:5254
55 // Add, remove, and query RenderProcessHost IDs that are associated with
56 // Instant processes.
57 void AddInstantProcess(int process_id);
[email protected]c5dec6292013-01-25 04:54:5258 bool IsInstantProcess(int process_id) const;
59
[email protected]6af41782013-06-22 13:49:1160 // Adds/Removes InstantService observers.
61 void AddObserver(InstantServiceObserver* observer);
62 void RemoveObserver(InstantServiceObserver* observer);
63
Kyle Milka64e205c2018-06-07 17:27:3164 // Register prefs associated with the NTP.
65 static void RegisterProfilePrefs(PrefRegistrySimple* registry);
66
[email protected]d572bfd2013-02-14 06:14:2067#if defined(UNIT_TEST)
68 int GetInstantProcessCount() const {
69 return process_ids_.size();
70 }
71#endif
[email protected]c5dec6292013-01-25 04:54:5272
treib2df1b7d2016-11-23 09:16:3273 // Invoked whenever an NTP is opened. Causes an async refresh of Most Visited
74 // items.
75 void OnNewTabPageOpened();
76
Xi Cheng72db9c0c2017-08-30 17:05:4077 // Most visited item APIs.
78 //
treib2df1b7d2016-11-23 09:16:3279 // Invoked when the Instant page wants to delete a Most Visited item.
[email protected]280be992013-06-04 03:19:5180 void DeleteMostVisitedItem(const GURL& url);
treib2df1b7d2016-11-23 09:16:3281 // Invoked when the Instant page wants to undo the deletion.
[email protected]280be992013-06-04 03:19:5182 void UndoMostVisitedDeletion(const GURL& url);
treib2df1b7d2016-11-23 09:16:3283 // Invoked when the Instant page wants to undo all Most Visited deletions.
[email protected]280be992013-06-04 03:19:5184 void UndoAllMostVisitedDeletions();
kristiparka52a3892018-07-24 18:46:4485 // Invoked when the Instant page wants to add a custom link.
kristiparkbe00c6562018-08-01 21:30:2786 bool AddCustomLink(const GURL& url, const std::string& title);
kristipark940dc202018-07-30 18:29:5587 // Invoked when the Instant page wants to update a custom link.
kristiparkbe00c6562018-08-01 21:30:2788 bool UpdateCustomLink(const GURL& url,
kristipark940dc202018-07-30 18:29:5589 const GURL& new_url,
90 const std::string& new_title);
kristiparka52a3892018-07-24 18:46:4491 // Invoked when the Instant page wants to delete a custom link.
kristiparkbe00c6562018-08-01 21:30:2792 bool DeleteCustomLink(const GURL& url);
93 // Invoked when the Instant page wants to undo the previous custom link
Ramya Nagarajan7673f052018-08-13 22:53:2994 // action. Returns false and does nothing if the profile is using a non-Google
95 // search provider.
96 bool UndoCustomLinkAction();
kristiparka52a3892018-07-24 18:46:4497 // Invoked when the Instant page wants to delete all custom links and use Most
Ramya Nagarajan7673f052018-08-13 22:53:2998 // Visited sites instead. Returns false and does nothing if the profile is
99 // using a non-Google search provider.
100 bool ResetCustomLinks();
[email protected]280be992013-06-04 03:19:51101
[email protected]6af41782013-06-22 13:49:11102 // Invoked by the InstantController to update theme information for NTP.
103 //
104 // TODO(kmadhusu): Invoking this from InstantController shouldn't be
105 // necessary. Investigate more and remove this from here.
Kyle Milkafb37a772018-06-25 16:09:19106 void UpdateThemeInfo();
[email protected]6af41782013-06-22 13:49:11107
[email protected]ed68ae32013-06-29 20:46:48108 // Invoked by the InstantController to update most visited items details for
109 // NTP.
110 void UpdateMostVisitedItemsInfo();
111
Marc Treibc9c35002017-10-16 10:09:33112 // Sends the current NTP URL to a renderer process.
113 void SendNewTabPageURLToRenderer(content::RenderProcessHost* rph);
[email protected]2309e912013-10-01 01:33:30114
Kyle Milka64e205c2018-06-07 17:27:31115 // Invoked when a custom background is selected on the NTP.
116 void SetCustomBackgroundURL(const GURL& url);
117
Kyle Milka6986ede2018-07-19 02:16:24118 // Invoked when a custom background with attributions is selected on the NTP.
119 void SetCustomBackgroundURLWithAttributions(
120 const GURL& background_url,
121 const std::string& attribution_line_1,
122 const std::string& attribution_line_2,
123 const GURL& action_url);
124
Kyle Milka3626a7d2018-07-03 06:23:55125 // Invoked when a user selected the "Upload an image" option on the NTP.
126 void SelectLocalBackgroundImage(const base::FilePath& path);
127
Kyle Milka6986ede2018-07-19 02:16:24128 // Used for testing.
129 ThemeBackgroundInfo* GetThemeInfoForTesting() { return theme_info_.get(); }
130
Kyle Milka31a76c6d2018-10-19 17:37:27131 void AddValidBackdropUrlForTesting(const GURL& url) const;
132
[email protected]c5dec6292013-01-25 04:54:52133 private:
kristiparkf8fa4132018-09-13 18:07:29134 class SearchProviderObserver;
135
[email protected]4ff347e2013-07-22 19:39:00136 friend class InstantExtendedTest;
[email protected]c8a118e2013-09-24 21:33:40137 friend class InstantUnitTestBase;
[email protected]4ff347e2013-07-22 19:39:00138
[email protected]df113a12013-07-24 04:56:08139 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
Marc Treib7b3985e2017-09-18 11:22:16140 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetNTPTileSuggestion);
[email protected]4ff347e2013-07-22 19:39:00141
[email protected]c4b2af22014-05-11 19:48:53142 // KeyedService:
Daniel Chenga542fca2014-10-21 09:51:29143 void Shutdown() override;
[email protected]c5dec6292013-01-25 04:54:52144
[email protected]c4b2af22014-05-11 19:48:53145 // content::NotificationObserver:
Daniel Chenga542fca2014-10-21 09:51:29146 void Observe(int type,
147 const content::NotificationSource& source,
148 const content::NotificationDetails& details) override;
[email protected]54e0ee62013-02-01 17:25:03149
[email protected]2309e912013-10-01 01:33:30150 // Called when a renderer process is terminated.
151 void OnRendererProcessTerminated(int process_id);
152
kristiparkf8fa4132018-09-13 18:07:29153 // Called when the search provider changes. Disables custom links if the
154 // search provider is not Google.
155 void OnSearchProviderChanged(bool is_google);
156
treibbb0c5af52016-12-09 17:34:15157 // ntp_tiles::MostVisitedSites::Observer implementation.
Friedrich Horschig9e8749f02017-08-10 09:17:31158 void OnURLsAvailable(
159 const std::map<ntp_tiles::SectionType, ntp_tiles::NTPTilesVector>&
160 sections) override;
treibbb0c5af52016-12-09 17:34:15161 void OnIconMadeAvailable(const GURL& site_url) override;
[email protected]280be992013-06-04 03:19:51162
[email protected]ed68ae32013-06-29 20:46:48163 void NotifyAboutMostVisitedItems();
Marc Treib5891b282017-08-22 09:44:30164 void NotifyAboutThemeInfo();
[email protected]ed68ae32013-06-29 20:46:48165
Marc Treib5891b282017-08-22 09:44:30166 void BuildThemeInfo();
[email protected]6af41782013-06-22 13:49:11167
Kyle Milka6986ede2018-07-19 02:16:24168 void ApplyOrResetCustomBackgroundThemeInfo();
169
Kyle Milka66bb07a2018-09-17 17:55:56170 void ApplyCustomBackgroundThemeInfo();
171 void ApplyCustomBackgroundThemeInfoFromLocalFile(bool file_exists);
172
Kyle Milka6986ede2018-07-19 02:16:24173 void ResetCustomBackgroundThemeInfo();
Kyle Milkafb37a772018-06-25 16:09:19174
Kyle Milka66bb07a2018-09-17 17:55:56175 void FallbackToDefaultThemeInfo();
176
Kyle Milka31a76c6d2018-10-19 17:37:27177 void RemoveLocalBackgroundImageCopy();
178
Kyle Milka3626a7d2018-07-03 06:23:55179 // Update the background pref to point to
180 // chrome-search://local-ntp/background.jpg
181 void SetBackgroundToLocalResource();
182
[email protected]672c8c12013-03-07 12:30:06183 Profile* const profile_;
184
[email protected]c5dec6292013-01-25 04:54:52185 // The process ids associated with Instant processes.
186 std::set<int> process_ids_;
187
Marc Treib7b3985e2017-09-18 11:22:16188 // InstantMostVisitedItems for NTP tiles, received from |most_visited_sites_|.
[email protected]ab01dd752013-06-08 05:13:36189 std::vector<InstantMostVisitedItem> most_visited_items_;
[email protected]e7868c82013-03-12 03:55:56190
[email protected]6af41782013-06-22 13:49:11191 // Theme-related data for NTP overlay to adopt themes.
dcheng4af48582016-04-19 00:29:35192 std::unique_ptr<ThemeBackgroundInfo> theme_info_;
[email protected]6af41782013-06-22 13:49:11193
Trent Apteda250ec3ab2018-08-19 08:52:19194 base::ObserverList<InstantServiceObserver>::Unchecked observers_;
[email protected]6af41782013-06-22 13:49:11195
[email protected]54e0ee62013-02-01 17:25:03196 content::NotificationRegistrar registrar_;
197
[email protected]672c8c12013-03-07 12:30:06198 scoped_refptr<InstantIOContext> instant_io_context_;
199
Marc Treib7b3985e2017-09-18 11:22:16200 // Data source for NTP tiles (aka Most Visited tiles). May be null.
treibbb0c5af52016-12-09 17:34:15201 std::unique_ptr<ntp_tiles::MostVisitedSites> most_visited_sites_;
mohan.reddy74595362014-09-23 20:35:34202
kristiparkf8fa4132018-09-13 18:07:29203 // Keeps track of any changes in search engine provider. May be null.
204 std::unique_ptr<SearchProviderObserver> search_provider_observer_;
205
Kyle Milka66bb07a2018-09-17 17:55:56206 PrefChangeRegistrar pref_change_registrar_;
207
208 PrefService* pref_service_;
209
Kyle Milka31a76c6d2018-10-19 17:37:27210 NtpBackgroundService* background_service_;
211
Kyle Milka3626a7d2018-07-03 06:23:55212 base::WeakPtrFactory<InstantService> weak_ptr_factory_;
213
[email protected]c5dec6292013-01-25 04:54:52214 DISALLOW_COPY_AND_ASSIGN(InstantService);
215};
216
[email protected]a7b8e43d2013-03-18 18:52:43217#endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_