OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/supervised_user/child_accounts/permission_request_creat
or_apiary.h" | 5 #include "chrome/browser/supervised_user/child_accounts/permission_request_creat
or_apiary.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 switches::kPermissionRequestApiScope); | 151 switches::kPermissionRequestApiScope); |
152 } else { | 152 } else { |
153 return kApiScope; | 153 return kApiScope; |
154 } | 154 } |
155 } | 155 } |
156 | 156 |
157 void PermissionRequestCreatorApiary::CreateRequest( | 157 void PermissionRequestCreatorApiary::CreateRequest( |
158 const std::string& request_type, | 158 const std::string& request_type, |
159 const std::string& object_ref, | 159 const std::string& object_ref, |
160 const SuccessCallback& callback) { | 160 const SuccessCallback& callback) { |
161 requests_.push_back( | 161 requests_.push_back(base::MakeUnique<Request>(request_type, object_ref, |
162 new Request(request_type, object_ref, callback, url_fetcher_id_)); | 162 callback, url_fetcher_id_)); |
163 StartFetching(requests_.back()); | 163 StartFetching(requests_.back().get()); |
164 } | 164 } |
165 | 165 |
166 void PermissionRequestCreatorApiary::StartFetching(Request* request) { | 166 void PermissionRequestCreatorApiary::StartFetching(Request* request) { |
167 OAuth2TokenService::ScopeSet scopes; | 167 OAuth2TokenService::ScopeSet scopes; |
168 scopes.insert(GetApiScope()); | 168 scopes.insert(GetApiScope()); |
169 request->access_token_request = oauth2_token_service_->StartRequest( | 169 request->access_token_request = oauth2_token_service_->StartRequest( |
170 account_id_, scopes, this); | 170 account_id_, scopes, this); |
171 } | 171 } |
172 | 172 |
173 void PermissionRequestCreatorApiary::OnGetTokenSuccess( | 173 void PermissionRequestCreatorApiary::OnGetTokenSuccess( |
174 const OAuth2TokenService::Request* request, | 174 const OAuth2TokenService::Request* request, |
175 const std::string& access_token, | 175 const std::string& access_token, |
176 const base::Time& expiration_time) { | 176 const base::Time& expiration_time) { |
177 RequestIterator it = requests_.begin(); | 177 auto it = requests_.begin(); |
178 while (it != requests_.end()) { | 178 while (it != requests_.end()) { |
179 if (request == (*it)->access_token_request.get()) | 179 if (request == (*it)->access_token_request.get()) |
180 break; | 180 break; |
181 ++it; | 181 ++it; |
182 } | 182 } |
183 DCHECK(it != requests_.end()); | 183 DCHECK(it != requests_.end()); |
184 (*it)->access_token = access_token; | 184 (*it)->access_token = access_token; |
185 | 185 |
186 net::NetworkTrafficAnnotationTag traffic_annotation = | 186 net::NetworkTrafficAnnotationTag traffic_annotation = |
187 net::DefineNetworkTrafficAnnotation("permission_request_creator", R"( | 187 net::DefineNetworkTrafficAnnotation("permission_request_creator", R"( |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 std::string body; | 231 std::string body; |
232 base::JSONWriter::Write(dict, &body); | 232 base::JSONWriter::Write(dict, &body); |
233 (*it)->url_fetcher->SetUploadData("application/json", body); | 233 (*it)->url_fetcher->SetUploadData("application/json", body); |
234 | 234 |
235 (*it)->url_fetcher->Start(); | 235 (*it)->url_fetcher->Start(); |
236 } | 236 } |
237 | 237 |
238 void PermissionRequestCreatorApiary::OnGetTokenFailure( | 238 void PermissionRequestCreatorApiary::OnGetTokenFailure( |
239 const OAuth2TokenService::Request* request, | 239 const OAuth2TokenService::Request* request, |
240 const GoogleServiceAuthError& error) { | 240 const GoogleServiceAuthError& error) { |
241 RequestIterator it = requests_.begin(); | 241 auto it = requests_.begin(); |
242 while (it != requests_.end()) { | 242 while (it != requests_.end()) { |
243 if (request == (*it)->access_token_request.get()) | 243 if (request == (*it)->access_token_request.get()) |
244 break; | 244 break; |
245 ++it; | 245 ++it; |
246 } | 246 } |
247 DCHECK(it != requests_.end()); | 247 DCHECK(it != requests_.end()); |
248 LOG(WARNING) << "Token error: " << error.ToString(); | 248 LOG(WARNING) << "Token error: " << error.ToString(); |
249 DispatchResult(it, false); | 249 DispatchResult(it, false); |
250 } | 250 } |
251 | 251 |
252 void PermissionRequestCreatorApiary::OnURLFetchComplete( | 252 void PermissionRequestCreatorApiary::OnURLFetchComplete( |
253 const URLFetcher* source) { | 253 const URLFetcher* source) { |
254 RequestIterator it = requests_.begin(); | 254 auto it = requests_.begin(); |
255 while (it != requests_.end()) { | 255 while (it != requests_.end()) { |
256 if (source == (*it)->url_fetcher.get()) | 256 if (source == (*it)->url_fetcher.get()) |
257 break; | 257 break; |
258 ++it; | 258 ++it; |
259 } | 259 } |
260 DCHECK(it != requests_.end()); | 260 DCHECK(it != requests_.end()); |
261 | 261 |
262 const net::URLRequestStatus& status = source->GetStatus(); | 262 const net::URLRequestStatus& status = source->GetStatus(); |
263 if (!status.is_success()) { | 263 if (!status.is_success()) { |
264 LOG(WARNING) << "Network error " << status.error(); | 264 LOG(WARNING) << "Network error " << status.error(); |
265 DispatchResult(it, false); | 265 DispatchResult(it, false); |
266 return; | 266 return; |
267 } | 267 } |
268 | 268 |
269 int response_code = source->GetResponseCode(); | 269 int response_code = source->GetResponseCode(); |
270 if (response_code == net::HTTP_UNAUTHORIZED && !(*it)->access_token_expired) { | 270 if (response_code == net::HTTP_UNAUTHORIZED && !(*it)->access_token_expired) { |
271 (*it)->access_token_expired = true; | 271 (*it)->access_token_expired = true; |
272 OAuth2TokenService::ScopeSet scopes; | 272 OAuth2TokenService::ScopeSet scopes; |
273 scopes.insert(GetApiScope()); | 273 scopes.insert(GetApiScope()); |
274 oauth2_token_service_->InvalidateAccessToken(account_id_, scopes, | 274 oauth2_token_service_->InvalidateAccessToken(account_id_, scopes, |
275 (*it)->access_token); | 275 (*it)->access_token); |
276 StartFetching(*it); | 276 StartFetching(it->get()); |
277 return; | 277 return; |
278 } | 278 } |
279 | 279 |
280 if (response_code != net::HTTP_OK) { | 280 if (response_code != net::HTTP_OK) { |
281 LOG(WARNING) << "HTTP error " << response_code; | 281 LOG(WARNING) << "HTTP error " << response_code; |
282 DispatchResult(it, false); | 282 DispatchResult(it, false); |
283 return; | 283 return; |
284 } | 284 } |
285 | 285 |
286 std::string response_body; | 286 std::string response_body; |
(...skipping 18 matching lines...) Expand all Loading... |
305 return; | 305 return; |
306 } | 306 } |
307 DispatchResult(it, true); | 307 DispatchResult(it, true); |
308 } | 308 } |
309 | 309 |
310 void PermissionRequestCreatorApiary::DispatchResult(RequestIterator it, | 310 void PermissionRequestCreatorApiary::DispatchResult(RequestIterator it, |
311 bool success) { | 311 bool success) { |
312 (*it)->callback.Run(success); | 312 (*it)->callback.Run(success); |
313 requests_.erase(it); | 313 requests_.erase(it); |
314 } | 314 } |
OLD | NEW |