blob: dca960e00f9eca037e7091c937cc5d8d9bf07317 [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"
[email protected]540380fc2014-03-14 10:10:3415#include "components/keyed_service/core/keyed_service.h"
[email protected]bf5c532d2014-07-05 00:29:5316#include "components/search_engines/template_url_service_observer.h"
[email protected]54e0ee62013-02-01 17:25:0317#include "content/public/browser/notification_observer.h"
18#include "content/public/browser/notification_registrar.h"
[email protected]c4b2af22014-05-11 19:48:5319#include "url/gurl.h"
[email protected]c5dec6292013-01-25 04:54:5220
[email protected]672c8c12013-03-07 12:30:0621class InstantIOContext;
[email protected]c4b2af22014-05-11 19:48:5322struct InstantMostVisitedItem;
23class InstantSearchPrerenderer;
[email protected]6af41782013-06-22 13:49:1124class InstantServiceObserver;
[email protected]672c8c12013-03-07 12:30:0625class Profile;
[email protected]c4b2af22014-05-11 19:48:5326struct TemplateURLData;
27class TemplateURLService;
28struct ThemeBackgroundInfo;
[email protected]6af41782013-06-22 13:49:1129class ThemeService;
[email protected]672c8c12013-03-07 12:30:0630
[email protected]4ff347e2013-07-22 19:39:0031namespace content {
[email protected]777590052014-01-17 22:11:5432class RenderProcessHost;
[email protected]4ff347e2013-07-22 19:39:0033}
34
[email protected]d572bfd2013-02-14 06:14:2035// Tracks render process host IDs that are associated with Instant.
[email protected]540380fc2014-03-14 10:10:3436class InstantService : public KeyedService,
[email protected]c4b2af22014-05-11 19:48:5337 public content::NotificationObserver,
38 public TemplateURLServiceObserver {
[email protected]c5dec6292013-01-25 04:54:5239 public:
[email protected]672c8c12013-03-07 12:30:0640 explicit InstantService(Profile* profile);
[email protected]c5dec6292013-01-25 04:54:5241 virtual ~InstantService();
42
43 // Add, remove, and query RenderProcessHost IDs that are associated with
44 // Instant processes.
45 void AddInstantProcess(int process_id);
[email protected]c5dec6292013-01-25 04:54:5246 bool IsInstantProcess(int process_id) const;
47
[email protected]6af41782013-06-22 13:49:1148 // Adds/Removes InstantService observers.
49 void AddObserver(InstantServiceObserver* observer);
50 void RemoveObserver(InstantServiceObserver* observer);
51
[email protected]d572bfd2013-02-14 06:14:2052#if defined(UNIT_TEST)
53 int GetInstantProcessCount() const {
54 return process_ids_.size();
55 }
56#endif
[email protected]c5dec6292013-01-25 04:54:5257
[email protected]8b169b4b2013-03-23 18:24:4658 // Most visited item API.
[email protected]e7868c82013-03-12 03:55:5659
[email protected]280be992013-06-04 03:19:5160 // Invoked by the InstantController when the Instant page wants to delete a
61 // Most Visited item.
62 void DeleteMostVisitedItem(const GURL& url);
63
64 // Invoked by the InstantController when the Instant page wants to undo the
65 // blacklist action.
66 void UndoMostVisitedDeletion(const GURL& url);
67
68 // Invoked by the InstantController when the Instant page wants to undo all
69 // Most Visited deletions.
70 void UndoAllMostVisitedDeletions();
71
[email protected]6af41782013-06-22 13:49:1172 // Invoked by the InstantController to update theme information for NTP.
73 //
74 // TODO(kmadhusu): Invoking this from InstantController shouldn't be
75 // necessary. Investigate more and remove this from here.
76 void UpdateThemeInfo();
77
[email protected]ed68ae32013-06-29 20:46:4878 // Invoked by the InstantController to update most visited items details for
79 // NTP.
80 void UpdateMostVisitedItemsInfo();
81
[email protected]2309e912013-10-01 01:33:3082 // Sends the current set of search URLs to a renderer process.
83 void SendSearchURLsToRenderer(content::RenderProcessHost* rph);
84
[email protected]553c7af2014-02-06 17:58:5885 // Invoked to notify the Instant page that the omnibox start margin has
86 // changed.
87 void OnOmniboxStartMarginChanged(int start_margin);
88
[email protected]e97887c2013-12-11 01:27:3189 InstantSearchPrerenderer* instant_search_prerenderer() {
[email protected]e97887c2013-12-11 01:27:3190 return instant_prerenderer_.get();
[email protected]e97887c2013-12-11 01:27:3191 }
92
[email protected]553c7af2014-02-06 17:58:5893 int omnibox_start_margin() const { return omnibox_start_margin_; }
94
[email protected]c5dec6292013-01-25 04:54:5295 private:
[email protected]4ff347e2013-07-22 19:39:0096 friend class InstantExtendedTest;
[email protected]c8a118e2013-09-24 21:33:4097 friend class InstantServiceTest;
[email protected]4ff347e2013-07-22 19:39:0098 friend class InstantTestBase;
[email protected]c8a118e2013-09-24 21:33:4099 friend class InstantUnitTestBase;
[email protected]4ff347e2013-07-22 19:39:00100
[email protected]4ff347e2013-07-22 19:39:00101 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest,
102 MANUAL_SearchesFromFakebox);
[email protected]df113a12013-07-24 04:56:08103 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
[email protected]c4b2af22014-05-11 19:48:53104 FRIEND_TEST_ALL_PREFIXES(InstantServiceEnabledTest,
105 SendsSearchURLsToRenderer);
[email protected]4ff347e2013-07-22 19:39:00106
[email protected]c4b2af22014-05-11 19:48:53107 // KeyedService:
mostynbfb66cb4f2014-10-07 09:15:42108 virtual void Shutdown() override;
[email protected]c5dec6292013-01-25 04:54:52109
[email protected]c4b2af22014-05-11 19:48:53110 // content::NotificationObserver:
[email protected]54e0ee62013-02-01 17:25:03111 virtual void Observe(int type,
112 const content::NotificationSource& source,
mostynbfb66cb4f2014-10-07 09:15:42113 const content::NotificationDetails& details) override;
[email protected]54e0ee62013-02-01 17:25:03114
[email protected]c4b2af22014-05-11 19:48:53115 // TemplateURLServiceObserver:
116 // Caches the previous value of the Default Search Provider and the Google
117 // base URL to filter out changes other than those affecting the Default
118 // Search Provider.
mostynbfb66cb4f2014-10-07 09:15:42119 virtual void OnTemplateURLServiceChanged() override;
[email protected]c4b2af22014-05-11 19:48:53120
[email protected]2309e912013-10-01 01:33:30121 // Called when a renderer process is terminated.
122 void OnRendererProcessTerminated(int process_id);
123
[email protected]280be992013-06-04 03:19:51124 // Called when we get new most visited items from TopSites, registered as an
125 // async callback. Parses them and sends them to the renderer via
126 // SendMostVisitedItems.
127 void OnMostVisitedItemsReceived(const history::MostVisitedURLList& data);
128
[email protected]ed68ae32013-06-29 20:46:48129 // Notifies the observer about the last known most visited items.
130 void NotifyAboutMostVisitedItems();
131
[email protected]6af41782013-06-22 13:49:11132 // Theme changed notification handler.
133 void OnThemeChanged(ThemeService* theme_service);
134
[email protected]e97887c2013-12-11 01:27:31135 void ResetInstantSearchPrerenderer();
136
[email protected]672c8c12013-03-07 12:30:06137 Profile* const profile_;
138
[email protected]c4b2af22014-05-11 19:48:53139 // The TemplateURLService that we are observing. It will outlive this
140 // InstantService due to the dependency declared in InstantServiceFactory.
141 TemplateURLService* template_url_service_;
142
[email protected]c5dec6292013-01-25 04:54:52143 // The process ids associated with Instant processes.
144 std::set<int> process_ids_;
145
[email protected]ab01dd752013-06-08 05:13:36146 // InstantMostVisitedItems sent to the Instant Pages.
147 std::vector<InstantMostVisitedItem> most_visited_items_;
[email protected]e7868c82013-03-12 03:55:56148
[email protected]6af41782013-06-22 13:49:11149 // Theme-related data for NTP overlay to adopt themes.
150 scoped_ptr<ThemeBackgroundInfo> theme_info_;
151
[email protected]553c7af2014-02-06 17:58:58152 // The start-edge margin of the omnibox, used by the Instant page to align
153 // text or assets properly with the omnibox.
154 int omnibox_start_margin_;
155
[email protected]6af41782013-06-22 13:49:11156 ObserverList<InstantServiceObserver> observers_;
157
[email protected]54e0ee62013-02-01 17:25:03158 content::NotificationRegistrar registrar_;
159
[email protected]672c8c12013-03-07 12:30:06160 scoped_refptr<InstantIOContext> instant_io_context_;
161
[email protected]e97887c2013-12-11 01:27:31162 // Set to NULL if the default search provider does not support Instant.
163 scoped_ptr<InstantSearchPrerenderer> instant_prerenderer_;
164
[email protected]c4b2af22014-05-11 19:48:53165 // Used to check whether notifications from TemplateURLService indicate a
166 // change that affects the default search provider.
167 scoped_ptr<TemplateURLData> previous_default_search_provider_;
168 GURL previous_google_base_url_;
169
mohan.reddy74595362014-09-23 20:35:34170 // Used for Top Sites async retrieval.
171 base::WeakPtrFactory<InstantService> weak_ptr_factory_;
172
[email protected]c5dec6292013-01-25 04:54:52173 DISALLOW_COPY_AND_ASSIGN(InstantService);
174};
175
[email protected]a7b8e43d2013-03-18 18:52:43176#endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_