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

Side by Side Diff: chrome/browser/managed_mode/permission_request_creator.cc

Issue 288913003: Add permission request creator class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/managed_mode/permission_request_creator.h"
6
7 #include "base/callback.h"
8 #include "base/command_line.h"
9 #include "base/json/json_reader.h"
10 #include "base/json/json_writer.h"
11 #include "base/logging.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/values.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "google_apis/gaia/gaia_constants.h"
16 #include "google_apis/gaia/google_service_auth_error.h"
17 #include "google_apis/gaia/oauth2_api_call_flow.h"
18 #include "google_apis/gaia/oauth2_token_service.h"
19 #include "net/base/escape.h"
20 #include "net/base/load_flags.h"
21 #include "net/base/net_errors.h"
22 #include "net/http/http_status_code.h"
23 #include "net/url_request/url_fetcher.h"
24 #include "net/url_request/url_request_status.h"
25
26 using base::Time;
27 using net::URLFetcher;
28 using net::URLFetcherDelegate;
29 using net::URLRequestContextGetter;
30
31 namespace {
32
33 const int kNumRetries = 1;
34 const char kIdKey[] = "id";
35 const char kNamespace[] = "CHROME";
36 const char kState[] = "PENDING";
37
38 static const char kAuthorizationHeaderFormat[] =
39 "Authorization: Bearer %s";
40
41 class PermissionRequestCreatorImpl
42 : public PermissionRequestCreator,
43 public OAuth2TokenService::Consumer,
44 public URLFetcherDelegate {
45 public:
46 PermissionRequestCreatorImpl(OAuth2TokenService* oauth2_token_service,
47 const std::string& account_id,
Bernhard Bauer 2014/05/15 13:56:40 Nit: alignment
Adrian Kuegel 2014/05/15 14:35:32 Done.
48 URLRequestContextGetter* context);
49 virtual ~PermissionRequestCreatorImpl();
50
51 // PermissionRequestCreator implementation:
52 virtual void CreatePermissionRequest(
53 const std::string& url_requested,
54 const PermissionRequestCallback& callback) OVERRIDE;
55
56 protected:
57 // OAuth2TokenService::Consumer implementation:
58 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
59 const std::string& access_token,
60 const Time& expiration_time) OVERRIDE;
61 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
62 const GoogleServiceAuthError& error) OVERRIDE;
63
64 // net::URLFetcherDelegate implementation.
65 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
66
67 private:
68 // Requests an access token, which is the first thing we need. This is where
69 // we restart when the returned access token has expired.
70 void StartFetching();
71
72 void DispatchNetworkError(int error_code);
73 void DispatchGoogleServiceAuthError(const GoogleServiceAuthError& error,
74 const std::string& token);
75 OAuth2TokenService* oauth2_token_service_;
76 std::string account_id_;
77 PermissionRequestCallback callback_;
78 URLRequestContextGetter* context_;
79 std::string url_requested_;
80 scoped_ptr<OAuth2TokenService::Request> access_token_request_;
81 std::string access_token_;
82 bool access_token_expired_;
83 scoped_ptr<URLFetcher> url_fetcher_;
84 };
85
86 PermissionRequestCreatorImpl::PermissionRequestCreatorImpl(
87 OAuth2TokenService* oauth2_token_service,
88 const std::string& account_id,
89 URLRequestContextGetter* context)
90 : OAuth2TokenService::Consumer("managed_user"),
91 oauth2_token_service_(oauth2_token_service),
92 account_id_(account_id),
93 context_(context),
94 access_token_expired_(false) {}
95
96 PermissionRequestCreatorImpl::~PermissionRequestCreatorImpl() {}
97
98 void PermissionRequestCreatorImpl::CreatePermissionRequest(
99 const std::string& url_requested,
100 const PermissionRequestCallback& callback) {
101 url_requested_ = url_requested;
102 callback_ = callback;
103 StartFetching();
104 }
105
106 void PermissionRequestCreatorImpl::StartFetching() {
107 OAuth2TokenService::ScopeSet scopes;
108 scopes.insert(GaiaConstants::kChromeSyncManagedOAuth2Scope);
109 access_token_request_ = oauth2_token_service_->StartRequest(
110 account_id_, scopes, this);
111 }
112
113 void PermissionRequestCreatorImpl::OnGetTokenSuccess(
114 const OAuth2TokenService::Request* request,
115 const std::string& access_token,
116 const Time& expiration_time) {
117 DCHECK_EQ(access_token_request_.get(), request);
118 access_token_ = access_token;
119 GURL url(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
120 switches::kPermissionRequestAPI));
121 const int id = 0;
122
123 url_fetcher_.reset(URLFetcher::Create(id, url, URLFetcher::POST, this));
124
125 url_fetcher_->SetRequestContext(context_);
126 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
127 net::LOAD_DO_NOT_SAVE_COOKIES);
128 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries);
129 url_fetcher_->AddExtraRequestHeader(
130 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str()));
131
132 base::DictionaryValue dict;
133 dict.SetStringWithoutPathExpansion("namespace", kNamespace);
134 dict.SetStringWithoutPathExpansion("objectRef", url_requested_);
135 dict.SetStringWithoutPathExpansion("state", kState);
136 std::string body;
137 base::JSONWriter::Write(&dict, &body);
138 url_fetcher_->SetUploadData("application/json", body);
139
140 url_fetcher_->Start();
141 }
142
143 void PermissionRequestCreatorImpl::OnGetTokenFailure(
144 const OAuth2TokenService::Request* request,
145 const GoogleServiceAuthError& error) {
146 DCHECK_EQ(access_token_request_.get(), request);
147 callback_.Run(error);
148 callback_.Reset();
149 }
150
151 void PermissionRequestCreatorImpl::OnURLFetchComplete(
152 const URLFetcher* source) {
153 const net::URLRequestStatus& status = source->GetStatus();
154 if (!status.is_success()) {
155 DispatchNetworkError(status.error());
156 return;
157 }
158
159 int response_code = source->GetResponseCode();
160 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) {
161 access_token_expired_ = true;
162 OAuth2TokenService::ScopeSet scopes;
163 scopes.insert(GaiaConstants::kChromeSyncManagedOAuth2Scope);
164 oauth2_token_service_->InvalidateToken(account_id_, scopes, access_token_);
165 StartFetching();
166 return;
167 }
168
169 if (response_code != net::HTTP_OK) {
170 DLOG(WARNING) << "HTTP error " << response_code;
171 DispatchGoogleServiceAuthError(
172 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED),
173 std::string());
174 return;
175 }
176
177 std::string response_body;
178 source->GetResponseAsString(&response_body);
179 scoped_ptr<base::Value> value(base::JSONReader::Read(response_body));
180 base::DictionaryValue* dict = NULL;
181 if (!value.get() || !value->GetAsDictionary(&dict)) {
Bernhard Bauer 2014/05/15 13:56:40 scoped_ptr has an implicit conversion to boolean,
Adrian Kuegel 2014/05/15 14:35:32 Done.
182 DispatchNetworkError(net::ERR_INVALID_RESPONSE);
183 return;
184 }
185 std::string id;
186 if (!dict->GetString(kIdKey, &id)) {
187 DispatchNetworkError(net::ERR_INVALID_RESPONSE);
188 return;
189 }
190 }
191
192 void PermissionRequestCreatorImpl::DispatchNetworkError(int error_code) {
193 DispatchGoogleServiceAuthError(
194 GoogleServiceAuthError::FromConnectionError(error_code), std::string());
195 }
196
197 void PermissionRequestCreatorImpl::DispatchGoogleServiceAuthError(
198 const GoogleServiceAuthError& error,
199 const std::string& token) {
200 callback_.Run(error);
201 callback_.Reset();
202 }
203
204 } // namespace
205
206 // static
207 scoped_ptr<PermissionRequestCreator>
208 PermissionRequestCreator::Create(OAuth2TokenService* oauth2_token_service,
209 const std::string& account_id,
Bernhard Bauer 2014/05/15 13:56:40 Nit: alignment
Adrian Kuegel 2014/05/15 14:35:32 Done.
210 URLRequestContextGetter* context) {
211 scoped_ptr<PermissionRequestCreator> creator(
212 new PermissionRequestCreatorImpl(oauth2_token_service, account_id,
213 context));
214 return creator.Pass();
215 }
216
217 PermissionRequestCreator::~PermissionRequestCreator() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698