Use ExtensionRegistryObserver instead of deprecated extension notification from c...
[chromium-blink-merge.git] / chrome / browser / extensions / extension_gcm_app_handler.cc
blob7336fb6382c67194eaec962e2c1e70cbe808e1cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/extension_gcm_app_handler.h"
7 #include "base/bind.h"
8 #include "base/lazy_instance.h"
9 #include "base/location.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/services/gcm/gcm_profile_service.h"
13 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
14 #include "components/gcm_driver/gcm_driver.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/browser/extension_system.h"
17 #include "extensions/common/extension.h"
18 #include "extensions/common/permissions/permissions_data.h"
20 #if !defined(OS_ANDROID)
21 #include "chrome/browser/extensions/api/gcm/gcm_api.h"
22 #endif
24 namespace extensions {
26 namespace {
28 base::LazyInstance<BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler> >
29 g_factory = LAZY_INSTANCE_INITIALIZER;
31 bool IsGCMPermissionEnabled(const Extension* extension) {
32 return PermissionsData::HasAPIPermission(extension, APIPermission::kGcm);
35 } // namespace
38 // static
39 BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler>*
40 ExtensionGCMAppHandler::GetFactoryInstance() {
41 return g_factory.Pointer();
44 ExtensionGCMAppHandler::ExtensionGCMAppHandler(content::BrowserContext* context)
45 : profile_(Profile::FromBrowserContext(context)),
46 extension_registry_observer_(this),
47 weak_factory_(this) {
48 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
49 #if !defined(OS_ANDROID)
50 js_event_router_.reset(new extensions::GcmJsEventRouter(profile_));
51 #endif
54 ExtensionGCMAppHandler::~ExtensionGCMAppHandler() {
55 const ExtensionSet& enabled_extensions =
56 ExtensionRegistry::Get(profile_)->enabled_extensions();
57 for (ExtensionSet::const_iterator extension = enabled_extensions.begin();
58 extension != enabled_extensions.end();
59 ++extension) {
60 if (IsGCMPermissionEnabled(extension->get()))
61 GetGCMDriver()->RemoveAppHandler((*extension)->id());
65 void ExtensionGCMAppHandler::ShutdownHandler() {
66 #if !defined(OS_ANDROID)
67 js_event_router_.reset();
68 #endif
71 void ExtensionGCMAppHandler::OnMessage(
72 const std::string& app_id,
73 const gcm::GCMClient::IncomingMessage& message) {
74 #if !defined(OS_ANDROID)
75 js_event_router_->OnMessage(app_id, message);
76 #endif
79 void ExtensionGCMAppHandler::OnMessagesDeleted(const std::string& app_id) {
80 #if !defined(OS_ANDROID)
81 js_event_router_->OnMessagesDeleted(app_id);
82 #endif
85 void ExtensionGCMAppHandler::OnSendError(
86 const std::string& app_id,
87 const gcm::GCMClient::SendErrorDetails& send_error_details) {
88 #if !defined(OS_ANDROID)
89 js_event_router_->OnSendError(app_id, send_error_details);
90 #endif
93 void ExtensionGCMAppHandler::OnExtensionLoaded(
94 content::BrowserContext* browser_context,
95 const Extension* extension) {
96 if (IsGCMPermissionEnabled(extension))
97 GetGCMDriver()->AddAppHandler(extension->id(), this);
100 void ExtensionGCMAppHandler::OnExtensionUnloaded(
101 content::BrowserContext* browser_context,
102 const Extension* extension,
103 UnloadedExtensionInfo::Reason reason) {
104 if (IsGCMPermissionEnabled(extension))
105 GetGCMDriver()->RemoveAppHandler(extension->id());
108 void ExtensionGCMAppHandler::OnExtensionUninstalled(
109 content::BrowserContext* browser_context,
110 const Extension* extension) {
111 if (IsGCMPermissionEnabled(extension)) {
112 GetGCMDriver()->Unregister(
113 extension->id(),
114 base::Bind(&ExtensionGCMAppHandler::OnUnregisterCompleted,
115 weak_factory_.GetWeakPtr(),
116 extension->id()));
117 GetGCMDriver()->RemoveAppHandler(extension->id());
121 gcm::GCMDriver* ExtensionGCMAppHandler::GetGCMDriver() const {
122 return gcm::GCMProfileServiceFactory::GetForProfile(profile_)->driver();
125 void ExtensionGCMAppHandler::OnUnregisterCompleted(
126 const std::string& app_id, gcm::GCMClient::Result result) {
127 // Nothing to do.
130 } // namespace extensions