OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/api/push_messaging/push_messaging_api.h" | 5 #include "chrome/browser/extensions/api/push_messaging/push_messaging_api.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
15 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler.h" | 15 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler.h" |
16 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
17 #include "chrome/browser/extensions/token_cache/token_cache_service.h" | 17 #include "chrome/browser/extensions/token_cache/token_cache_service.h" |
18 #include "chrome/browser/extensions/token_cache/token_cache_service_factory.h" | 18 #include "chrome/browser/extensions/token_cache/token_cache_service_factory.h" |
| 19 #include "chrome/browser/invalidation/invalidation_auth_provider.h" |
19 #include "chrome/browser/invalidation/invalidation_service.h" | 20 #include "chrome/browser/invalidation/invalidation_service.h" |
20 #include "chrome/browser/invalidation/invalidation_service_factory.h" | 21 #include "chrome/browser/invalidation/invalidation_service_factory.h" |
21 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 23 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
23 #include "chrome/browser/signin/signin_manager.h" | 24 #include "chrome/browser/signin/signin_manager.h" |
24 #include "chrome/browser/signin/signin_manager_factory.h" | 25 #include "chrome/browser/signin/signin_manager_factory.h" |
25 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | |
26 #include "chrome/common/extensions/api/push_messaging.h" | 26 #include "chrome/common/extensions/api/push_messaging.h" |
27 #include "components/signin/core/profile_oauth2_token_service.h" | 27 #include "components/signin/core/profile_oauth2_token_service.h" |
28 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
29 #include "content/public/browser/notification_details.h" | 29 #include "content/public/browser/notification_details.h" |
30 #include "content/public/browser/notification_source.h" | 30 #include "content/public/browser/notification_source.h" |
31 #include "extensions/browser/event_router.h" | 31 #include "extensions/browser/event_router.h" |
32 #include "extensions/browser/extension_system.h" | 32 #include "extensions/browser/extension_system.h" |
33 #include "extensions/browser/extension_system_provider.h" | 33 #include "extensions/browser/extension_system_provider.h" |
34 #include "extensions/browser/extensions_browser_client.h" | 34 #include "extensions/browser/extensions_browser_client.h" |
35 #include "extensions/common/extension.h" | 35 #include "extensions/common/extension.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 EXTENSION_FUNCTION_VALIDATE(params.get()); | 96 EXTENSION_FUNCTION_VALIDATE(params.get()); |
97 | 97 |
98 if (params && params->interactive) { | 98 if (params && params->interactive) { |
99 interactive_ = *params->interactive; | 99 interactive_ = *params->interactive; |
100 } | 100 } |
101 | 101 |
102 // Balanced in ReportResult() | 102 // Balanced in ReportResult() |
103 AddRef(); | 103 AddRef(); |
104 | 104 |
105 if (!IsUserLoggedIn()) { | 105 if (!IsUserLoggedIn()) { |
106 if (interactive_) { | 106 invalidation::InvalidationAuthProvider* auth_provider = |
107 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()) | 107 GetInvalidationAuthProvider(); |
108 ->AddObserver(this); | 108 if (interactive_ && auth_provider->ShowLoginUI()) { |
109 LoginUIServiceFactory::GetForProfile(GetProfile())->ShowLoginPopup(); | 109 auth_provider->GetTokenService()->AddObserver(this); |
110 return true; | 110 return true; |
111 } else { | 111 } else { |
112 error_ = kUserNotSignedIn; | 112 error_ = kUserNotSignedIn; |
113 ReportResult(std::string(), error_); | 113 ReportResult(std::string(), error_); |
114 return false; | 114 return false; |
115 } | 115 } |
116 } | 116 } |
117 | 117 |
118 DVLOG(2) << "Logged in profile name: " << GetProfile()->GetProfileName(); | 118 DVLOG(2) << "Logged in profile name: " << GetProfile()->GetProfileName(); |
119 | 119 |
120 StartAccessTokenFetch(); | 120 StartAccessTokenFetch(); |
121 return true; | 121 return true; |
122 } | 122 } |
123 | 123 |
124 void PushMessagingGetChannelIdFunction::StartAccessTokenFetch() { | 124 void PushMessagingGetChannelIdFunction::StartAccessTokenFetch() { |
125 std::vector<std::string> scope_vector = | 125 std::vector<std::string> scope_vector = |
126 extensions::ObfuscatedGaiaIdFetcher::GetScopes(); | 126 extensions::ObfuscatedGaiaIdFetcher::GetScopes(); |
127 OAuth2TokenService::ScopeSet scopes(scope_vector.begin(), scope_vector.end()); | 127 OAuth2TokenService::ScopeSet scopes(scope_vector.begin(), scope_vector.end()); |
128 ProfileOAuth2TokenService* token_service = | 128 invalidation::InvalidationAuthProvider* auth_provider = |
129 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()); | 129 GetInvalidationAuthProvider(); |
130 SigninManagerBase* signin_manager = | 130 fetcher_access_token_request_ = |
131 SigninManagerFactory::GetForProfile(GetProfile()); | 131 auth_provider->GetTokenService()->StartRequest( |
132 fetcher_access_token_request_ = token_service->StartRequest( | 132 auth_provider->GetAccountId(), scopes, this); |
133 signin_manager->GetAuthenticatedAccountId(), scopes, this); | |
134 } | 133 } |
135 | 134 |
136 void PushMessagingGetChannelIdFunction::OnRefreshTokenAvailable( | 135 void PushMessagingGetChannelIdFunction::OnRefreshTokenAvailable( |
137 const std::string& account_id) { | 136 const std::string& account_id) { |
138 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()) | 137 GetInvalidationAuthProvider()->GetTokenService()->RemoveObserver(this); |
139 ->RemoveObserver(this); | |
140 DVLOG(2) << "Newly logged in: " << GetProfile()->GetProfileName(); | 138 DVLOG(2) << "Newly logged in: " << GetProfile()->GetProfileName(); |
141 StartAccessTokenFetch(); | 139 StartAccessTokenFetch(); |
142 } | 140 } |
143 | 141 |
144 void PushMessagingGetChannelIdFunction::OnGetTokenSuccess( | 142 void PushMessagingGetChannelIdFunction::OnGetTokenSuccess( |
145 const OAuth2TokenService::Request* request, | 143 const OAuth2TokenService::Request* request, |
146 const std::string& access_token, | 144 const std::string& access_token, |
147 const base::Time& expiration_time) { | 145 const base::Time& expiration_time) { |
148 DCHECK_EQ(fetcher_access_token_request_.get(), request); | 146 DCHECK_EQ(fetcher_access_token_request_.get(), request); |
149 fetcher_access_token_request_.reset(); | 147 fetcher_access_token_request_.reset(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 token_cache->RetrieveToken(GaiaConstants::kObfuscatedGaiaId); | 181 token_cache->RetrieveToken(GaiaConstants::kObfuscatedGaiaId); |
184 if (!gaia_id.empty()) { | 182 if (!gaia_id.empty()) { |
185 ReportResult(gaia_id, std::string()); | 183 ReportResult(gaia_id, std::string()); |
186 return; | 184 return; |
187 } | 185 } |
188 | 186 |
189 fetcher_->Start(); | 187 fetcher_->Start(); |
190 } | 188 } |
191 | 189 |
192 // Check if the user is logged in. | 190 // Check if the user is logged in. |
193 bool PushMessagingGetChannelIdFunction::IsUserLoggedIn() const { | 191 bool PushMessagingGetChannelIdFunction::IsUserLoggedIn() { |
194 ProfileOAuth2TokenService* token_service = | 192 invalidation::InvalidationAuthProvider* auth_provider = |
195 ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()); | 193 GetInvalidationAuthProvider(); |
196 SigninManagerBase* signin_manager = | 194 return auth_provider->GetTokenService()->RefreshTokenIsAvailable( |
197 SigninManagerFactory::GetForProfile(GetProfile()); | 195 auth_provider->GetAccountId()); |
198 return token_service->RefreshTokenIsAvailable( | |
199 signin_manager->GetAuthenticatedAccountId()); | |
200 } | 196 } |
201 | 197 |
202 void PushMessagingGetChannelIdFunction::ReportResult( | 198 void PushMessagingGetChannelIdFunction::ReportResult( |
203 const std::string& gaia_id, const std::string& error_string) { | 199 const std::string& gaia_id, const std::string& error_string) { |
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
205 | 201 |
206 BuildAndSendResult(gaia_id, error_string); | 202 BuildAndSendResult(gaia_id, error_string); |
207 | 203 |
208 // Cache the obfuscated ID locally. It never changes for this user, | 204 // Cache the obfuscated ID locally. It never changes for this user, |
209 // and if we call the web API too often, we get errors due to rate limiting. | 205 // and if we call the web API too often, we get errors due to rate limiting. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 error_text = base::IntToString(error.state()); | 253 error_text = base::IntToString(error.state()); |
258 } | 254 } |
259 | 255 |
260 DVLOG(1) << "GetChannelId status: '" << error_text << "'"; | 256 DVLOG(1) << "GetChannelId status: '" << error_text << "'"; |
261 | 257 |
262 // If we had bad credentials, try the logon again. | 258 // If we had bad credentials, try the logon again. |
263 switch (error.state()) { | 259 switch (error.state()) { |
264 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS: | 260 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS: |
265 case GoogleServiceAuthError::ACCOUNT_DELETED: | 261 case GoogleServiceAuthError::ACCOUNT_DELETED: |
266 case GoogleServiceAuthError::ACCOUNT_DISABLED: { | 262 case GoogleServiceAuthError::ACCOUNT_DISABLED: { |
267 if (interactive_) { | 263 if (!interactive_ || !GetInvalidationAuthProvider()->ShowLoginUI()) { |
268 LoginUIService* login_ui_service = | |
269 LoginUIServiceFactory::GetForProfile(GetProfile()); | |
270 // content::NotificationObserver will be called if token is issued. | |
271 login_ui_service->ShowLoginPopup(); | |
272 } else { | |
273 ReportResult(std::string(), error_text); | 264 ReportResult(std::string(), error_text); |
274 } | 265 } |
275 return; | 266 return; |
276 } | 267 } |
277 default: | 268 default: |
278 // Return error to caller. | 269 // Return error to caller. |
279 ReportResult(std::string(), error_text); | 270 ReportResult(std::string(), error_text); |
280 return; | 271 return; |
281 } | 272 } |
282 } | 273 } |
283 | 274 |
| 275 invalidation::InvalidationAuthProvider* |
| 276 PushMessagingGetChannelIdFunction::GetInvalidationAuthProvider() { |
| 277 return invalidation::InvalidationServiceFactory::GetForProfile(GetProfile()) |
| 278 ->GetInvalidationAuthProvider(); |
| 279 } |
| 280 |
284 PushMessagingAPI::PushMessagingAPI(content::BrowserContext* context) | 281 PushMessagingAPI::PushMessagingAPI(content::BrowserContext* context) |
285 : profile_(Profile::FromBrowserContext(context)) { | 282 : profile_(Profile::FromBrowserContext(context)) { |
286 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, | 283 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
287 content::Source<Profile>(profile_->GetOriginalProfile())); | 284 content::Source<Profile>(profile_->GetOriginalProfile())); |
288 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 285 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
289 content::Source<Profile>(profile_->GetOriginalProfile())); | 286 content::Source<Profile>(profile_->GetOriginalProfile())); |
290 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 287 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
291 content::Source<Profile>(profile_->GetOriginalProfile())); | 288 content::Source<Profile>(profile_->GetOriginalProfile())); |
292 } | 289 } |
293 | 290 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 } | 361 } |
365 | 362 |
366 template <> | 363 template <> |
367 void | 364 void |
368 BrowserContextKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { | 365 BrowserContextKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { |
369 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 366 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
370 DependsOn(invalidation::InvalidationServiceFactory::GetInstance()); | 367 DependsOn(invalidation::InvalidationServiceFactory::GetInstance()); |
371 } | 368 } |
372 | 369 |
373 } // namespace extensions | 370 } // namespace extensions |
OLD | NEW |