blob: 3bdb9c1021f3d4642f77f1e51ba360c1a3889453 [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 Milka64e205c2018-06-07 17:27:3123#include "components/prefs/pref_registry_simple.h"
[email protected]54e0ee62013-02-01 17:25:0324#include "content/public/browser/notification_observer.h"
25#include "content/public/browser/notification_registrar.h"
[email protected]c4b2af22014-05-11 19:48:5326#include "url/gurl.h"
[email protected]c5dec6292013-01-25 04:54:5227
Marc Treib9028a6a2017-12-06 16:21:5128#if defined(OS_ANDROID)
29#error "Instant is only used on desktop";
30#endif
31
[email protected]672c8c12013-03-07 12:30:0632class InstantIOContext;
[email protected]6af41782013-06-22 13:49:1133class InstantServiceObserver;
[email protected]672c8c12013-03-07 12:30:0634class Profile;
treibc6892192017-02-15 13:03:3735struct InstantMostVisitedItem;
[email protected]c4b2af22014-05-11 19:48:5336struct ThemeBackgroundInfo;
[email protected]672c8c12013-03-07 12:30:0637
[email protected]4ff347e2013-07-22 19:39:0038namespace content {
[email protected]777590052014-01-17 22:11:5439class RenderProcessHost;
[email protected]4ff347e2013-07-22 19:39:0040}
41
Marc Treibbe5f3feb2017-09-04 09:53:1642// Tracks render process host IDs that are associated with Instant, i.e.
43// processes that are used to render an NTP. Also responsible for keeping
44// necessary information (most visited tiles and theme info) updated in those
45// renderer processes.
[email protected]540380fc2014-03-14 10:10:3446class InstantService : public KeyedService,
[email protected]c4b2af22014-05-11 19:48:5347 public content::NotificationObserver,
treibbb0c5af52016-12-09 17:34:1548 public ntp_tiles::MostVisitedSites::Observer {
[email protected]c5dec6292013-01-25 04:54:5249 public:
[email protected]672c8c12013-03-07 12:30:0650 explicit InstantService(Profile* profile);
Daniel Chenga542fca2014-10-21 09:51:2951 ~InstantService() override;
[email protected]c5dec6292013-01-25 04:54:5252
53 // Add, remove, and query RenderProcessHost IDs that are associated with
54 // Instant processes.
55 void AddInstantProcess(int process_id);
[email protected]c5dec6292013-01-25 04:54:5256 bool IsInstantProcess(int process_id) const;
57
[email protected]6af41782013-06-22 13:49:1158 // Adds/Removes InstantService observers.
59 void AddObserver(InstantServiceObserver* observer);
60 void RemoveObserver(InstantServiceObserver* observer);
61
Kyle Milka64e205c2018-06-07 17:27:3162 // Register prefs associated with the NTP.
63 static void RegisterProfilePrefs(PrefRegistrySimple* registry);
64
[email protected]d572bfd2013-02-14 06:14:2065#if defined(UNIT_TEST)
66 int GetInstantProcessCount() const {
67 return process_ids_.size();
68 }
69#endif
[email protected]c5dec6292013-01-25 04:54:5270
treib2df1b7d2016-11-23 09:16:3271 // Invoked whenever an NTP is opened. Causes an async refresh of Most Visited
72 // items.
73 void OnNewTabPageOpened();
74
Xi Cheng72db9c0c2017-08-30 17:05:4075 // Most visited item APIs.
76 //
treib2df1b7d2016-11-23 09:16:3277 // Invoked when the Instant page wants to delete a Most Visited item.
[email protected]280be992013-06-04 03:19:5178 void DeleteMostVisitedItem(const GURL& url);
treib2df1b7d2016-11-23 09:16:3279 // Invoked when the Instant page wants to undo the deletion.
[email protected]280be992013-06-04 03:19:5180 void UndoMostVisitedDeletion(const GURL& url);
treib2df1b7d2016-11-23 09:16:3281 // Invoked when the Instant page wants to undo all Most Visited deletions.
[email protected]280be992013-06-04 03:19:5182 void UndoAllMostVisitedDeletions();
kristiparka52a3892018-07-24 18:46:4483 // Invoked when the Instant page wants to add a custom link.
84 void AddCustomLink(const GURL& url, const std::string& title);
kristipark940dc202018-07-30 18:29:5585 // Invoked when the Instant page wants to update a custom link.
86 void UpdateCustomLink(const GURL& url,
87 const GURL& new_url,
88 const std::string& new_title);
kristiparka52a3892018-07-24 18:46:4489 // Invoked when the Instant page wants to delete a custom link.
90 void DeleteCustomLink(const GURL& url);
91 // Invoked when the Instant page wants to restore the previously deleted
92 // custom link.
93 void UndoDeleteCustomLink();
94 // Invoked when the Instant page wants to delete all custom links and use Most
95 // Visited sites instead.
96 void ResetCustomLinks();
[email protected]280be992013-06-04 03:19:5197
[email protected]6af41782013-06-22 13:49:1198 // Invoked by the InstantController to update theme information for NTP.
99 //
100 // TODO(kmadhusu): Invoking this from InstantController shouldn't be
101 // necessary. Investigate more and remove this from here.
Kyle Milkafb37a772018-06-25 16:09:19102 void UpdateThemeInfo();
[email protected]6af41782013-06-22 13:49:11103
[email protected]ed68ae32013-06-29 20:46:48104 // Invoked by the InstantController to update most visited items details for
105 // NTP.
106 void UpdateMostVisitedItemsInfo();
107
Marc Treibc9c35002017-10-16 10:09:33108 // Sends the current NTP URL to a renderer process.
109 void SendNewTabPageURLToRenderer(content::RenderProcessHost* rph);
[email protected]2309e912013-10-01 01:33:30110
Kyle Milka64e205c2018-06-07 17:27:31111 // Invoked when a custom background is selected on the NTP.
112 void SetCustomBackgroundURL(const GURL& url);
113
Kyle Milka6986ede2018-07-19 02:16:24114 // Invoked when a custom background with attributions is selected on the NTP.
115 void SetCustomBackgroundURLWithAttributions(
116 const GURL& background_url,
117 const std::string& attribution_line_1,
118 const std::string& attribution_line_2,
119 const GURL& action_url);
120
Kyle Milka3626a7d2018-07-03 06:23:55121 // Invoked when a user selected the "Upload an image" option on the NTP.
122 void SelectLocalBackgroundImage(const base::FilePath& path);
123
Kyle Milka6986ede2018-07-19 02:16:24124 // Used for testing.
125 ThemeBackgroundInfo* GetThemeInfoForTesting() { return theme_info_.get(); }
126
[email protected]c5dec6292013-01-25 04:54:52127 private:
[email protected]4ff347e2013-07-22 19:39:00128 friend class InstantExtendedTest;
[email protected]c8a118e2013-09-24 21:33:40129 friend class InstantUnitTestBase;
[email protected]4ff347e2013-07-22 19:39:00130
[email protected]df113a12013-07-24 04:56:08131 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
Marc Treib7b3985e2017-09-18 11:22:16132 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetNTPTileSuggestion);
[email protected]4ff347e2013-07-22 19:39:00133
[email protected]c4b2af22014-05-11 19:48:53134 // KeyedService:
Daniel Chenga542fca2014-10-21 09:51:29135 void Shutdown() override;
[email protected]c5dec6292013-01-25 04:54:52136
[email protected]c4b2af22014-05-11 19:48:53137 // content::NotificationObserver:
Daniel Chenga542fca2014-10-21 09:51:29138 void Observe(int type,
139 const content::NotificationSource& source,
140 const content::NotificationDetails& details) override;
[email protected]54e0ee62013-02-01 17:25:03141
[email protected]2309e912013-10-01 01:33:30142 // Called when a renderer process is terminated.
143 void OnRendererProcessTerminated(int process_id);
144
treibbb0c5af52016-12-09 17:34:15145 // ntp_tiles::MostVisitedSites::Observer implementation.
Friedrich Horschig9e8749f02017-08-10 09:17:31146 void OnURLsAvailable(
147 const std::map<ntp_tiles::SectionType, ntp_tiles::NTPTilesVector>&
148 sections) override;
treibbb0c5af52016-12-09 17:34:15149 void OnIconMadeAvailable(const GURL& site_url) override;
[email protected]280be992013-06-04 03:19:51150
[email protected]ed68ae32013-06-29 20:46:48151 void NotifyAboutMostVisitedItems();
Marc Treib5891b282017-08-22 09:44:30152 void NotifyAboutThemeInfo();
[email protected]ed68ae32013-06-29 20:46:48153
Marc Treib5891b282017-08-22 09:44:30154 void BuildThemeInfo();
[email protected]6af41782013-06-22 13:49:11155
Kyle Milka6986ede2018-07-19 02:16:24156 void ApplyOrResetCustomBackgroundThemeInfo();
157
158 void ResetCustomBackgroundThemeInfo();
Kyle Milkafb37a772018-06-25 16:09:19159
Kyle Milka3626a7d2018-07-03 06:23:55160 // Update the background pref to point to
161 // chrome-search://local-ntp/background.jpg
162 void SetBackgroundToLocalResource();
163
[email protected]672c8c12013-03-07 12:30:06164 Profile* const profile_;
165
[email protected]c5dec6292013-01-25 04:54:52166 // The process ids associated with Instant processes.
167 std::set<int> process_ids_;
168
Marc Treib7b3985e2017-09-18 11:22:16169 // InstantMostVisitedItems for NTP tiles, received from |most_visited_sites_|.
[email protected]ab01dd752013-06-08 05:13:36170 std::vector<InstantMostVisitedItem> most_visited_items_;
[email protected]e7868c82013-03-12 03:55:56171
[email protected]6af41782013-06-22 13:49:11172 // Theme-related data for NTP overlay to adopt themes.
dcheng4af48582016-04-19 00:29:35173 std::unique_ptr<ThemeBackgroundInfo> theme_info_;
[email protected]6af41782013-06-22 13:49:11174
brettwd195c952015-06-02 17:31:12175 base::ObserverList<InstantServiceObserver> observers_;
[email protected]6af41782013-06-22 13:49:11176
[email protected]54e0ee62013-02-01 17:25:03177 content::NotificationRegistrar registrar_;
178
[email protected]672c8c12013-03-07 12:30:06179 scoped_refptr<InstantIOContext> instant_io_context_;
180
Marc Treib7b3985e2017-09-18 11:22:16181 // Data source for NTP tiles (aka Most Visited tiles). May be null.
treibbb0c5af52016-12-09 17:34:15182 std::unique_ptr<ntp_tiles::MostVisitedSites> most_visited_sites_;
mohan.reddy74595362014-09-23 20:35:34183
Kyle Milka3626a7d2018-07-03 06:23:55184 base::WeakPtrFactory<InstantService> weak_ptr_factory_;
185
[email protected]c5dec6292013-01-25 04:54:52186 DISALLOW_COPY_AND_ASSIGN(InstantService);
187};
188
[email protected]a7b8e43d2013-03-18 18:52:43189#endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_