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.
5 #ifndef COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_
6 #define COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_
8 #include "base/callback_forward.h"
9 #include "base/callback_list.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "components/google/core/browser/google_url_tracker_client.h"
13 #include "components/keyed_service/core/keyed_service.h"
14 #include "net/base/network_change_notifier.h"
15 #include "net/url_request/url_fetcher.h"
16 #include "net/url_request/url_fetcher_delegate.h"
25 namespace user_prefs
{
26 class PrefRegistrySyncable
;
29 // This object is responsible for checking the Google URL once per network
30 // change. The current value is saved to prefs.
32 // Most consumers should only call google_url(). Consumers who need to be
33 // notified when things change should register a callback that provides the
34 // original and updated values via RegisterCallback().
36 // To protect users' privacy and reduce server load, no updates will be
37 // performed (ever) unless at least one consumer registers interest by calling
38 // RequestServerCheck().
39 class GoogleURLTracker
40 : public net::URLFetcherDelegate
,
41 public net::NetworkChangeNotifier::NetworkChangeObserver
,
44 // Callback that is called when the Google URL is updated. The arguments are
45 // the old and new URLs.
46 typedef base::Callback
<void()> OnGoogleURLUpdatedCallback
;
47 typedef base::CallbackList
<void()> CallbackList
;
48 typedef CallbackList::Subscription Subscription
;
50 // The constructor does different things depending on which of these values
51 // you pass it. Hopefully these are self-explanatory.
57 static const char kDefaultGoogleHomepage
[];
59 // Only the GoogleURLTrackerFactory and tests should call this.
60 GoogleURLTracker(scoped_ptr
<GoogleURLTrackerClient
> client
, Mode mode
);
62 ~GoogleURLTracker() override
;
64 // Register user preferences for GoogleURLTracker.
65 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
68 // Returns the current Google homepage URL.
69 const GURL
& google_url() const { return google_url_
; }
71 // Requests that the tracker perform a server check to update the Google URL
72 // as necessary. If |force| is false, this will happen at most once per
73 // network change, not sooner than five seconds after startup (checks
74 // requested before that time will occur then; checks requested afterwards
75 // will occur immediately, if no other checks have been made during this run).
76 // If |force| is true, and the tracker has already performed any requested
77 // check, it will check again.
78 void RequestServerCheck(bool force
);
80 scoped_ptr
<Subscription
> RegisterCallback(
81 const OnGoogleURLUpdatedCallback
& cb
);
84 friend class GoogleURLTrackerTest
;
85 friend class SyncTest
;
87 static const char kSearchDomainCheckURL
[];
89 // net::URLFetcherDelegate:
90 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
92 // NetworkChangeNotifier::IPAddressObserver:
93 void OnNetworkChanged(
94 net::NetworkChangeNotifier::ConnectionType type
) override
;
97 void Shutdown() override
;
99 // Sets |need_to_fetch_| and attempts to start a fetch.
100 void SetNeedToFetch();
102 // Called when the five second startup sleep has finished. Runs any pending
106 // Starts the fetch of the up-to-date Google URL if we actually want to fetch
107 // it and can currently do so.
108 void StartFetchIfDesirable();
110 CallbackList callback_list_
;
112 scoped_ptr
<GoogleURLTrackerClient
> client_
;
115 scoped_ptr
<net::URLFetcher
> fetcher_
;
117 bool in_startup_sleep_
; // True if we're in the five-second "no fetching"
118 // period that begins at browser start.
119 bool already_fetched_
; // True if we've already fetched a URL once this run;
120 // we won't fetch again until after a restart.
121 bool need_to_fetch_
; // True if a consumer actually wants us to fetch an
122 // updated URL. If this is never set, we won't
123 // bother to fetch anything.
124 // Consumers should register a callback via
125 // RegisterCallback().
126 base::WeakPtrFactory
<GoogleURLTracker
> weak_ptr_factory_
;
128 DISALLOW_COPY_AND_ASSIGN(GoogleURLTracker
);
131 #endif // COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_URL_TRACKER_H_