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/child_accounts/permission_request_creat
or_apiary.h" | 5 #include "chrome/browser/supervised_user/child_accounts/permission_request_creat
or_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 15 matching lines...) Expand all Loading... |
26 #include "net/url_request/url_request_status.h" | 26 #include "net/url_request/url_request_status.h" |
27 #include "url/gurl.h" | 27 #include "url/gurl.h" |
28 | 28 |
29 using net::URLFetcher; | 29 using net::URLFetcher; |
30 | 30 |
31 const char kApiUrl[] = | 31 const char kApiUrl[] = |
32 "https://www.googleapis.com/kidsmanagement/v1/people/me/permissionRequests"; | 32 "https://www.googleapis.com/kidsmanagement/v1/people/me/permissionRequests"; |
33 const char kApiScope[] = "https://www.googleapis.com/auth/kid.permission"; | 33 const char kApiScope[] = "https://www.googleapis.com/auth/kid.permission"; |
34 | 34 |
35 const int kNumRetries = 1; | 35 const int kNumRetries = 1; |
36 const char kNamespace[] = "PERMISSION_CHROME_URL"; | 36 |
| 37 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; |
| 38 |
| 39 // Request keys. |
| 40 const char kNamespaceKey[] = "namespace"; |
| 41 const char kObjectRefKey[] = "objectRef"; |
| 42 const char kStateKey[] = "state"; |
| 43 |
| 44 // Request values. |
| 45 const char kNamespaceURLRequest[] = "PERMISSION_CHROME_URL"; |
| 46 const char kNamespaceUpdateRequest[] = "PERMISSION_CHROME_CWS_ITEM_UPDATE"; |
37 const char kState[] = "PENDING"; | 47 const char kState[] = "PENDING"; |
38 | 48 |
| 49 // Response keys. |
39 const char kPermissionRequestKey[] = "permissionRequest"; | 50 const char kPermissionRequestKey[] = "permissionRequest"; |
40 const char kIdKey[] = "id"; | 51 const char kIdKey[] = "id"; |
41 | 52 |
42 static const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; | |
43 | |
44 struct PermissionRequestCreatorApiary::Request { | 53 struct PermissionRequestCreatorApiary::Request { |
45 Request(const GURL& url_requested, | 54 Request(const std::string& request_namespace, |
| 55 const std::string& object_ref, |
46 const SuccessCallback& callback, | 56 const SuccessCallback& callback, |
47 int url_fetcher_id); | 57 int url_fetcher_id); |
48 ~Request(); | 58 ~Request(); |
49 | 59 |
50 GURL url_requested; | 60 std::string request_namespace; |
| 61 std::string object_ref; |
51 SuccessCallback callback; | 62 SuccessCallback callback; |
52 scoped_ptr<OAuth2TokenService::Request> access_token_request; | 63 scoped_ptr<OAuth2TokenService::Request> access_token_request; |
53 std::string access_token; | 64 std::string access_token; |
54 bool access_token_expired; | 65 bool access_token_expired; |
55 int url_fetcher_id; | 66 int url_fetcher_id; |
56 scoped_ptr<net::URLFetcher> url_fetcher; | 67 scoped_ptr<URLFetcher> url_fetcher; |
57 }; | 68 }; |
58 | 69 |
59 PermissionRequestCreatorApiary::Request::Request( | 70 PermissionRequestCreatorApiary::Request::Request( |
60 const GURL& url_requested, | 71 const std::string& request_namespace, |
| 72 const std::string& object_ref, |
61 const SuccessCallback& callback, | 73 const SuccessCallback& callback, |
62 int url_fetcher_id) | 74 int url_fetcher_id) |
63 : url_requested(url_requested), | 75 : request_namespace(request_namespace), |
| 76 object_ref(object_ref), |
64 callback(callback), | 77 callback(callback), |
65 access_token_expired(false), | 78 access_token_expired(false), |
66 url_fetcher_id(url_fetcher_id) { | 79 url_fetcher_id(url_fetcher_id) { |
67 } | 80 } |
68 | 81 |
69 PermissionRequestCreatorApiary::Request::~Request() {} | 82 PermissionRequestCreatorApiary::Request::~Request() {} |
70 | 83 |
71 PermissionRequestCreatorApiary::PermissionRequestCreatorApiary( | 84 PermissionRequestCreatorApiary::PermissionRequestCreatorApiary( |
72 OAuth2TokenService* oauth2_token_service, | 85 OAuth2TokenService* oauth2_token_service, |
73 const std::string& account_id, | 86 const std::string& account_id, |
(...skipping 16 matching lines...) Expand all Loading... |
90 return make_scoped_ptr(new PermissionRequestCreatorApiary( | 103 return make_scoped_ptr(new PermissionRequestCreatorApiary( |
91 token_service, | 104 token_service, |
92 signin->GetAuthenticatedAccountId(), | 105 signin->GetAuthenticatedAccountId(), |
93 profile->GetRequestContext())); | 106 profile->GetRequestContext())); |
94 } | 107 } |
95 | 108 |
96 bool PermissionRequestCreatorApiary::IsEnabled() const { | 109 bool PermissionRequestCreatorApiary::IsEnabled() const { |
97 return true; | 110 return true; |
98 } | 111 } |
99 | 112 |
100 void PermissionRequestCreatorApiary::CreatePermissionRequest( | 113 void PermissionRequestCreatorApiary::CreateURLAccessRequest( |
101 const GURL& url_requested, | 114 const GURL& url_requested, |
102 const SuccessCallback& callback) { | 115 const SuccessCallback& callback) { |
103 requests_.push_back(new Request(url_requested, callback, url_fetcher_id_)); | 116 CreateRequest(kNamespaceURLRequest, url_requested.spec(), callback); |
104 StartFetching(requests_.back()); | 117 } |
| 118 |
| 119 void PermissionRequestCreatorApiary::CreateExtensionUpdateRequest( |
| 120 const std::string& extension_id, |
| 121 const SuccessCallback& callback) { |
| 122 CreateRequest(kNamespaceUpdateRequest, extension_id, callback); |
105 } | 123 } |
106 | 124 |
107 GURL PermissionRequestCreatorApiary::GetApiUrl() const { | 125 GURL PermissionRequestCreatorApiary::GetApiUrl() const { |
108 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 126 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
109 switches::kPermissionRequestApiUrl)) { | 127 switches::kPermissionRequestApiUrl)) { |
110 GURL url(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 128 GURL url(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
111 switches::kPermissionRequestApiUrl)); | 129 switches::kPermissionRequestApiUrl)); |
112 LOG_IF(WARNING, !url.is_valid()) | 130 LOG_IF(WARNING, !url.is_valid()) |
113 << "Got invalid URL for " << switches::kPermissionRequestApiUrl; | 131 << "Got invalid URL for " << switches::kPermissionRequestApiUrl; |
114 return url; | 132 return url; |
115 } else { | 133 } else { |
116 return GURL(kApiUrl); | 134 return GURL(kApiUrl); |
117 } | 135 } |
118 } | 136 } |
119 | 137 |
120 std::string PermissionRequestCreatorApiary::GetApiScope() const { | 138 std::string PermissionRequestCreatorApiary::GetApiScope() const { |
121 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 139 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
122 switches::kPermissionRequestApiScope)) { | 140 switches::kPermissionRequestApiScope)) { |
123 return base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 141 return base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
124 switches::kPermissionRequestApiScope); | 142 switches::kPermissionRequestApiScope); |
125 } else { | 143 } else { |
126 return kApiScope; | 144 return kApiScope; |
127 } | 145 } |
128 } | 146 } |
129 | 147 |
| 148 void PermissionRequestCreatorApiary::CreateRequest( |
| 149 const std::string& request_namespace, |
| 150 const std::string& object_ref, |
| 151 const SuccessCallback& callback) { |
| 152 requests_.push_back( |
| 153 new Request(request_namespace, object_ref, callback, url_fetcher_id_)); |
| 154 StartFetching(requests_.back()); |
| 155 } |
| 156 |
130 void PermissionRequestCreatorApiary::StartFetching(Request* request) { | 157 void PermissionRequestCreatorApiary::StartFetching(Request* request) { |
131 OAuth2TokenService::ScopeSet scopes; | 158 OAuth2TokenService::ScopeSet scopes; |
132 scopes.insert(GetApiScope()); | 159 scopes.insert(GetApiScope()); |
133 request->access_token_request = oauth2_token_service_->StartRequest( | 160 request->access_token_request = oauth2_token_service_->StartRequest( |
134 account_id_, scopes, this); | 161 account_id_, scopes, this); |
135 } | 162 } |
136 | 163 |
137 void PermissionRequestCreatorApiary::OnGetTokenSuccess( | 164 void PermissionRequestCreatorApiary::OnGetTokenSuccess( |
138 const OAuth2TokenService::Request* request, | 165 const OAuth2TokenService::Request* request, |
139 const std::string& access_token, | 166 const std::string& access_token, |
(...skipping 13 matching lines...) Expand all Loading... |
153 this)); | 180 this)); |
154 | 181 |
155 (*it)->url_fetcher->SetRequestContext(context_); | 182 (*it)->url_fetcher->SetRequestContext(context_); |
156 (*it)->url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 183 (*it)->url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
157 net::LOAD_DO_NOT_SAVE_COOKIES); | 184 net::LOAD_DO_NOT_SAVE_COOKIES); |
158 (*it)->url_fetcher->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); | 185 (*it)->url_fetcher->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); |
159 (*it)->url_fetcher->AddExtraRequestHeader( | 186 (*it)->url_fetcher->AddExtraRequestHeader( |
160 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str())); | 187 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str())); |
161 | 188 |
162 base::DictionaryValue dict; | 189 base::DictionaryValue dict; |
163 dict.SetStringWithoutPathExpansion("namespace", kNamespace); | 190 dict.SetStringWithoutPathExpansion(kNamespaceKey, (*it)->request_namespace); |
164 dict.SetStringWithoutPathExpansion("objectRef", (*it)->url_requested.spec()); | 191 dict.SetStringWithoutPathExpansion(kObjectRefKey, (*it)->object_ref); |
165 dict.SetStringWithoutPathExpansion("state", kState); | 192 dict.SetStringWithoutPathExpansion(kStateKey, kState); |
| 193 |
166 std::string body; | 194 std::string body; |
167 base::JSONWriter::Write(&dict, &body); | 195 base::JSONWriter::Write(&dict, &body); |
168 (*it)->url_fetcher->SetUploadData("application/json", body); | 196 (*it)->url_fetcher->SetUploadData("application/json", body); |
169 | 197 |
170 (*it)->url_fetcher->Start(); | 198 (*it)->url_fetcher->Start(); |
171 } | 199 } |
172 | 200 |
173 void PermissionRequestCreatorApiary::OnGetTokenFailure( | 201 void PermissionRequestCreatorApiary::OnGetTokenFailure( |
174 const OAuth2TokenService::Request* request, | 202 const OAuth2TokenService::Request* request, |
175 const GoogleServiceAuthError& error) { | 203 const GoogleServiceAuthError& error) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 it, GoogleServiceAuthError::FromConnectionError(error_code)); | 276 it, GoogleServiceAuthError::FromConnectionError(error_code)); |
249 } | 277 } |
250 | 278 |
251 void PermissionRequestCreatorApiary::DispatchGoogleServiceAuthError( | 279 void PermissionRequestCreatorApiary::DispatchGoogleServiceAuthError( |
252 RequestIterator it, | 280 RequestIterator it, |
253 const GoogleServiceAuthError& error) { | 281 const GoogleServiceAuthError& error) { |
254 VLOG(1) << "GoogleServiceAuthError: " << error.ToString(); | 282 VLOG(1) << "GoogleServiceAuthError: " << error.ToString(); |
255 (*it)->callback.Run(false); | 283 (*it)->callback.Run(false); |
256 requests_.erase(it); | 284 requests_.erase(it); |
257 } | 285 } |
OLD | NEW |