Home | History | Annotate | Download | only in browser
      1 // Copyright 2014 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 COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_
      6 #define COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <utility>
     11 
     12 #include "base/callback_forward.h"
     13 #include "base/callback_list.h"
     14 #include "base/gtest_prod_util.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/memory/weak_ptr.h"
     17 #include "components/google/core/browser/google_url_tracker_client.h"
     18 #include "components/google/core/browser/google_url_tracker_map_entry.h"
     19 #include "components/keyed_service/core/keyed_service.h"
     20 #include "net/base/network_change_notifier.h"
     21 #include "net/url_request/url_fetcher.h"
     22 #include "net/url_request/url_fetcher_delegate.h"
     23 #include "url/gurl.h"
     24 
     25 class GoogleURLTrackerNavigationHelper;
     26 class PrefService;
     27 
     28 namespace infobars {
     29 class InfoBar;
     30 }
     31 
     32 // This object is responsible for checking the Google URL once per network
     33 // change, and if necessary prompting the user to see if they want to change to
     34 // using it.  The current and last prompted values are saved to prefs.
     35 //
     36 // Most consumers should only call google_url().  Consumers who need to be
     37 // notified when things change should register a callback that provides the
     38 // original and updated values via RegisterCallback().
     39 //
     40 // To protect users' privacy and reduce server load, no updates will be
     41 // performed (ever) unless at least one consumer registers interest by calling
     42 // RequestServerCheck().
     43 class GoogleURLTracker : public net::URLFetcherDelegate,
     44                          public net::NetworkChangeNotifier::IPAddressObserver,
     45                          public KeyedService {
     46  public:
     47   // Callback that is called when the Google URL is updated. The arguments are
     48   // the old and new URLs.
     49   typedef base::Callback<void(GURL, GURL)> OnGoogleURLUpdatedCallback;
     50   typedef base::CallbackList<void(GURL, GURL)> CallbackList;
     51   typedef CallbackList::Subscription Subscription;
     52 
     53   // The constructor does different things depending on which of these values
     54   // you pass it.  Hopefully these are self-explanatory.
     55   enum Mode {
     56     NORMAL_MODE,
     57     UNIT_TEST_MODE,
     58   };
     59 
     60   static const char kDefaultGoogleHomepage[];
     61 
     62   // Only the GoogleURLTrackerFactory and tests should call this.
     63   GoogleURLTracker(scoped_ptr<GoogleURLTrackerClient> client, Mode mode);
     64 
     65   virtual ~GoogleURLTracker();
     66 
     67   // Returns the current Google homepage URL.
     68   const GURL& google_url() const { return google_url_; }
     69 
     70   // Requests that the tracker perform a server check to update the Google URL
     71   // as necessary.  If |force| is false, this will happen at most once per
     72   // network change, not sooner than five seconds after startup (checks
     73   // requested before that time will occur then; checks requested afterwards
     74   // will occur immediately, if no other checks have been made during this run).
     75   // If |force| is true, and the tracker has already performed any requested
     76   // check, it will check again.
     77   void RequestServerCheck(bool force);
     78 
     79   // Notifies the tracker that the user has started a Google search.
     80   // If prompting is necessary, we then listen for the subsequent pending
     81   // navigation to get the appropriate NavigationHelper. When the load
     82   // commits, we'll show the infobar.
     83   void SearchCommitted();
     84 
     85   // No one but GoogleURLTrackerInfoBarDelegate or test code should call these.
     86   void AcceptGoogleURL(bool redo_searches);
     87   void CancelGoogleURL();
     88   const GURL& fetched_google_url() const { return fetched_google_url_; }
     89   GoogleURLTrackerClient* client() { return client_.get(); }
     90 
     91   // No one but GoogleURLTrackerMapEntry should call this.
     92   void DeleteMapEntryForManager(
     93       const infobars::InfoBarManager* infobar_manager);
     94 
     95   // Called by the client after SearchCommitted() registers listeners,
     96   // to indicate that we've received the "load now pending" notification.
     97   // |nav_helper| is the GoogleURLTrackerNavigationHelper associated with this
     98   // navigation; |infobar_manager| is the InfoBarManager of the associated tab;
     99   // and |pending_id| is the unique ID of the newly pending NavigationEntry.
    100   // If there is already a visible GoogleURLTracker infobar for this tab, this
    101   // function resets its associated pending entry ID to the new ID.  Otherwise
    102   // this function creates a map entry for the associated tab.
    103   virtual void OnNavigationPending(
    104       scoped_ptr<GoogleURLTrackerNavigationHelper> nav_helper,
    105       infobars::InfoBarManager* infobar_manager,
    106       int pending_id);
    107 
    108   // Called by the navigation observer once a load we're watching commits.
    109   // |infobar_manager| is the same as for OnNavigationPending();
    110   // |search_url| is guaranteed to be valid.
    111   virtual void OnNavigationCommitted(infobars::InfoBarManager* infobar_manager,
    112                                      const GURL& search_url);
    113 
    114   // Called by the navigation observer when a tab closes.
    115   virtual void OnTabClosed(GoogleURLTrackerNavigationHelper* nav_helper);
    116 
    117   scoped_ptr<Subscription> RegisterCallback(
    118       const OnGoogleURLUpdatedCallback& cb);
    119 
    120  private:
    121   friend class GoogleURLTrackerTest;
    122   friend class SyncTest;
    123 
    124   typedef std::map<const infobars::InfoBarManager*, GoogleURLTrackerMapEntry*>
    125       EntryMap;
    126 
    127   static const char kSearchDomainCheckURL[];
    128 
    129   // net::URLFetcherDelegate:
    130   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
    131 
    132   // NetworkChangeNotifier::IPAddressObserver:
    133   virtual void OnIPAddressChanged() OVERRIDE;
    134 
    135   // KeyedService:
    136   virtual void Shutdown() OVERRIDE;
    137 
    138   // Registers consumer interest in getting an updated URL from the server.
    139   // Observe chrome::NOTIFICATION_GOOGLE_URL_UPDATED to be notified when the URL
    140   // changes.
    141   void SetNeedToFetch();
    142 
    143   // Called when the five second startup sleep has finished.  Runs any pending
    144   // fetch.
    145   void FinishSleep();
    146 
    147   // Starts the fetch of the up-to-date Google URL if we actually want to fetch
    148   // it and can currently do so.
    149   void StartFetchIfDesirable();
    150 
    151   // Closes all map entries.  If |redo_searches| is true, this also triggers
    152   // each tab with an infobar to re-perform the user's search, but on the new
    153   // Google TLD.
    154   void CloseAllEntries(bool redo_searches);
    155 
    156   // Unregisters any listeners for the navigation helper in |map_entry|.
    157   // This sanity-DCHECKs that these are registered (or not) in the specific
    158   // cases we expect.  (|must_be_listening_for_commit| is used purely for this
    159   // sanity-checking.)  This also unregisters the global navigation pending
    160   // listener if there are no remaining listeners for navigation commits, as we
    161   // no longer need them until another search is committed.
    162   void UnregisterForEntrySpecificNotifications(
    163       GoogleURLTrackerMapEntry* map_entry,
    164       bool must_be_listening_for_commit);
    165 
    166   void NotifyGoogleURLUpdated(GURL old_url, GURL new_url);
    167 
    168   CallbackList callback_list_;
    169 
    170   scoped_ptr<GoogleURLTrackerClient> client_;
    171 
    172   GURL google_url_;
    173   GURL fetched_google_url_;
    174   scoped_ptr<net::URLFetcher> fetcher_;
    175   int fetcher_id_;
    176   bool in_startup_sleep_;  // True if we're in the five-second "no fetching"
    177                            // period that begins at browser start.
    178   bool already_fetched_;   // True if we've already fetched a URL once this run;
    179                            // we won't fetch again until after a restart.
    180   bool need_to_fetch_;     // True if a consumer actually wants us to fetch an
    181                            // updated URL.  If this is never set, we won't
    182                            // bother to fetch anything.
    183                            // Consumers should register a callback via
    184                            // RegisterCallback().
    185   bool need_to_prompt_;    // True if the last fetched Google URL is not
    186                            // matched with current user's default Google URL
    187                            // nor the last prompted Google URL.
    188   bool search_committed_;  // True when we're expecting a notification of a new
    189                            // pending search navigation.
    190   EntryMap entry_map_;
    191   base::WeakPtrFactory<GoogleURLTracker> weak_ptr_factory_;
    192 
    193   DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker);
    194 };
    195 
    196 #endif  // COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_
    197