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

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

Powered by Google App Engine
This is Rietveld 408576698