blob: b48c84452005db0f3ee844662941c426439172d4 [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"
[email protected]280be992013-06-04 03:19:5114#include "base/memory/weak_ptr.h"
[email protected]6af41782013-06-22 13:49:1115#include "base/observer_list.h"
sdefresnebc766ef2014-09-25 09:28:1316#include "components/history/core/browser/history_types.h"
sdefresneedf9e01f2015-01-13 19:45:4117#include "components/history/core/browser/top_sites_observer.h"
[email protected]540380fc2014-03-14 10:10:3418#include "components/keyed_service/core/keyed_service.h"
[email protected]bf5c532d2014-07-05 00:29:5319#include "components/search_engines/template_url_service_observer.h"
fserba485e912015-08-05 20:26:0820#include "components/suggestions/proto/suggestions.pb.h"
21#include "components/suggestions/suggestions_service.h"
[email protected]54e0ee62013-02-01 17:25:0322#include "content/public/browser/notification_observer.h"
23#include "content/public/browser/notification_registrar.h"
[email protected]c4b2af22014-05-11 19:48:5324#include "url/gurl.h"
[email protected]c5dec6292013-01-25 04:54:5225
[email protected]672c8c12013-03-07 12:30:0626class InstantIOContext;
[email protected]c4b2af22014-05-11 19:48:5327struct InstantMostVisitedItem;
28class InstantSearchPrerenderer;
[email protected]6af41782013-06-22 13:49:1129class InstantServiceObserver;
[email protected]672c8c12013-03-07 12:30:0630class Profile;
[email protected]c4b2af22014-05-11 19:48:5331struct TemplateURLData;
32class TemplateURLService;
33struct ThemeBackgroundInfo;
[email protected]6af41782013-06-22 13:49:1134class ThemeService;
[email protected]672c8c12013-03-07 12:30:0635
[email protected]4ff347e2013-07-22 19:39:0036namespace content {
[email protected]777590052014-01-17 22:11:5437class RenderProcessHost;
[email protected]4ff347e2013-07-22 19:39:0038}
39
[email protected]d572bfd2013-02-14 06:14:2040// Tracks render process host IDs that are associated with Instant.
[email protected]540380fc2014-03-14 10:10:3441class InstantService : public KeyedService,
[email protected]c4b2af22014-05-11 19:48:5342 public content::NotificationObserver,
sdefresneedf9e01f2015-01-13 19:45:4143 public TemplateURLServiceObserver,
44 public history::TopSitesObserver {
[email protected]c5dec6292013-01-25 04:54:5245 public:
[email protected]672c8c12013-03-07 12:30:0646 explicit InstantService(Profile* profile);
Daniel Chenga542fca2014-10-21 09:51:2947 ~InstantService() override;
[email protected]c5dec6292013-01-25 04:54:5248
49 // Add, remove, and query RenderProcessHost IDs that are associated with
50 // Instant processes.
51 void AddInstantProcess(int process_id);
[email protected]c5dec6292013-01-25 04:54:5252 bool IsInstantProcess(int process_id) const;
53
[email protected]6af41782013-06-22 13:49:1154 // Adds/Removes InstantService observers.
55 void AddObserver(InstantServiceObserver* observer);
56 void RemoveObserver(InstantServiceObserver* observer);
57
[email protected]d572bfd2013-02-14 06:14:2058#if defined(UNIT_TEST)
59 int GetInstantProcessCount() const {
60 return process_ids_.size();
61 }
62#endif
[email protected]c5dec6292013-01-25 04:54:5263
[email protected]8b169b4b2013-03-23 18:24:4664 // Most visited item API.
[email protected]e7868c82013-03-12 03:55:5665
[email protected]280be992013-06-04 03:19:5166 // Invoked by the InstantController when the Instant page wants to delete a
67 // Most Visited item.
68 void DeleteMostVisitedItem(const GURL& url);
69
70 // Invoked by the InstantController when the Instant page wants to undo the
71 // blacklist action.
72 void UndoMostVisitedDeletion(const GURL& url);
73
74 // Invoked by the InstantController when the Instant page wants to undo all
75 // Most Visited deletions.
76 void UndoAllMostVisitedDeletions();
77
[email protected]6af41782013-06-22 13:49:1178 // Invoked by the InstantController to update theme information for NTP.
79 //
80 // TODO(kmadhusu): Invoking this from InstantController shouldn't be
81 // necessary. Investigate more and remove this from here.
82 void UpdateThemeInfo();
83
[email protected]ed68ae32013-06-29 20:46:4884 // Invoked by the InstantController to update most visited items details for
85 // NTP.
86 void UpdateMostVisitedItemsInfo();
87
[email protected]2309e912013-10-01 01:33:3088 // Sends the current set of search URLs to a renderer process.
89 void SendSearchURLsToRenderer(content::RenderProcessHost* rph);
90
dchengd523a41a2016-02-04 20:06:1291 // Used to validate that the URL the NTP is trying to navigate to is actually
92 // a URL on the most visited items / suggested items list.
93 bool IsValidURLForNavigation(const GURL& url) const;
94
[email protected]e97887c2013-12-11 01:27:3195 InstantSearchPrerenderer* instant_search_prerenderer() {
[email protected]e97887c2013-12-11 01:27:3196 return instant_prerenderer_.get();
[email protected]e97887c2013-12-11 01:27:3197 }
98
[email protected]c5dec6292013-01-25 04:54:5299 private:
[email protected]4ff347e2013-07-22 19:39:00100 friend class InstantExtendedTest;
[email protected]c8a118e2013-09-24 21:33:40101 friend class InstantServiceTest;
[email protected]4ff347e2013-07-22 19:39:00102 friend class InstantTestBase;
[email protected]c8a118e2013-09-24 21:33:40103 friend class InstantUnitTestBase;
[email protected]4ff347e2013-07-22 19:39:00104
[email protected]4ff347e2013-07-22 19:39:00105 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest,
106 MANUAL_SearchesFromFakebox);
[email protected]df113a12013-07-24 04:56:08107 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
[email protected]c4b2af22014-05-11 19:48:53108 FRIEND_TEST_ALL_PREFIXES(InstantServiceEnabledTest,
109 SendsSearchURLsToRenderer);
zminfddc3e32015-12-09 22:59:08110 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetSuggestionFromServiceSide);
111 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetSuggestionFromClientSide);
[email protected]4ff347e2013-07-22 19:39:00112
[email protected]c4b2af22014-05-11 19:48:53113 // KeyedService:
Daniel Chenga542fca2014-10-21 09:51:29114 void Shutdown() override;
[email protected]c5dec6292013-01-25 04:54:52115
[email protected]c4b2af22014-05-11 19:48:53116 // content::NotificationObserver:
Daniel Chenga542fca2014-10-21 09:51:29117 void Observe(int type,
118 const content::NotificationSource& source,
119 const content::NotificationDetails& details) override;
[email protected]54e0ee62013-02-01 17:25:03120
[email protected]c4b2af22014-05-11 19:48:53121 // TemplateURLServiceObserver:
122 // Caches the previous value of the Default Search Provider and the Google
123 // base URL to filter out changes other than those affecting the Default
124 // Search Provider.
Daniel Chenga542fca2014-10-21 09:51:29125 void OnTemplateURLServiceChanged() override;
[email protected]c4b2af22014-05-11 19:48:53126
sdefresneedf9e01f2015-01-13 19:45:41127 // TopSitesObserver:
128 void TopSitesLoaded(history::TopSites* top_sites) override;
fserbdb575112015-06-29 21:31:59129 void TopSitesChanged(history::TopSites* top_sites,
130 ChangeReason change_reason) override;
sdefresneedf9e01f2015-01-13 19:45:41131
[email protected]2309e912013-10-01 01:33:30132 // Called when a renderer process is terminated.
133 void OnRendererProcessTerminated(int process_id);
134
fserba485e912015-08-05 20:26:08135 // Called when SuggestionsService has a new suggestions profile available.
136 void OnSuggestionsAvailable(const suggestions::SuggestionsProfile& profile);
137
[email protected]280be992013-06-04 03:19:51138 // Called when we get new most visited items from TopSites, registered as an
139 // async callback. Parses them and sends them to the renderer via
140 // SendMostVisitedItems.
141 void OnMostVisitedItemsReceived(const history::MostVisitedURLList& data);
142
[email protected]ed68ae32013-06-29 20:46:48143 // Notifies the observer about the last known most visited items.
144 void NotifyAboutMostVisitedItems();
145
pcc3a7259562015-04-15 22:21:07146#if defined(ENABLE_THEMES)
[email protected]6af41782013-06-22 13:49:11147 // Theme changed notification handler.
estade68691b282015-12-11 21:50:40148 void OnThemeChanged();
pcc3a7259562015-04-15 22:21:07149#endif
[email protected]6af41782013-06-22 13:49:11150
[email protected]e97887c2013-12-11 01:27:31151 void ResetInstantSearchPrerenderer();
152
[email protected]672c8c12013-03-07 12:30:06153 Profile* const profile_;
154
[email protected]c4b2af22014-05-11 19:48:53155 // The TemplateURLService that we are observing. It will outlive this
156 // InstantService due to the dependency declared in InstantServiceFactory.
157 TemplateURLService* template_url_service_;
158
[email protected]c5dec6292013-01-25 04:54:52159 // The process ids associated with Instant processes.
160 std::set<int> process_ids_;
161
fserba485e912015-08-05 20:26:08162 // InstantMostVisitedItems from TopSites.
[email protected]ab01dd752013-06-08 05:13:36163 std::vector<InstantMostVisitedItem> most_visited_items_;
[email protected]e7868c82013-03-12 03:55:56164
fserba485e912015-08-05 20:26:08165 // InstantMostVisitedItems from SuggestionService.
166 std::vector<InstantMostVisitedItem> suggestions_items_;
167
[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
fserba485e912015-08-05 20:26:08185 // Suggestions Service to fetch server suggestions.
186 suggestions::SuggestionsService* suggestions_service_;
187
treib16070ce2016-03-11 11:57:40188 // Subscription to the SuggestionsService.
dcheng4af48582016-04-19 00:29:35189 std::unique_ptr<
treib16070ce2016-03-11 11:57:40190 suggestions::SuggestionsService::ResponseCallbackList::Subscription>
191 suggestions_subscription_;
192
mohan.reddy74595362014-09-23 20:35:34193 // Used for Top Sites async retrieval.
194 base::WeakPtrFactory<InstantService> weak_ptr_factory_;
195
[email protected]c5dec6292013-01-25 04:54:52196 DISALLOW_COPY_AND_ASSIGN(InstantService);
197};
198
[email protected]a7b8e43d2013-03-18 18:52:43199#endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_