blob: 8892528906e2d0e1781527fefc5449717ece3ccb [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"
[email protected]280be992013-06-04 03:19:5112#include "base/memory/weak_ptr.h"
[email protected]6af41782013-06-22 13:49:1113#include "base/observer_list.h"
sdefresnebc766ef2014-09-25 09:28:1314#include "components/history/core/browser/history_types.h"
sdefresneedf9e01f2015-01-13 19:45:4115#include "components/history/core/browser/top_sites_observer.h"
[email protected]540380fc2014-03-14 10:10:3416#include "components/keyed_service/core/keyed_service.h"
[email protected]bf5c532d2014-07-05 00:29:5317#include "components/search_engines/template_url_service_observer.h"
fserba485e912015-08-05 20:26:0818#include "components/suggestions/proto/suggestions.pb.h"
19#include "components/suggestions/suggestions_service.h"
[email protected]54e0ee62013-02-01 17:25:0320#include "content/public/browser/notification_observer.h"
21#include "content/public/browser/notification_registrar.h"
[email protected]c4b2af22014-05-11 19:48:5322#include "url/gurl.h"
[email protected]c5dec6292013-01-25 04:54:5223
[email protected]672c8c12013-03-07 12:30:0624class InstantIOContext;
[email protected]c4b2af22014-05-11 19:48:5325struct InstantMostVisitedItem;
26class InstantSearchPrerenderer;
[email protected]6af41782013-06-22 13:49:1127class InstantServiceObserver;
[email protected]672c8c12013-03-07 12:30:0628class Profile;
[email protected]c4b2af22014-05-11 19:48:5329struct TemplateURLData;
30class TemplateURLService;
31struct ThemeBackgroundInfo;
[email protected]6af41782013-06-22 13:49:1132class ThemeService;
[email protected]672c8c12013-03-07 12:30:0633
[email protected]4ff347e2013-07-22 19:39:0034namespace content {
[email protected]777590052014-01-17 22:11:5435class RenderProcessHost;
[email protected]4ff347e2013-07-22 19:39:0036}
37
[email protected]d572bfd2013-02-14 06:14:2038// Tracks render process host IDs that are associated with Instant.
[email protected]540380fc2014-03-14 10:10:3439class InstantService : public KeyedService,
[email protected]c4b2af22014-05-11 19:48:5340 public content::NotificationObserver,
sdefresneedf9e01f2015-01-13 19:45:4141 public TemplateURLServiceObserver,
42 public history::TopSitesObserver {
[email protected]c5dec6292013-01-25 04:54:5243 public:
[email protected]672c8c12013-03-07 12:30:0644 explicit InstantService(Profile* profile);
Daniel Chenga542fca2014-10-21 09:51:2945 ~InstantService() override;
[email protected]c5dec6292013-01-25 04:54:5246
47 // Add, remove, and query RenderProcessHost IDs that are associated with
48 // Instant processes.
49 void AddInstantProcess(int process_id);
[email protected]c5dec6292013-01-25 04:54:5250 bool IsInstantProcess(int process_id) const;
51
[email protected]6af41782013-06-22 13:49:1152 // Adds/Removes InstantService observers.
53 void AddObserver(InstantServiceObserver* observer);
54 void RemoveObserver(InstantServiceObserver* observer);
55
[email protected]d572bfd2013-02-14 06:14:2056#if defined(UNIT_TEST)
57 int GetInstantProcessCount() const {
58 return process_ids_.size();
59 }
60#endif
[email protected]c5dec6292013-01-25 04:54:5261
[email protected]8b169b4b2013-03-23 18:24:4662 // Most visited item API.
[email protected]e7868c82013-03-12 03:55:5663
[email protected]280be992013-06-04 03:19:5164 // Invoked by the InstantController when the Instant page wants to delete a
65 // Most Visited item.
66 void DeleteMostVisitedItem(const GURL& url);
67
68 // Invoked by the InstantController when the Instant page wants to undo the
69 // blacklist action.
70 void UndoMostVisitedDeletion(const GURL& url);
71
72 // Invoked by the InstantController when the Instant page wants to undo all
73 // Most Visited deletions.
74 void UndoAllMostVisitedDeletions();
75
[email protected]6af41782013-06-22 13:49:1176 // Invoked by the InstantController to update theme information for NTP.
77 //
78 // TODO(kmadhusu): Invoking this from InstantController shouldn't be
79 // necessary. Investigate more and remove this from here.
80 void UpdateThemeInfo();
81
[email protected]ed68ae32013-06-29 20:46:4882 // Invoked by the InstantController to update most visited items details for
83 // NTP.
84 void UpdateMostVisitedItemsInfo();
85
[email protected]2309e912013-10-01 01:33:3086 // Sends the current set of search URLs to a renderer process.
87 void SendSearchURLsToRenderer(content::RenderProcessHost* rph);
88
[email protected]553c7af2014-02-06 17:58:5889 // Invoked to notify the Instant page that the omnibox start margin has
90 // changed.
91 void OnOmniboxStartMarginChanged(int start_margin);
92
[email protected]e97887c2013-12-11 01:27:3193 InstantSearchPrerenderer* instant_search_prerenderer() {
[email protected]e97887c2013-12-11 01:27:3194 return instant_prerenderer_.get();
[email protected]e97887c2013-12-11 01:27:3195 }
96
[email protected]553c7af2014-02-06 17:58:5897 int omnibox_start_margin() const { return omnibox_start_margin_; }
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);
[email protected]4ff347e2013-07-22 19:39:00110
[email protected]c4b2af22014-05-11 19:48:53111 // KeyedService:
Daniel Chenga542fca2014-10-21 09:51:29112 void Shutdown() override;
[email protected]c5dec6292013-01-25 04:54:52113
[email protected]c4b2af22014-05-11 19:48:53114 // content::NotificationObserver:
Daniel Chenga542fca2014-10-21 09:51:29115 void Observe(int type,
116 const content::NotificationSource& source,
117 const content::NotificationDetails& details) override;
[email protected]54e0ee62013-02-01 17:25:03118
[email protected]c4b2af22014-05-11 19:48:53119 // TemplateURLServiceObserver:
120 // Caches the previous value of the Default Search Provider and the Google
121 // base URL to filter out changes other than those affecting the Default
122 // Search Provider.
Daniel Chenga542fca2014-10-21 09:51:29123 void OnTemplateURLServiceChanged() override;
[email protected]c4b2af22014-05-11 19:48:53124
sdefresneedf9e01f2015-01-13 19:45:41125 // TopSitesObserver:
126 void TopSitesLoaded(history::TopSites* top_sites) override;
fserbdb575112015-06-29 21:31:59127 void TopSitesChanged(history::TopSites* top_sites,
128 ChangeReason change_reason) override;
sdefresneedf9e01f2015-01-13 19:45:41129
[email protected]2309e912013-10-01 01:33:30130 // Called when a renderer process is terminated.
131 void OnRendererProcessTerminated(int process_id);
132
fserba485e912015-08-05 20:26:08133 // Called when SuggestionsService has a new suggestions profile available.
134 void OnSuggestionsAvailable(const suggestions::SuggestionsProfile& profile);
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
138 // SendMostVisitedItems.
139 void OnMostVisitedItemsReceived(const history::MostVisitedURLList& data);
140
[email protected]ed68ae32013-06-29 20:46:48141 // Notifies the observer about the last known most visited items.
142 void NotifyAboutMostVisitedItems();
143
pcc3a7259562015-04-15 22:21:07144#if defined(ENABLE_THEMES)
[email protected]6af41782013-06-22 13:49:11145 // Theme changed notification handler.
146 void OnThemeChanged(ThemeService* theme_service);
pcc3a7259562015-04-15 22:21:07147#endif
[email protected]6af41782013-06-22 13:49:11148
[email protected]e97887c2013-12-11 01:27:31149 void ResetInstantSearchPrerenderer();
150
[email protected]672c8c12013-03-07 12:30:06151 Profile* const profile_;
152
[email protected]c4b2af22014-05-11 19:48:53153 // The TemplateURLService that we are observing. It will outlive this
154 // InstantService due to the dependency declared in InstantServiceFactory.
155 TemplateURLService* template_url_service_;
156
[email protected]c5dec6292013-01-25 04:54:52157 // The process ids associated with Instant processes.
158 std::set<int> process_ids_;
159
fserba485e912015-08-05 20:26:08160 // InstantMostVisitedItems from TopSites.
[email protected]ab01dd752013-06-08 05:13:36161 std::vector<InstantMostVisitedItem> most_visited_items_;
[email protected]e7868c82013-03-12 03:55:56162
fserba485e912015-08-05 20:26:08163 // InstantMostVisitedItems from SuggestionService.
164 std::vector<InstantMostVisitedItem> suggestions_items_;
165
[email protected]6af41782013-06-22 13:49:11166 // Theme-related data for NTP overlay to adopt themes.
167 scoped_ptr<ThemeBackgroundInfo> theme_info_;
168
[email protected]553c7af2014-02-06 17:58:58169 // The start-edge margin of the omnibox, used by the Instant page to align
170 // text or assets properly with the omnibox.
171 int omnibox_start_margin_;
172
brettwd195c952015-06-02 17:31:12173 base::ObserverList<InstantServiceObserver> observers_;
[email protected]6af41782013-06-22 13:49:11174
[email protected]54e0ee62013-02-01 17:25:03175 content::NotificationRegistrar registrar_;
176
[email protected]672c8c12013-03-07 12:30:06177 scoped_refptr<InstantIOContext> instant_io_context_;
178
[email protected]e97887c2013-12-11 01:27:31179 // Set to NULL if the default search provider does not support Instant.
180 scoped_ptr<InstantSearchPrerenderer> instant_prerenderer_;
181
[email protected]c4b2af22014-05-11 19:48:53182 // Used to check whether notifications from TemplateURLService indicate a
183 // change that affects the default search provider.
184 scoped_ptr<TemplateURLData> previous_default_search_provider_;
185 GURL previous_google_base_url_;
186
fserba485e912015-08-05 20:26:08187 // Suggestions Service to fetch server suggestions.
188 suggestions::SuggestionsService* suggestions_service_;
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_