blob: 064ca1db30aa7e21846c3eff2825d659fe0d0794 [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
dcheng4af48582016-04-19 00:29:358#include <memory>
[email protected]c5dec6292013-01-25 04:54:529#include <set>
[email protected]ab01dd752013-06-08 05:13:3610#include <vector>
[email protected]c5dec6292013-01-25 04:54:5211
[email protected]c4b2af22014-05-11 19:48:5312#include "base/gtest_prod_util.h"
avib896c712015-12-26 02:10:4313#include "base/macros.h"
treibbb0c5af52016-12-09 17:34:1514#include "base/memory/ref_counted.h"
[email protected]280be992013-06-04 03:19:5115#include "base/memory/weak_ptr.h"
[email protected]6af41782013-06-22 13:49:1116#include "base/observer_list.h"
brettw9b0866f2016-12-11 02:34:0617#include "build/build_config.h"
sdefresnebc766ef2014-09-25 09:28:1318#include "components/history/core/browser/history_types.h"
sdefresneedf9e01f2015-01-13 19:45:4119#include "components/history/core/browser/top_sites_observer.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"
[email protected]bf5c532d2014-07-05 00:29:5323#include "components/search_engines/template_url_service_observer.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
[email protected]672c8c12013-03-07 12:30:0628class InstantIOContext;
[email protected]c4b2af22014-05-11 19:48:5329struct InstantMostVisitedItem;
30class InstantSearchPrerenderer;
[email protected]6af41782013-06-22 13:49:1131class InstantServiceObserver;
[email protected]672c8c12013-03-07 12:30:0632class Profile;
[email protected]c4b2af22014-05-11 19:48:5333struct TemplateURLData;
34class TemplateURLService;
35struct ThemeBackgroundInfo;
[email protected]672c8c12013-03-07 12:30:0636
[email protected]4ff347e2013-07-22 19:39:0037namespace content {
[email protected]777590052014-01-17 22:11:5438class RenderProcessHost;
[email protected]4ff347e2013-07-22 19:39:0039}
40
treibbb0c5af52016-12-09 17:34:1541namespace history {
42class TopSites;
43}
44
[email protected]d572bfd2013-02-14 06:14:2045// Tracks render process host IDs that are associated with Instant.
[email protected]540380fc2014-03-14 10:10:3446class InstantService : public KeyedService,
[email protected]c4b2af22014-05-11 19:48:5347 public content::NotificationObserver,
sdefresneedf9e01f2015-01-13 19:45:4148 public TemplateURLServiceObserver,
treibbb0c5af52016-12-09 17:34:1549 public history::TopSitesObserver,
50 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
[email protected]d572bfd2013-02-14 06:14:2064#if defined(UNIT_TEST)
65 int GetInstantProcessCount() const {
66 return process_ids_.size();
67 }
68#endif
[email protected]c5dec6292013-01-25 04:54:5269
treib2df1b7d2016-11-23 09:16:3270 // Invoked whenever an NTP is opened. Causes an async refresh of Most Visited
71 // items.
72 void OnNewTabPageOpened();
73
[email protected]8b169b4b2013-03-23 18:24:4674 // Most visited item API.
[email protected]e7868c82013-03-12 03:55:5675
treib2df1b7d2016-11-23 09:16:3276 // Invoked when the Instant page wants to delete a Most Visited item.
[email protected]280be992013-06-04 03:19:5177 void DeleteMostVisitedItem(const GURL& url);
78
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);
81
treib2df1b7d2016-11-23 09:16:3282 // Invoked when the Instant page wants to undo all Most Visited deletions.
[email protected]280be992013-06-04 03:19:5183 void UndoAllMostVisitedDeletions();
84
[email protected]6af41782013-06-22 13:49:1185 // Invoked by the InstantController to update theme information for NTP.
86 //
87 // TODO(kmadhusu): Invoking this from InstantController shouldn't be
88 // necessary. Investigate more and remove this from here.
89 void UpdateThemeInfo();
90
[email protected]ed68ae32013-06-29 20:46:4891 // Invoked by the InstantController to update most visited items details for
92 // NTP.
93 void UpdateMostVisitedItemsInfo();
94
[email protected]2309e912013-10-01 01:33:3095 // Sends the current set of search URLs to a renderer process.
96 void SendSearchURLsToRenderer(content::RenderProcessHost* rph);
97
treib809206532016-11-10 15:48:3398 InstantSearchPrerenderer* GetInstantSearchPrerenderer();
[email protected]e97887c2013-12-11 01:27:3199
[email protected]c5dec6292013-01-25 04:54:52100 private:
[email protected]4ff347e2013-07-22 19:39:00101 friend class InstantExtendedTest;
[email protected]c8a118e2013-09-24 21:33:40102 friend class InstantServiceTest;
[email protected]4ff347e2013-07-22 19:39:00103 friend class InstantTestBase;
[email protected]c8a118e2013-09-24 21:33:40104 friend class InstantUnitTestBase;
[email protected]4ff347e2013-07-22 19:39:00105
[email protected]4ff347e2013-07-22 19:39:00106 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest,
107 MANUAL_SearchesFromFakebox);
[email protected]df113a12013-07-24 04:56:08108 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
[email protected]c4b2af22014-05-11 19:48:53109 FRIEND_TEST_ALL_PREFIXES(InstantServiceEnabledTest,
110 SendsSearchURLsToRenderer);
zminfddc3e32015-12-09 22:59:08111 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetSuggestionFromServiceSide);
112 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetSuggestionFromClientSide);
[email protected]4ff347e2013-07-22 19:39:00113
[email protected]c4b2af22014-05-11 19:48:53114 // KeyedService:
Daniel Chenga542fca2014-10-21 09:51:29115 void Shutdown() override;
[email protected]c5dec6292013-01-25 04:54:52116
[email protected]c4b2af22014-05-11 19:48:53117 // content::NotificationObserver:
Daniel Chenga542fca2014-10-21 09:51:29118 void Observe(int type,
119 const content::NotificationSource& source,
120 const content::NotificationDetails& details) override;
[email protected]54e0ee62013-02-01 17:25:03121
[email protected]c4b2af22014-05-11 19:48:53122 // TemplateURLServiceObserver:
123 // Caches the previous value of the Default Search Provider and the Google
124 // base URL to filter out changes other than those affecting the Default
125 // Search Provider.
Daniel Chenga542fca2014-10-21 09:51:29126 void OnTemplateURLServiceChanged() override;
[email protected]c4b2af22014-05-11 19:48:53127
sdefresneedf9e01f2015-01-13 19:45:41128 // TopSitesObserver:
129 void TopSitesLoaded(history::TopSites* top_sites) override;
fserbdb575112015-06-29 21:31:59130 void TopSitesChanged(history::TopSites* top_sites,
131 ChangeReason change_reason) override;
sdefresneedf9e01f2015-01-13 19:45:41132
[email protected]2309e912013-10-01 01:33:30133 // Called when a renderer process is terminated.
134 void OnRendererProcessTerminated(int process_id);
135
[email protected]280be992013-06-04 03:19:51136 // Called when we get new most visited items from TopSites, registered as an
137 // async callback. Parses them and sends them to the renderer via
treib94638f32016-07-14 10:39:11138 // NotifyAboutMostVisitedItems.
treibbb0c5af52016-12-09 17:34:15139 void OnTopSitesReceived(const history::MostVisitedURLList& data);
140
141 // ntp_tiles::MostVisitedSites::Observer implementation.
142 void OnMostVisitedURLsAvailable(
143 const ntp_tiles::NTPTilesVector& tiles) override;
144 void OnIconMadeAvailable(const GURL& site_url) override;
[email protected]280be992013-06-04 03:19:51145
[email protected]ed68ae32013-06-29 20:46:48146 // Notifies the observer about the last known most visited items.
147 void NotifyAboutMostVisitedItems();
148
brettw9b0866f2016-12-11 02:34:06149#if !defined(OS_ANDROID)
[email protected]6af41782013-06-22 13:49:11150 // Theme changed notification handler.
estade68691b282015-12-11 21:50:40151 void OnThemeChanged();
pcc3a7259562015-04-15 22:21:07152#endif
[email protected]6af41782013-06-22 13:49:11153
treib809206532016-11-10 15:48:33154 void ResetInstantSearchPrerendererIfNecessary();
[email protected]e97887c2013-12-11 01:27:31155
[email protected]672c8c12013-03-07 12:30:06156 Profile* const profile_;
157
[email protected]c4b2af22014-05-11 19:48:53158 // The TemplateURLService that we are observing. It will outlive this
159 // InstantService due to the dependency declared in InstantServiceFactory.
160 TemplateURLService* template_url_service_;
161
[email protected]c5dec6292013-01-25 04:54:52162 // The process ids associated with Instant processes.
163 std::set<int> process_ids_;
164
fserba485e912015-08-05 20:26:08165 // InstantMostVisitedItems from TopSites.
[email protected]ab01dd752013-06-08 05:13:36166 std::vector<InstantMostVisitedItem> most_visited_items_;
[email protected]e7868c82013-03-12 03:55:56167
[email protected]6af41782013-06-22 13:49:11168 // Theme-related data for NTP overlay to adopt themes.
dcheng4af48582016-04-19 00:29:35169 std::unique_ptr<ThemeBackgroundInfo> theme_info_;
[email protected]6af41782013-06-22 13:49:11170
brettwd195c952015-06-02 17:31:12171 base::ObserverList<InstantServiceObserver> observers_;
[email protected]6af41782013-06-22 13:49:11172
[email protected]54e0ee62013-02-01 17:25:03173 content::NotificationRegistrar registrar_;
174
[email protected]672c8c12013-03-07 12:30:06175 scoped_refptr<InstantIOContext> instant_io_context_;
176
[email protected]e97887c2013-12-11 01:27:31177 // Set to NULL if the default search provider does not support Instant.
dcheng4af48582016-04-19 00:29:35178 std::unique_ptr<InstantSearchPrerenderer> instant_prerenderer_;
[email protected]e97887c2013-12-11 01:27:31179
[email protected]c4b2af22014-05-11 19:48:53180 // Used to check whether notifications from TemplateURLService indicate a
181 // change that affects the default search provider.
dcheng4af48582016-04-19 00:29:35182 std::unique_ptr<TemplateURLData> previous_default_search_provider_;
[email protected]c4b2af22014-05-11 19:48:53183 GURL previous_google_base_url_;
184
treibbb0c5af52016-12-09 17:34:15185 // Data sources for NTP tiles (aka Most Visited tiles). Only one of these will
186 // be non-null.
187 std::unique_ptr<ntp_tiles::MostVisitedSites> most_visited_sites_;
188 scoped_refptr<history::TopSites> top_sites_;
189
mohan.reddy74595362014-09-23 20:35:34190 // Used for Top Sites async retrieval.
191 base::WeakPtrFactory<InstantService> weak_ptr_factory_;
192
[email protected]c5dec6292013-01-25 04:54:52193 DISALLOW_COPY_AND_ASSIGN(InstantService);
194};
195
[email protected]a7b8e43d2013-03-18 18:52:43196#endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_