- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / search / instant_service.h
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
5 #ifndef CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_
6 #define CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
12
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"
28
29 class GURL;
30 class InstantIOContext;
31 class InstantServiceObserver;
32 class InstantTestBase;
33 class InstantServiceTest;
34 class Profile;
35 class ThemeService;
36
37 namespace content {
38 class WebContents;
39 }
40
41 namespace net {
42 class URLRequest;
43 }
44
45 // Tracks render process host IDs that are associated with Instant.
46 class InstantService : public BrowserContextKeyedService,
47                        public content::NotificationObserver {
48  public:
49   explicit InstantService(Profile* profile);
50   virtual ~InstantService();
51
52   // Add, remove, and query RenderProcessHost IDs that are associated with
53   // Instant processes.
54   void AddInstantProcess(int process_id);
55   bool IsInstantProcess(int process_id) const;
56
57   // Adds/Removes InstantService observers.
58   void AddObserver(InstantServiceObserver* observer);
59   void RemoveObserver(InstantServiceObserver* observer);
60
61 #if defined(UNIT_TEST)
62   int GetInstantProcessCount() const {
63     return process_ids_.size();
64   }
65 #endif
66
67   // Most visited item API.
68
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
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
87   // Invoked by the InstantController to update most visited items details for
88   // NTP.
89   void UpdateMostVisitedItemsInfo();
90
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
107   // Sends the current set of search URLs to a renderer process.
108   void SendSearchURLsToRenderer(content::RenderProcessHost* rph);
109
110  private:
111   friend class InstantExtendedTest;
112   friend class InstantServiceTest;
113   friend class InstantTestBase;
114   friend class InstantUnitTestBase;
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);
122   FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
123   FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, SendsSearchURLsToRenderer);
124
125   // Overridden from BrowserContextKeyedService:
126   virtual void Shutdown() OVERRIDE;
127
128   // Overridden from content::NotificationObserver:
129   virtual void Observe(int type,
130                        const content::NotificationSource& source,
131                        const content::NotificationDetails& details) OVERRIDE;
132
133   // Called when a renderer process is terminated.
134   void OnRendererProcessTerminated(int process_id);
135
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
141   // Notifies the observer about the last known most visited items.
142   void NotifyAboutMostVisitedItems();
143
144   // Theme changed notification handler.
145   void OnThemeChanged(ThemeService* theme_service);
146
147   void OnGoogleURLUpdated(Profile* profile,
148                           GoogleURLTracker::UpdatedDetails* details);
149
150   void OnDefaultSearchProviderChanged(const std::string& pref_name);
151
152   // Used by tests.
153   InstantNTPPrerenderer* ntp_prerenderer();
154
155   Profile* const profile_;
156
157   // The process ids associated with Instant processes.
158   std::set<int> process_ids_;
159
160   // InstantMostVisitedItems sent to the Instant Pages.
161   std::vector<InstantMostVisitedItem> most_visited_items_;
162
163   // Theme-related data for NTP overlay to adopt themes.
164   scoped_ptr<ThemeBackgroundInfo> theme_info_;
165
166   ObserverList<InstantServiceObserver> observers_;
167
168   content::NotificationRegistrar registrar_;
169
170   PrefChangeRegistrar profile_pref_registrar_;
171
172   scoped_refptr<InstantIOContext> instant_io_context_;
173
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
180   // Used for Top Sites async retrieval.
181   base::WeakPtrFactory<InstantService> weak_ptr_factory_;
182
183   DISALLOW_COPY_AND_ASSIGN(InstantService);
184 };
185
186 #endif  // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_