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"
22 AuthPrewarmer::AuthPrewarmer()
23 : doing_prewarm_(false) {
26 AuthPrewarmer::~AuthPrewarmer() {
27 if (registrar_
.IsRegistered(
29 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED
,
30 content::Source
<Profile
>(ProfileHelper::GetSigninProfile()))) {
33 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED
,
34 content::Source
<Profile
>(ProfileHelper::GetSigninProfile()));
36 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this,
40 void AuthPrewarmer::PrewarmAuthentication(
41 const base::Closure
& completion_callback
) {
43 LOG(ERROR
) << "PrewarmAuthentication called twice.";
46 doing_prewarm_
= true;
47 completion_callback_
= completion_callback
;
48 if (GetRequestContext() && IsNetworkConnected()) {
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()) {
60 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED
,
61 content::Source
<Profile
>(ProfileHelper::GetSigninProfile()));
65 void AuthPrewarmer::DefaultNetworkChanged(const NetworkState
* network
) {
67 return; // Still no default (connected) network.
69 NetworkHandler::Get()->network_state_handler()
70 ->RemoveObserver(this, FROM_HERE
);
71 if (GetRequestContext())
75 void AuthPrewarmer::Observe(int type
,
76 const content::NotificationSource
& source
,
77 const content::NotificationDetails
& details
) {
79 case chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED
:
82 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED
,
83 content::Source
<Profile
>(ProfileHelper::GetSigninProfile()));
84 if (IsNetworkConnected())
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(
102 chrome_browser_net::UrlInfo::EARLY_LOAD_MOTIVATED
,
104 GetRequestContext());
106 if (!completion_callback_
.is_null()) {
107 content::BrowserThread::PostTask(content::BrowserThread::UI
,
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