blob: 96abba728917f116fa9e310d3aef752ce4264ec5 [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
8#include <set>
[email protected]ab01dd752013-06-08 05:13:369#include <vector>
[email protected]c5dec6292013-01-25 04:54:5210
[email protected]c4b2af22014-05-11 19:48:5311#include "base/gtest_prod_util.h"
avib896c712015-12-26 02:10:4312#include "base/macros.h"
[email protected]280be992013-06-04 03:19:5113#include "base/memory/weak_ptr.h"
[email protected]6af41782013-06-22 13:49:1114#include "base/observer_list.h"
sdefresnebc766ef2014-09-25 09:28:1315#include "components/history/core/browser/history_types.h"
sdefresneedf9e01f2015-01-13 19:45:4116#include "components/history/core/browser/top_sites_observer.h"
[email protected]540380fc2014-03-14 10:10:3417#include "components/keyed_service/core/keyed_service.h"
[email protected]bf5c532d2014-07-05 00:29:5318#include "components/search_engines/template_url_service_observer.h"
fserba485e912015-08-05 20:26:0819#include "components/suggestions/proto/suggestions.pb.h"
20#include "components/suggestions/suggestions_service.h"
[email protected]54e0ee62013-02-01 17:25:0321#include "content/public/browser/notification_observer.h"
22#include "content/public/browser/notification_registrar.h"
[email protected]c4b2af22014-05-11 19:48:5323#include "url/gurl.h"
[email protected]c5dec6292013-01-25 04:54:5224
[email protected]672c8c12013-03-07 12:30:0625class InstantIOContext;
[email protected]c4b2af22014-05-11 19:48:5326struct InstantMostVisitedItem;
27class InstantSearchPrerenderer;
[email protected]6af41782013-06-22 13:49:1128class InstantServiceObserver;
[email protected]672c8c12013-03-07 12:30:0629class Profile;
[email protected]c4b2af22014-05-11 19:48:5330struct TemplateURLData;
31class TemplateURLService;
32struct ThemeBackgroundInfo;
[email protected]6af41782013-06-22 13:49:1133class ThemeService;
[email protected]672c8c12013-03-07 12:30:0634
[email protected]4ff347e2013-07-22 19:39:0035namespace content {
[email protected]777590052014-01-17 22:11:5436class RenderProcessHost;
[email protected]4ff347e2013-07-22 19:39:0037}
38
[email protected]d572bfd2013-02-14 06:14:2039// Tracks render process host IDs that are associated with Instant.
[email protected]540380fc2014-03-14 10:10:3440class InstantService : public KeyedService,
[email protected]c4b2af22014-05-11 19:48:5341 public content::NotificationObserver,
sdefresneedf9e01f2015-01-13 19:45:4142 public TemplateURLServiceObserver,
43 public history::TopSitesObserver {
[email protected]c5dec6292013-01-25 04:54:5244 public:
[email protected]672c8c12013-03-07 12:30:0645 explicit InstantService(Profile* profile);
Daniel Chenga542fca2014-10-21 09:51:2946 ~InstantService() override;
[email protected]c5dec6292013-01-25 04:54:5247
48 // Add, remove, and query RenderProcessHost IDs that are associated with
49 // Instant processes.
50 void AddInstantProcess(int process_id);
[email protected]c5dec6292013-01-25 04:54:5251 bool IsInstantProcess(int process_id) const;
52
[email protected]6af41782013-06-22 13:49:1153 // Adds/Removes InstantService observers.
54 void AddObserver(InstantServiceObserver* observer);
55 void RemoveObserver(InstantServiceObserver* observer);
56
[email protected]d572bfd2013-02-14 06:14:2057#if defined(UNIT_TEST)
58 int GetInstantProcessCount() const {
59 return process_ids_.size();
60 }
61#endif
[email protected]c5dec6292013-01-25 04:54:5262
[email protected]8b169b4b2013-03-23 18:24:4663 // Most visited item API.
[email protected]e7868c82013-03-12 03:55:5664
[email protected]280be992013-06-04 03:19:5165 // Invoked by the InstantController when the Instant page wants to delete a
66 // Most Visited item.
67 void DeleteMostVisitedItem(const GURL& url);
68
69 // Invoked by the InstantController when the Instant page wants to undo the
70 // blacklist action.
71 void UndoMostVisitedDeletion(const GURL& url);
72
73 // Invoked by the InstantController when the Instant page wants to undo all
74 // Most Visited deletions.
75 void UndoAllMostVisitedDeletions();
76
[email protected]6af41782013-06-22 13:49:1177 // Invoked by the InstantController to update theme information for NTP.
78 //
79 // TODO(kmadhusu): Invoking this from InstantController shouldn't be
80 // necessary. Investigate more and remove this from here.
81 void UpdateThemeInfo();
82
[email protected]ed68ae32013-06-29 20:46:4883 // Invoked by the InstantController to update most visited items details for
84 // NTP.
85 void UpdateMostVisitedItemsInfo();
86
[email protected]2309e912013-10-01 01:33:3087 // Sends the current set of search URLs to a renderer process.
88 void SendSearchURLsToRenderer(content::RenderProcessHost* rph);
89
[email protected]553c7af2014-02-06 17:58:5890 // Invoked to notify the Instant page that the omnibox start margin has
91 // changed.
92 void OnOmniboxStartMarginChanged(int start_margin);
93
[email protected]e97887c2013-12-11 01:27:3194 InstantSearchPrerenderer* instant_search_prerenderer() {
[email protected]e97887c2013-12-11 01:27:3195 return instant_prerenderer_.get();
[email protected]e97887c2013-12-11 01:27:3196 }
97
[email protected]553c7af2014-02-06 17:58:5898 int omnibox_start_margin() const { return omnibox_start_margin_; }
99
[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
fserba485e912015-08-05 20:26:08136 // Called when SuggestionsService has a new suggestions profile available.
137 void OnSuggestionsAvailable(const suggestions::SuggestionsProfile& profile);
138
[email protected]280be992013-06-04 03:19:51139 // Called when we get new most visited items from TopSites, registered as an
140 // async callback. Parses them and sends them to the renderer via
141 // SendMostVisitedItems.
142 void OnMostVisitedItemsReceived(const history::MostVisitedURLList& data);
143
[email protected]ed68ae32013-06-29 20:46:48144 // Notifies the observer about the last known most visited items.
145 void NotifyAboutMostVisitedItems();
146
pcc3a7259562015-04-15 22:21:07147#if defined(ENABLE_THEMES)
[email protected]6af41782013-06-22 13:49:11148 // Theme changed notification handler.
estade68691b282015-12-11 21:50:40149 void OnThemeChanged();
pcc3a7259562015-04-15 22:21:07150#endif
[email protected]6af41782013-06-22 13:49:11151
[email protected]e97887c2013-12-11 01:27:31152 void ResetInstantSearchPrerenderer();
153
[email protected]672c8c12013-03-07 12:30:06154 Profile* const profile_;
155
[email protected]c4b2af22014-05-11 19:48:53156 // The TemplateURLService that we are observing. It will outlive this
157 // InstantService due to the dependency declared in InstantServiceFactory.
158 TemplateURLService* template_url_service_;
159
[email protected]c5dec6292013-01-25 04:54:52160 // The process ids associated with Instant processes.
161 std::set<int> process_ids_;
162
fserba485e912015-08-05 20:26:08163 // InstantMostVisitedItems from TopSites.
[email protected]ab01dd752013-06-08 05:13:36164 std::vector<InstantMostVisitedItem> most_visited_items_;
[email protected]e7868c82013-03-12 03:55:56165
fserba485e912015-08-05 20:26:08166 // InstantMostVisitedItems from SuggestionService.
167 std::vector<InstantMostVisitedItem> suggestions_items_;
168
[email protected]6af41782013-06-22 13:49:11169 // Theme-related data for NTP overlay to adopt themes.
170 scoped_ptr<ThemeBackgroundInfo> theme_info_;
171
[email protected]553c7af2014-02-06 17:58:58172 // The start-edge margin of the omnibox, used by the Instant page to align
173 // text or assets properly with the omnibox.
174 int omnibox_start_margin_;
175
brettwd195c952015-06-02 17:31:12176 base::ObserverList<InstantServiceObserver> observers_;
[email protected]6af41782013-06-22 13:49:11177
[email protected]54e0ee62013-02-01 17:25:03178 content::NotificationRegistrar registrar_;
179
[email protected]672c8c12013-03-07 12:30:06180 scoped_refptr<InstantIOContext> instant_io_context_;
181
[email protected]e97887c2013-12-11 01:27:31182 // Set to NULL if the default search provider does not support Instant.
183 scoped_ptr<InstantSearchPrerenderer> instant_prerenderer_;
184
[email protected]c4b2af22014-05-11 19:48:53185 // Used to check whether notifications from TemplateURLService indicate a
186 // change that affects the default search provider.
187 scoped_ptr<TemplateURLData> previous_default_search_provider_;
188 GURL previous_google_base_url_;
189
fserba485e912015-08-05 20:26:08190 // Suggestions Service to fetch server suggestions.
191 suggestions::SuggestionsService* suggestions_service_;
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_