blob: e261d6cfb9fc37d9607aba85a25029573e9e5309 [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
[email protected]e7868c82013-03-12 03:55:568#include <map>
[email protected]c5dec6292013-01-25 04:54:529#include <set>
[email protected]e7868c82013-03-12 03:55:5610#include <string>
[email protected]ab01dd752013-06-08 05:13:3611#include <vector>
[email protected]c5dec6292013-01-25 04:54:5212
13#include "base/basictypes.h"
14#include "base/compiler_specific.h"
[email protected]4ff347e2013-07-22 19:39:0015#include "base/gtest_prod_util.h"
[email protected]672c8c12013-03-07 12:30:0616#include "base/memory/ref_counted.h"
[email protected]6af41782013-06-22 13:49:1117#include "base/memory/scoped_ptr.h"
[email protected]280be992013-06-04 03:19:5118#include "base/memory/weak_ptr.h"
[email protected]6af41782013-06-22 13:49:1119#include "base/observer_list.h"
[email protected]c8a118e2013-09-24 21:33:4020#include "base/prefs/pref_change_registrar.h"
21#include "chrome/browser/google/google_url_tracker.h"
[email protected]280be992013-06-04 03:19:5122#include "chrome/browser/history/history_types.h"
[email protected]4ff347e2013-07-22 19:39:0023#include "chrome/browser/ui/search/instant_ntp_prerenderer.h"
[email protected]ab01dd752013-06-08 05:13:3624#include "chrome/common/instant_types.h"
[email protected]0dd6f2032013-05-20 23:33:4025#include "components/browser_context_keyed_service/browser_context_keyed_service.h"
[email protected]54e0ee62013-02-01 17:25:0326#include "content/public/browser/notification_observer.h"
27#include "content/public/browser/notification_registrar.h"
[email protected]c5dec6292013-01-25 04:54:5228
[email protected]e7868c82013-03-12 03:55:5629class GURL;
[email protected]672c8c12013-03-07 12:30:0630class InstantIOContext;
[email protected]6af41782013-06-22 13:49:1131class InstantServiceObserver;
[email protected]2309e912013-10-01 01:33:3032class InstantTestBase;
33class InstantServiceTest;
[email protected]672c8c12013-03-07 12:30:0634class Profile;
[email protected]6af41782013-06-22 13:49:1135class ThemeService;
[email protected]672c8c12013-03-07 12:30:0636
[email protected]4ff347e2013-07-22 19:39:0037namespace content {
38class WebContents;
39}
40
[email protected]e7868c82013-03-12 03:55:5641namespace net {
42class URLRequest;
43}
44
[email protected]d572bfd2013-02-14 06:14:2045// Tracks render process host IDs that are associated with Instant.
[email protected]f1484c52013-05-22 23:25:4446class InstantService : public BrowserContextKeyedService,
[email protected]54e0ee62013-02-01 17:25:0347 public content::NotificationObserver {
[email protected]c5dec6292013-01-25 04:54:5248 public:
[email protected]672c8c12013-03-07 12:30:0649 explicit InstantService(Profile* profile);
[email protected]c5dec6292013-01-25 04:54:5250 virtual ~InstantService();
51
52 // Add, remove, and query RenderProcessHost IDs that are associated with
53 // Instant processes.
54 void AddInstantProcess(int process_id);
[email protected]c5dec6292013-01-25 04:54:5255 bool IsInstantProcess(int process_id) const;
56
[email protected]6af41782013-06-22 13:49:1157 // Adds/Removes InstantService observers.
58 void AddObserver(InstantServiceObserver* observer);
59 void RemoveObserver(InstantServiceObserver* observer);
60
[email protected]d572bfd2013-02-14 06:14:2061#if defined(UNIT_TEST)
62 int GetInstantProcessCount() const {
63 return process_ids_.size();
64 }
65#endif
[email protected]c5dec6292013-01-25 04:54:5266
[email protected]8b169b4b2013-03-23 18:24:4667 // Most visited item API.
[email protected]e7868c82013-03-12 03:55:5668
[email protected]280be992013-06-04 03:19:5169 // Invoked by the InstantController when the Instant page wants to delete a
70 // Most Visited item.
71 void DeleteMostVisitedItem(const GURL& url);
72
73 // Invoked by the InstantController when the Instant page wants to undo the
74 // blacklist action.
75 void UndoMostVisitedDeletion(const GURL& url);
76
77 // Invoked by the InstantController when the Instant page wants to undo all
78 // Most Visited deletions.
79 void UndoAllMostVisitedDeletions();
80
[email protected]6af41782013-06-22 13:49:1181 // Invoked by the InstantController to update theme information for NTP.
82 //
83 // TODO(kmadhusu): Invoking this from InstantController shouldn't be
84 // necessary. Investigate more and remove this from here.
85 void UpdateThemeInfo();
86
[email protected]ed68ae32013-06-29 20:46:4887 // Invoked by the InstantController to update most visited items details for
88 // NTP.
89 void UpdateMostVisitedItemsInfo();
90
[email protected]4ff347e2013-07-22 19:39:0091 // 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;
95
96 // The NTP WebContents. May be NULL. InstantNTPPrerenderer retains ownership.
97 content::WebContents* GetNTPContents() const;
98
99 // Notifies InstantService about the creation of a BrowserInstantController
100 // object. Used to preload InstantNTP.
101 void OnBrowserInstantControllerCreated();
102
103 // Notifies InstantService about the destruction of a BrowserInstantController
104 // object. Used to destroy the preloaded InstantNTP.
105 void OnBrowserInstantControllerDestroyed();
106
[email protected]2309e912013-10-01 01:33:30107 // Sends the current set of search URLs to a renderer process.
108 void SendSearchURLsToRenderer(content::RenderProcessHost* rph);
109
[email protected]c5dec6292013-01-25 04:54:52110 private:
[email protected]4ff347e2013-07-22 19:39:00111 friend class InstantExtendedTest;
[email protected]c8a118e2013-09-24 21:33:40112 friend class InstantServiceTest;
[email protected]4ff347e2013-07-22 19:39:00113 friend class InstantTestBase;
[email protected]c8a118e2013-09-24 21:33:40114 friend class InstantUnitTestBase;
[email protected]4ff347e2013-07-22 19:39:00115
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);
[email protected]df113a12013-07-24 04:56:08122 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
[email protected]2309e912013-10-01 01:33:30123 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, SendsSearchURLsToRenderer);
[email protected]4ff347e2013-07-22 19:39:00124
[email protected]f1484c52013-05-22 23:25:44125 // Overridden from BrowserContextKeyedService:
[email protected]c5dec6292013-01-25 04:54:52126 virtual void Shutdown() OVERRIDE;
127
[email protected]d572bfd2013-02-14 06:14:20128 // Overridden from content::NotificationObserver:
[email protected]54e0ee62013-02-01 17:25:03129 virtual void Observe(int type,
130 const content::NotificationSource& source,
131 const content::NotificationDetails& details) OVERRIDE;
132
[email protected]2309e912013-10-01 01:33:30133 // Called when a renderer process is terminated.
134 void OnRendererProcessTerminated(int process_id);
135
[email protected]280be992013-06-04 03:19:51136 // 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);
140
[email protected]ed68ae32013-06-29 20:46:48141 // Notifies the observer about the last known most visited items.
142 void NotifyAboutMostVisitedItems();
143
[email protected]6af41782013-06-22 13:49:11144 // Theme changed notification handler.
145 void OnThemeChanged(ThemeService* theme_service);
146
[email protected]c8a118e2013-09-24 21:33:40147 void OnGoogleURLUpdated(Profile* profile,
148 GoogleURLTracker::UpdatedDetails* details);
149
150 void OnDefaultSearchProviderChanged(const std::string& pref_name);
151
[email protected]4ff347e2013-07-22 19:39:00152 // Used by tests.
153 InstantNTPPrerenderer* ntp_prerenderer();
154
[email protected]672c8c12013-03-07 12:30:06155 Profile* const profile_;
156
[email protected]c5dec6292013-01-25 04:54:52157 // The process ids associated with Instant processes.
158 std::set<int> process_ids_;
159
[email protected]ab01dd752013-06-08 05:13:36160 // InstantMostVisitedItems sent to the Instant Pages.
161 std::vector<InstantMostVisitedItem> most_visited_items_;
[email protected]e7868c82013-03-12 03:55:56162
[email protected]6af41782013-06-22 13:49:11163 // Theme-related data for NTP overlay to adopt themes.
164 scoped_ptr<ThemeBackgroundInfo> theme_info_;
165
166 ObserverList<InstantServiceObserver> observers_;
167
[email protected]54e0ee62013-02-01 17:25:03168 content::NotificationRegistrar registrar_;
169
[email protected]c8a118e2013-09-24 21:33:40170 PrefChangeRegistrar profile_pref_registrar_;
171
[email protected]672c8c12013-03-07 12:30:06172 scoped_refptr<InstantIOContext> instant_io_context_;
173
[email protected]4ff347e2013-07-22 19:39:00174 InstantNTPPrerenderer ntp_prerenderer_;
175
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_;
179
[email protected]280be992013-06-04 03:19:51180 // Used for Top Sites async retrieval.
181 base::WeakPtrFactory<InstantService> weak_ptr_factory_;
182
[email protected]c5dec6292013-01-25 04:54:52183 DISALLOW_COPY_AND_ASSIGN(InstantService);
184};
185
[email protected]a7b8e43d2013-03-18 18:52:43186#endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_