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 18 matching lines...) Expand all Loading... |
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 struct PermissionRequestCreatorApiary::Request { | 38 struct PermissionRequestCreatorApiary::Request { |
39 Request(const GURL& url_requested, const base::Closure& callback); | 39 Request(const GURL& url_requested, |
| 40 const base::Closure& callback, |
| 41 int url_fetcher_id); |
40 ~Request(); | 42 ~Request(); |
41 | 43 |
42 GURL url_requested; | 44 GURL url_requested; |
43 base::Closure callback; | 45 base::Closure callback; |
44 scoped_ptr<OAuth2TokenService::Request> access_token_request; | 46 scoped_ptr<OAuth2TokenService::Request> access_token_request; |
45 std::string access_token; | 47 std::string access_token; |
46 bool access_token_expired; | 48 bool access_token_expired; |
| 49 int url_fetcher_id; |
47 scoped_ptr<net::URLFetcher> url_fetcher; | 50 scoped_ptr<net::URLFetcher> url_fetcher; |
48 }; | 51 }; |
49 | 52 |
50 PermissionRequestCreatorApiary::Request::Request(const GURL& url_requested, | 53 PermissionRequestCreatorApiary::Request::Request(const GURL& url_requested, |
51 const base::Closure& callback) | 54 const base::Closure& callback, |
| 55 int url_fetcher_id) |
52 : url_requested(url_requested), | 56 : url_requested(url_requested), |
53 callback(callback), | 57 callback(callback), |
54 access_token_expired(false) { | 58 access_token_expired(false), |
| 59 url_fetcher_id(url_fetcher_id) { |
55 } | 60 } |
56 | 61 |
57 PermissionRequestCreatorApiary::Request::~Request() {} | 62 PermissionRequestCreatorApiary::Request::~Request() {} |
58 | 63 |
59 PermissionRequestCreatorApiary::PermissionRequestCreatorApiary( | 64 PermissionRequestCreatorApiary::PermissionRequestCreatorApiary( |
| 65 const GURL& api_url, |
60 OAuth2TokenService* oauth2_token_service, | 66 OAuth2TokenService* oauth2_token_service, |
61 scoped_ptr<SupervisedUserSigninManagerWrapper> signin_wrapper, | 67 scoped_ptr<SupervisedUserSigninManagerWrapper> signin_wrapper, |
62 net::URLRequestContextGetter* context) | 68 net::URLRequestContextGetter* context) |
63 : OAuth2TokenService::Consumer("permissions_creator"), | 69 : OAuth2TokenService::Consumer("permissions_creator"), |
| 70 url_(api_url), |
64 oauth2_token_service_(oauth2_token_service), | 71 oauth2_token_service_(oauth2_token_service), |
65 signin_wrapper_(signin_wrapper.Pass()), | 72 signin_wrapper_(signin_wrapper.Pass()), |
66 context_(context) {} | 73 context_(context), |
| 74 url_fetcher_id_(0) { |
| 75 DCHECK(url_.is_valid()); |
| 76 } |
67 | 77 |
68 PermissionRequestCreatorApiary::~PermissionRequestCreatorApiary() {} | 78 PermissionRequestCreatorApiary::~PermissionRequestCreatorApiary() {} |
69 | 79 |
70 // static | 80 // static |
71 scoped_ptr<PermissionRequestCreator> | 81 scoped_ptr<PermissionRequestCreator> |
72 PermissionRequestCreatorApiary::CreateWithProfile(Profile* profile) { | 82 PermissionRequestCreatorApiary::CreateWithProfile(const GURL& api_url, |
| 83 Profile* profile) { |
73 ProfileOAuth2TokenService* token_service = | 84 ProfileOAuth2TokenService* token_service = |
74 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 85 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
75 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); | 86 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); |
76 scoped_ptr<SupervisedUserSigninManagerWrapper> signin_wrapper( | 87 scoped_ptr<SupervisedUserSigninManagerWrapper> signin_wrapper( |
77 new SupervisedUserSigninManagerWrapper(profile, signin)); | 88 new SupervisedUserSigninManagerWrapper(profile, signin)); |
78 scoped_ptr<PermissionRequestCreator> creator( | 89 scoped_ptr<PermissionRequestCreator> creator( |
79 new PermissionRequestCreatorApiary( | 90 new PermissionRequestCreatorApiary(api_url, |
80 token_service, signin_wrapper.Pass(), profile->GetRequestContext())); | 91 token_service, |
| 92 signin_wrapper.Pass(), |
| 93 profile->GetRequestContext())); |
81 return creator.Pass(); | 94 return creator.Pass(); |
82 } | 95 } |
83 | 96 |
84 void PermissionRequestCreatorApiary::CreatePermissionRequest( | 97 void PermissionRequestCreatorApiary::CreatePermissionRequest( |
85 const GURL& url_requested, | 98 const GURL& url_requested, |
86 const base::Closure& callback) { | 99 const base::Closure& callback) { |
87 requests_.push_back(new Request(url_requested, callback)); | 100 requests_.push_back(new Request(url_requested, callback, url_fetcher_id_)); |
88 StartFetching(requests_.back()); | 101 StartFetching(requests_.back()); |
89 } | 102 } |
90 | 103 |
91 std::string PermissionRequestCreatorApiary::GetApiScopeToUse() const { | 104 std::string PermissionRequestCreatorApiary::GetApiScopeToUse() const { |
92 if (CommandLine::ForCurrentProcess()->HasSwitch( | 105 if (CommandLine::ForCurrentProcess()->HasSwitch( |
93 switches::kPermissionRequestApiScope)) { | 106 switches::kPermissionRequestApiScope)) { |
94 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 107 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
95 switches::kPermissionRequestApiScope); | 108 switches::kPermissionRequestApiScope); |
96 } else { | 109 } else { |
97 return signin_wrapper_->GetSyncScopeToUse(); | 110 return signin_wrapper_->GetSyncScopeToUse(); |
(...skipping 12 matching lines...) Expand all Loading... |
110 const std::string& access_token, | 123 const std::string& access_token, |
111 const base::Time& expiration_time) { | 124 const base::Time& expiration_time) { |
112 RequestIterator it = requests_.begin(); | 125 RequestIterator it = requests_.begin(); |
113 while (it != requests_.end()) { | 126 while (it != requests_.end()) { |
114 if (request == (*it)->access_token_request.get()) | 127 if (request == (*it)->access_token_request.get()) |
115 break; | 128 break; |
116 ++it; | 129 ++it; |
117 } | 130 } |
118 DCHECK(it != requests_.end()); | 131 DCHECK(it != requests_.end()); |
119 (*it)->access_token = access_token; | 132 (*it)->access_token = access_token; |
120 GURL url(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
121 switches::kPermissionRequestApiUrl)); | |
122 const int id = 0; | |
123 | 133 |
124 (*it)->url_fetcher.reset(URLFetcher::Create(id, url, URLFetcher::POST, this)); | 134 (*it)->url_fetcher.reset( |
| 135 URLFetcher::Create((*it)->url_fetcher_id, url_, URLFetcher::POST, this)); |
125 | 136 |
126 (*it)->url_fetcher->SetRequestContext(context_); | 137 (*it)->url_fetcher->SetRequestContext(context_); |
127 (*it)->url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 138 (*it)->url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
128 net::LOAD_DO_NOT_SAVE_COOKIES); | 139 net::LOAD_DO_NOT_SAVE_COOKIES); |
129 (*it)->url_fetcher->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); | 140 (*it)->url_fetcher->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); |
130 (*it)->url_fetcher->AddExtraRequestHeader( | 141 (*it)->url_fetcher->AddExtraRequestHeader( |
131 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str())); | 142 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str())); |
132 | 143 |
133 base::DictionaryValue dict; | 144 base::DictionaryValue dict; |
134 dict.SetStringWithoutPathExpansion("namespace", kNamespace); | 145 dict.SetStringWithoutPathExpansion("namespace", kNamespace); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 DispatchGoogleServiceAuthError( | 222 DispatchGoogleServiceAuthError( |
212 it, GoogleServiceAuthError::FromConnectionError(error_code)); | 223 it, GoogleServiceAuthError::FromConnectionError(error_code)); |
213 } | 224 } |
214 | 225 |
215 void PermissionRequestCreatorApiary::DispatchGoogleServiceAuthError( | 226 void PermissionRequestCreatorApiary::DispatchGoogleServiceAuthError( |
216 RequestIterator it, | 227 RequestIterator it, |
217 const GoogleServiceAuthError& error) { | 228 const GoogleServiceAuthError& error) { |
218 (*it)->callback.Run(); | 229 (*it)->callback.Run(); |
219 requests_.erase(it); | 230 requests_.erase(it); |
220 } | 231 } |
OLD | NEW |