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 "components/suggestions/proto/suggestions.pb.h"
19 #include "components/suggestions/suggestions_service.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
24 class InstantIOContext
;
25 struct InstantMostVisitedItem
;
26 class InstantSearchPrerenderer
;
27 class InstantServiceObserver
;
29 struct TemplateURLData
;
30 class TemplateURLService
;
31 struct ThemeBackgroundInfo
;
35 class RenderProcessHost
;
38 // Tracks render process host IDs that are associated with Instant.
39 class InstantService
: public KeyedService
,
40 public content::NotificationObserver
,
41 public TemplateURLServiceObserver
,
42 public history::TopSitesObserver
{
44 explicit InstantService(Profile
* profile
);
45 ~InstantService() override
;
47 // Add, remove, and query RenderProcessHost IDs that are associated with
49 void AddInstantProcess(int process_id
);
50 bool IsInstantProcess(int process_id
) const;
52 // Adds/Removes InstantService observers.
53 void AddObserver(InstantServiceObserver
* observer
);
54 void RemoveObserver(InstantServiceObserver
* observer
);
56 #if defined(UNIT_TEST)
57 int GetInstantProcessCount() const {
58 return process_ids_
.size();
62 // Most visited item API.
64 // Invoked by the InstantController when the Instant page wants to delete a
66 void DeleteMostVisitedItem(const GURL
& url
);
68 // Invoked by the InstantController when the Instant page wants to undo the
70 void UndoMostVisitedDeletion(const GURL
& url
);
72 // Invoked by the InstantController when the Instant page wants to undo all
73 // Most Visited deletions.
74 void UndoAllMostVisitedDeletions();
76 // Invoked by the InstantController to update theme information for NTP.
78 // TODO(kmadhusu): Invoking this from InstantController shouldn't be
79 // necessary. Investigate more and remove this from here.
80 void UpdateThemeInfo();
82 // Invoked by the InstantController to update most visited items details for
84 void UpdateMostVisitedItemsInfo();
86 // Sends the current set of search URLs to a renderer process.
87 void SendSearchURLsToRenderer(content::RenderProcessHost
* rph
);
89 // Invoked to notify the Instant page that the omnibox start margin has
91 void OnOmniboxStartMarginChanged(int start_margin
);
93 InstantSearchPrerenderer
* instant_search_prerenderer() {
94 return instant_prerenderer_
.get();
97 int omnibox_start_margin() const { return omnibox_start_margin_
; }
100 friend class InstantExtendedTest
;
101 friend class InstantServiceTest
;
102 friend class InstantTestBase
;
103 friend class InstantUnitTestBase
;
105 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest
,
106 MANUAL_SearchesFromFakebox
);
107 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, ProcessIsolation
);
108 FRIEND_TEST_ALL_PREFIXES(InstantServiceEnabledTest
,
109 SendsSearchURLsToRenderer
);
112 void Shutdown() override
;
114 // content::NotificationObserver:
115 void Observe(int type
,
116 const content::NotificationSource
& source
,
117 const content::NotificationDetails
& details
) override
;
119 // 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
123 void OnTemplateURLServiceChanged() override
;
126 void TopSitesLoaded(history::TopSites
* top_sites
) override
;
127 void TopSitesChanged(history::TopSites
* top_sites
,
128 ChangeReason change_reason
) override
;
130 // Called when a renderer process is terminated.
131 void OnRendererProcessTerminated(int process_id
);
133 // Called when SuggestionsService has a new suggestions profile available.
134 void OnSuggestionsAvailable(const suggestions::SuggestionsProfile
& profile
);
136 // 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
);
141 // Notifies the observer about the last known most visited items.
142 void NotifyAboutMostVisitedItems();
144 #if defined(ENABLE_THEMES)
145 // Theme changed notification handler.
146 void OnThemeChanged(ThemeService
* theme_service
);
149 void ResetInstantSearchPrerenderer();
151 Profile
* const profile_
;
153 // The TemplateURLService that we are observing. It will outlive this
154 // InstantService due to the dependency declared in InstantServiceFactory.
155 TemplateURLService
* template_url_service_
;
157 // The process ids associated with Instant processes.
158 std::set
<int> process_ids_
;
160 // InstantMostVisitedItems from TopSites.
161 std::vector
<InstantMostVisitedItem
> most_visited_items_
;
163 // InstantMostVisitedItems from SuggestionService.
164 std::vector
<InstantMostVisitedItem
> suggestions_items_
;
166 // Theme-related data for NTP overlay to adopt themes.
167 scoped_ptr
<ThemeBackgroundInfo
> theme_info_
;
169 // 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_
;
173 base::ObserverList
<InstantServiceObserver
> observers_
;
175 content::NotificationRegistrar registrar_
;
177 scoped_refptr
<InstantIOContext
> instant_io_context_
;
179 // Set to NULL if the default search provider does not support Instant.
180 scoped_ptr
<InstantSearchPrerenderer
> instant_prerenderer_
;
182 // 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_
;
187 // Suggestions Service to fetch server suggestions.
188 suggestions::SuggestionsService
* suggestions_service_
;
190 // Used for Top Sites async retrieval.
191 base::WeakPtrFactory
<InstantService
> weak_ptr_factory_
;
193 DISALLOW_COPY_AND_ASSIGN(InstantService
);
196 #endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_