Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1012)

Side by Side Diff: components/signin/core/browser/device_activity_fetcher.cc

Issue 1087933002: Cross Device Promo - Main Eligibility Flow (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pre-review Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 DeviceActivityFetcher::~DeviceActivityFetcher() {
66 }
67
68 void DeviceActivityFetcher::Start() {
69 fetcher_retries_ = 0;
70 login_hint_ = std::string();
71 StartFetchingListIdpSessions();
72 }
73
74 void DeviceActivityFetcher::StartFetchingListIdpSessions() {
75 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this,
76 GaiaConstants::kChromeSource, signin_client_->GetURLRequestContext()));
77 gaia_auth_fetcher_->StartListIDPSessions(kSyncListDevicesScope,
78 kChromeDomain);
79 }
80
81 void DeviceActivityFetcher::StartFetchingGetTokenResponse() {
82 gaia_auth_fetcher_.reset(new GaiaAuthFetcher(this,
83 GaiaConstants::kChromeSource, signin_client_->GetURLRequestContext()));
84 gaia_auth_fetcher_->StartGetTokenResponse(kSyncListDevicesScope,
85 kChromeDomain,
86 login_hint_);
87 }
88
89 void DeviceActivityFetcher::StartFetchingListDevices() {
90 // Call the Sync Endpoint.
91 url_fetcher_ = net::URLFetcher::Create(GURL(kListDevicesEndpoint),
92 net::URLFetcher::GET,
93 this);
94 url_fetcher_->SetRequestContext(signin_client_->GetURLRequestContext());
95 if (!access_token_.empty()) {
96 url_fetcher_->SetExtraRequestHeaders(
97 base::StringPrintf(kAuthorizationHeader, access_token_.c_str()));
98 }
99 url_fetcher_->Start();
100 }
101
102 void DeviceActivityFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
103 // TODO(mlerman): Uncomment the code below once we have a working proto.
104
105 // std::string response_string;
106 // ListDevices list_devices;
107
108 // if (!source->GetStatus().is_success()) {
109 // VLOG(1) << "Failed to fetch listdevices response. Retrying.";
110 // if (++fetcher_retries_ < kMaxFetcherRetries) {
111 // fetcher_backoff_.InformOfRequest(false);
112 // fetcher_timer_.Start(
113 // FROM_HERE, fetcher_backoff_.GetTimeUntilRelease(), this,
114 // &DeviceActivityFetcher::StartFetchingListDevices);
115 // return;
116 // } else {
117 // observer_->OnFetchDeviceActivityFailure();
118 // }
119 // }
120
121 // net::HttpStatusCode response_status = static_cast<net::HttpStatusCode>(
122 // source->GetResponseCode());
123 // if (response_status == net::HTTP_BAD_REQUEST ||
124 // response_status == net::HTTP_UNAUTHORIZED) {
125 // // BAD_REQUEST indicates that the request was malformed.
126 // // UNAUTHORIZED indicates that security token didn't match the id.
127 // VLOG(1) << "No point retrying the checkin with status: "
128 // << response_status << ". Checkin failed.";
129 // CheckinRequestStatus status = response_status == net::HTTP_BAD_REQUEST ?
130 // HTTP_BAD_REQUEST : HTTP_UNAUTHORIZED;
131 // RecordCheckinStatusAndReportUMA(status, recorder_, false);
132 // callback_.Run(response_proto);
133 // return;
134 // }
135
136 // if (response_status != net::HTTP_OK ||
137 // !source->GetResponseAsString(&response_string) ||
138 // !list_devices.ParseFromString(response_string)) {
139 // LOG(ERROR) << "Failed to get list devices response. HTTP Status: "
140 // << response_status;
141 // if (++fetcher_retries_ < kMaxFetcherRetries) {
142 // fetcher_backoff_.InformOfRequest(false);
143 // fetcher_timer_.Start(
144 // FROM_HERE, fetcher_backoff_.GetTimeUntilRelease(), this,
145 // &DeviceActivityFetcher::StartFetchingListDevices);
146 // return;
147 // } else {
148 // observer_->OnFetchDeviceActivityFailure();
149 // }
150 // }
151
152 std::vector<DeviceActivity> devices;
153 // TODO(mlerman): Fill |devices| from the proto in |source|.
154
155 observer_->OnFetchDeviceActivitySuccess(devices);
156 }
157
158
159 void DeviceActivityFetcher::OnListIdpSessionsSuccess(
160 const std::string& login_hint) {
161 fetcher_backoff_.InformOfRequest(true);
162 login_hint_ = login_hint;
163 access_token_ = std::string();
164 StartFetchingGetTokenResponse();
165 }
166
167 void DeviceActivityFetcher::OnListIdpSessionsError(
168 const GoogleServiceAuthError& error) {
169 if (++fetcher_retries_ < kMaxFetcherRetries &&
170 IsTransientError(error)) {
171 fetcher_backoff_.InformOfRequest(false);
172 fetcher_timer_.Start(
173 FROM_HERE, fetcher_backoff_.GetTimeUntilRelease(), this,
174 &DeviceActivityFetcher::StartFetchingListIdpSessions);
175 return;
176 }
177 observer_->OnFetchDeviceActivityFailure();
178 }
179
180 void DeviceActivityFetcher::OnGetTokenResponseSuccess(
181 const ClientOAuthResult& result) {
182 fetcher_backoff_.InformOfRequest(true);
183 access_token_ = result.access_token;
184 StartFetchingListDevices();
185 }
186
187 void DeviceActivityFetcher::OnGetTokenResponseError(
188 const GoogleServiceAuthError& error) {
189 if (++fetcher_retries_ < kMaxFetcherRetries &&
190 IsTransientError(error)) {
191 fetcher_backoff_.InformOfRequest(false);
192 fetcher_timer_.Start(
193 FROM_HERE, fetcher_backoff_.GetTimeUntilRelease(), this,
194 &DeviceActivityFetcher::StartFetchingGetTokenResponse);
195 return;
196 }
197 observer_->OnFetchDeviceActivityFailure();
198 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698