| 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/permission_request_creator_apiary.h" | 5 #include "chrome/browser/supervised_user/permission_request_creator_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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 } | 62 } |
| 63 | 63 |
| 64 void PermissionRequestCreatorApiary::CreatePermissionRequest( | 64 void PermissionRequestCreatorApiary::CreatePermissionRequest( |
| 65 const GURL& url_requested, | 65 const GURL& url_requested, |
| 66 const base::Closure& callback) { | 66 const base::Closure& callback) { |
| 67 url_requested_ = url_requested; | 67 url_requested_ = url_requested; |
| 68 callback_ = callback; | 68 callback_ = callback; |
| 69 StartFetching(); | 69 StartFetching(); |
| 70 } | 70 } |
| 71 | 71 |
| 72 std::string PermissionRequestCreatorApiary::GetApiScopeToUse() const { |
| 73 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 74 switches::kPermissionRequestApiScope)) { |
| 75 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 76 switches::kPermissionRequestApiScope); |
| 77 } else { |
| 78 return signin_wrapper_->GetSyncScopeToUse(); |
| 79 } |
| 80 } |
| 81 |
| 72 void PermissionRequestCreatorApiary::StartFetching() { | 82 void PermissionRequestCreatorApiary::StartFetching() { |
| 73 OAuth2TokenService::ScopeSet scopes; | 83 OAuth2TokenService::ScopeSet scopes; |
| 74 if (CommandLine::ForCurrentProcess()->HasSwitch( | 84 scopes.insert(GetApiScopeToUse()); |
| 75 switches::kPermissionRequestApiScope)) { | |
| 76 scopes.insert(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 77 switches::kPermissionRequestApiScope)); | |
| 78 } else { | |
| 79 scopes.insert(signin_wrapper_->GetSyncScopeToUse()); | |
| 80 } | |
| 81 access_token_request_ = oauth2_token_service_->StartRequest( | 85 access_token_request_ = oauth2_token_service_->StartRequest( |
| 82 signin_wrapper_->GetAccountIdToUse(), scopes, this); | 86 signin_wrapper_->GetAccountIdToUse(), scopes, this); |
| 83 } | 87 } |
| 84 | 88 |
| 85 void PermissionRequestCreatorApiary::OnGetTokenSuccess( | 89 void PermissionRequestCreatorApiary::OnGetTokenSuccess( |
| 86 const OAuth2TokenService::Request* request, | 90 const OAuth2TokenService::Request* request, |
| 87 const std::string& access_token, | 91 const std::string& access_token, |
| 88 const base::Time& expiration_time) { | 92 const base::Time& expiration_time) { |
| 89 DCHECK_EQ(access_token_request_.get(), request); | 93 DCHECK_EQ(access_token_request_.get(), request); |
| 90 access_token_ = access_token; | 94 access_token_ = access_token; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 const net::URLRequestStatus& status = source->GetStatus(); | 129 const net::URLRequestStatus& status = source->GetStatus(); |
| 126 if (!status.is_success()) { | 130 if (!status.is_success()) { |
| 127 DispatchNetworkError(status.error()); | 131 DispatchNetworkError(status.error()); |
| 128 return; | 132 return; |
| 129 } | 133 } |
| 130 | 134 |
| 131 int response_code = source->GetResponseCode(); | 135 int response_code = source->GetResponseCode(); |
| 132 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) { | 136 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) { |
| 133 access_token_expired_ = true; | 137 access_token_expired_ = true; |
| 134 OAuth2TokenService::ScopeSet scopes; | 138 OAuth2TokenService::ScopeSet scopes; |
| 135 scopes.insert(signin_wrapper_->GetSyncScopeToUse()); | 139 scopes.insert(GetApiScopeToUse()); |
| 136 oauth2_token_service_->InvalidateToken( | 140 oauth2_token_service_->InvalidateToken( |
| 137 signin_wrapper_->GetAccountIdToUse(), scopes, access_token_); | 141 signin_wrapper_->GetAccountIdToUse(), scopes, access_token_); |
| 138 StartFetching(); | 142 StartFetching(); |
| 139 return; | 143 return; |
| 140 } | 144 } |
| 141 | 145 |
| 142 if (response_code != net::HTTP_OK) { | 146 if (response_code != net::HTTP_OK) { |
| 143 DLOG(WARNING) << "HTTP error " << response_code; | 147 DLOG(WARNING) << "HTTP error " << response_code; |
| 144 DispatchGoogleServiceAuthError( | 148 DispatchGoogleServiceAuthError( |
| 145 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED)); | 149 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 166 void PermissionRequestCreatorApiary::DispatchNetworkError(int error_code) { | 170 void PermissionRequestCreatorApiary::DispatchNetworkError(int error_code) { |
| 167 DispatchGoogleServiceAuthError( | 171 DispatchGoogleServiceAuthError( |
| 168 GoogleServiceAuthError::FromConnectionError(error_code)); | 172 GoogleServiceAuthError::FromConnectionError(error_code)); |
| 169 } | 173 } |
| 170 | 174 |
| 171 void PermissionRequestCreatorApiary::DispatchGoogleServiceAuthError( | 175 void PermissionRequestCreatorApiary::DispatchGoogleServiceAuthError( |
| 172 const GoogleServiceAuthError& error) { | 176 const GoogleServiceAuthError& error) { |
| 173 callback_.Run(); | 177 callback_.Run(); |
| 174 callback_.Reset(); | 178 callback_.Reset(); |
| 175 } | 179 } |
| OLD | NEW |