Home | History | Annotate | Download | only in search
      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/browser/ui/search/instant_search_prerenderer.h"
     25 #include "chrome/common/instant_types.h"
     26 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
     27 #include "content/public/browser/notification_observer.h"
     28 #include "content/public/browser/notification_registrar.h"
     29 
     30 class GURL;
     31 class InstantIOContext;
     32 class InstantServiceObserver;
     33 class InstantTestBase;
     34 class InstantServiceTest;
     35 class Profile;
     36 class ThemeService;
     37 
     38 namespace content {
     39 class WebContents;
     40 }
     41 
     42 namespace net {
     43 class URLRequest;
     44 }
     45 
     46 // Tracks render process host IDs that are associated with Instant.
     47 class InstantService : public BrowserContextKeyedService,
     48                        public content::NotificationObserver {
     49  public:
     50   explicit InstantService(Profile* profile);
     51   virtual ~InstantService();
     52 
     53   // Add, remove, and query RenderProcessHost IDs that are associated with
     54   // Instant processes.
     55   void AddInstantProcess(int process_id);
     56   bool IsInstantProcess(int process_id) const;
     57 
     58   // Adds/Removes InstantService observers.
     59   void AddObserver(InstantServiceObserver* observer);
     60   void RemoveObserver(InstantServiceObserver* observer);
     61 
     62 #if defined(UNIT_TEST)
     63   int GetInstantProcessCount() const {
     64     return process_ids_.size();
     65   }
     66 #endif
     67 
     68   // Most visited item API.
     69 
     70   // Invoked by the InstantController when the Instant page wants to delete a
     71   // Most Visited item.
     72   void DeleteMostVisitedItem(const GURL& url);
     73 
     74   // Invoked by the InstantController when the Instant page wants to undo the
     75   // blacklist action.
     76   void UndoMostVisitedDeletion(const GURL& url);
     77 
     78   // Invoked by the InstantController when the Instant page wants to undo all
     79   // Most Visited deletions.
     80   void UndoAllMostVisitedDeletions();
     81 
     82   // Invoked by the InstantController to update theme information for NTP.
     83   //
     84   // TODO(kmadhusu): Invoking this from InstantController shouldn't be
     85   // necessary. Investigate more and remove this from here.
     86   void UpdateThemeInfo();
     87 
     88   // Invoked by the InstantController to update most visited items details for
     89   // NTP.
     90   void UpdateMostVisitedItemsInfo();
     91 
     92   // Forwards the request to InstantNTPPrerenderer to release and return the
     93   // preloaded InstantNTP WebContents. May be NULL. InstantNTPPrerenderer will
     94   // load a new InstantNTP after releasing the preloaded contents.
     95   scoped_ptr<content::WebContents> ReleaseNTPContents() WARN_UNUSED_RESULT;
     96 
     97   // The NTP WebContents. May be NULL. InstantNTPPrerenderer retains ownership.
     98   content::WebContents* GetNTPContents() const;
     99 
    100   // Notifies InstantService about the creation of a BrowserInstantController
    101   // object. Used to preload InstantNTP.
    102   void OnBrowserInstantControllerCreated();
    103 
    104   // Notifies InstantService about the destruction of a BrowserInstantController
    105   // object. Used to destroy the preloaded InstantNTP.
    106   void OnBrowserInstantControllerDestroyed();
    107 
    108   // Sends the current set of search URLs to a renderer process.
    109   void SendSearchURLsToRenderer(content::RenderProcessHost* rph);
    110 
    111   InstantSearchPrerenderer* instant_search_prerenderer() {
    112     return instant_prerenderer_.get();
    113   }
    114 
    115  private:
    116   friend class InstantExtendedTest;
    117   friend class InstantServiceTest;
    118   friend class InstantTestBase;
    119   friend class InstantUnitTestBase;
    120 
    121   FRIEND_TEST_ALL_PREFIXES(InstantExtendedNetworkTest,
    122                            NTPReactsToNetworkChanges);
    123   FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest,
    124                            MANUAL_ShowsGoogleNTP);
    125   FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest,
    126                            MANUAL_SearchesFromFakebox);
    127   FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
    128   FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, SendsSearchURLsToRenderer);
    129 
    130   // Overridden from BrowserContextKeyedService:
    131   virtual void Shutdown() OVERRIDE;
    132 
    133   // Overridden from content::NotificationObserver:
    134   virtual void Observe(int type,
    135                        const content::NotificationSource& source,
    136                        const content::NotificationDetails& details) OVERRIDE;
    137 
    138   // Called when a renderer process is terminated.
    139   void OnRendererProcessTerminated(int process_id);
    140 
    141   // Called when we get new most visited items from TopSites, registered as an
    142   // async callback. Parses them and sends them to the renderer via
    143   // SendMostVisitedItems.
    144   void OnMostVisitedItemsReceived(const history::MostVisitedURLList& data);
    145 
    146   // Notifies the observer about the last known most visited items.
    147   void NotifyAboutMostVisitedItems();
    148 
    149   // Theme changed notification handler.
    150   void OnThemeChanged(ThemeService* theme_service);
    151 
    152   void OnGoogleURLUpdated(Profile* profile,
    153                           GoogleURLTracker::UpdatedDetails* details);
    154 
    155   void OnDefaultSearchProviderChanged(const std::string& pref_name);
    156 
    157   // Used by tests.
    158   InstantNTPPrerenderer* ntp_prerenderer();
    159 
    160   void ResetInstantSearchPrerenderer();
    161 
    162   Profile* const profile_;
    163 
    164   // The process ids associated with Instant processes.
    165   std::set<int> process_ids_;
    166 
    167   // InstantMostVisitedItems sent to the Instant Pages.
    168   std::vector<InstantMostVisitedItem> most_visited_items_;
    169 
    170   // Theme-related data for NTP overlay to adopt themes.
    171   scoped_ptr<ThemeBackgroundInfo> theme_info_;
    172 
    173   ObserverList<InstantServiceObserver> observers_;
    174 
    175   content::NotificationRegistrar registrar_;
    176 
    177   PrefChangeRegistrar profile_pref_registrar_;
    178 
    179   scoped_refptr<InstantIOContext> instant_io_context_;
    180 
    181   InstantNTPPrerenderer ntp_prerenderer_;
    182 
    183   // Total number of BrowserInstantController objects (does not include objects
    184   // created for OTR browser windows). Used to preload and delete InstantNTP.
    185   size_t browser_instant_controller_object_count_;
    186 
    187   // Set to NULL if the default search provider does not support Instant.
    188   scoped_ptr<InstantSearchPrerenderer> instant_prerenderer_;
    189 
    190   // Used for Top Sites async retrieval.
    191   base::WeakPtrFactory<InstantService> weak_ptr_factory_;
    192 
    193   DISALLOW_COPY_AND_ASSIGN(InstantService);
    194 };
    195 
    196 #endif  // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_
    197