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