summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_gcm_app_handler.cc
diff options
context:
space:
mode:
authorjianli@chromium.org <jianli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-20 06:52:20 +0000
committerjianli@chromium.org <jianli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-20 06:52:20 +0000
commit240fc7e573c027877e36cdbdc2e772c9baf968ba (patch)
tree51f1e2c56e47864ac6db6e46fbf2457f1b89e3aa /chrome/browser/extensions/extension_gcm_app_handler.cc
parent123741ad39b7773675fc0f87345ef2b502b57796 (diff)
downloadchromium_src-240fc7e573c027877e36cdbdc2e772c9baf968ba.zip
chromium_src-240fc7e573c027877e36cdbdc2e772c9baf968ba.tar.gz
chromium_src-240fc7e573c027877e36cdbdc2e772c9baf968ba.tar.bz2
[GCM] Move extension specific logic out of GCMProfileService
A new interface GCMAppHandler is introduce for app specific logic. For extensions, ExtensionGCMAppHandler is created for both event dispatching and extension notification handling. BUG=343268 TBR=tim@chromium.org Review URL: https://codereview.chromium.org/196143003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@258235 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/extension_gcm_app_handler.cc')
-rw-r--r--chrome/browser/extensions/extension_gcm_app_handler.cc156
1 files changed, 156 insertions, 0 deletions
diff --git a/chrome/browser/extensions/extension_gcm_app_handler.cc b/chrome/browser/extensions/extension_gcm_app_handler.cc
new file mode 100644
index 0000000..3ed99a0
--- /dev/null
+++ b/chrome/browser/extensions/extension_gcm_app_handler.cc
@@ -0,0 +1,156 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/extensions/extension_gcm_app_handler.h"
+
+#include "base/bind.h"
+#include "base/location.h"
+#include "chrome/browser/chrome_notification_types.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/services/gcm/gcm_profile_service.h"
+#include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
+#include "content/public/browser/notification_details.h"
+#include "content/public/browser/notification_source.h"
+#include "extensions/browser/extension_registry.h"
+#include "extensions/browser/extension_system.h"
+#include "extensions/common/extension.h"
+#include "extensions/common/one_shot_event.h"
+#include "extensions/common/permissions/permissions_data.h"
+
+#if !defined(OS_ANDROID)
+#include "chrome/browser/extensions/api/gcm/gcm_api.h"
+#endif
+
+namespace extensions {
+
+namespace {
+
+bool IsGCMPermissionEnabled(const Extension* extension) {
+ return PermissionsData::HasAPIPermission(extension, APIPermission::kGcm);
+}
+
+} // namespace
+
+ExtensionGCMAppHandler::ExtensionGCMAppHandler(Profile* profile)
+ : profile_(profile),
+ weak_factory_(this) {
+ // Listen to various extension related notifications.
+ registrar_.Add(this,
+ chrome::NOTIFICATION_EXTENSION_LOADED,
+ content::Source<Profile>(profile_));
+ registrar_.Add(this,
+ chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
+ content::Source<Profile>(profile_));
+ registrar_.Add(this,
+ chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
+ content::Source<Profile>(profile_));
+
+ // Register app handlers when the extension system is ready. It might be ready
+ // now.
+ ExtensionSystem::Get(profile_)->ready().Post(
+ FROM_HERE,
+ base::Bind(&ExtensionGCMAppHandler::OnExtensionsReady,
+ weak_factory_.GetWeakPtr()));
+
+#if !defined(OS_ANDROID)
+ js_event_router_.reset(new extensions::GcmJsEventRouter(profile_));
+#endif
+}
+
+ExtensionGCMAppHandler::~ExtensionGCMAppHandler() {
+ const ExtensionSet& enabled_extensions =
+ ExtensionRegistry::Get(profile_)->enabled_extensions();
+ for (ExtensionSet::const_iterator extension = enabled_extensions.begin();
+ extension != enabled_extensions.end();
+ ++extension) {
+ if (IsGCMPermissionEnabled(extension->get()))
+ GetGCMProfileService()->RemoveAppHandler((*extension)->id());
+ }
+}
+
+void ExtensionGCMAppHandler::ShutdownHandler() {
+#if !defined(OS_ANDROID)
+ js_event_router_.reset();
+#endif
+}
+
+void ExtensionGCMAppHandler::OnMessage(
+ const std::string& app_id,
+ const gcm::GCMClient::IncomingMessage& message) {
+#if !defined(OS_ANDROID)
+ js_event_router_->OnMessage(app_id, message);
+#endif
+}
+
+void ExtensionGCMAppHandler::OnMessagesDeleted(const std::string& app_id) {
+#if !defined(OS_ANDROID)
+ js_event_router_->OnMessagesDeleted(app_id);
+#endif
+}
+
+void ExtensionGCMAppHandler::OnSendError(
+ const std::string& app_id,
+ const gcm::GCMClient::SendErrorDetails& send_error_details) {
+#if !defined(OS_ANDROID)
+ js_event_router_->OnSendError(app_id, send_error_details);
+#endif
+}
+
+void ExtensionGCMAppHandler::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 (IsGCMPermissionEnabled(extension))
+ GetGCMProfileService()->AddAppHandler(extension->id(), this);
+ break;
+ }
+ case chrome:: NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
+ const Extension* extension =
+ content::Details<UnloadedExtensionInfo>(details)->extension;
+ if (IsGCMPermissionEnabled(extension))
+ GetGCMProfileService()->RemoveAppHandler(extension->id());
+ break;
+ }
+ case chrome:: NOTIFICATION_EXTENSION_UNINSTALLED: {
+ const Extension* extension = content::Details<Extension>(details).ptr();
+ if (IsGCMPermissionEnabled(extension)) {
+ GetGCMProfileService()->Unregister(
+ extension->id(),
+ base::Bind(&ExtensionGCMAppHandler::OnUnregisterCompleted,
+ weak_factory_.GetWeakPtr(),
+ extension->id()));
+ GetGCMProfileService()->RemoveAppHandler(extension->id());
+ }
+ break;
+ }
+ default:
+ NOTREACHED();
+ }
+}
+
+gcm::GCMProfileService* ExtensionGCMAppHandler::GetGCMProfileService() const {
+ return gcm::GCMProfileServiceFactory::GetForProfile(profile_);
+}
+
+void ExtensionGCMAppHandler::OnExtensionsReady() {
+ // Register app handler for those loaded extensions that use GCM.
+ const ExtensionSet& enabled_extensions =
+ ExtensionRegistry::Get(profile_)->enabled_extensions();
+ for (ExtensionSet::const_iterator extension = enabled_extensions.begin();
+ extension != enabled_extensions.end();
+ ++extension) {
+ if (IsGCMPermissionEnabled(extension->get()))
+ GetGCMProfileService()->AddAppHandler((*extension)->id(), this);
+ }
+}
+
+void ExtensionGCMAppHandler::OnUnregisterCompleted(
+ const std::string& app_id, gcm::GCMClient::Result result) {
+ // Nothing to do.
+}
+
+} // namespace extensions