Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(486)

Side by Side Diff: chrome/browser/extensions/api/push_messaging/push_messaging_api.cc

Issue 275383002: Use ExtensionRegistryObserver instead of deprecated extension notification from c/b/e/api. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address review Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/extensions/api/push_messaging/push_messaging_api.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 10 matching lines...) Expand all
21 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 21 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
22 #include "chrome/browser/signin/signin_manager_factory.h" 22 #include "chrome/browser/signin/signin_manager_factory.h"
23 #include "chrome/common/extensions/api/push_messaging.h" 23 #include "chrome/common/extensions/api/push_messaging.h"
24 #include "components/invalidation/invalidation_service.h" 24 #include "components/invalidation/invalidation_service.h"
25 #include "components/signin/core/browser/profile_oauth2_token_service.h" 25 #include "components/signin/core/browser/profile_oauth2_token_service.h"
26 #include "components/signin/core/browser/signin_manager.h" 26 #include "components/signin/core/browser/signin_manager.h"
27 #include "content/public/browser/browser_thread.h" 27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/notification_details.h" 28 #include "content/public/browser/notification_details.h"
29 #include "content/public/browser/notification_source.h" 29 #include "content/public/browser/notification_source.h"
30 #include "extensions/browser/event_router.h" 30 #include "extensions/browser/event_router.h"
31 #include "extensions/browser/extension_registry.h"
31 #include "extensions/browser/extension_system_provider.h" 32 #include "extensions/browser/extension_system_provider.h"
32 #include "extensions/browser/extensions_browser_client.h" 33 #include "extensions/browser/extensions_browser_client.h"
33 #include "extensions/common/extension.h" 34 #include "extensions/common/extension.h"
34 #include "extensions/common/permissions/api_permission.h" 35 #include "extensions/common/permissions/api_permission.h"
35 #include "google_apis/gaia/gaia_constants.h" 36 #include "google_apis/gaia/gaia_constants.h"
36 #include "google_apis/gaia/identity_provider.h" 37 #include "google_apis/gaia/identity_provider.h"
37 38
38 using content::BrowserThread; 39 using content::BrowserThread;
39 40
40 const char kChannelIdSeparator[] = "/"; 41 const char kChannelIdSeparator[] = "/";
(...skipping 27 matching lines...) Expand all
68 glue::Message message; 69 glue::Message message;
69 message.subchannel_id = subchannel; 70 message.subchannel_id = subchannel;
70 message.payload = payload; 71 message.payload = payload;
71 72
72 DVLOG(2) << "PushMessagingEventRouter::OnMessage" 73 DVLOG(2) << "PushMessagingEventRouter::OnMessage"
73 << " payload = '" << payload 74 << " payload = '" << payload
74 << "' subchannel = '" << subchannel 75 << "' subchannel = '" << subchannel
75 << "' extension = '" << extension_id << "'"; 76 << "' extension = '" << extension_id << "'";
76 77
77 scoped_ptr<base::ListValue> args(glue::OnMessage::Create(message)); 78 scoped_ptr<base::ListValue> args(glue::OnMessage::Create(message));
78 scoped_ptr<extensions::Event> event( 79 scoped_ptr<Event> event(new Event(glue::OnMessage::kEventName, args.Pass()));
79 new extensions::Event(glue::OnMessage::kEventName, args.Pass()));
80 event->restrict_to_browser_context = profile_; 80 event->restrict_to_browser_context = profile_;
81 EventRouter::Get(profile_)->DispatchEventToExtension( 81 EventRouter::Get(profile_)->DispatchEventToExtension(
82 extension_id, event.Pass()); 82 extension_id, event.Pass());
83 } 83 }
84 84
85 // GetChannelId class functions 85 // GetChannelId class functions
86 86
87 PushMessagingGetChannelIdFunction::PushMessagingGetChannelIdFunction() 87 PushMessagingGetChannelIdFunction::PushMessagingGetChannelIdFunction()
88 : OAuth2TokenService::Consumer("push_messaging"), 88 : OAuth2TokenService::Consumer("push_messaging"),
89 interactive_(false) {} 89 interactive_(false) {}
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 return; 280 return;
281 } 281 }
282 default: 282 default:
283 // Return error to caller. 283 // Return error to caller.
284 ReportResult(std::string(), error_text); 284 ReportResult(std::string(), error_text);
285 return; 285 return;
286 } 286 }
287 } 287 }
288 288
289 PushMessagingAPI::PushMessagingAPI(content::BrowserContext* context) 289 PushMessagingAPI::PushMessagingAPI(content::BrowserContext* context)
290 : profile_(Profile::FromBrowserContext(context)) { 290 : extension_registry_observer_(this),
291 profile_(Profile::FromBrowserContext(context)) {
292 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
291 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, 293 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
292 content::Source<Profile>(profile_->GetOriginalProfile())); 294 content::Source<Profile>(profile_->GetOriginalProfile()));
293 registrar_.Add(this,
294 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
295 content::Source<Profile>(profile_->GetOriginalProfile()));
296 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
297 content::Source<Profile>(profile_->GetOriginalProfile()));
298 } 295 }
299 296
300 PushMessagingAPI::~PushMessagingAPI() { 297 PushMessagingAPI::~PushMessagingAPI() {
301 } 298 }
302 299
303 // static 300 // static
304 PushMessagingAPI* PushMessagingAPI::Get(content::BrowserContext* context) { 301 PushMessagingAPI* PushMessagingAPI::Get(content::BrowserContext* context) {
305 return BrowserContextKeyedAPIFactory<PushMessagingAPI>::Get(context); 302 return BrowserContextKeyedAPIFactory<PushMessagingAPI>::Get(context);
306 } 303 }
307 304
308 void PushMessagingAPI::Shutdown() { 305 void PushMessagingAPI::Shutdown() {
309 event_router_.reset(); 306 event_router_.reset();
310 handler_.reset(); 307 handler_.reset();
311 } 308 }
312 309
313 static base::LazyInstance<BrowserContextKeyedAPIFactory<PushMessagingAPI> > 310 static base::LazyInstance<BrowserContextKeyedAPIFactory<PushMessagingAPI> >
314 g_factory = LAZY_INSTANCE_INITIALIZER; 311 g_factory = LAZY_INSTANCE_INITIALIZER;
315 312
316 // static 313 // static
317 BrowserContextKeyedAPIFactory<PushMessagingAPI>* 314 BrowserContextKeyedAPIFactory<PushMessagingAPI>*
318 PushMessagingAPI::GetFactoryInstance() { 315 PushMessagingAPI::GetFactoryInstance() {
319 return g_factory.Pointer(); 316 return g_factory.Pointer();
320 } 317 }
321 318
319 bool PushMessagingAPI::InitEventRouterAndHandler() {
320 invalidation::InvalidationService* invalidation_service =
321 invalidation::InvalidationServiceFactory::GetForProfile(profile_);
322 if (!invalidation_service)
323 return false;
324
325 if (!event_router_)
326 event_router_.reset(new PushMessagingEventRouter(profile_));
327 if (!handler_) {
328 handler_.reset(new PushMessagingInvalidationHandler(invalidation_service,
329 event_router_.get()));
330 }
331
332 return true;
333 }
334
335 void PushMessagingAPI::OnExtensionLoaded(
336 content::BrowserContext* browser_context,
337 const Extension* extension) {
338 if (!InitEventRouterAndHandler())
339 return;
340
341 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) {
342 handler_->RegisterExtension(extension->id());
343 }
344 }
345
346 void PushMessagingAPI::OnExtensionUnloaded(
347 content::BrowserContext* browser_context,
348 const Extension* extension,
349 UnloadedExtensionInfo::Reason reason) {
350 if (!InitEventRouterAndHandler())
351 return;
352
353 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) {
354 handler_->UnregisterExtension(extension->id());
355 }
356 }
357
322 void PushMessagingAPI::Observe(int type, 358 void PushMessagingAPI::Observe(int type,
323 const content::NotificationSource& source, 359 const content::NotificationSource& source,
324 const content::NotificationDetails& details) { 360 const content::NotificationDetails& details) {
325 invalidation::InvalidationService* invalidation_service = 361 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_INSTALLED);
326 invalidation::InvalidationServiceFactory::GetForProfile(profile_); 362 if (!InitEventRouterAndHandler())
327 if (!invalidation_service)
328 return; 363 return;
329 364
330 if (!event_router_) 365 const Extension* extension =
331 event_router_.reset(new PushMessagingEventRouter(profile_)); 366 content::Details<const InstalledExtensionInfo>(details)->extension;
332 if (!handler_) { 367 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) {
333 handler_.reset(new PushMessagingInvalidationHandler( 368 handler_->SuppressInitialInvalidationsForExtension(extension->id());
334 invalidation_service, event_router_.get()));
335 }
336 switch (type) {
337 case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
338 const Extension* extension =
339 content::Details<const InstalledExtensionInfo>(details)->extension;
340 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) {
341 handler_->SuppressInitialInvalidationsForExtension(extension->id());
342 }
343 break;
344 }
345 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: {
346 const Extension* extension = content::Details<Extension>(details).ptr();
347 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) {
348 handler_->RegisterExtension(extension->id());
349 }
350 break;
351 }
352 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
353 const Extension* extension =
354 content::Details<UnloadedExtensionInfo>(details)->extension;
355 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) {
356 handler_->UnregisterExtension(extension->id());
357 }
358 break;
359 }
360 default:
361 NOTREACHED();
362 } 369 }
363 } 370 }
364 371
365 void PushMessagingAPI::SetMapperForTest( 372 void PushMessagingAPI::SetMapperForTest(
366 scoped_ptr<PushMessagingInvalidationMapper> mapper) { 373 scoped_ptr<PushMessagingInvalidationMapper> mapper) {
367 handler_ = mapper.Pass(); 374 handler_ = mapper.Pass();
368 } 375 }
369 376
370 template <> 377 template <>
371 void 378 void
372 BrowserContextKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { 379 BrowserContextKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() {
373 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); 380 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
374 DependsOn(invalidation::InvalidationServiceFactory::GetInstance()); 381 DependsOn(invalidation::InvalidationServiceFactory::GetInstance());
375 } 382 }
376 383
377 } // namespace extensions 384 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/push_messaging/push_messaging_api.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698