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

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: 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
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 void PushMessagingAPI::OnExtensionLoaded(
320 content::BrowserContext* browser_context,
321 const Extension* extension) {
322 invalidation::InvalidationService* invalidation_service =
323 invalidation::InvalidationServiceFactory::GetForProfile(profile_);
324 if (!invalidation_service)
325 return;
326
327 if (!event_router_)
328 event_router_.reset(new PushMessagingEventRouter(profile_));
329 if (!handler_) {
330 handler_.reset(new PushMessagingInvalidationHandler(invalidation_service,
331 event_router_.get()));
332 }
not at google - send to devlin 2014/05/12 17:53:27 can you pull this initialization code into a funct
limasdf 2014/05/12 23:20:16 Done.
333 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) {
334 handler_->RegisterExtension(extension->id());
335 }
336 }
337
338 void PushMessagingAPI::OnExtensionUnloaded(
339 content::BrowserContext* browser_context,
340 const Extension* extension,
341 UnloadedExtensionInfo::Reason reason) {
342 invalidation::InvalidationService* invalidation_service =
343 invalidation::InvalidationServiceFactory::GetForProfile(profile_);
344 if (!invalidation_service)
345 return;
346
347 if (!event_router_)
348 event_router_.reset(new PushMessagingEventRouter(profile_));
349 if (!handler_) {
350 handler_.reset(new PushMessagingInvalidationHandler(invalidation_service,
351 event_router_.get()));
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) {
361 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_INSTALLED);
362
325 invalidation::InvalidationService* invalidation_service = 363 invalidation::InvalidationService* invalidation_service =
326 invalidation::InvalidationServiceFactory::GetForProfile(profile_); 364 invalidation::InvalidationServiceFactory::GetForProfile(profile_);
327 if (!invalidation_service) 365 if (!invalidation_service)
328 return; 366 return;
329 367
330 if (!event_router_) 368 if (!event_router_)
331 event_router_.reset(new PushMessagingEventRouter(profile_)); 369 event_router_.reset(new PushMessagingEventRouter(profile_));
332 if (!handler_) { 370 if (!handler_) {
333 handler_.reset(new PushMessagingInvalidationHandler( 371 handler_.reset(new PushMessagingInvalidationHandler(
334 invalidation_service, event_router_.get())); 372 invalidation_service, event_router_.get()));
335 } 373 }
336 switch (type) { 374
337 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { 375 const Extension* extension =
338 const Extension* extension = 376 content::Details<const InstalledExtensionInfo>(details)->extension;
339 content::Details<const InstalledExtensionInfo>(details)->extension; 377 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) {
340 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) { 378 handler_->SuppressInitialInvalidationsForExtension(extension->id());
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 } 379 }
363 } 380 }
364 381
365 void PushMessagingAPI::SetMapperForTest( 382 void PushMessagingAPI::SetMapperForTest(
366 scoped_ptr<PushMessagingInvalidationMapper> mapper) { 383 scoped_ptr<PushMessagingInvalidationMapper> mapper) {
367 handler_ = mapper.Pass(); 384 handler_ = mapper.Pass();
368 } 385 }
369 386
370 template <> 387 template <>
371 void 388 void
372 BrowserContextKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { 389 BrowserContextKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() {
373 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); 390 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
374 DependsOn(invalidation::InvalidationServiceFactory::GetInstance()); 391 DependsOn(invalidation::InvalidationServiceFactory::GetInstance());
375 } 392 }
376 393
377 } // namespace extensions 394 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698