[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" |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 15 | #include "base/memory/scoped_ptr.h" |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 17 | #include "base/observer_list.h" |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 18 | #include "base/prefs/pref_change_registrar.h" |
| 19 | #include "chrome/browser/google/google_url_tracker.h" |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 20 | #include "chrome/browser/history/history_types.h" |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 21 | #include "chrome/browser/ui/search/instant_search_prerenderer.h" |
[email protected] | ab01dd75 | 2013-06-08 05:13:36 | [diff] [blame] | 22 | #include "chrome/common/instant_types.h" |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame^] | 23 | #include "components/keyed_service/core/keyed_service.h" |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 24 | #include "content/public/browser/notification_observer.h" |
| 25 | #include "content/public/browser/notification_registrar.h" |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 26 | |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 27 | class GURL; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 28 | class InstantIOContext; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 29 | class InstantServiceObserver; |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 30 | class InstantTestBase; |
| 31 | class InstantServiceTest; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 32 | class Profile; |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 33 | class ThemeService; |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 34 | |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 35 | namespace content { |
[email protected] | 77759005 | 2014-01-17 22:11:54 | [diff] [blame] | 36 | class RenderProcessHost; |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 37 | } |
| 38 | |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 39 | namespace net { |
| 40 | class URLRequest; |
| 41 | } |
| 42 | |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 43 | // Tracks render process host IDs that are associated with Instant. |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame^] | 44 | class InstantService : public KeyedService, |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 45 | public content::NotificationObserver { |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 46 | public: |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 47 | explicit InstantService(Profile* profile); |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 48 | virtual ~InstantService(); |
| 49 | |
| 50 | // Add, remove, and query RenderProcessHost IDs that are associated with |
| 51 | // Instant processes. |
| 52 | void AddInstantProcess(int process_id); |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 53 | bool IsInstantProcess(int process_id) const; |
| 54 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 55 | // Adds/Removes InstantService observers. |
| 56 | void AddObserver(InstantServiceObserver* observer); |
| 57 | void RemoveObserver(InstantServiceObserver* observer); |
| 58 | |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 59 | #if defined(UNIT_TEST) |
| 60 | int GetInstantProcessCount() const { |
| 61 | return process_ids_.size(); |
| 62 | } |
| 63 | #endif |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 64 | |
[email protected] | 8b169b4b | 2013-03-23 18:24:46 | [diff] [blame] | 65 | // Most visited item API. |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 66 | |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 67 | // Invoked by the InstantController when the Instant page wants to delete a |
| 68 | // Most Visited item. |
| 69 | void DeleteMostVisitedItem(const GURL& url); |
| 70 | |
| 71 | // Invoked by the InstantController when the Instant page wants to undo the |
| 72 | // blacklist action. |
| 73 | void UndoMostVisitedDeletion(const GURL& url); |
| 74 | |
| 75 | // Invoked by the InstantController when the Instant page wants to undo all |
| 76 | // Most Visited deletions. |
| 77 | void UndoAllMostVisitedDeletions(); |
| 78 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 79 | // Invoked by the InstantController to update theme information for NTP. |
| 80 | // |
| 81 | // TODO(kmadhusu): Invoking this from InstantController shouldn't be |
| 82 | // necessary. Investigate more and remove this from here. |
| 83 | void UpdateThemeInfo(); |
| 84 | |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 85 | // Invoked by the InstantController to update most visited items details for |
| 86 | // NTP. |
| 87 | void UpdateMostVisitedItemsInfo(); |
| 88 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 89 | // Sends the current set of search URLs to a renderer process. |
| 90 | void SendSearchURLsToRenderer(content::RenderProcessHost* rph); |
| 91 | |
[email protected] | 553c7af | 2014-02-06 17:58:58 | [diff] [blame] | 92 | // Invoked to notify the Instant page that the omnibox start margin has |
| 93 | // changed. |
| 94 | void OnOmniboxStartMarginChanged(int start_margin); |
| 95 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 96 | InstantSearchPrerenderer* instant_search_prerenderer() { |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 97 | return instant_prerenderer_.get(); |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 553c7af | 2014-02-06 17:58:58 | [diff] [blame] | 100 | int omnibox_start_margin() const { return omnibox_start_margin_; } |
| 101 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 102 | private: |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 103 | friend class InstantExtendedTest; |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 104 | friend class InstantServiceTest; |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 105 | friend class InstantTestBase; |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 106 | friend class InstantUnitTestBase; |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 107 | |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 108 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest, |
| 109 | MANUAL_SearchesFromFakebox); |
[email protected] | df113a1 | 2013-07-24 04:56:08 | [diff] [blame] | 110 | FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation); |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 111 | FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, SendsSearchURLsToRenderer); |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 112 | |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame^] | 113 | // Overridden from KeyedService: |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 114 | virtual void Shutdown() OVERRIDE; |
| 115 | |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 116 | // Overridden from content::NotificationObserver: |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 117 | virtual void Observe(int type, |
| 118 | const content::NotificationSource& source, |
| 119 | const content::NotificationDetails& details) OVERRIDE; |
| 120 | |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 121 | // Called when a renderer process is terminated. |
| 122 | void OnRendererProcessTerminated(int process_id); |
| 123 | |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 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); |
| 128 | |
[email protected] | ed68ae3 | 2013-06-29 20:46:48 | [diff] [blame] | 129 | // Notifies the observer about the last known most visited items. |
| 130 | void NotifyAboutMostVisitedItems(); |
| 131 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 132 | // Theme changed notification handler. |
| 133 | void OnThemeChanged(ThemeService* theme_service); |
| 134 | |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 135 | void OnGoogleURLUpdated(Profile* profile, |
| 136 | GoogleURLTracker::UpdatedDetails* details); |
| 137 | |
| 138 | void OnDefaultSearchProviderChanged(const std::string& pref_name); |
| 139 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 140 | void ResetInstantSearchPrerenderer(); |
| 141 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 142 | Profile* const profile_; |
| 143 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 144 | // The process ids associated with Instant processes. |
| 145 | std::set<int> process_ids_; |
| 146 | |
[email protected] | ab01dd75 | 2013-06-08 05:13:36 | [diff] [blame] | 147 | // InstantMostVisitedItems sent to the Instant Pages. |
| 148 | std::vector<InstantMostVisitedItem> most_visited_items_; |
[email protected] | e7868c8 | 2013-03-12 03:55:56 | [diff] [blame] | 149 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 150 | // Theme-related data for NTP overlay to adopt themes. |
| 151 | scoped_ptr<ThemeBackgroundInfo> theme_info_; |
| 152 | |
[email protected] | 553c7af | 2014-02-06 17:58:58 | [diff] [blame] | 153 | // The start-edge margin of the omnibox, used by the Instant page to align |
| 154 | // text or assets properly with the omnibox. |
| 155 | int omnibox_start_margin_; |
| 156 | |
[email protected] | 6af4178 | 2013-06-22 13:49:11 | [diff] [blame] | 157 | ObserverList<InstantServiceObserver> observers_; |
| 158 | |
[email protected] | 54e0ee6 | 2013-02-01 17:25:03 | [diff] [blame] | 159 | content::NotificationRegistrar registrar_; |
| 160 | |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 161 | PrefChangeRegistrar profile_pref_registrar_; |
| 162 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 163 | scoped_refptr<InstantIOContext> instant_io_context_; |
| 164 | |
[email protected] | e97887c | 2013-12-11 01:27:31 | [diff] [blame] | 165 | // Set to NULL if the default search provider does not support Instant. |
| 166 | scoped_ptr<InstantSearchPrerenderer> instant_prerenderer_; |
| 167 | |
[email protected] | 280be99 | 2013-06-04 03:19:51 | [diff] [blame] | 168 | // Used for Top Sites async retrieval. |
| 169 | base::WeakPtrFactory<InstantService> weak_ptr_factory_; |
| 170 | |
[email protected] | c5dec629 | 2013-01-25 04:54:52 | [diff] [blame] | 171 | DISALLOW_COPY_AND_ASSIGN(InstantService); |
| 172 | }; |
| 173 | |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 174 | #endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_ |