Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / login / auth / auth_prewarmer.cc
blob8ff0306de49a4d72aa0700cfebe554314ab0944d
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 #include "chrome/browser/chromeos/login/auth/auth_prewarmer.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/chromeos/login/helper.h"
9 #include "chrome/browser/chromeos/profiles/profile_helper.h"
10 #include "chrome/browser/net/preconnect.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chromeos/network/network_handler.h"
13 #include "chromeos/network/network_state.h"
14 #include "chromeos/network/network_state_handler.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/notification_source.h"
17 #include "google_apis/gaia/gaia_urls.h"
18 #include "url/gurl.h"
20 namespace chromeos {
22 AuthPrewarmer::AuthPrewarmer()
23 : doing_prewarm_(false) {
26 AuthPrewarmer::~AuthPrewarmer() {
27 if (registrar_.IsRegistered(
28 this,
29 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
30 content::Source<Profile>(ProfileHelper::GetSigninProfile()))) {
31 registrar_.Remove(
32 this,
33 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
34 content::Source<Profile>(ProfileHelper::GetSigninProfile()));
36 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this,
37 FROM_HERE);
40 void AuthPrewarmer::PrewarmAuthentication(
41 const base::Closure& completion_callback) {
42 if (doing_prewarm_) {
43 LOG(ERROR) << "PrewarmAuthentication called twice.";
44 return;
46 doing_prewarm_ = true;
47 completion_callback_ = completion_callback;
48 if (GetRequestContext() && IsNetworkConnected()) {
49 DoPrewarm();
50 return;
52 if (!IsNetworkConnected()) {
53 // DefaultNetworkChanged will get called when a network becomes connected.
54 NetworkHandler::Get()->network_state_handler()
55 ->AddObserver(this, FROM_HERE);
57 if (!GetRequestContext()) {
58 registrar_.Add(
59 this,
60 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
61 content::Source<Profile>(ProfileHelper::GetSigninProfile()));
65 void AuthPrewarmer::DefaultNetworkChanged(const NetworkState* network) {
66 if (!network)
67 return; // Still no default (connected) network.
69 NetworkHandler::Get()->network_state_handler()
70 ->RemoveObserver(this, FROM_HERE);
71 if (GetRequestContext())
72 DoPrewarm();
75 void AuthPrewarmer::Observe(int type,
76 const content::NotificationSource& source,
77 const content::NotificationDetails& details) {
78 switch (type) {
79 case chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED:
80 registrar_.Remove(
81 this,
82 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
83 content::Source<Profile>(ProfileHelper::GetSigninProfile()));
84 if (IsNetworkConnected())
85 DoPrewarm();
86 break;
87 default:
88 NOTREACHED();
92 void AuthPrewarmer::DoPrewarm() {
93 const int kConnectionsNeeded = 1;
95 std::vector<GURL> urls;
96 urls.push_back(GaiaUrls::GetInstance()->service_login_url());
98 for (size_t i = 0; i < urls.size(); ++i) {
99 chrome_browser_net::PreconnectOnUIThread(
100 urls[i],
101 urls[i],
102 chrome_browser_net::UrlInfo::EARLY_LOAD_MOTIVATED,
103 kConnectionsNeeded,
104 GetRequestContext());
106 if (!completion_callback_.is_null()) {
107 content::BrowserThread::PostTask(content::BrowserThread::UI,
108 FROM_HERE,
109 completion_callback_);
113 bool AuthPrewarmer::IsNetworkConnected() const {
114 NetworkStateHandler* nsh = NetworkHandler::Get()->network_state_handler();
115 return (nsh->ConnectedNetworkByType(NetworkTypePattern::Default()) != NULL);
118 net::URLRequestContextGetter* AuthPrewarmer::GetRequestContext() const {
119 return login::GetSigninContext();
122 } // namespace chromeos