[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 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. |
| 4 | |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |
| 6 | #define CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 7 | |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 8 | #include <map> |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 9 | #include <set> |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 10 | #include <string> |
[email protected] | ab01dd75 | 2013-06-08 05:13:36 | [diff] [blame] | 11 | #include <vector> |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 12 | |
| 13 | #include "base/basictypes.h" |
| 14 | #include "base/compiler_specific.h" |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 15 | #include "base/gtest_prod_util.h" |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 17 | #include "base/memory/scoped_ptr.h" |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 18 | #include "base/memory/weak_ptr.h" |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 19 | #include "base/observer_list.h" |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 20 | #include "base/prefs/pref_change_registrar.h" |
| 21 | #include "chrome/browser/google/google_url_tracker.h" |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 22 | #include "chrome/browser/history/history_types.h" |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 23 | #include "chrome/browser/ui/search/instant_ntp_prerenderer.h" |
[email protected] | ab01dd75 | 2013-06-08 05:13:36 | [diff] [blame] | 24 | #include "chrome/common/instant_types.h" |
[email protected] | 0dd6f203 | 2013-05-20 23:33:40 | [diff] [blame] | 25 | #include "components/browser_context_keyed_service/browser_context_keyed_service.h" |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 26 | #include "content/public/browser/notification_observer.h" |
| 27 | #include "content/public/browser/notification_registrar.h" |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 28 | |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 29 | class GURL; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 30 | class InstantIOContext; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 31 | class InstantServiceObserver; |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame^] | 32 | class InstantTestBase; |
| 33 | class InstantServiceTest; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 34 | class Profile; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 35 | class ThemeService; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 36 | |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 37 | namespace content { |
| 38 | class WebContents; |
| 39 | } |
| 40 | |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 41 | namespace net { |
| 42 | class URLRequest; |
| 43 | } |
| 44 | |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 45 | // Tracks render process host IDs that are associated with Instant. |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame] | 46 | class InstantService : public BrowserContextKeyedService, |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 47 | public content::NotificationObserver { |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 48 | public: |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 49 | explicit InstantService(Profile* profile); |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 50 | 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] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 55 | bool IsInstantProcess(int process_id) const; |
| 56 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 57 | // Adds/Removes InstantService observers. |
| 58 | void AddObserver(InstantServiceObserver* observer); |
| 59 | void RemoveObserver(InstantServiceObserver* observer); |
| 60 | |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 61 | #if defined(UNIT_TEST) |
| 62 | int GetInstantProcessCount() const { |
| 63 | return process_ids_.size(); |
| 64 | } |
| 65 | #endif |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 66 | |
[email protected] | 8b169b4b | 2013-03-23 18:24:46 | [diff] [blame] | 67 | // Most visited item API. |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 68 | |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 69 | // 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] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 81 | // 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] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 87 | // Invoked by the InstantController to update most visited items details for |
| 88 | // NTP. |
| 89 | void UpdateMostVisitedItemsInfo(); |
| 90 | |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 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; |
| 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] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame^] | 107 | // Sends the current set of search URLs to a renderer process. |
| 108 | void SendSearchURLsToRenderer(content::RenderProcessHost* rph); |
| 109 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 110 | private: |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 111 | friend class InstantExtendedTest; |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 112 | friend class InstantServiceTest; |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 113 | friend class InstantTestBase; |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 114 | friend class InstantUnitTestBase; |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 115 | |
| 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] | df113a1 | 2013-07-24 04:56:08 | [diff] [blame] | 122 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation); |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame^] | 123 | FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, SendsSearchURLsToRenderer); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 124 | |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame] | 125 | // Overridden from BrowserContextKeyedService: |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 126 | virtual void Shutdown() OVERRIDE; |
| 127 | |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 128 | // Overridden from content::NotificationObserver: |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 129 | virtual void Observe(int type, |
| 130 | const content::NotificationSource& source, |
| 131 | const content::NotificationDetails& details) OVERRIDE; |
| 132 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame^] | 133 | // Called when a renderer process is terminated. |
| 134 | void OnRendererProcessTerminated(int process_id); |
| 135 | |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 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); |
| 140 | |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 141 | // Notifies the observer about the last known most visited items. |
| 142 | void NotifyAboutMostVisitedItems(); |
| 143 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 144 | // Theme changed notification handler. |
| 145 | void OnThemeChanged(ThemeService* theme_service); |
| 146 | |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 147 | void OnGoogleURLUpdated(Profile* profile, |
| 148 | GoogleURLTracker::UpdatedDetails* details); |
| 149 | |
| 150 | void OnDefaultSearchProviderChanged(const std::string& pref_name); |
| 151 | |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 152 | // Used by tests. |
| 153 | InstantNTPPrerenderer* ntp_prerenderer(); |
| 154 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 155 | Profile* const profile_; |
| 156 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 157 | // The process ids associated with Instant processes. |
| 158 | std::set<int> process_ids_; |
| 159 | |
[email protected] | ab01dd75 | 2013-06-08 05:13:36 | [diff] [blame] | 160 | // InstantMostVisitedItems sent to the Instant Pages. |
| 161 | std::vector<InstantMostVisitedItem> most_visited_items_; |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 162 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 163 | // Theme-related data for NTP overlay to adopt themes. |
| 164 | scoped_ptr<ThemeBackgroundInfo> theme_info_; |
| 165 | |
| 166 | ObserverList<InstantServiceObserver> observers_; |
| 167 | |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 168 | content::NotificationRegistrar registrar_; |
| 169 | |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 170 | PrefChangeRegistrar profile_pref_registrar_; |
| 171 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 172 | scoped_refptr<InstantIOContext> instant_io_context_; |
| 173 | |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 174 | 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] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 180 | // Used for Top Sites async retrieval. |
| 181 | base::WeakPtrFactory<InstantService> weak_ptr_factory_; |
| 182 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 183 | DISALLOW_COPY_AND_ASSIGN(InstantService); |
| 184 | }; |
| 185 | |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 186 | #endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |