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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_GCM_APP_HANDLER_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_GCM_APP_HANDLER_H_
10 #include "base/compiler_specific.h"
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/scoped_observer.h"
15 #include "components/gcm_driver/gcm_app_handler.h"
16 #include "components/gcm_driver/gcm_client.h"
17 #include "components/gcm_driver/instance_id/instance_id.h"
18 #include "extensions/browser/browser_context_keyed_api_factory.h"
19 #include "extensions/browser/extension_registry_observer.h"
29 class GCMProfileService
;
31 namespace instance_id
{
32 class InstanceIDDriver
;
35 namespace extensions
{
37 class ExtensionRegistry
;
38 class GcmJsEventRouter
;
40 // Defines the interface to provide handling logic for a given app.
41 class ExtensionGCMAppHandler
: public gcm::GCMAppHandler
,
42 public BrowserContextKeyedAPI
,
43 public ExtensionRegistryObserver
{
45 explicit ExtensionGCMAppHandler(content::BrowserContext
* context
);
46 ~ExtensionGCMAppHandler() override
;
48 // BrowserContextKeyedAPI implementation.
49 static BrowserContextKeyedAPIFactory
<ExtensionGCMAppHandler
>*
52 // gcm::GCMAppHandler implementation.
53 void ShutdownHandler() override
;
54 void OnMessage(const std::string
& app_id
,
55 const gcm::GCMClient::IncomingMessage
& message
) override
;
56 void OnMessagesDeleted(const std::string
& app_id
) override
;
58 const std::string
& app_id
,
59 const gcm::GCMClient::SendErrorDetails
& send_error_details
) override
;
60 void OnSendAcknowledged(const std::string
& app_id
,
61 const std::string
& message_id
) override
;
64 // Could be overridden by testing purpose.
65 virtual void OnUnregisterCompleted(const std::string
& app_id
,
66 gcm::GCMClient::Result result
);
67 virtual void OnDeleteIDCompleted(const std::string
& app_id
,
68 instance_id::InstanceID::Result result
);
69 virtual void AddAppHandler(const std::string
& app_id
);
70 virtual void RemoveAppHandler(const std::string
& app_id
);
72 gcm::GCMDriver
* GetGCMDriver() const;
73 instance_id::InstanceIDDriver
* GetInstanceIDDriver() const;
76 friend class BrowserContextKeyedAPIFactory
<ExtensionGCMAppHandler
>;
78 // ExtensionRegistryObserver implementation.
79 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
80 const Extension
* extension
) override
;
81 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
82 const Extension
* extension
,
83 UnloadedExtensionInfo::Reason reason
) override
;
84 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
85 const Extension
* extension
,
86 extensions::UninstallReason reason
) override
;
88 void RemoveInstanceID(const std::string
& app_id
);
89 void AddDummyAppHandler();
90 void RemoveDummyAppHandler();
92 // BrowserContextKeyedAPI implementation.
93 static const char* service_name() { return "ExtensionGCMAppHandler"; }
94 static const bool kServiceIsNULLWhileTesting
= true;
98 // Listen to extension load, unloaded notifications.
99 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
100 extension_registry_observer_
;
102 scoped_ptr
<extensions::GcmJsEventRouter
> js_event_router_
;
104 base::WeakPtrFactory
<ExtensionGCMAppHandler
> weak_factory_
;
106 DISALLOW_COPY_AND_ASSIGN(ExtensionGCMAppHandler
);
109 } // namespace extensions
111 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_GCM_APP_HANDLER_H_