Chromium Code Reviews| Index: chrome/browser/extensions/api/push_messaging/push_messaging_api.cc |
| diff --git a/chrome/browser/extensions/api/push_messaging/push_messaging_api.cc b/chrome/browser/extensions/api/push_messaging/push_messaging_api.cc |
| index cc92f87bf20c337da9829d319bb89c3c14820560..a8f562829aeb08b6c669361bd2d1201f04f2e818 100644 |
| --- a/chrome/browser/extensions/api/push_messaging/push_messaging_api.cc |
| +++ b/chrome/browser/extensions/api/push_messaging/push_messaging_api.cc |
| @@ -4,18 +4,31 @@ |
| #include "chrome/browser/extensions/api/push_messaging/push_messaging_api.h" |
| +#include <set> |
| + |
| #include "base/bind.h" |
| +#include "base/logging.h" |
| #include "base/values.h" |
| +#include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler.h" |
| #include "chrome/browser/extensions/event_names.h" |
| #include "chrome/browser/extensions/event_router.h" |
| +#include "chrome/browser/extensions/extension_service.h" |
| +#include "chrome/browser/extensions/extension_system.h" |
| #include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/sync/profile_sync_service.h" |
| +#include "chrome/browser/sync/profile_sync_service_factory.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| #include "chrome/common/extensions/api/experimental_push_messaging.h" |
| -#include "content/public/browser/browser_thread.h" |
| +#include "chrome/common/extensions/extension.h" |
| +#include "chrome/common/extensions/extension_set.h" |
| +#include "chrome/common/extensions/permissions/api_permission.h" |
| +#include "content/public/browser/notification_details.h" |
| +#include "content/public/browser/notification_source.h" |
| #include "googleurl/src/gurl.h" |
| namespace extensions { |
| -namespace glue = extensions::api::experimental_push_messaging; |
| +namespace glue = api::experimental_push_messaging; |
| PushMessagingEventRouter::PushMessagingEventRouter(Profile* profile) |
| : profile_(profile) { |
| @@ -24,7 +37,35 @@ PushMessagingEventRouter::PushMessagingEventRouter(Profile* profile) |
| PushMessagingEventRouter::~PushMessagingEventRouter() {} |
| void PushMessagingEventRouter::Init() { |
| - // TODO(dcheng): Add hooks into InvalidationHandler when landed. |
| + registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| + content::Source<Profile>(profile_->GetOriginalProfile())); |
| + registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| + content::Source<Profile>(profile_->GetOriginalProfile())); |
| + // TODO(dcheng): This is not testable. We need to allow handler_ to be |
| + // dependency injected. |
| + const ExtensionSet* extensions = |
|
dcheng
2012/08/09 20:43:54
It's unclear from the comments whether this is onl
|
| + ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
| + std::set<std::string> push_messaging_extensions; |
| + for (ExtensionSet::const_iterator it = extensions->begin(); |
| + it != extensions->end(); ++it) { |
| + const Extension* extension = *it; |
| + if (extension->HasAPIPermission(APIPermission::kPushMessaging)) { |
| + push_messaging_extensions.insert(extension->id()); |
| + } |
| + } |
| + handler_.reset(new PushMessagingInvalidationHandler( |
| + ProfileSyncServiceFactory::GetForProfile(profile_), |
| + this, |
| + push_messaging_extensions)); |
| +} |
| + |
| +void PushMessagingEventRouter::Shutdown() { |
| + handler_.reset(); |
| +} |
| + |
| +void PushMessagingEventRouter::SetMapperForTest( |
| + scoped_ptr<PushMessagingInvalidationMapper> mapper) { |
| + handler_ = mapper.Pass(); |
| } |
| void PushMessagingEventRouter::OnMessage(const std::string& extension_id, |
| @@ -35,7 +76,7 @@ void PushMessagingEventRouter::OnMessage(const std::string& extension_id, |
| message.payload = payload; |
| scoped_ptr<base::ListValue> args(glue::OnMessage::Create(message)); |
| - profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
| + ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( |
| extension_id, |
| event_names::kOnPushMessage, |
| args.Pass(), |
| @@ -43,4 +84,31 @@ void PushMessagingEventRouter::OnMessage(const std::string& extension_id, |
| GURL()); |
| } |
| +void PushMessagingEventRouter::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + switch (type) { |
| + case chrome::NOTIFICATION_EXTENSION_LOADED: { |
| + const Extension* extension = content::Details<Extension>(details).ptr(); |
| + if (extension->HasAPIPermission(APIPermission::kPushMessaging)) { |
| + handler_->RegisterExtension(extension->id()); |
| + } |
| + break; |
| + } |
| + case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
|
dcheng
2012/08/09 20:43:54
I believe this doesn't get called when a Profile i
|
| + // TODO(dcheng): Does this get dispatched when shutting down? Should be |
| + // no-op in that case. |
| + const Extension* extension = |
| + content::Details<UnloadedExtensionInfo>(details)->extension; |
| + if (extension->HasAPIPermission(APIPermission::kPushMessaging)) { |
| + handler_->UnregisterExtension(extension->id()); |
| + } |
| + break; |
| + } |
| + default: |
| + NOTREACHED(); |
| + } |
| +} |
| + |
| } // namespace extensions |