| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 16 #include "chrome/browser/signin/signin_manager_factory.h" | 17 #include "chrome/browser/signin/signin_manager_factory.h" |
| 17 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 18 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 19 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 19 #include "components/signin/core/browser/signin_manager.h" | 20 #include "components/signin/core/browser/signin_manager.h" |
| 20 #include "components/signin/core/browser/signin_manager_base.h" | 21 #include "components/signin/core/browser/signin_manager_base.h" |
| 21 #include "google_apis/gaia/google_service_auth_error.h" | 22 #include "google_apis/gaia/google_service_auth_error.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 struct PermissionRequestCreatorApiary::Request { | 54 struct PermissionRequestCreatorApiary::Request { |
| 54 Request(const std::string& request_type, | 55 Request(const std::string& request_type, |
| 55 const std::string& object_ref, | 56 const std::string& object_ref, |
| 56 const SuccessCallback& callback, | 57 const SuccessCallback& callback, |
| 57 int url_fetcher_id); | 58 int url_fetcher_id); |
| 58 ~Request(); | 59 ~Request(); |
| 59 | 60 |
| 60 std::string request_type; | 61 std::string request_type; |
| 61 std::string object_ref; | 62 std::string object_ref; |
| 62 SuccessCallback callback; | 63 SuccessCallback callback; |
| 63 scoped_ptr<OAuth2TokenService::Request> access_token_request; | 64 std::unique_ptr<OAuth2TokenService::Request> access_token_request; |
| 64 std::string access_token; | 65 std::string access_token; |
| 65 bool access_token_expired; | 66 bool access_token_expired; |
| 66 int url_fetcher_id; | 67 int url_fetcher_id; |
| 67 scoped_ptr<URLFetcher> url_fetcher; | 68 std::unique_ptr<URLFetcher> url_fetcher; |
| 68 }; | 69 }; |
| 69 | 70 |
| 70 PermissionRequestCreatorApiary::Request::Request( | 71 PermissionRequestCreatorApiary::Request::Request( |
| 71 const std::string& request_type, | 72 const std::string& request_type, |
| 72 const std::string& object_ref, | 73 const std::string& object_ref, |
| 73 const SuccessCallback& callback, | 74 const SuccessCallback& callback, |
| 74 int url_fetcher_id) | 75 int url_fetcher_id) |
| 75 : request_type(request_type), | 76 : request_type(request_type), |
| 76 object_ref(object_ref), | 77 object_ref(object_ref), |
| 77 callback(callback), | 78 callback(callback), |
| (...skipping 10 matching lines...) Expand all Loading... |
| 88 : OAuth2TokenService::Consumer("permissions_creator"), | 89 : OAuth2TokenService::Consumer("permissions_creator"), |
| 89 oauth2_token_service_(oauth2_token_service), | 90 oauth2_token_service_(oauth2_token_service), |
| 90 account_id_(account_id), | 91 account_id_(account_id), |
| 91 context_(context), | 92 context_(context), |
| 92 url_fetcher_id_(0) { | 93 url_fetcher_id_(0) { |
| 93 } | 94 } |
| 94 | 95 |
| 95 PermissionRequestCreatorApiary::~PermissionRequestCreatorApiary() {} | 96 PermissionRequestCreatorApiary::~PermissionRequestCreatorApiary() {} |
| 96 | 97 |
| 97 // static | 98 // static |
| 98 scoped_ptr<PermissionRequestCreator> | 99 std::unique_ptr<PermissionRequestCreator> |
| 99 PermissionRequestCreatorApiary::CreateWithProfile(Profile* profile) { | 100 PermissionRequestCreatorApiary::CreateWithProfile(Profile* profile) { |
| 100 ProfileOAuth2TokenService* token_service = | 101 ProfileOAuth2TokenService* token_service = |
| 101 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 102 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 102 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); | 103 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); |
| 103 return make_scoped_ptr(new PermissionRequestCreatorApiary( | 104 return base::WrapUnique(new PermissionRequestCreatorApiary( |
| 104 token_service, | 105 token_service, signin->GetAuthenticatedAccountId(), |
| 105 signin->GetAuthenticatedAccountId(), | |
| 106 profile->GetRequestContext())); | 106 profile->GetRequestContext())); |
| 107 } | 107 } |
| 108 | 108 |
| 109 bool PermissionRequestCreatorApiary::IsEnabled() const { | 109 bool PermissionRequestCreatorApiary::IsEnabled() const { |
| 110 return true; | 110 return true; |
| 111 } | 111 } |
| 112 | 112 |
| 113 void PermissionRequestCreatorApiary::CreateURLAccessRequest( | 113 void PermissionRequestCreatorApiary::CreateURLAccessRequest( |
| 114 const GURL& url_requested, | 114 const GURL& url_requested, |
| 115 const SuccessCallback& callback) { | 115 const SuccessCallback& callback) { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 } | 239 } |
| 240 | 240 |
| 241 if (response_code != net::HTTP_OK) { | 241 if (response_code != net::HTTP_OK) { |
| 242 LOG(WARNING) << "HTTP error " << response_code; | 242 LOG(WARNING) << "HTTP error " << response_code; |
| 243 DispatchResult(it, false); | 243 DispatchResult(it, false); |
| 244 return; | 244 return; |
| 245 } | 245 } |
| 246 | 246 |
| 247 std::string response_body; | 247 std::string response_body; |
| 248 source->GetResponseAsString(&response_body); | 248 source->GetResponseAsString(&response_body); |
| 249 scoped_ptr<base::Value> value = base::JSONReader::Read(response_body); | 249 std::unique_ptr<base::Value> value = base::JSONReader::Read(response_body); |
| 250 base::DictionaryValue* dict = NULL; | 250 base::DictionaryValue* dict = NULL; |
| 251 if (!value || !value->GetAsDictionary(&dict)) { | 251 if (!value || !value->GetAsDictionary(&dict)) { |
| 252 LOG(WARNING) << "Invalid top-level dictionary"; | 252 LOG(WARNING) << "Invalid top-level dictionary"; |
| 253 DispatchResult(it, false); | 253 DispatchResult(it, false); |
| 254 return; | 254 return; |
| 255 } | 255 } |
| 256 base::DictionaryValue* permission_dict = NULL; | 256 base::DictionaryValue* permission_dict = NULL; |
| 257 if (!dict->GetDictionary(kPermissionRequestKey, &permission_dict)) { | 257 if (!dict->GetDictionary(kPermissionRequestKey, &permission_dict)) { |
| 258 LOG(WARNING) << "Permission request not found"; | 258 LOG(WARNING) << "Permission request not found"; |
| 259 DispatchResult(it, false); | 259 DispatchResult(it, false); |
| 260 return; | 260 return; |
| 261 } | 261 } |
| 262 std::string id; | 262 std::string id; |
| 263 if (!permission_dict->GetString(kIdKey, &id)) { | 263 if (!permission_dict->GetString(kIdKey, &id)) { |
| 264 LOG(WARNING) << "ID not found"; | 264 LOG(WARNING) << "ID not found"; |
| 265 DispatchResult(it, false); | 265 DispatchResult(it, false); |
| 266 return; | 266 return; |
| 267 } | 267 } |
| 268 DispatchResult(it, true); | 268 DispatchResult(it, true); |
| 269 } | 269 } |
| 270 | 270 |
| 271 void PermissionRequestCreatorApiary::DispatchResult(RequestIterator it, | 271 void PermissionRequestCreatorApiary::DispatchResult(RequestIterator it, |
| 272 bool success) { | 272 bool success) { |
| 273 (*it)->callback.Run(success); | 273 (*it)->callback.Run(success); |
| 274 requests_.erase(it); | 274 requests_.erase(it); |
| 275 } | 275 } |
| OLD | NEW |