1 // 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.
5 #ifndef CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_
6 #define CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "components/history/core/browser/history_types.h"
15 #include "components/history/core/browser/top_sites_observer.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "components/search_engines/template_url_service_observer.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
22 class InstantIOContext
;
23 struct InstantMostVisitedItem
;
24 class InstantSearchPrerenderer
;
25 class InstantServiceObserver
;
27 struct TemplateURLData
;
28 class TemplateURLService
;
29 struct ThemeBackgroundInfo
;
33 class RenderProcessHost
;
36 // Tracks render process host IDs that are associated with Instant.
37 class InstantService
: public KeyedService
,
38 public content::NotificationObserver
,
39 public TemplateURLServiceObserver
,
40 public history::TopSitesObserver
{
42 explicit InstantService(Profile
* profile
);
43 ~InstantService() override
;
45 // Add, remove, and query RenderProcessHost IDs that are associated with
47 void AddInstantProcess(int process_id
);
48 bool IsInstantProcess(int process_id
) const;
50 // Adds/Removes InstantService observers.
51 void AddObserver(InstantServiceObserver
* observer
);
52 void RemoveObserver(InstantServiceObserver
* observer
);
54 #if defined(UNIT_TEST)
55 int GetInstantProcessCount() const {
56 return process_ids_
.size();
60 // Most visited item API.
62 // Invoked by the InstantController when the Instant page wants to delete a
64 void DeleteMostVisitedItem(const GURL
& url
);
66 // Invoked by the InstantController when the Instant page wants to undo the
68 void UndoMostVisitedDeletion(const GURL
& url
);
70 // Invoked by the InstantController when the Instant page wants to undo all
71 // Most Visited deletions.
72 void UndoAllMostVisitedDeletions();
74 // Invoked by the InstantController to update theme information for NTP.
76 // TODO(kmadhusu): Invoking this from InstantController shouldn't be
77 // necessary. Investigate more and remove this from here.
78 void UpdateThemeInfo();
80 // Invoked by the InstantController to update most visited items details for
82 void UpdateMostVisitedItemsInfo();
84 // Sends the current set of search URLs to a renderer process.
85 void SendSearchURLsToRenderer(content::RenderProcessHost
* rph
);
87 // Invoked to notify the Instant page that the omnibox start margin has
89 void OnOmniboxStartMarginChanged(int start_margin
);
91 InstantSearchPrerenderer
* instant_search_prerenderer() {
92 return instant_prerenderer_
.get();
95 int omnibox_start_margin() const { return omnibox_start_margin_
; }
98 friend class InstantExtendedTest
;
99 friend class InstantServiceTest
;
100 friend class InstantTestBase
;
101 friend class InstantUnitTestBase
;
103 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest
,
104 MANUAL_SearchesFromFakebox
);
105 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, ProcessIsolation
);
106 FRIEND_TEST_ALL_PREFIXES(InstantServiceEnabledTest
,
107 SendsSearchURLsToRenderer
);
110 void Shutdown() override
;
112 // content::NotificationObserver:
113 void Observe(int type
,
114 const content::NotificationSource
& source
,
115 const content::NotificationDetails
& details
) override
;
117 // TemplateURLServiceObserver:
118 // Caches the previous value of the Default Search Provider and the Google
119 // base URL to filter out changes other than those affecting the Default
121 void OnTemplateURLServiceChanged() override
;
124 void TopSitesLoaded(history::TopSites
* top_sites
) override
;
125 void TopSitesChanged(history::TopSites
* top_sites
) override
;
127 // Called when a renderer process is terminated.
128 void OnRendererProcessTerminated(int process_id
);
130 // Called when we get new most visited items from TopSites, registered as an
131 // async callback. Parses them and sends them to the renderer via
132 // SendMostVisitedItems.
133 void OnMostVisitedItemsReceived(const history::MostVisitedURLList
& data
);
135 // Notifies the observer about the last known most visited items.
136 void NotifyAboutMostVisitedItems();
138 // Theme changed notification handler.
139 void OnThemeChanged(ThemeService
* theme_service
);
141 void ResetInstantSearchPrerenderer();
143 Profile
* const profile_
;
145 // The TemplateURLService that we are observing. It will outlive this
146 // InstantService due to the dependency declared in InstantServiceFactory.
147 TemplateURLService
* template_url_service_
;
149 // The process ids associated with Instant processes.
150 std::set
<int> process_ids_
;
152 // InstantMostVisitedItems sent to the Instant Pages.
153 std::vector
<InstantMostVisitedItem
> most_visited_items_
;
155 // Theme-related data for NTP overlay to adopt themes.
156 scoped_ptr
<ThemeBackgroundInfo
> theme_info_
;
158 // The start-edge margin of the omnibox, used by the Instant page to align
159 // text or assets properly with the omnibox.
160 int omnibox_start_margin_
;
162 ObserverList
<InstantServiceObserver
> observers_
;
164 content::NotificationRegistrar registrar_
;
166 scoped_refptr
<InstantIOContext
> instant_io_context_
;
168 // Set to NULL if the default search provider does not support Instant.
169 scoped_ptr
<InstantSearchPrerenderer
> instant_prerenderer_
;
171 // Used to check whether notifications from TemplateURLService indicate a
172 // change that affects the default search provider.
173 scoped_ptr
<TemplateURLData
> previous_default_search_provider_
;
174 GURL previous_google_base_url_
;
176 // Used for Top Sites async retrieval.
177 base::WeakPtrFactory
<InstantService
> weak_ptr_factory_
;
179 DISALLOW_COPY_AND_ASSIGN(InstantService
);
182 #endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_