| Index: chrome/browser/supervised_user/permission_request_creator_apiary.cc
|
| diff --git a/chrome/browser/supervised_user/permission_request_creator_apiary.cc b/chrome/browser/supervised_user/permission_request_creator_apiary.cc
|
| index 5209262226bd7caaa33252833d78cad5792ea387..7f3c97f4de691c5033d3dc7cfe3eb786f60c845f 100644
|
| --- a/chrome/browser/supervised_user/permission_request_creator_apiary.cc
|
| +++ b/chrome/browser/supervised_user/permission_request_creator_apiary.cc
|
| @@ -35,6 +35,15 @@ const char kState[] = "PENDING";
|
|
|
| static const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s";
|
|
|
| +PermissionRequestCreatorApiary::Request::Request(const GURL& url_requested,
|
| + const base::Closure& callback)
|
| + : url_requested(url_requested),
|
| + callback(callback),
|
| + access_token_expired(false) {
|
| +}
|
| +
|
| +PermissionRequestCreatorApiary::Request::~Request() {}
|
| +
|
| PermissionRequestCreatorApiary::PermissionRequestCreatorApiary(
|
| OAuth2TokenService* oauth2_token_service,
|
| scoped_ptr<SupervisedUserSigninManagerWrapper> signin_wrapper,
|
| @@ -42,8 +51,7 @@ PermissionRequestCreatorApiary::PermissionRequestCreatorApiary(
|
| : OAuth2TokenService::Consumer("permissions_creator"),
|
| oauth2_token_service_(oauth2_token_service),
|
| signin_wrapper_(signin_wrapper.Pass()),
|
| - context_(context),
|
| - access_token_expired_(false) {}
|
| + context_(context) {}
|
|
|
| PermissionRequestCreatorApiary::~PermissionRequestCreatorApiary() {}
|
|
|
| @@ -64,9 +72,8 @@ PermissionRequestCreatorApiary::CreateWithProfile(Profile* profile) {
|
| void PermissionRequestCreatorApiary::CreatePermissionRequest(
|
| const GURL& url_requested,
|
| const base::Closure& callback) {
|
| - url_requested_ = url_requested;
|
| - callback_ = callback;
|
| - StartFetching();
|
| + requests_.push_back(new Request(url_requested, callback));
|
| + StartFetching(requests_.back());
|
| }
|
|
|
| std::string PermissionRequestCreatorApiary::GetApiScopeToUse() const {
|
| @@ -79,10 +86,10 @@ std::string PermissionRequestCreatorApiary::GetApiScopeToUse() const {
|
| }
|
| }
|
|
|
| -void PermissionRequestCreatorApiary::StartFetching() {
|
| +void PermissionRequestCreatorApiary::StartFetching(Request* request) {
|
| OAuth2TokenService::ScopeSet scopes;
|
| scopes.insert(GetApiScopeToUse());
|
| - access_token_request_ = oauth2_token_service_->StartRequest(
|
| + request->access_token_request = oauth2_token_service_->StartRequest(
|
| signin_wrapper_->GetAccountIdToUse(), scopes, this);
|
| }
|
|
|
| @@ -90,63 +97,83 @@ void PermissionRequestCreatorApiary::OnGetTokenSuccess(
|
| const OAuth2TokenService::Request* request,
|
| const std::string& access_token,
|
| const base::Time& expiration_time) {
|
| - DCHECK_EQ(access_token_request_.get(), request);
|
| - access_token_ = access_token;
|
| + RequestIterator it = requests_.begin();
|
| + while (it != requests_.end()) {
|
| + if (request == (*it)->access_token_request.get())
|
| + break;
|
| + ++it;
|
| + }
|
| + DCHECK(it != requests_.end());
|
| + (*it)->access_token = access_token;
|
| GURL url(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
|
| switches::kPermissionRequestApiUrl));
|
| const int id = 0;
|
|
|
| - url_fetcher_.reset(URLFetcher::Create(id, url, URLFetcher::POST, this));
|
| + (*it)->url_fetcher.reset(URLFetcher::Create(id, url, URLFetcher::POST, this));
|
|
|
| - url_fetcher_->SetRequestContext(context_);
|
| - url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
|
| - net::LOAD_DO_NOT_SAVE_COOKIES);
|
| - url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries);
|
| - url_fetcher_->AddExtraRequestHeader(
|
| + (*it)->url_fetcher->SetRequestContext(context_);
|
| + (*it)->url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
|
| + net::LOAD_DO_NOT_SAVE_COOKIES);
|
| + (*it)->url_fetcher->SetAutomaticallyRetryOnNetworkChanges(kNumRetries);
|
| + (*it)->url_fetcher->AddExtraRequestHeader(
|
| base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str()));
|
|
|
| base::DictionaryValue dict;
|
| dict.SetStringWithoutPathExpansion("namespace", kNamespace);
|
| - dict.SetStringWithoutPathExpansion("objectRef", url_requested_.spec());
|
| + dict.SetStringWithoutPathExpansion("objectRef", (*it)->url_requested.spec());
|
| dict.SetStringWithoutPathExpansion("state", kState);
|
| std::string body;
|
| base::JSONWriter::Write(&dict, &body);
|
| - url_fetcher_->SetUploadData("application/json", body);
|
| + (*it)->url_fetcher->SetUploadData("application/json", body);
|
|
|
| - url_fetcher_->Start();
|
| + (*it)->url_fetcher->Start();
|
| }
|
|
|
| void PermissionRequestCreatorApiary::OnGetTokenFailure(
|
| const OAuth2TokenService::Request* request,
|
| const GoogleServiceAuthError& error) {
|
| - DCHECK_EQ(access_token_request_.get(), request);
|
| - callback_.Run();
|
| - callback_.Reset();
|
| + RequestIterator it = requests_.begin();
|
| + while (it != requests_.end()) {
|
| + if (request == (*it)->access_token_request.get())
|
| + break;
|
| + ++it;
|
| + }
|
| + DCHECK(it != requests_.end());
|
| + (*it)->callback.Run();
|
| + requests_.erase(it);
|
| }
|
|
|
| void PermissionRequestCreatorApiary::OnURLFetchComplete(
|
| const URLFetcher* source) {
|
| + RequestIterator it = requests_.begin();
|
| + while (it != requests_.end()) {
|
| + if (source == (*it)->url_fetcher.get())
|
| + break;
|
| + ++it;
|
| + }
|
| + DCHECK(it != requests_.end());
|
| +
|
| const net::URLRequestStatus& status = source->GetStatus();
|
| if (!status.is_success()) {
|
| - DispatchNetworkError(status.error());
|
| + DispatchNetworkError(it, status.error());
|
| return;
|
| }
|
|
|
| int response_code = source->GetResponseCode();
|
| - if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) {
|
| - access_token_expired_ = true;
|
| + if (response_code == net::HTTP_UNAUTHORIZED && !(*it)->access_token_expired) {
|
| + (*it)->access_token_expired = true;
|
| OAuth2TokenService::ScopeSet scopes;
|
| scopes.insert(GetApiScopeToUse());
|
| oauth2_token_service_->InvalidateToken(
|
| - signin_wrapper_->GetAccountIdToUse(), scopes, access_token_);
|
| - StartFetching();
|
| + signin_wrapper_->GetAccountIdToUse(), scopes, (*it)->access_token);
|
| + StartFetching(*it);
|
| return;
|
| }
|
|
|
| if (response_code != net::HTTP_OK) {
|
| DLOG(WARNING) << "HTTP error " << response_code;
|
| DispatchGoogleServiceAuthError(
|
| - GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED));
|
| + it, GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED));
|
| return;
|
| }
|
|
|
| @@ -155,25 +182,27 @@ void PermissionRequestCreatorApiary::OnURLFetchComplete(
|
| scoped_ptr<base::Value> value(base::JSONReader::Read(response_body));
|
| base::DictionaryValue* dict = NULL;
|
| if (!value || !value->GetAsDictionary(&dict)) {
|
| - DispatchNetworkError(net::ERR_INVALID_RESPONSE);
|
| + DispatchNetworkError(it, net::ERR_INVALID_RESPONSE);
|
| return;
|
| }
|
| std::string id;
|
| if (!dict->GetString(kIdKey, &id)) {
|
| - DispatchNetworkError(net::ERR_INVALID_RESPONSE);
|
| + DispatchNetworkError(it, net::ERR_INVALID_RESPONSE);
|
| return;
|
| }
|
| - callback_.Run();
|
| - callback_.Reset();
|
| + (*it)->callback.Run();
|
| + requests_.erase(it);
|
| }
|
|
|
| -void PermissionRequestCreatorApiary::DispatchNetworkError(int error_code) {
|
| +void PermissionRequestCreatorApiary::DispatchNetworkError(RequestIterator it,
|
| + int error_code) {
|
| DispatchGoogleServiceAuthError(
|
| - GoogleServiceAuthError::FromConnectionError(error_code));
|
| + it, GoogleServiceAuthError::FromConnectionError(error_code));
|
| }
|
|
|
| void PermissionRequestCreatorApiary::DispatchGoogleServiceAuthError(
|
| + RequestIterator it,
|
| const GoogleServiceAuthError& error) {
|
| - callback_.Run();
|
| - callback_.Reset();
|
| + (*it)->callback.Run();
|
| + requests_.erase(it);
|
| }
|
|
|