Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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. | |
| 4 | |
| 5 #include "components/signin/core/browser/device_activity_fetcher.h" | |
| 6 | |
| 7 #include "base/strings/stringprintf.h" | |
| 8 #include "components/signin/core/browser/signin_client.h" | |
| 9 #include "google_apis/gaia/gaia_auth_fetcher.h" | |
| 10 #include "google_apis/gaia/gaia_constants.h" | |
| 11 #include "google_apis/gaia/gaia_urls.h" | |
| 12 #include "google_apis/gaia/google_service_auth_error.h" | |
| 13 #include "net/http/http_status_code.h" | |
| 14 #include "net/url_request/url_fetcher.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const char* kSyncListDevicesScope = | |
| 19 "https://www.googleapis.com/auth/chromesynclistdevices"; | |
| 20 const char* kChromeDomain = "http://www.chrome.com"; | |
| 21 const char* kListDevicesEndpoint = "http://127.0.0.1"; | |
| 22 // Template for optional authorization header when using an OAuth access token. | |
| 23 const char kAuthorizationHeader[] = "Authorization: Bearer %s"; | |
| 24 | |
| 25 // In case of an error while fetching using the GaiaAuthFetcher, retry with | |
| 26 // exponential backoff. Try up to 9 times within 10 minutes. | |
| 27 const net::BackoffEntry::Policy kBackoffPolicy = { | |
| 28 // Number of initial errors (in sequence) to ignore before applying | |
| 29 // exponential back-off rules. | |
| 30 0, | |
| 31 // Initial delay for exponential backoff in ms. | |
| 32 1000, | |
| 33 // Factor by which the waiting time will be multiplied. | |
| 34 2, | |
| 35 // Fuzzing percentage. ex: 10% will spread requests randomly | |
| 36 // between 90%-100% of the calculated time. | |
| 37 0.1, // 10% | |
| 38 // Maximum amount of time we are willing to delay our request in ms. | |
| 39 1000 * 60 * 15, // 15 minutes. | |
| 40 // Time to keep an entry from being discarded even when it | |
| 41 // has no significant state, -1 to never discard. | |
| 42 -1, | |
| 43 // Don't use initial delay unless the last request was an error. | |
| 44 false, | |
| 45 }; | |
| 46 | |
| 47 const int kMaxFetcherRetries = 9; | |
| 48 | |
| 49 bool IsTransientError(const GoogleServiceAuthError& error) { | |
|
Alexei Svitkine (slow)
2015/05/19 15:23:49
Nit: This doesn't seem much more readable that the
Mike Lerman
2015/05/19 16:53:17
Done.
| |
| 50 return error.IsTransientError(); | |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 DeviceActivityFetcher::DeviceActivityFetcher( | |
| 56 SigninClient* signin_client, | |
| 57 DeviceActivityFetcher::Observer* observer) | |
| 58 : fetcher_backoff_(&kBackoffPolicy), | |
| 59 fetcher_retries_(0), | |
| 60 signin_client_(signin_client), | |
| 61 observer_(observer) {} | |
| 62 | |
| 63 DeviceActivityFetcher::~DeviceActivityFetcher() { | |
| 64 } | |
| 65 | |
| 66 void DeviceActivityFetcher::Start() { | |
| 67 fetcher_retries_ = 0; | |
| 68 login_hint_ = std::string(); | |
| 69 StartFetchingListIdpSessions(); | |
| 70 } | |
| 71 | |
| 72 void DeviceActivityFetcher::Stop() { | |
| 73 if (gaia_auth_fetcher_) | |
| 74 gaia_auth_fetcher_->CancelRequest(); | |
| 75 if (url_fetcher_) | |
| 76 url_fetcher_.reset(); | |
| 77 } | |
| 78 | |
| 79 void DeviceActivityFetcher::StartFetchingListIdpSessions() { | |
| 80 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this, | |
| 81 GaiaConstants::kChromeSource, signin_client_->GetURLRequestContext())); | |
| 82 gaia_auth_fetcher_->StartListIDPSessions(kSyncListDevicesScope, | |
| 83 kChromeDomain); | |
| 84 } | |
| 85 | |
| 86 void DeviceActivityFetcher::StartFetchingGetTokenResponse() { | |
| 87 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this, | |
| 88 GaiaConstants::kChromeSource, signin_client_->GetURLRequestContext())); | |
| 89 gaia_auth_fetcher_->StartGetTokenResponse(kSyncListDevicesScope, | |
| 90 kChromeDomain, | |
| 91 login_hint_); | |
| 92 } | |
| 93 | |
| 94 void DeviceActivityFetcher::StartFetchingListDevices() { | |
| 95 // Call the Sync Endpoint. | |
| 96 url_fetcher_ = net::URLFetcher::Create(GURL(kListDevicesEndpoint), | |
| 97 net::URLFetcher::GET, | |
| 98 this); | |
| 99 url_fetcher_->SetRequestContext(signin_client_->GetURLRequestContext()); | |
| 100 if (!access_token_.empty()) { | |
| 101 url_fetcher_->SetExtraRequestHeaders( | |
| 102 base::StringPrintf(kAuthorizationHeader, access_token_.c_str())); | |
| 103 } | |
| 104 url_fetcher_->Start(); | |
| 105 } | |
| 106 | |
| 107 void DeviceActivityFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 108 // TODO(mlerman): Uncomment the code below once we have a working proto. | |
|
Alexei Svitkine (slow)
2015/05/19 15:23:49
Reference a crbug?
Mike Lerman
2015/05/19 16:53:17
Done.
| |
| 109 | |
| 110 // std::string response_string; | |
| 111 // ListDevices list_devices; | |
| 112 | |
| 113 // if (!source->GetStatus().is_success()) { | |
| 114 // VLOG(1) << "Failed to fetch listdevices response. Retrying."; | |
| 115 // if (++fetcher_retries_ < kMaxFetcherRetries) { | |
| 116 // fetcher_backoff_.InformOfRequest(false); | |
| 117 // fetcher_timer_.Start( | |
| 118 // FROM_HERE, fetcher_backoff_.GetTimeUntilRelease(), this, | |
| 119 // &DeviceActivityFetcher::StartFetchingListDevices); | |
| 120 // return; | |
| 121 // } else { | |
| 122 // observer_->OnFetchDeviceActivityFailure(); | |
| 123 // return; | |
| 124 // } | |
| 125 // } | |
| 126 | |
| 127 // net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>( | |
| 128 // source->GetResponseCode()); | |
| 129 // if (response_status == net::HTTP_BAD_REQUEST || | |
| 130 // response_status == net::HTTP_UNAUTHORIZED) { | |
| 131 // // BAD_REQUEST indicates that the request was malformed. | |
| 132 // // UNAUTHORIZED indicates that security token didn't match the id. | |
| 133 // VLOG(1) << "No point retrying the checkin with status: " | |
| 134 // << response_status << ". Checkin failed."; | |
| 135 // CheckinRequestStatus status = response_status == net::HTTP_BAD_REQUEST ? | |
| 136 // HTTP_BAD_REQUEST : HTTP_UNAUTHORIZED; | |
| 137 // RecordCheckinStatusAndReportUMA(status, recorder_, false); | |
| 138 // callback_.Run(response_proto); | |
| 139 // return; | |
| 140 // } | |
| 141 | |
| 142 // if (response_status != net::HTTP_OK || | |
| 143 // !source->GetResponseAsString(&response_string) || | |
| 144 // !list_devices.ParseFromString(response_string)) { | |
| 145 // LOG(ERROR) << "Failed to get list devices response. HTTP Status: " | |
| 146 // << response_status; | |
| 147 // if (++fetcher_retries_ < kMaxFetcherRetries) { | |
| 148 // fetcher_backoff_.InformOfRequest(false); | |
| 149 // fetcher_timer_.Start( | |
| 150 // FROM_HERE, fetcher_backoff_.GetTimeUntilRelease(), this, | |
| 151 // &DeviceActivityFetcher::StartFetchingListDevices); | |
| 152 // return; | |
| 153 // } else { | |
| 154 // observer_->OnFetchDeviceActivityFailure(); | |
| 155 // return; | |
| 156 // } | |
| 157 // } | |
| 158 | |
| 159 std::vector<DeviceActivity> devices; | |
| 160 // TODO(mlerman): Fill |devices| from the proto in |source|. | |
| 161 | |
| 162 observer_->OnFetchDeviceActivitySuccess(devices); | |
| 163 } | |
| 164 | |
| 165 | |
| 166 void DeviceActivityFetcher::OnListIdpSessionsSuccess( | |
| 167 const std::string& login_hint) { | |
| 168 fetcher_backoff_.InformOfRequest(true); | |
| 169 login_hint_ = login_hint; | |
| 170 access_token_ = std::string(); | |
| 171 StartFetchingGetTokenResponse(); | |
| 172 } | |
| 173 | |
| 174 void DeviceActivityFetcher::OnListIdpSessionsError( | |
| 175 const GoogleServiceAuthError& error) { | |
| 176 if (++fetcher_retries_ < kMaxFetcherRetries && | |
| 177 IsTransientError(error)) { | |
| 178 fetcher_backoff_.InformOfRequest(false); | |
| 179 fetcher_timer_.Start( | |
| 180 FROM_HERE, fetcher_backoff_.GetTimeUntilRelease(), this, | |
| 181 &DeviceActivityFetcher::StartFetchingListIdpSessions); | |
| 182 return; | |
| 183 } | |
| 184 observer_->OnFetchDeviceActivityFailure(); | |
| 185 } | |
| 186 | |
| 187 void DeviceActivityFetcher::OnGetTokenResponseSuccess( | |
| 188 const ClientOAuthResult& result) { | |
| 189 fetcher_backoff_.InformOfRequest(true); | |
| 190 access_token_ = result.access_token; | |
| 191 StartFetchingListDevices(); | |
| 192 } | |
| 193 | |
| 194 void DeviceActivityFetcher::OnGetTokenResponseError( | |
| 195 const GoogleServiceAuthError& error) { | |
| 196 if (++fetcher_retries_ < kMaxFetcherRetries && IsTransientError(error)) { | |
| 197 fetcher_backoff_.InformOfRequest(false); | |
| 198 fetcher_timer_.Start( | |
| 199 FROM_HERE, fetcher_backoff_.GetTimeUntilRelease(), this, | |
| 200 &DeviceActivityFetcher::StartFetchingGetTokenResponse); | |
| 201 return; | |
| 202 } | |
| 203 observer_->OnFetchDeviceActivityFailure(); | |
| 204 } | |
| OLD | NEW |