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

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: more alexei comments (histogram summaries, small refactorings) 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 // TODO(mlerman,anthonyvd): Replace the three parameters following with the
19 // necessary parameters for calling the ListDevices endpoint, once live.
Alexei Svitkine (slow) 2015/05/19 19:59:44 crbug please
Mike Lerman 2015/05/19 20:54:04 Done.
20 const char kSyncListDevicesScope[] =
21 "https://www.googleapis.com/auth/chromesynclistdevices";
22 const char kChromeDomain[] = "http://www.chrome.com";
23 const char kListDevicesEndpoint[] = "http://127.0.0.1";
24 // Template for optional authorization header when using an OAuth access token.
25 const char kAuthorizationHeader[] = "Authorization: Bearer %s";
26
27 // In case of an error while fetching using the GaiaAuthFetcher, retry with
28 // exponential backoff. Try up to 9 times within 10 minutes.
29 const net::BackoffEntry::Policy kBackoffPolicy = {
30 // Number of initial errors (in sequence) to ignore before applying
31 // exponential back-off rules.
32 0,
33 // Initial delay for exponential backoff in ms.
34 1000,
35 // Factor by which the waiting time will be multiplied.
36 2,
37 // Fuzzing percentage. ex: 10% will spread requests randomly
38 // between 90%-100% of the calculated time.
39 0.1, // 10%
40 // Maximum amount of time we are willing to delay our request in ms.
41 1000 * 60 * 15, // 15 minutes.
42 // Time to keep an entry from being discarded even when it
43 // has no significant state, -1 to never discard.
44 -1,
45 // Don't use initial delay unless the last request was an error.
46 false,
47 };
48
49 const int kMaxFetcherRetries = 9;
50
51 } // namespace
52
53 DeviceActivityFetcher::DeviceActivityFetcher(
54 SigninClient* signin_client,
55 DeviceActivityFetcher::Observer* observer)
56 : fetcher_backoff_(&kBackoffPolicy),
57 fetcher_retries_(0),
58 signin_client_(signin_client),
59 observer_(observer) {
60 }
61
62 DeviceActivityFetcher::~DeviceActivityFetcher() {
63 }
64
65 void DeviceActivityFetcher::Start() {
66 fetcher_retries_ = 0;
67 login_hint_ = std::string();
68 StartFetchingListIdpSessions();
69 }
70
71 void DeviceActivityFetcher::Stop() {
72 if (gaia_auth_fetcher_)
73 gaia_auth_fetcher_->CancelRequest();
74 if (url_fetcher_)
75 url_fetcher_.reset();
76 }
77
78 void DeviceActivityFetcher::StartFetchingListIdpSessions() {
79 gaia_auth_fetcher_.reset(
80 new GaiaAuthFetcher(this, GaiaConstants::kChromeSource,
81 signin_client_->GetURLRequestContext()));
82 gaia_auth_fetcher_->StartListIDPSessions(kSyncListDevicesScope,
83 kChromeDomain);
84 }
85
86 void DeviceActivityFetcher::StartFetchingGetTokenResponse() {
87 gaia_auth_fetcher_.reset(
88 new GaiaAuthFetcher(this, GaiaConstants::kChromeSource,
89 signin_client_->GetURLRequestContext()));
90 gaia_auth_fetcher_->StartGetTokenResponse(kSyncListDevicesScope,
91 kChromeDomain, 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, this);
98 url_fetcher_->SetRequestContext(signin_client_->GetURLRequestContext());
99 if (!access_token_.empty()) {
100 url_fetcher_->SetExtraRequestHeaders(
101 base::StringPrintf(kAuthorizationHeader, access_token_.c_str()));
102 }
103 url_fetcher_->Start();
104 }
105
106 void DeviceActivityFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
107 // TODO(mlerman): Uncomment the code below once we have a working proto.
108 // Work done under crbug.com/463611
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 void DeviceActivityFetcher::OnListIdpSessionsSuccess(
166 const std::string& login_hint) {
167 fetcher_backoff_.InformOfRequest(true);
168 login_hint_ = login_hint;
169 access_token_ = std::string();
170 StartFetchingGetTokenResponse();
171 }
172
173 void DeviceActivityFetcher::OnListIdpSessionsError(
174 const GoogleServiceAuthError& error) {
175 if (++fetcher_retries_ < kMaxFetcherRetries && error.IsTransientError()) {
176 fetcher_backoff_.InformOfRequest(false);
177 fetcher_timer_.Start(FROM_HERE, fetcher_backoff_.GetTimeUntilRelease(),
178 this,
179 &DeviceActivityFetcher::StartFetchingListIdpSessions);
180 return;
181 }
182 observer_->OnFetchDeviceActivityFailure();
183 }
184
185 void DeviceActivityFetcher::OnGetTokenResponseSuccess(
186 const ClientOAuthResult& result) {
187 fetcher_backoff_.InformOfRequest(true);
188 access_token_ = result.access_token;
189 StartFetchingListDevices();
190 }
191
192 void DeviceActivityFetcher::OnGetTokenResponseError(
193 const GoogleServiceAuthError& error) {
194 if (++fetcher_retries_ < kMaxFetcherRetries && error.IsTransientError()) {
195 fetcher_backoff_.InformOfRequest(false);
196 fetcher_timer_.Start(FROM_HERE, fetcher_backoff_.GetTimeUntilRelease(),
197 this,
198 &DeviceActivityFetcher::StartFetchingGetTokenResponse);
199 return;
200 }
201 observer_->OnFetchDeviceActivityFailure();
202 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698