Chromium Code Reviews| 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 void | |
| 52 MergeSessionHelper::ExternalCcResultFetcher::OnGetCheckConnectionInfoSuccess( | |
| 53 const std::string& data) { | |
| 54 base::Value* value = base::JSONReader::Read(data); | |
| 55 const base::ListValue* list; | |
| 56 if (!value || !value->GetAsList(&list)) | |
| 57 return; | |
| 58 | |
| 59 // Start a fetcher for each connection URL that needs to be checked. | |
| 60 for (size_t i = 0; i < list->GetSize(); ++i) { | |
| 61 const base::DictionaryValue* dict; | |
| 62 if (list->GetDictionary(i, &dict)) { | |
| 63 std::string token; | |
| 64 std::string url; | |
| 65 if (dict->GetString("carryBackToken", &token) && | |
| 66 dict->GetString("url", &url)) { | |
| 67 results_[token] = "null"; | |
| 68 net::URLFetcher* fetcher = CreateFetcher(GURL(url)); | |
| 69 fetchers_[fetcher->GetOriginalURL()] = std::make_pair(token, fetcher); | |
| 70 fetcher->Start(); | |
| 71 } | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 // Some fetches may timeout. Start a timer to decide when the result fetcher | |
| 76 // has waited long enough. | |
| 77 // TODO(rogerta): I have no idea how to long to wait before timing out. | |
|
guohui
2014/08/05 19:14:07
nits: extra to
Roger Tawa OOO till Jul 10th
2014/08/08 19:29:53
Done.
| |
| 78 // Gaia folks say this should take no more than 2 second even in mobile. | |
| 79 // This will need to be tweaked. | |
| 80 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(5), | |
| 81 this, &MergeSessionHelper::ExternalCcResultFetcher::Timeout); | |
| 82 } | |
| 83 | |
| 84 net::URLFetcher* MergeSessionHelper::ExternalCcResultFetcher::CreateFetcher( | |
| 85 const GURL& url) { | |
| 86 net::URLFetcher* fetcher = net::URLFetcher::Create( | |
| 87 0, | |
| 88 url, | |
| 89 net::URLFetcher::GET, | |
| 90 this); | |
| 91 fetcher->SetRequestContext(helper_->request_context()); | |
| 92 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
| 93 net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 94 | |
| 95 // Fetchers are sometimes cancelled because a network change was detected, | |
| 96 // especially at startup and after sign-in on ChromeOS. | |
| 97 fetcher->SetAutomaticallyRetryOnNetworkChanges(1); | |
| 98 return fetcher; | |
| 99 } | |
| 100 | |
| 101 void MergeSessionHelper::ExternalCcResultFetcher::OnURLFetchComplete( | |
| 102 const net::URLFetcher* source) { | |
| 103 const GURL& url = source->GetOriginalURL(); | |
| 104 const net::URLRequestStatus& status = source->GetStatus(); | |
| 105 int response_code = source->GetResponseCode(); | |
| 106 if (status.is_success() && response_code == net::HTTP_OK && | |
| 107 fetchers_.count(url) > 0) { | |
| 108 std::string data; | |
| 109 source->GetResponseAsString(&data); | |
| 110 // Only up to the first 16 characters of the response are important. | |
| 111 // Truncate if needed. | |
| 112 if (data.size() > 16) | |
| 113 data.resize(16); | |
| 114 results_[fetchers_[url].first] = data; | |
| 115 | |
| 116 // If we have received all the responses we expect, cancel the timer and | |
| 117 // report the result. | |
| 118 DCHECK_EQ(source, fetchers_[url].second); | |
| 119 fetchers_.erase(url); | |
| 120 delete source; | |
| 121 if (fetchers_.size() == 0) { | |
| 122 timer_.Stop(); | |
| 123 CleanupTransientState(); | |
| 124 } | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void MergeSessionHelper::ExternalCcResultFetcher::Timeout() { | |
| 129 CleanupTransientState(); | |
| 130 } | |
| 131 | |
| 132 void MergeSessionHelper::ExternalCcResultFetcher::CleanupTransientState() { | |
| 133 gaia_auth_fetcher_.reset(); | |
| 134 | |
| 135 for (URLToTokenAndFetcher::const_iterator it = fetchers_.begin(); | |
| 136 it != fetchers_.end(); ++it) { | |
| 137 delete it->second.second; | |
| 138 } | |
| 139 fetchers_.clear(); | |
| 140 } | |
| 141 | |
| 142 | |
| 14 MergeSessionHelper::MergeSessionHelper( | 143 MergeSessionHelper::MergeSessionHelper( |
| 15 OAuth2TokenService* token_service, | 144 OAuth2TokenService* token_service, |
| 16 net::URLRequestContextGetter* request_context, | 145 net::URLRequestContextGetter* request_context, |
| 17 Observer* observer) | 146 Observer* observer) |
| 18 : token_service_(token_service), | 147 : token_service_(token_service), |
| 19 request_context_(request_context) { | 148 request_context_(request_context), |
| 149 result_fetcher_(this) { | |
| 20 if (observer) | 150 if (observer) |
| 21 AddObserver(observer); | 151 AddObserver(observer); |
| 22 } | 152 } |
| 23 | 153 |
| 24 MergeSessionHelper::~MergeSessionHelper() { | 154 MergeSessionHelper::~MergeSessionHelper() { |
| 25 DCHECK(accounts_.empty()); | 155 DCHECK(accounts_.empty()); |
| 26 } | 156 } |
| 27 | 157 |
| 28 void MergeSessionHelper::LogIn(const std::string& account_id) { | 158 void MergeSessionHelper::LogIn(const std::string& account_id) { |
| 29 DCHECK(!account_id.empty()); | 159 DCHECK(!account_id.empty()); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 void MergeSessionHelper::SignalComplete( | 234 void MergeSessionHelper::SignalComplete( |
| 105 const std::string& account_id, | 235 const std::string& account_id, |
| 106 const GoogleServiceAuthError& error) { | 236 const GoogleServiceAuthError& error) { |
| 107 // Its possible for the observer to delete |this| object. Don't access | 237 // Its possible for the observer to delete |this| object. Don't access |
| 108 // access any members after this calling the observer. This method should | 238 // access any members after this calling the observer. This method should |
| 109 // be the last call in any other method. | 239 // be the last call in any other method. |
| 110 FOR_EACH_OBSERVER(Observer, observer_list_, | 240 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 111 MergeSessionCompleted(account_id, error)); | 241 MergeSessionCompleted(account_id, error)); |
| 112 } | 242 } |
| 113 | 243 |
| 244 void MergeSessionHelper::StartFetchingExternalCcResult() { | |
| 245 result_fetcher_.Start(); | |
| 246 } | |
| 247 | |
| 114 void MergeSessionHelper::StartLogOutUrlFetch() { | 248 void MergeSessionHelper::StartLogOutUrlFetch() { |
| 115 DCHECK(accounts_.front().empty()); | 249 DCHECK(accounts_.front().empty()); |
| 116 VLOG(1) << "MergeSessionHelper::StartLogOutUrlFetch"; | 250 VLOG(1) << "MergeSessionHelper::StartLogOutUrlFetch"; |
| 117 GURL logout_url(GaiaUrls::GetInstance()->service_logout_url()); | 251 GURL logout_url(GaiaUrls::GetInstance()->service_logout_url()); |
| 118 net::URLFetcher* fetcher = | 252 net::URLFetcher* fetcher = |
| 119 net::URLFetcher::Create(logout_url, net::URLFetcher::GET, this); | 253 net::URLFetcher::Create(logout_url, net::URLFetcher::GET, this); |
| 120 fetcher->SetRequestContext(request_context_); | 254 fetcher->SetRequestContext(request_context_); |
| 121 fetcher->Start(); | 255 fetcher->Start(); |
| 122 } | 256 } |
| 123 | 257 |
| 124 void MergeSessionHelper::OnUbertokenSuccess(const std::string& uber_token) { | 258 void MergeSessionHelper::OnUbertokenSuccess(const std::string& uber_token) { |
| 125 VLOG(1) << "MergeSessionHelper::OnUbertokenSuccess" | 259 VLOG(1) << "MergeSessionHelper::OnUbertokenSuccess" |
| 126 << " account=" << accounts_.front(); | 260 << " account=" << accounts_.front(); |
| 127 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this, | 261 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this, |
| 128 GaiaConstants::kChromeSource, | 262 GaiaConstants::kChromeSource, |
| 129 request_context_)); | 263 request_context_)); |
| 130 gaia_auth_fetcher_->StartMergeSession(uber_token); | 264 gaia_auth_fetcher_->StartMergeSession(uber_token, |
| 265 result_fetcher_.GetExternalCcResult()); | |
|
guohui
2014/08/05 19:14:07
is it guaranteed that all eternal cc results are a
guohui
2014/08/06 20:52:27
could you also add comments here that it is not gu
Roger Tawa OOO till Jul 10th
2014/08/08 19:29:53
Nope. We don't want to delay the merge session, s
Roger Tawa OOO till Jul 10th
2014/08/08 19:29:53
Done.
| |
| 131 } | 266 } |
| 132 | 267 |
| 133 void MergeSessionHelper::OnUbertokenFailure( | 268 void MergeSessionHelper::OnUbertokenFailure( |
| 134 const GoogleServiceAuthError& error) { | 269 const GoogleServiceAuthError& error) { |
| 135 VLOG(1) << "Failed to retrieve ubertoken" | 270 VLOG(1) << "Failed to retrieve ubertoken" |
| 136 << " account=" << accounts_.front() | 271 << " account=" << accounts_.front() |
| 137 << " error=" << error.ToString(); | 272 << " error=" << error.ToString(); |
| 138 const std::string account_id = accounts_.front(); | 273 const std::string account_id = accounts_.front(); |
| 139 HandleNextAccount(); | 274 HandleNextAccount(); |
| 140 SignalComplete(account_id, error); | 275 SignalComplete(account_id, error); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 VLOG(1) << "MergeSessionHelper::HandleNextAccount: no more"; | 315 VLOG(1) << "MergeSessionHelper::HandleNextAccount: no more"; |
| 181 uber_token_fetcher_.reset(); | 316 uber_token_fetcher_.reset(); |
| 182 } else { | 317 } else { |
| 183 if (accounts_.front().empty()) { | 318 if (accounts_.front().empty()) { |
| 184 StartLogOutUrlFetch(); | 319 StartLogOutUrlFetch(); |
| 185 } else { | 320 } else { |
| 186 StartFetching(); | 321 StartFetching(); |
| 187 } | 322 } |
| 188 } | 323 } |
| 189 } | 324 } |
| OLD | NEW |