OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "google_apis/gaia/merge_session_helper.h" | 5 #include "google_apis/gaia/merge_session_helper.h" |
6 | 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/json/json_reader.h" |
| 10 #include "base/stl_util.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/time/time.h" |
| 14 #include "base/values.h" |
7 #include "google_apis/gaia/gaia_auth_fetcher.h" | 15 #include "google_apis/gaia/gaia_auth_fetcher.h" |
8 #include "google_apis/gaia/gaia_constants.h" | 16 #include "google_apis/gaia/gaia_constants.h" |
9 #include "google_apis/gaia/gaia_urls.h" | 17 #include "google_apis/gaia/gaia_urls.h" |
10 #include "google_apis/gaia/oauth2_token_service.h" | 18 #include "google_apis/gaia/oauth2_token_service.h" |
| 19 #include "net/base/load_flags.h" |
| 20 #include "net/http/http_status_code.h" |
11 #include "net/url_request/url_fetcher.h" | 21 #include "net/url_request/url_fetcher.h" |
12 #include "net/url_request/url_fetcher_delegate.h" | 22 #include "net/url_request/url_fetcher_delegate.h" |
13 | 23 |
| 24 MergeSessionHelper::ExternalCcResultFetcher::ExternalCcResultFetcher( |
| 25 MergeSessionHelper* helper) : helper_(helper) { |
| 26 DCHECK(helper_); |
| 27 } |
| 28 |
| 29 MergeSessionHelper::ExternalCcResultFetcher::~ExternalCcResultFetcher() { |
| 30 CleanupTransientState(); |
| 31 } |
| 32 |
| 33 std::string MergeSessionHelper::ExternalCcResultFetcher::GetExternalCcResult() { |
| 34 std::vector<std::string> results; |
| 35 for (ResultMap::const_iterator it = results_.begin(); it != results_.end(); |
| 36 ++it) { |
| 37 results.push_back(it->first + ":" + it->second); |
| 38 } |
| 39 return JoinString(results, ","); |
| 40 } |
| 41 |
| 42 void MergeSessionHelper::ExternalCcResultFetcher::Start() { |
| 43 CleanupTransientState(); |
| 44 results_.clear(); |
| 45 gaia_auth_fetcher_.reset( |
| 46 new GaiaAuthFetcher(this, GaiaConstants::kChromeSource, |
| 47 helper_->request_context())); |
| 48 gaia_auth_fetcher_->StartGetCheckConnectionInfo(); |
| 49 } |
| 50 |
| 51 bool MergeSessionHelper::ExternalCcResultFetcher::IsRunning() { |
| 52 return gaia_auth_fetcher_ || fetchers_.size() > 0u; |
| 53 } |
| 54 |
| 55 void |
| 56 MergeSessionHelper::ExternalCcResultFetcher::OnGetCheckConnectionInfoSuccess( |
| 57 const std::string& data) { |
| 58 base::Value* value = base::JSONReader::Read(data); |
| 59 const base::ListValue* list; |
| 60 if (!value || !value->GetAsList(&list)) |
| 61 return; |
| 62 |
| 63 // Start a fetcher for each connection URL that needs to be checked. |
| 64 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 65 const base::DictionaryValue* dict; |
| 66 if (list->GetDictionary(i, &dict)) { |
| 67 std::string token; |
| 68 std::string url; |
| 69 if (dict->GetString("carryBackToken", &token) && |
| 70 dict->GetString("url", &url)) { |
| 71 results_[token] = "null"; |
| 72 net::URLFetcher* fetcher = CreateFetcher(GURL(url)); |
| 73 fetchers_[fetcher->GetOriginalURL()] = std::make_pair(token, fetcher); |
| 74 fetcher->Start(); |
| 75 } |
| 76 } |
| 77 } |
| 78 |
| 79 // Some fetches may timeout. Start a timer to decide when the result fetcher |
| 80 // has waited long enough. |
| 81 // TODO(rogerta): I have no idea how long to wait before timing out. |
| 82 // Gaia folks say this should take no more than 2 second even in mobile. |
| 83 // This will need to be tweaked. |
| 84 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(5), |
| 85 this, &MergeSessionHelper::ExternalCcResultFetcher::Timeout); |
| 86 } |
| 87 |
| 88 net::URLFetcher* MergeSessionHelper::ExternalCcResultFetcher::CreateFetcher( |
| 89 const GURL& url) { |
| 90 net::URLFetcher* fetcher = net::URLFetcher::Create( |
| 91 0, |
| 92 url, |
| 93 net::URLFetcher::GET, |
| 94 this); |
| 95 fetcher->SetRequestContext(helper_->request_context()); |
| 96 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 97 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 98 |
| 99 // Fetchers are sometimes cancelled because a network change was detected, |
| 100 // especially at startup and after sign-in on ChromeOS. |
| 101 fetcher->SetAutomaticallyRetryOnNetworkChanges(1); |
| 102 return fetcher; |
| 103 } |
| 104 |
| 105 void MergeSessionHelper::ExternalCcResultFetcher::OnURLFetchComplete( |
| 106 const net::URLFetcher* source) { |
| 107 const GURL& url = source->GetOriginalURL(); |
| 108 const net::URLRequestStatus& status = source->GetStatus(); |
| 109 int response_code = source->GetResponseCode(); |
| 110 if (status.is_success() && response_code == net::HTTP_OK && |
| 111 fetchers_.count(url) > 0) { |
| 112 std::string data; |
| 113 source->GetResponseAsString(&data); |
| 114 // Only up to the first 16 characters of the response are important. |
| 115 // Truncate if needed. |
| 116 if (data.size() > 16) |
| 117 data.resize(16); |
| 118 results_[fetchers_[url].first] = data; |
| 119 |
| 120 // If we have received all the responses we expect, cancel the timer and |
| 121 // report the result. |
| 122 DCHECK_EQ(source, fetchers_[url].second); |
| 123 fetchers_.erase(url); |
| 124 delete source; |
| 125 if (fetchers_.size() == 0) { |
| 126 timer_.Stop(); |
| 127 CleanupTransientState(); |
| 128 } |
| 129 } |
| 130 } |
| 131 |
| 132 void MergeSessionHelper::ExternalCcResultFetcher::Timeout() { |
| 133 CleanupTransientState(); |
| 134 } |
| 135 |
| 136 void MergeSessionHelper::ExternalCcResultFetcher::CleanupTransientState() { |
| 137 gaia_auth_fetcher_.reset(); |
| 138 |
| 139 for (URLToTokenAndFetcher::const_iterator it = fetchers_.begin(); |
| 140 it != fetchers_.end(); ++it) { |
| 141 delete it->second.second; |
| 142 } |
| 143 fetchers_.clear(); |
| 144 } |
| 145 |
| 146 |
14 MergeSessionHelper::MergeSessionHelper( | 147 MergeSessionHelper::MergeSessionHelper( |
15 OAuth2TokenService* token_service, | 148 OAuth2TokenService* token_service, |
16 net::URLRequestContextGetter* request_context, | 149 net::URLRequestContextGetter* request_context, |
17 Observer* observer) | 150 Observer* observer) |
18 : token_service_(token_service), | 151 : token_service_(token_service), |
19 request_context_(request_context) { | 152 request_context_(request_context), |
| 153 result_fetcher_(this) { |
20 if (observer) | 154 if (observer) |
21 AddObserver(observer); | 155 AddObserver(observer); |
22 } | 156 } |
23 | 157 |
24 MergeSessionHelper::~MergeSessionHelper() { | 158 MergeSessionHelper::~MergeSessionHelper() { |
25 DCHECK(accounts_.empty()); | 159 DCHECK(accounts_.empty()); |
26 } | 160 } |
27 | 161 |
28 void MergeSessionHelper::LogIn(const std::string& account_id) { | 162 void MergeSessionHelper::LogIn(const std::string& account_id) { |
29 DCHECK(!account_id.empty()); | 163 DCHECK(!account_id.empty()); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 void MergeSessionHelper::SignalComplete( | 238 void MergeSessionHelper::SignalComplete( |
105 const std::string& account_id, | 239 const std::string& account_id, |
106 const GoogleServiceAuthError& error) { | 240 const GoogleServiceAuthError& error) { |
107 // Its possible for the observer to delete |this| object. Don't access | 241 // Its possible for the observer to delete |this| object. Don't access |
108 // access any members after this calling the observer. This method should | 242 // access any members after this calling the observer. This method should |
109 // be the last call in any other method. | 243 // be the last call in any other method. |
110 FOR_EACH_OBSERVER(Observer, observer_list_, | 244 FOR_EACH_OBSERVER(Observer, observer_list_, |
111 MergeSessionCompleted(account_id, error)); | 245 MergeSessionCompleted(account_id, error)); |
112 } | 246 } |
113 | 247 |
| 248 void MergeSessionHelper::StartFetchingExternalCcResult() { |
| 249 result_fetcher_.Start(); |
| 250 } |
| 251 |
| 252 bool MergeSessionHelper::StillFetchingExternalCcResult() { |
| 253 return result_fetcher_.IsRunning(); |
| 254 } |
| 255 |
114 void MergeSessionHelper::StartLogOutUrlFetch() { | 256 void MergeSessionHelper::StartLogOutUrlFetch() { |
115 DCHECK(accounts_.front().empty()); | 257 DCHECK(accounts_.front().empty()); |
116 VLOG(1) << "MergeSessionHelper::StartLogOutUrlFetch"; | 258 VLOG(1) << "MergeSessionHelper::StartLogOutUrlFetch"; |
117 GURL logout_url(GaiaUrls::GetInstance()->service_logout_url()); | 259 GURL logout_url(GaiaUrls::GetInstance()->service_logout_url()); |
118 net::URLFetcher* fetcher = | 260 net::URLFetcher* fetcher = |
119 net::URLFetcher::Create(logout_url, net::URLFetcher::GET, this); | 261 net::URLFetcher::Create(logout_url, net::URLFetcher::GET, this); |
120 fetcher->SetRequestContext(request_context_); | 262 fetcher->SetRequestContext(request_context_); |
121 fetcher->Start(); | 263 fetcher->Start(); |
122 } | 264 } |
123 | 265 |
124 void MergeSessionHelper::OnUbertokenSuccess(const std::string& uber_token) { | 266 void MergeSessionHelper::OnUbertokenSuccess(const std::string& uber_token) { |
125 VLOG(1) << "MergeSessionHelper::OnUbertokenSuccess" | 267 VLOG(1) << "MergeSessionHelper::OnUbertokenSuccess" |
126 << " account=" << accounts_.front(); | 268 << " account=" << accounts_.front(); |
127 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this, | 269 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this, |
128 GaiaConstants::kChromeSource, | 270 GaiaConstants::kChromeSource, |
129 request_context_)); | 271 request_context_)); |
130 gaia_auth_fetcher_->StartMergeSession(uber_token); | 272 |
| 273 // It's possible that not all external checks have completed. |
| 274 // GetExternalCcResult() returns results for those that have. |
| 275 gaia_auth_fetcher_->StartMergeSession(uber_token, |
| 276 result_fetcher_.GetExternalCcResult()); |
131 } | 277 } |
132 | 278 |
133 void MergeSessionHelper::OnUbertokenFailure( | 279 void MergeSessionHelper::OnUbertokenFailure( |
134 const GoogleServiceAuthError& error) { | 280 const GoogleServiceAuthError& error) { |
135 VLOG(1) << "Failed to retrieve ubertoken" | 281 VLOG(1) << "Failed to retrieve ubertoken" |
136 << " account=" << accounts_.front() | 282 << " account=" << accounts_.front() |
137 << " error=" << error.ToString(); | 283 << " error=" << error.ToString(); |
138 const std::string account_id = accounts_.front(); | 284 const std::string account_id = accounts_.front(); |
139 HandleNextAccount(); | 285 HandleNextAccount(); |
140 SignalComplete(account_id, error); | 286 SignalComplete(account_id, error); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 VLOG(1) << "MergeSessionHelper::HandleNextAccount: no more"; | 326 VLOG(1) << "MergeSessionHelper::HandleNextAccount: no more"; |
181 uber_token_fetcher_.reset(); | 327 uber_token_fetcher_.reset(); |
182 } else { | 328 } else { |
183 if (accounts_.front().empty()) { | 329 if (accounts_.front().empty()) { |
184 StartLogOutUrlFetch(); | 330 StartLogOutUrlFetch(); |
185 } else { | 331 } else { |
186 StartFetching(); | 332 StartFetching(); |
187 } | 333 } |
188 } | 334 } |
189 } | 335 } |
OLD | NEW |