| 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, // 20% |
| 38 // Maximum amount of time we are willing to delay our request in ms. |
| 39 1000 * 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) { |
| 50 return error.state() == GoogleServiceAuthError::CONNECTION_FAILED || |
| 51 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE || |
| 52 error.state() == GoogleServiceAuthError::REQUEST_CANCELED; |
| 53 } |
| 54 |
| 55 } // namespace |
| 56 |
| 57 DeviceActivityFetcher::DeviceActivityFetcher( |
| 58 SigninClient* signin_client, |
| 59 DeviceActivityFetcher::Observer* observer) |
| 60 : fetcher_backoff_(&kBackoffPolicy), |
| 61 fetcher_retries_(0), |
| 62 signin_client_(signin_client), |
| 63 observer_(observer) {} |
| 64 |
| 65 void DeviceActivityFetcher::Start() { |
| 66 fetcher_retries_ = 0; |
| 67 login_hint_ = std::string(); |
| 68 StartFetchingListIdpSessions(); |
| 69 } |
| 70 |
| 71 void DeviceActivityFetcher::StartFetchingListIdpSessions() { |
| 72 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this, |
| 73 GaiaConstants::kChromeSource, signin_client_->GetURLRequestContext())); |
| 74 gaia_auth_fetcher_->StartListIDPSessions(kSyncListDevicesScope, |
| 75 kChromeDomain); |
| 76 } |
| 77 |
| 78 void DeviceActivityFetcher::StartFetchingGetTokenResponse() { |
| 79 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this, |
| 80 GaiaConstants::kChromeSource, signin_client_->GetURLRequestContext())); |
| 81 gaia_auth_fetcher_->StartGetTokenResponse(kSyncListDevicesScope, |
| 82 kChromeDomain, |
| 83 login_hint_); |
| 84 } |
| 85 |
| 86 void DeviceActivityFetcher::StartFetchingListDevices() { |
| 87 // Call the Sync Endpoint. |
| 88 url_fetcher_ = net::URLFetcher::Create(GURL(kListDevicesEndpoint), |
| 89 net::URLFetcher::GET, |
| 90 this); |
| 91 url_fetcher_->SetRequestContext(signin_client_->GetURLRequestContext()); |
| 92 if (!access_token_.empty()) { |
| 93 url_fetcher_->SetExtraRequestHeaders( |
| 94 base::StringPrintf(kAuthorizationHeader, access_token_.c_str())); |
| 95 } |
| 96 url_fetcher_->Start(); |
| 97 } |
| 98 |
| 99 void DeviceActivityFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 100 // TODO(mlerman): Uncomment the code below once we have a working proto. |
| 101 |
| 102 // std::string response_string; |
| 103 // ListDevices list_devices; |
| 104 |
| 105 // if (!source->GetStatus().is_success()) { |
| 106 // VLOG(1) << "Failed to fetch listdevices response. Retrying."; |
| 107 // if (++fetcher_retries_ < kMaxFetcherRetries) { |
| 108 // fetcher_backoff_.InformOfRequest(false); |
| 109 // fetcher_timer_.Start( |
| 110 // FROM_HERE, fetcher_backoff_.GetTimeUntilRelease(), this, |
| 111 // &DeviceActivityFetcher::StartFetchingListDevices); |
| 112 // return; |
| 113 // } else { |
| 114 // observer_->OnFetchDeviceActivityFailure(); |
| 115 // } |
| 116 // } |
| 117 |
| 118 // net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>( |
| 119 // source->GetResponseCode()); |
| 120 // if (response_status == net::HTTP_BAD_REQUEST || |
| 121 // response_status == net::HTTP_UNAUTHORIZED) { |
| 122 // // BAD_REQUEST indicates that the request was malformed. |
| 123 // // UNAUTHORIZED indicates that security token didn't match the id. |
| 124 // VLOG(1) << "No point retrying the checkin with status: " |
| 125 // << response_status << ". Checkin failed."; |
| 126 // CheckinRequestStatus status = response_status == net::HTTP_BAD_REQUEST ? |
| 127 // HTTP_BAD_REQUEST : HTTP_UNAUTHORIZED; |
| 128 // RecordCheckinStatusAndReportUMA(status, recorder_, false); |
| 129 // callback_.Run(response_proto); |
| 130 // return; |
| 131 // } |
| 132 |
| 133 // if (response_status != net::HTTP_OK || |
| 134 // !source->GetResponseAsString(&response_string) || |
| 135 // !list_devices.ParseFromString(response_string)) { |
| 136 // LOG(ERROR) << "Failed to get list devices response. HTTP Status: " |
| 137 // << response_status; |
| 138 // if (++fetcher_retries_ < kMaxFetcherRetries) { |
| 139 // fetcher_backoff_.InformOfRequest(false); |
| 140 // fetcher_timer_.Start( |
| 141 // FROM_HERE, fetcher_backoff_.GetTimeUntilRelease(), this, |
| 142 // &DeviceActivityFetcher::StartFetchingListDevices); |
| 143 // return; |
| 144 // } else { |
| 145 // observer_->OnFetchDeviceActivityFailure(); |
| 146 // } |
| 147 // } |
| 148 |
| 149 std::vector<DeviceActivity> devices; |
| 150 // TODO(mlerman): Fill |devices| from the proto in |source|. |
| 151 |
| 152 observer_->OnFetchDeviceActivitySuccess(devices); |
| 153 } |
| 154 |
| 155 |
| 156 void DeviceActivityFetcher::OnListIdpSessionsSuccess( |
| 157 const std::string& login_hint) { |
| 158 fetcher_backoff_.InformOfRequest(true); |
| 159 login_hint_ = login_hint; |
| 160 access_token_ = std::string(); |
| 161 StartFetchingGetTokenResponse(); |
| 162 } |
| 163 |
| 164 void DeviceActivityFetcher::OnListIdpSessionsError( |
| 165 const GoogleServiceAuthError& error) { |
| 166 if (++fetcher_retries_ < kMaxFetcherRetries && |
| 167 IsTransientError(error)) { |
| 168 fetcher_backoff_.InformOfRequest(false); |
| 169 fetcher_timer_.Start( |
| 170 FROM_HERE, fetcher_backoff_.GetTimeUntilRelease(), this, |
| 171 &DeviceActivityFetcher::StartFetchingListIdpSessions); |
| 172 return; |
| 173 } |
| 174 } |
| 175 |
| 176 void DeviceActivityFetcher::OnGetTokenResponseSuccess( |
| 177 const ClientOAuthResult& result) { |
| 178 fetcher_backoff_.InformOfRequest(true); |
| 179 access_token_ = result.access_token; |
| 180 StartFetchingListDevices(); |
| 181 } |
| 182 |
| 183 void DeviceActivityFetcher::OnGetTokenResponseError( |
| 184 const GoogleServiceAuthError& error) { |
| 185 if (++fetcher_retries_ < kMaxFetcherRetries && |
| 186 IsTransientError(error)) { |
| 187 fetcher_backoff_.InformOfRequest(false); |
| 188 fetcher_timer_.Start( |
| 189 FROM_HERE, fetcher_backoff_.GetTimeUntilRelease(), this, |
| 190 &DeviceActivityFetcher::StartFetchingGetTokenResponse); |
| 191 return; |
| 192 } |
| 193 } |
| OLD | NEW |