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_service_factory.h" | 19 #include "chrome/browser/invalidation/profile_invalidation_provider.h" |
| 20 #include "chrome/browser/invalidation/profile_invalidation_provider_factory.h" |
20 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
22 #include "chrome/browser/signin/signin_manager_factory.h" | 23 #include "chrome/browser/signin/signin_manager_factory.h" |
23 #include "chrome/common/extensions/api/push_messaging.h" | 24 #include "chrome/common/extensions/api/push_messaging.h" |
24 #include "components/invalidation/invalidation_service.h" | 25 #include "components/invalidation/invalidation_service.h" |
25 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 26 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
26 #include "components/signin/core/browser/signin_manager.h" | 27 #include "components/signin/core/browser/signin_manager.h" |
27 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
28 #include "content/public/browser/notification_details.h" | 29 #include "content/public/browser/notification_details.h" |
29 #include "content/public/browser/notification_source.h" | 30 #include "content/public/browser/notification_source.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 glue::GetChannelId::Params::Create(*args_)); | 98 glue::GetChannelId::Params::Create(*args_)); |
98 EXTENSION_FUNCTION_VALIDATE(params.get()); | 99 EXTENSION_FUNCTION_VALIDATE(params.get()); |
99 | 100 |
100 if (params && params->interactive) { | 101 if (params && params->interactive) { |
101 interactive_ = *params->interactive; | 102 interactive_ = *params->interactive; |
102 } | 103 } |
103 | 104 |
104 // Balanced in ReportResult() | 105 // Balanced in ReportResult() |
105 AddRef(); | 106 AddRef(); |
106 | 107 |
107 invalidation::InvalidationService* invalidation_service = | 108 invalidation::ProfileInvalidationProvider* invalidation_provider = |
108 invalidation::InvalidationServiceFactory::GetForProfile(GetProfile()); | 109 invalidation::ProfileInvalidationProviderFactory::GetForProfile( |
109 if (!invalidation_service) { | 110 GetProfile()); |
| 111 if (!invalidation_provider) { |
110 error_ = kAPINotAvailableForUser; | 112 error_ = kAPINotAvailableForUser; |
111 ReportResult(std::string(), error_); | 113 ReportResult(std::string(), error_); |
112 return false; | 114 return false; |
113 } | 115 } |
114 | 116 |
115 IdentityProvider* identity_provider = | 117 IdentityProvider* identity_provider = |
116 invalidation_service->GetIdentityProvider(); | 118 invalidation_provider->GetInvalidationService()->GetIdentityProvider(); |
117 if (!identity_provider->GetTokenService()->RefreshTokenIsAvailable( | 119 if (!identity_provider->GetTokenService()->RefreshTokenIsAvailable( |
118 identity_provider->GetActiveAccountId())) { | 120 identity_provider->GetActiveAccountId())) { |
119 if (interactive_ && identity_provider->RequestLogin()) { | 121 if (interactive_ && identity_provider->RequestLogin()) { |
120 identity_provider->AddActiveAccountRefreshTokenObserver(this); | 122 identity_provider->AddActiveAccountRefreshTokenObserver(this); |
121 return true; | 123 return true; |
122 } else { | 124 } else { |
123 error_ = kUserNotSignedIn; | 125 error_ = kUserNotSignedIn; |
124 ReportResult(std::string(), error_); | 126 ReportResult(std::string(), error_); |
125 return false; | 127 return false; |
126 } | 128 } |
127 } | 129 } |
128 | 130 |
129 DVLOG(2) << "Logged in profile name: " << GetProfile()->GetProfileName(); | 131 DVLOG(2) << "Logged in profile name: " << GetProfile()->GetProfileName(); |
130 | 132 |
131 StartAccessTokenFetch(); | 133 StartAccessTokenFetch(); |
132 return true; | 134 return true; |
133 } | 135 } |
134 | 136 |
135 void PushMessagingGetChannelIdFunction::StartAccessTokenFetch() { | 137 void PushMessagingGetChannelIdFunction::StartAccessTokenFetch() { |
136 invalidation::InvalidationService* invalidation_service = | 138 invalidation::ProfileInvalidationProvider* invalidation_provider = |
137 invalidation::InvalidationServiceFactory::GetForProfile(GetProfile()); | 139 invalidation::ProfileInvalidationProviderFactory::GetForProfile( |
138 CHECK(invalidation_service); | 140 GetProfile()); |
| 141 CHECK(invalidation_provider); |
139 IdentityProvider* identity_provider = | 142 IdentityProvider* identity_provider = |
140 invalidation_service->GetIdentityProvider(); | 143 invalidation_provider->GetInvalidationService()->GetIdentityProvider(); |
141 | 144 |
142 std::vector<std::string> scope_vector = | 145 std::vector<std::string> scope_vector = |
143 extensions::ObfuscatedGaiaIdFetcher::GetScopes(); | 146 extensions::ObfuscatedGaiaIdFetcher::GetScopes(); |
144 OAuth2TokenService::ScopeSet scopes(scope_vector.begin(), scope_vector.end()); | 147 OAuth2TokenService::ScopeSet scopes(scope_vector.begin(), scope_vector.end()); |
145 fetcher_access_token_request_ = | 148 fetcher_access_token_request_ = |
146 identity_provider->GetTokenService()->StartRequest( | 149 identity_provider->GetTokenService()->StartRequest( |
147 identity_provider->GetActiveAccountId(), scopes, this); | 150 identity_provider->GetActiveAccountId(), scopes, this); |
148 } | 151 } |
149 | 152 |
150 void PushMessagingGetChannelIdFunction::OnRefreshTokenAvailable( | 153 void PushMessagingGetChannelIdFunction::OnRefreshTokenAvailable( |
151 const std::string& account_id) { | 154 const std::string& account_id) { |
152 invalidation::InvalidationService* invalidation_service = | 155 invalidation::ProfileInvalidationProvider* invalidation_provider = |
153 invalidation::InvalidationServiceFactory::GetForProfile(GetProfile()); | 156 invalidation::ProfileInvalidationProviderFactory::GetForProfile( |
154 CHECK(invalidation_service); | 157 GetProfile()); |
155 invalidation_service->GetIdentityProvider()-> | 158 CHECK(invalidation_provider); |
| 159 invalidation_provider->GetInvalidationService()->GetIdentityProvider()-> |
156 RemoveActiveAccountRefreshTokenObserver(this); | 160 RemoveActiveAccountRefreshTokenObserver(this); |
157 DVLOG(2) << "Newly logged in: " << GetProfile()->GetProfileName(); | 161 DVLOG(2) << "Newly logged in: " << GetProfile()->GetProfileName(); |
158 StartAccessTokenFetch(); | 162 StartAccessTokenFetch(); |
159 } | 163 } |
160 | 164 |
161 void PushMessagingGetChannelIdFunction::OnGetTokenSuccess( | 165 void PushMessagingGetChannelIdFunction::OnGetTokenSuccess( |
162 const OAuth2TokenService::Request* request, | 166 const OAuth2TokenService::Request* request, |
163 const std::string& access_token, | 167 const std::string& access_token, |
164 const base::Time& expiration_time) { | 168 const base::Time& expiration_time) { |
165 DCHECK_EQ(fetcher_access_token_request_.get(), request); | 169 DCHECK_EQ(fetcher_access_token_request_.get(), request); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 error_text = base::IntToString(error.state()); | 268 error_text = base::IntToString(error.state()); |
265 } | 269 } |
266 | 270 |
267 DVLOG(1) << "GetChannelId status: '" << error_text << "'"; | 271 DVLOG(1) << "GetChannelId status: '" << error_text << "'"; |
268 | 272 |
269 // If we had bad credentials, try the logon again. | 273 // If we had bad credentials, try the logon again. |
270 switch (error.state()) { | 274 switch (error.state()) { |
271 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS: | 275 case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS: |
272 case GoogleServiceAuthError::ACCOUNT_DELETED: | 276 case GoogleServiceAuthError::ACCOUNT_DELETED: |
273 case GoogleServiceAuthError::ACCOUNT_DISABLED: { | 277 case GoogleServiceAuthError::ACCOUNT_DISABLED: { |
274 invalidation::InvalidationService* invalidation_service = | 278 invalidation::ProfileInvalidationProvider* invalidation_provider = |
275 invalidation::InvalidationServiceFactory::GetForProfile(GetProfile()); | 279 invalidation::ProfileInvalidationProviderFactory::GetForProfile( |
276 CHECK(invalidation_service); | 280 GetProfile()); |
277 if (!interactive_ || | 281 CHECK(invalidation_provider); |
278 !invalidation_service->GetIdentityProvider()->RequestLogin()) { | 282 if (!interactive_ || !invalidation_provider->GetInvalidationService()-> |
| 283 GetIdentityProvider()->RequestLogin()) { |
279 ReportResult(std::string(), error_text); | 284 ReportResult(std::string(), error_text); |
280 } | 285 } |
281 return; | 286 return; |
282 } | 287 } |
283 default: | 288 default: |
284 // Return error to caller. | 289 // Return error to caller. |
285 ReportResult(std::string(), error_text); | 290 ReportResult(std::string(), error_text); |
286 return; | 291 return; |
287 } | 292 } |
288 } | 293 } |
(...skipping 23 matching lines...) Expand all Loading... |
312 static base::LazyInstance<BrowserContextKeyedAPIFactory<PushMessagingAPI> > | 317 static base::LazyInstance<BrowserContextKeyedAPIFactory<PushMessagingAPI> > |
313 g_factory = LAZY_INSTANCE_INITIALIZER; | 318 g_factory = LAZY_INSTANCE_INITIALIZER; |
314 | 319 |
315 // static | 320 // static |
316 BrowserContextKeyedAPIFactory<PushMessagingAPI>* | 321 BrowserContextKeyedAPIFactory<PushMessagingAPI>* |
317 PushMessagingAPI::GetFactoryInstance() { | 322 PushMessagingAPI::GetFactoryInstance() { |
318 return g_factory.Pointer(); | 323 return g_factory.Pointer(); |
319 } | 324 } |
320 | 325 |
321 bool PushMessagingAPI::InitEventRouterAndHandler() { | 326 bool PushMessagingAPI::InitEventRouterAndHandler() { |
322 invalidation::InvalidationService* invalidation_service = | 327 invalidation::ProfileInvalidationProvider* invalidation_provider = |
323 invalidation::InvalidationServiceFactory::GetForProfile(profile_); | 328 invalidation::ProfileInvalidationProviderFactory::GetForProfile(profile_); |
324 if (!invalidation_service) | 329 if (!invalidation_provider) |
325 return false; | 330 return false; |
326 | 331 |
327 if (!event_router_) | 332 if (!event_router_) |
328 event_router_.reset(new PushMessagingEventRouter(profile_)); | 333 event_router_.reset(new PushMessagingEventRouter(profile_)); |
329 if (!handler_) { | 334 if (!handler_) { |
330 handler_.reset(new PushMessagingInvalidationHandler(invalidation_service, | 335 handler_.reset(new PushMessagingInvalidationHandler( |
331 event_router_.get())); | 336 invalidation_provider->GetInvalidationService(), |
| 337 event_router_.get())); |
332 } | 338 } |
333 | 339 |
334 return true; | 340 return true; |
335 } | 341 } |
336 | 342 |
337 void PushMessagingAPI::OnExtensionLoaded( | 343 void PushMessagingAPI::OnExtensionLoaded( |
338 content::BrowserContext* browser_context, | 344 content::BrowserContext* browser_context, |
339 const Extension* extension) { | 345 const Extension* extension) { |
340 if (!InitEventRouterAndHandler()) | 346 if (!InitEventRouterAndHandler()) |
341 return; | 347 return; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 | 382 |
377 void PushMessagingAPI::SetMapperForTest( | 383 void PushMessagingAPI::SetMapperForTest( |
378 scoped_ptr<PushMessagingInvalidationMapper> mapper) { | 384 scoped_ptr<PushMessagingInvalidationMapper> mapper) { |
379 handler_ = mapper.Pass(); | 385 handler_ = mapper.Pass(); |
380 } | 386 } |
381 | 387 |
382 template <> | 388 template <> |
383 void | 389 void |
384 BrowserContextKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { | 390 BrowserContextKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { |
385 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 391 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
386 DependsOn(invalidation::InvalidationServiceFactory::GetInstance()); | 392 DependsOn(invalidation::ProfileInvalidationProviderFactory::GetInstance()); |
387 } | 393 } |
388 | 394 |
389 } // namespace extensions | 395 } // namespace extensions |
OLD | NEW |