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_
13 #include "base/basictypes.h"
14 #include "base/compiler_specific.h"
15 #include "base/gtest_prod_util.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/observer_list.h"
20 #include "base/prefs/pref_change_registrar.h"
21 #include "chrome/browser/google/google_url_tracker.h"
22 #include "chrome/browser/history/history_types.h"
23 #include "chrome/browser/ui/search/instant_ntp_prerenderer.h"
24 #include "chrome/common/instant_types.h"
25 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
26 #include "content/public/browser/notification_observer.h"
27 #include "content/public/browser/notification_registrar.h"
30 class InstantIOContext;
31 class InstantServiceObserver;
32 class InstantTestBase;
33 class InstantServiceTest;
45 // Tracks render process host IDs that are associated with Instant.
46 class InstantService : public BrowserContextKeyedService,
47 public content::NotificationObserver {
49 explicit InstantService(Profile* profile);
50 virtual ~InstantService();
52 // Add, remove, and query RenderProcessHost IDs that are associated with
54 void AddInstantProcess(int process_id);
55 bool IsInstantProcess(int process_id) const;
57 // Adds/Removes InstantService observers.
58 void AddObserver(InstantServiceObserver* observer);
59 void RemoveObserver(InstantServiceObserver* observer);
61 #if defined(UNIT_TEST)
62 int GetInstantProcessCount() const {
63 return process_ids_.size();
67 // Most visited item API.
69 // Invoked by the InstantController when the Instant page wants to delete a
71 void DeleteMostVisitedItem(const GURL& url);
73 // Invoked by the InstantController when the Instant page wants to undo the
75 void UndoMostVisitedDeletion(const GURL& url);
77 // Invoked by the InstantController when the Instant page wants to undo all
78 // Most Visited deletions.
79 void UndoAllMostVisitedDeletions();
81 // Invoked by the InstantController to update theme information for NTP.
83 // TODO(kmadhusu): Invoking this from InstantController shouldn't be
84 // necessary. Investigate more and remove this from here.
85 void UpdateThemeInfo();
87 // Invoked by the InstantController to update most visited items details for
89 void UpdateMostVisitedItemsInfo();
91 // Forwards the request to InstantNTPPrerenderer to release and return the
92 // preloaded InstantNTP WebContents. May be NULL. InstantNTPPrerenderer will
93 // load a new InstantNTP after releasing the preloaded contents.
94 scoped_ptr<content::WebContents> ReleaseNTPContents() WARN_UNUSED_RESULT;
96 // The NTP WebContents. May be NULL. InstantNTPPrerenderer retains ownership.
97 content::WebContents* GetNTPContents() const;
99 // Notifies InstantService about the creation of a BrowserInstantController
100 // object. Used to preload InstantNTP.
101 void OnBrowserInstantControllerCreated();
103 // Notifies InstantService about the destruction of a BrowserInstantController
104 // object. Used to destroy the preloaded InstantNTP.
105 void OnBrowserInstantControllerDestroyed();
107 // Sends the current set of search URLs to a renderer process.
108 void SendSearchURLsToRenderer(content::RenderProcessHost* rph);
111 friend class InstantExtendedTest;
112 friend class InstantServiceTest;
113 friend class InstantTestBase;
114 friend class InstantUnitTestBase;
116 FRIEND_TEST_ALL_PREFIXES(InstantExtendedNetworkTest,
117 NTPReactsToNetworkChanges);
118 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest,
119 MANUAL_ShowsGoogleNTP);
120 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest,
121 MANUAL_SearchesFromFakebox);
122 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
123 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, SendsSearchURLsToRenderer);
125 // Overridden from BrowserContextKeyedService:
126 virtual void Shutdown() OVERRIDE;
128 // Overridden from content::NotificationObserver:
129 virtual void Observe(int type,
130 const content::NotificationSource& source,
131 const content::NotificationDetails& details) OVERRIDE;
133 // Called when a renderer process is terminated.
134 void OnRendererProcessTerminated(int process_id);
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 // Theme changed notification handler.
145 void OnThemeChanged(ThemeService* theme_service);
147 void OnGoogleURLUpdated(Profile* profile,
148 GoogleURLTracker::UpdatedDetails* details);
150 void OnDefaultSearchProviderChanged(const std::string& pref_name);
153 InstantNTPPrerenderer* ntp_prerenderer();
155 Profile* const profile_;
157 // The process ids associated with Instant processes.
158 std::set<int> process_ids_;
160 // InstantMostVisitedItems sent to the Instant Pages.
161 std::vector<InstantMostVisitedItem> most_visited_items_;
163 // Theme-related data for NTP overlay to adopt themes.
164 scoped_ptr<ThemeBackgroundInfo> theme_info_;
166 ObserverList<InstantServiceObserver> observers_;
168 content::NotificationRegistrar registrar_;
170 PrefChangeRegistrar profile_pref_registrar_;
172 scoped_refptr<InstantIOContext> instant_io_context_;
174 InstantNTPPrerenderer ntp_prerenderer_;
176 // Total number of BrowserInstantController objects (does not include objects
177 // created for OTR browser windows). Used to preload and delete InstantNTP.
178 size_t browser_instant_controller_object_count_;
180 // Used for Top Sites async retrieval.
181 base::WeakPtrFactory<InstantService> weak_ptr_factory_;
183 DISALLOW_COPY_AND_ASSIGN(InstantService);
186 #endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_