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/keyed_service/core/keyed_service.h"
16 #include "components/search_engines/template_url_service_observer.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
21 class InstantIOContext
;
22 struct InstantMostVisitedItem
;
23 class InstantSearchPrerenderer
;
24 class InstantServiceObserver
;
26 struct TemplateURLData
;
27 class TemplateURLService
;
28 struct ThemeBackgroundInfo
;
32 class RenderProcessHost
;
35 // Tracks render process host IDs that are associated with Instant.
36 class InstantService
: public KeyedService
,
37 public content::NotificationObserver
,
38 public TemplateURLServiceObserver
{
40 explicit InstantService(Profile
* profile
);
41 virtual ~InstantService();
43 // Add, remove, and query RenderProcessHost IDs that are associated with
45 void AddInstantProcess(int process_id
);
46 bool IsInstantProcess(int process_id
) const;
48 // Adds/Removes InstantService observers.
49 void AddObserver(InstantServiceObserver
* observer
);
50 void RemoveObserver(InstantServiceObserver
* observer
);
52 #if defined(UNIT_TEST)
53 int GetInstantProcessCount() const {
54 return process_ids_
.size();
58 // Most visited item API.
60 // Invoked by the InstantController when the Instant page wants to delete a
62 void DeleteMostVisitedItem(const GURL
& url
);
64 // Invoked by the InstantController when the Instant page wants to undo the
66 void UndoMostVisitedDeletion(const GURL
& url
);
68 // Invoked by the InstantController when the Instant page wants to undo all
69 // Most Visited deletions.
70 void UndoAllMostVisitedDeletions();
72 // Invoked by the InstantController to update theme information for NTP.
74 // TODO(kmadhusu): Invoking this from InstantController shouldn't be
75 // necessary. Investigate more and remove this from here.
76 void UpdateThemeInfo();
78 // Invoked by the InstantController to update most visited items details for
80 void UpdateMostVisitedItemsInfo();
82 // Sends the current set of search URLs to a renderer process.
83 void SendSearchURLsToRenderer(content::RenderProcessHost
* rph
);
85 // Invoked to notify the Instant page that the omnibox start margin has
87 void OnOmniboxStartMarginChanged(int start_margin
);
89 InstantSearchPrerenderer
* instant_search_prerenderer() {
90 return instant_prerenderer_
.get();
93 int omnibox_start_margin() const { return omnibox_start_margin_
; }
96 friend class InstantExtendedTest
;
97 friend class InstantServiceTest
;
98 friend class InstantTestBase
;
99 friend class InstantUnitTestBase
;
101 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest
,
102 MANUAL_SearchesFromFakebox
);
103 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, ProcessIsolation
);
104 FRIEND_TEST_ALL_PREFIXES(InstantServiceEnabledTest
,
105 SendsSearchURLsToRenderer
);
108 virtual void Shutdown() override
;
110 // content::NotificationObserver:
111 virtual void Observe(int type
,
112 const content::NotificationSource
& source
,
113 const content::NotificationDetails
& details
) override
;
115 // 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
119 virtual void OnTemplateURLServiceChanged() override
;
121 // Called when a renderer process is terminated.
122 void OnRendererProcessTerminated(int process_id
);
124 // 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
);
129 // Notifies the observer about the last known most visited items.
130 void NotifyAboutMostVisitedItems();
132 // Theme changed notification handler.
133 void OnThemeChanged(ThemeService
* theme_service
);
135 void ResetInstantSearchPrerenderer();
137 Profile
* const profile_
;
139 // The TemplateURLService that we are observing. It will outlive this
140 // InstantService due to the dependency declared in InstantServiceFactory.
141 TemplateURLService
* template_url_service_
;
143 // The process ids associated with Instant processes.
144 std::set
<int> process_ids_
;
146 // InstantMostVisitedItems sent to the Instant Pages.
147 std::vector
<InstantMostVisitedItem
> most_visited_items_
;
149 // Theme-related data for NTP overlay to adopt themes.
150 scoped_ptr
<ThemeBackgroundInfo
> theme_info_
;
152 // 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_
;
156 ObserverList
<InstantServiceObserver
> observers_
;
158 content::NotificationRegistrar registrar_
;
160 scoped_refptr
<InstantIOContext
> instant_io_context_
;
162 // Set to NULL if the default search provider does not support Instant.
163 scoped_ptr
<InstantSearchPrerenderer
> instant_prerenderer_
;
165 // 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_
;
170 // Used for Top Sites async retrieval.
171 base::WeakPtrFactory
<InstantService
> weak_ptr_factory_
;
173 DISALLOW_COPY_AND_ASSIGN(InstantService
);
176 #endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_