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