OLD | NEW |
---|---|
(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_apiary.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/browser/profiles/profile.h" | |
15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
16 #include "chrome/browser/signin/signin_manager_factory.h" | |
17 #include "chrome/browser/sync/managed_user_signin_manager_wrapper.h" | |
18 #include "chrome/common/chrome_switches.h" | |
19 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
20 #include "components/signin/core/browser/signin_manager.h" | |
21 #include "components/signin/core/browser/signin_manager_base.h" | |
22 #include "google_apis/gaia/google_service_auth_error.h" | |
23 #include "net/base/load_flags.h" | |
24 #include "net/base/net_errors.h" | |
25 #include "net/http/http_status_code.h" | |
26 #include "net/url_request/url_fetcher.h" | |
27 #include "net/url_request/url_request_status.h" | |
28 | |
29 using net::URLFetcher; | |
30 | |
31 const int kNumRetries = 1; | |
32 const char kIdKey[] = "id"; | |
33 const char kNamespace[] = "CHROME"; | |
34 const char kState[] = "PENDING"; | |
35 | |
36 static const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; | |
37 | |
38 PermissionRequestCreatorApiary::PermissionRequestCreatorApiary( | |
39 OAuth2TokenService* oauth2_token_service, | |
40 scoped_ptr<ManagedUserSigninManagerWrapper> signin_wrapper, | |
41 net::URLRequestContextGetter* context) | |
42 : OAuth2TokenService::Consumer("permissions_creator"), | |
43 oauth2_token_service_(oauth2_token_service), | |
44 signin_wrapper_(signin_wrapper.Pass()), | |
45 context_(context), | |
46 access_token_expired_(false) {} | |
47 | |
48 PermissionRequestCreatorApiary::~PermissionRequestCreatorApiary() {} | |
49 | |
50 // static | |
51 scoped_ptr<PermissionRequestCreator> | |
52 PermissionRequestCreatorApiary::CreateWithProfile(Profile* profile) { | |
53 ProfileOAuth2TokenService* token_service = | |
54 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
55 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); | |
56 scoped_ptr<ManagedUserSigninManagerWrapper> signin_wrapper( | |
57 new ManagedUserSigninManagerWrapper(profile, signin)); | |
58 scoped_ptr<PermissionRequestCreator> creator( | |
59 new PermissionRequestCreatorApiary(token_service, | |
60 signin_wrapper.Pass(), | |
Bernhard Bauer
2014/05/20 09:04:04
Nit: align
Adrian Kuegel
2014/05/20 09:15:44
Thanks for catching. Just to be sure, I just ran g
Bernhard Bauer
2014/05/20 09:21:41
I am actively not going to have an opinion on this
| |
61 profile->GetRequestContext())); | |
62 return creator.Pass(); | |
63 } | |
64 | |
65 void PermissionRequestCreatorApiary::CreatePermissionRequest( | |
66 const std::string& url_requested, | |
67 const base::Closure& callback) { | |
68 url_requested_ = url_requested; | |
69 callback_ = callback; | |
70 StartFetching(); | |
71 } | |
72 | |
73 void PermissionRequestCreatorApiary::StartFetching() { | |
74 OAuth2TokenService::ScopeSet scopes; | |
75 scopes.insert(signin_wrapper_->GetSyncScopeToUse()); | |
76 access_token_request_ = oauth2_token_service_->StartRequest( | |
77 signin_wrapper_->GetAccountIdToUse(), scopes, this); | |
78 } | |
79 | |
80 void PermissionRequestCreatorApiary::OnGetTokenSuccess( | |
81 const OAuth2TokenService::Request* request, | |
82 const std::string& access_token, | |
83 const base::Time& expiration_time) { | |
84 DCHECK_EQ(access_token_request_.get(), request); | |
85 access_token_ = access_token; | |
86 GURL url(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
87 switches::kPermissionRequestApiUrl)); | |
88 const int id = 0; | |
89 | |
90 url_fetcher_.reset(URLFetcher::Create(id, url, URLFetcher::POST, this)); | |
91 | |
92 url_fetcher_->SetRequestContext(context_); | |
93 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
94 net::LOAD_DO_NOT_SAVE_COOKIES); | |
95 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); | |
96 url_fetcher_->AddExtraRequestHeader( | |
97 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str())); | |
98 | |
99 base::DictionaryValue dict; | |
100 dict.SetStringWithoutPathExpansion("namespace", kNamespace); | |
101 dict.SetStringWithoutPathExpansion("objectRef", url_requested_); | |
102 dict.SetStringWithoutPathExpansion("state", kState); | |
103 std::string body; | |
104 base::JSONWriter::Write(&dict, &body); | |
105 url_fetcher_->SetUploadData("application/json", body); | |
106 | |
107 url_fetcher_->Start(); | |
108 } | |
109 | |
110 void PermissionRequestCreatorApiary::OnGetTokenFailure( | |
111 const OAuth2TokenService::Request* request, | |
112 const GoogleServiceAuthError& error) { | |
113 DCHECK_EQ(access_token_request_.get(), request); | |
114 callback_.Run(); | |
115 callback_.Reset(); | |
116 } | |
117 | |
118 void PermissionRequestCreatorApiary::OnURLFetchComplete( | |
119 const URLFetcher* source) { | |
120 const net::URLRequestStatus& status = source->GetStatus(); | |
121 if (!status.is_success()) { | |
122 DispatchNetworkError(status.error()); | |
123 return; | |
124 } | |
125 | |
126 int response_code = source->GetResponseCode(); | |
127 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) { | |
128 access_token_expired_ = true; | |
129 OAuth2TokenService::ScopeSet scopes; | |
130 scopes.insert(signin_wrapper_->GetSyncScopeToUse()); | |
131 oauth2_token_service_->InvalidateToken( | |
132 signin_wrapper_->GetAccountIdToUse(), scopes, access_token_); | |
133 StartFetching(); | |
134 return; | |
135 } | |
136 | |
137 if (response_code != net::HTTP_OK) { | |
138 DLOG(WARNING) << "HTTP error " << response_code; | |
139 DispatchGoogleServiceAuthError( | |
140 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED)); | |
141 return; | |
142 } | |
143 | |
144 std::string response_body; | |
145 source->GetResponseAsString(&response_body); | |
146 scoped_ptr<base::Value> value(base::JSONReader::Read(response_body)); | |
147 base::DictionaryValue* dict = NULL; | |
148 if (!value || !value->GetAsDictionary(&dict)) { | |
149 DispatchNetworkError(net::ERR_INVALID_RESPONSE); | |
150 return; | |
151 } | |
152 std::string id; | |
153 if (!dict->GetString(kIdKey, &id)) { | |
154 DispatchNetworkError(net::ERR_INVALID_RESPONSE); | |
155 return; | |
156 } | |
157 callback_.Run(); | |
158 callback_.Reset(); | |
159 } | |
160 | |
161 void PermissionRequestCreatorApiary::DispatchNetworkError(int error_code) { | |
162 DispatchGoogleServiceAuthError( | |
163 GoogleServiceAuthError::FromConnectionError(error_code)); | |
164 } | |
165 | |
166 void PermissionRequestCreatorApiary::DispatchGoogleServiceAuthError( | |
167 const GoogleServiceAuthError& error) { | |
168 callback_.Run(); | |
169 callback_.Reset(); | |
170 } | |
OLD | NEW |