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

Side by Side Diff: chrome/browser/supervised_user/permission_request_creator_apiary.cc

Issue 592353002: PermissionRequestCreatorApiary: Support for multiple simultaneous requests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: move struct into .cc Created 6 years, 2 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
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 17 matching lines...) Expand all
28 28
29 using net::URLFetcher; 29 using net::URLFetcher;
30 30
31 const int kNumRetries = 1; 31 const int kNumRetries = 1;
32 const char kIdKey[] = "id"; 32 const char kIdKey[] = "id";
33 const char kNamespace[] = "CHROME"; 33 const char kNamespace[] = "CHROME";
34 const char kState[] = "PENDING"; 34 const char kState[] = "PENDING";
35 35
36 static const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; 36 static const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s";
37 37
38 struct PermissionRequestCreatorApiary::Request {
39 Request(const GURL& url_requested, const base::Closure& callback);
40 ~Request();
41
42 GURL url_requested;
43 base::Closure callback;
44 scoped_ptr<OAuth2TokenService::Request> access_token_request;
45 std::string access_token;
46 bool access_token_expired;
47 scoped_ptr<net::URLFetcher> url_fetcher;
48 };
49
50 PermissionRequestCreatorApiary::Request::Request(const GURL& url_requested,
51 const base::Closure& callback)
52 : url_requested(url_requested),
53 callback(callback),
54 access_token_expired(false) {
55 }
56
57 PermissionRequestCreatorApiary::Request::~Request() {}
58
38 PermissionRequestCreatorApiary::PermissionRequestCreatorApiary( 59 PermissionRequestCreatorApiary::PermissionRequestCreatorApiary(
39 OAuth2TokenService* oauth2_token_service, 60 OAuth2TokenService* oauth2_token_service,
40 scoped_ptr<SupervisedUserSigninManagerWrapper> signin_wrapper, 61 scoped_ptr<SupervisedUserSigninManagerWrapper> signin_wrapper,
41 net::URLRequestContextGetter* context) 62 net::URLRequestContextGetter* context)
42 : OAuth2TokenService::Consumer("permissions_creator"), 63 : OAuth2TokenService::Consumer("permissions_creator"),
43 oauth2_token_service_(oauth2_token_service), 64 oauth2_token_service_(oauth2_token_service),
44 signin_wrapper_(signin_wrapper.Pass()), 65 signin_wrapper_(signin_wrapper.Pass()),
45 context_(context), 66 context_(context) {}
46 access_token_expired_(false) {}
47 67
48 PermissionRequestCreatorApiary::~PermissionRequestCreatorApiary() {} 68 PermissionRequestCreatorApiary::~PermissionRequestCreatorApiary() {}
49 69
50 // static 70 // static
51 scoped_ptr<PermissionRequestCreator> 71 scoped_ptr<PermissionRequestCreator>
52 PermissionRequestCreatorApiary::CreateWithProfile(Profile* profile) { 72 PermissionRequestCreatorApiary::CreateWithProfile(Profile* profile) {
53 ProfileOAuth2TokenService* token_service = 73 ProfileOAuth2TokenService* token_service =
54 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); 74 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
55 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); 75 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile);
56 scoped_ptr<SupervisedUserSigninManagerWrapper> signin_wrapper( 76 scoped_ptr<SupervisedUserSigninManagerWrapper> signin_wrapper(
57 new SupervisedUserSigninManagerWrapper(profile, signin)); 77 new SupervisedUserSigninManagerWrapper(profile, signin));
58 scoped_ptr<PermissionRequestCreator> creator( 78 scoped_ptr<PermissionRequestCreator> creator(
59 new PermissionRequestCreatorApiary( 79 new PermissionRequestCreatorApiary(
60 token_service, signin_wrapper.Pass(), profile->GetRequestContext())); 80 token_service, signin_wrapper.Pass(), profile->GetRequestContext()));
61 return creator.Pass(); 81 return creator.Pass();
62 } 82 }
63 83
64 void PermissionRequestCreatorApiary::CreatePermissionRequest( 84 void PermissionRequestCreatorApiary::CreatePermissionRequest(
65 const GURL& url_requested, 85 const GURL& url_requested,
66 const base::Closure& callback) { 86 const base::Closure& callback) {
67 url_requested_ = url_requested; 87 requests_.push_back(new Request(url_requested, callback));
68 callback_ = callback; 88 StartFetching(requests_.back());
69 StartFetching();
70 } 89 }
71 90
72 std::string PermissionRequestCreatorApiary::GetApiScopeToUse() const { 91 std::string PermissionRequestCreatorApiary::GetApiScopeToUse() const {
73 if (CommandLine::ForCurrentProcess()->HasSwitch( 92 if (CommandLine::ForCurrentProcess()->HasSwitch(
74 switches::kPermissionRequestApiScope)) { 93 switches::kPermissionRequestApiScope)) {
75 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 94 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
76 switches::kPermissionRequestApiScope); 95 switches::kPermissionRequestApiScope);
77 } else { 96 } else {
78 return signin_wrapper_->GetSyncScopeToUse(); 97 return signin_wrapper_->GetSyncScopeToUse();
79 } 98 }
80 } 99 }
81 100
82 void PermissionRequestCreatorApiary::StartFetching() { 101 void PermissionRequestCreatorApiary::StartFetching(Request* request) {
83 OAuth2TokenService::ScopeSet scopes; 102 OAuth2TokenService::ScopeSet scopes;
84 scopes.insert(GetApiScopeToUse()); 103 scopes.insert(GetApiScopeToUse());
85 access_token_request_ = oauth2_token_service_->StartRequest( 104 request->access_token_request = oauth2_token_service_->StartRequest(
86 signin_wrapper_->GetAccountIdToUse(), scopes, this); 105 signin_wrapper_->GetAccountIdToUse(), scopes, this);
87 } 106 }
88 107
89 void PermissionRequestCreatorApiary::OnGetTokenSuccess( 108 void PermissionRequestCreatorApiary::OnGetTokenSuccess(
90 const OAuth2TokenService::Request* request, 109 const OAuth2TokenService::Request* request,
91 const std::string& access_token, 110 const std::string& access_token,
92 const base::Time& expiration_time) { 111 const base::Time& expiration_time) {
93 DCHECK_EQ(access_token_request_.get(), request); 112 RequestIterator it = requests_.begin();
94 access_token_ = access_token; 113 while (it != requests_.end()) {
114 if (request == (*it)->access_token_request.get())
115 break;
116 ++it;
117 }
118 DCHECK(it != requests_.end());
119 (*it)->access_token = access_token;
95 GURL url(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 120 GURL url(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
96 switches::kPermissionRequestApiUrl)); 121 switches::kPermissionRequestApiUrl));
97 const int id = 0; 122 const int id = 0;
98 123
99 url_fetcher_.reset(URLFetcher::Create(id, url, URLFetcher::POST, this)); 124 (*it)->url_fetcher.reset(URLFetcher::Create(id, url, URLFetcher::POST, this));
100 125
101 url_fetcher_->SetRequestContext(context_); 126 (*it)->url_fetcher->SetRequestContext(context_);
102 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 127 (*it)->url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
103 net::LOAD_DO_NOT_SAVE_COOKIES); 128 net::LOAD_DO_NOT_SAVE_COOKIES);
104 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); 129 (*it)->url_fetcher->SetAutomaticallyRetryOnNetworkChanges(kNumRetries);
105 url_fetcher_->AddExtraRequestHeader( 130 (*it)->url_fetcher->AddExtraRequestHeader(
106 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str())); 131 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str()));
107 132
108 base::DictionaryValue dict; 133 base::DictionaryValue dict;
109 dict.SetStringWithoutPathExpansion("namespace", kNamespace); 134 dict.SetStringWithoutPathExpansion("namespace", kNamespace);
110 dict.SetStringWithoutPathExpansion("objectRef", url_requested_.spec()); 135 dict.SetStringWithoutPathExpansion("objectRef", (*it)->url_requested.spec());
111 dict.SetStringWithoutPathExpansion("state", kState); 136 dict.SetStringWithoutPathExpansion("state", kState);
112 std::string body; 137 std::string body;
113 base::JSONWriter::Write(&dict, &body); 138 base::JSONWriter::Write(&dict, &body);
114 url_fetcher_->SetUploadData("application/json", body); 139 (*it)->url_fetcher->SetUploadData("application/json", body);
115 140
116 url_fetcher_->Start(); 141 (*it)->url_fetcher->Start();
117 } 142 }
118 143
119 void PermissionRequestCreatorApiary::OnGetTokenFailure( 144 void PermissionRequestCreatorApiary::OnGetTokenFailure(
120 const OAuth2TokenService::Request* request, 145 const OAuth2TokenService::Request* request,
121 const GoogleServiceAuthError& error) { 146 const GoogleServiceAuthError& error) {
122 DCHECK_EQ(access_token_request_.get(), request); 147 RequestIterator it = requests_.begin();
123 callback_.Run(); 148 while (it != requests_.end()) {
124 callback_.Reset(); 149 if (request == (*it)->access_token_request.get())
150 break;
151 ++it;
152 }
153 DCHECK(it != requests_.end());
154 (*it)->callback.Run();
155 requests_.erase(it);
125 } 156 }
126 157
127 void PermissionRequestCreatorApiary::OnURLFetchComplete( 158 void PermissionRequestCreatorApiary::OnURLFetchComplete(
128 const URLFetcher* source) { 159 const URLFetcher* source) {
160 RequestIterator it = requests_.begin();
161 while (it != requests_.end()) {
162 if (source == (*it)->url_fetcher.get())
163 break;
164 ++it;
165 }
166 DCHECK(it != requests_.end());
167
129 const net::URLRequestStatus& status = source->GetStatus(); 168 const net::URLRequestStatus& status = source->GetStatus();
130 if (!status.is_success()) { 169 if (!status.is_success()) {
131 DispatchNetworkError(status.error()); 170 DispatchNetworkError(it, status.error());
132 return; 171 return;
133 } 172 }
134 173
135 int response_code = source->GetResponseCode(); 174 int response_code = source->GetResponseCode();
136 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) { 175 if (response_code == net::HTTP_UNAUTHORIZED && !(*it)->access_token_expired) {
137 access_token_expired_ = true; 176 (*it)->access_token_expired = true;
138 OAuth2TokenService::ScopeSet scopes; 177 OAuth2TokenService::ScopeSet scopes;
139 scopes.insert(GetApiScopeToUse()); 178 scopes.insert(GetApiScopeToUse());
140 oauth2_token_service_->InvalidateToken( 179 oauth2_token_service_->InvalidateToken(
141 signin_wrapper_->GetAccountIdToUse(), scopes, access_token_); 180 signin_wrapper_->GetAccountIdToUse(), scopes, (*it)->access_token);
142 StartFetching(); 181 StartFetching(*it);
143 return; 182 return;
144 } 183 }
145 184
146 if (response_code != net::HTTP_OK) { 185 if (response_code != net::HTTP_OK) {
147 DLOG(WARNING) << "HTTP error " << response_code; 186 DLOG(WARNING) << "HTTP error " << response_code;
148 DispatchGoogleServiceAuthError( 187 DispatchGoogleServiceAuthError(
149 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED)); 188 it, GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED));
150 return; 189 return;
151 } 190 }
152 191
153 std::string response_body; 192 std::string response_body;
154 source->GetResponseAsString(&response_body); 193 source->GetResponseAsString(&response_body);
155 scoped_ptr<base::Value> value(base::JSONReader::Read(response_body)); 194 scoped_ptr<base::Value> value(base::JSONReader::Read(response_body));
156 base::DictionaryValue* dict = NULL; 195 base::DictionaryValue* dict = NULL;
157 if (!value || !value->GetAsDictionary(&dict)) { 196 if (!value || !value->GetAsDictionary(&dict)) {
158 DispatchNetworkError(net::ERR_INVALID_RESPONSE); 197 DispatchNetworkError(it, net::ERR_INVALID_RESPONSE);
159 return; 198 return;
160 } 199 }
161 std::string id; 200 std::string id;
162 if (!dict->GetString(kIdKey, &id)) { 201 if (!dict->GetString(kIdKey, &id)) {
163 DispatchNetworkError(net::ERR_INVALID_RESPONSE); 202 DispatchNetworkError(it, net::ERR_INVALID_RESPONSE);
164 return; 203 return;
165 } 204 }
166 callback_.Run(); 205 (*it)->callback.Run();
167 callback_.Reset(); 206 requests_.erase(it);
168 } 207 }
169 208
170 void PermissionRequestCreatorApiary::DispatchNetworkError(int error_code) { 209 void PermissionRequestCreatorApiary::DispatchNetworkError(RequestIterator it,
210 int error_code) {
171 DispatchGoogleServiceAuthError( 211 DispatchGoogleServiceAuthError(
172 GoogleServiceAuthError::FromConnectionError(error_code)); 212 it, GoogleServiceAuthError::FromConnectionError(error_code));
173 } 213 }
174 214
175 void PermissionRequestCreatorApiary::DispatchGoogleServiceAuthError( 215 void PermissionRequestCreatorApiary::DispatchGoogleServiceAuthError(
216 RequestIterator it,
176 const GoogleServiceAuthError& error) { 217 const GoogleServiceAuthError& error) {
177 callback_.Run(); 218 (*it)->callback.Run();
178 callback_.Reset(); 219 requests_.erase(it);
179 } 220 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698