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/profiles/profile_helper.h"
9 #include "chrome/browser/net/preconnect.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chromeos/network/network_handler.h"
12 #include "chromeos/network/network_state.h"
13 #include "chromeos/network/network_state_handler.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/notification_source.h"
16 #include "google_apis/gaia/gaia_urls.h"
21 AuthPrewarmer::AuthPrewarmer()
22 : doing_prewarm_(false) {
25 AuthPrewarmer::~AuthPrewarmer() {
26 if (registrar_.IsRegistered(
28 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
29 content::Source<Profile>(ProfileHelper::GetSigninProfile()))) {
32 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
33 content::Source<Profile>(ProfileHelper::GetSigninProfile()));
35 NetworkHandler::Get()->network_state_handler()->RemoveObserver(this,
39 void AuthPrewarmer::PrewarmAuthentication(
40 const base::Closure& completion_callback) {
42 LOG(ERROR) << "PrewarmAuthentication called twice.";
45 doing_prewarm_ = true;
46 completion_callback_ = completion_callback;
47 if (GetRequestContext() && IsNetworkConnected()) {
51 if (!IsNetworkConnected()) {
52 // DefaultNetworkChanged will get called when a network becomes connected.
53 NetworkHandler::Get()->network_state_handler()
54 ->AddObserver(this, FROM_HERE);
56 if (!GetRequestContext()) {
59 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
60 content::Source<Profile>(ProfileHelper::GetSigninProfile()));
64 void AuthPrewarmer::DefaultNetworkChanged(const NetworkState* network) {
66 return; // Still no default (connected) network.
68 NetworkHandler::Get()->network_state_handler()
69 ->RemoveObserver(this, FROM_HERE);
70 if (GetRequestContext())
74 void AuthPrewarmer::Observe(int type,
75 const content::NotificationSource& source,
76 const content::NotificationDetails& details) {
78 case chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED:
81 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
82 content::Source<Profile>(ProfileHelper::GetSigninProfile()));
83 if (IsNetworkConnected())
91 void AuthPrewarmer::DoPrewarm() {
92 const int kConnectionsNeeded = 1;
94 std::vector<GURL> urls;
95 urls.push_back(GaiaUrls::GetInstance()->client_login_url());
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 ProfileHelper::GetSigninProfile()->GetRequestContext();
122 } // namespace chromeos