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 "extensions/browser/browser_context_keyed_api_factory.h"
18 #include "extensions/browser/extension_registry_observer.h"
28 class GCMProfileService;
31 namespace extensions {
33 class ExtensionRegistry;
34 class GcmJsEventRouter;
36 // Defines the interface to provide handling logic for a given app.
37 class ExtensionGCMAppHandler : public gcm::GCMAppHandler,
38 public BrowserContextKeyedAPI,
39 public ExtensionRegistryObserver {
41 explicit ExtensionGCMAppHandler(content::BrowserContext* context);
42 virtual ~ExtensionGCMAppHandler();
44 // BrowserContextKeyedAPI implementation.
45 static BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler>*
48 // gcm::GCMAppHandler implementation.
49 virtual void ShutdownHandler() OVERRIDE;
50 virtual void OnMessage(
51 const std::string& app_id,
52 const gcm::GCMClient::IncomingMessage& message) OVERRIDE;
53 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE;
54 virtual void OnSendError(
55 const std::string& app_id,
56 const gcm::GCMClient::SendErrorDetails& send_error_details) OVERRIDE;
57 virtual void OnSendAcknowledged(const std::string& app_id,
58 const std::string& message_id) OVERRIDE;
61 // Could be overridden by testing purpose.
62 virtual void OnUnregisterCompleted(const std::string& app_id,
63 gcm::GCMClient::Result result);
64 virtual void AddAppHandler(const std::string& app_id);
65 virtual void RemoveAppHandler(const std::string& app_id);
67 gcm::GCMDriver* GetGCMDriver() const;
70 friend class BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler>;
72 // ExtensionRegistryObserver implementation.
73 virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
74 const Extension* extension) OVERRIDE;
75 virtual void OnExtensionUnloaded(
76 content::BrowserContext* browser_context,
77 const Extension* extension,
78 UnloadedExtensionInfo::Reason reason) OVERRIDE;
79 virtual void OnExtensionUninstalled(
80 content::BrowserContext* browser_context,
81 const Extension* extension,
82 extensions::UninstallReason reason) OVERRIDE;
84 void AddDummyAppHandler();
85 void RemoveDummyAppHandler();
87 // BrowserContextKeyedAPI implementation.
88 static const char* service_name() { return "ExtensionGCMAppHandler"; }
89 static const bool kServiceIsNULLWhileTesting = true;
93 // Listen to extension load, unloaded notifications.
94 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
95 extension_registry_observer_;
97 scoped_ptr<extensions::GcmJsEventRouter> js_event_router_;
99 base::WeakPtrFactory<ExtensionGCMAppHandler> weak_factory_;
101 DISALLOW_COPY_AND_ASSIGN(ExtensionGCMAppHandler);
104 } // namespace extensions
106 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_GCM_APP_HANDLER_H_