[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_GCM_APP_HANDLER_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_EXTENSION_GCM_APP_HANDLER_H_ |
| 7 | |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 8 | #include <string> |
| 9 | |
| 10 | #include "base/compiler_specific.h" |
| 11 | #include "base/macros.h" |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
| 13 | #include "base/memory/weak_ptr.h" |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 14 | #include "base/scoped_observer.h" |
[email protected] | 446f73c2 | 2014-05-14 20:47:18 | [diff] [blame] | 15 | #include "components/gcm_driver/gcm_app_handler.h" |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 16 | #include "components/gcm_driver/gcm_client.h" |
jianli | 7898ae71 | 2015-06-04 19:47:06 | [diff] [blame^] | 17 | #include "components/gcm_driver/instance_id/instance_id.h" |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 18 | #include "extensions/browser/browser_context_keyed_api_factory.h" |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 19 | #include "extensions/browser/extension_registry_observer.h" |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 20 | |
| 21 | class Profile; |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 22 | |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 23 | namespace content { |
| 24 | class BrowserContext; |
| 25 | } |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 26 | |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 27 | namespace gcm { |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 28 | class GCMDriver; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 29 | class GCMProfileService; |
| 30 | } |
jianli | 7898ae71 | 2015-06-04 19:47:06 | [diff] [blame^] | 31 | namespace instance_id { |
| 32 | class InstanceIDDriver; |
| 33 | } |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 34 | |
| 35 | namespace extensions { |
| 36 | |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 37 | class ExtensionRegistry; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 38 | class GcmJsEventRouter; |
| 39 | |
| 40 | // Defines the interface to provide handling logic for a given app. |
| 41 | class ExtensionGCMAppHandler : public gcm::GCMAppHandler, |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 42 | public BrowserContextKeyedAPI, |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 43 | public ExtensionRegistryObserver { |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 44 | public: |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 45 | explicit ExtensionGCMAppHandler(content::BrowserContext* context); |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 46 | ~ExtensionGCMAppHandler() override; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 47 | |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 48 | // BrowserContextKeyedAPI implementation. |
| 49 | static BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler>* |
| 50 | GetFactoryInstance(); |
| 51 | |
| 52 | // gcm::GCMAppHandler implementation. |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 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; |
| 57 | void OnSendError( |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 58 | const std::string& app_id, |
mostynb | a15bee1 | 2014-10-04 00:40:32 | [diff] [blame] | 59 | const gcm::GCMClient::SendErrorDetails& send_error_details) override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 60 | void OnSendAcknowledged(const std::string& app_id, |
| 61 | const std::string& message_id) override; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 62 | |
[email protected] | 2ac4673 | 2014-04-03 23:14:31 | [diff] [blame] | 63 | protected: |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 64 | // Could be overridden by testing purpose. |
[email protected] | 2ac4673 | 2014-04-03 23:14:31 | [diff] [blame] | 65 | virtual void OnUnregisterCompleted(const std::string& app_id, |
| 66 | gcm::GCMClient::Result result); |
jianli | 7898ae71 | 2015-06-04 19:47:06 | [diff] [blame^] | 67 | virtual void OnDeleteIDCompleted(const std::string& app_id, |
| 68 | instance_id::InstanceID::Result result); |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 69 | virtual void AddAppHandler(const std::string& app_id); |
| 70 | virtual void RemoveAppHandler(const std::string& app_id); |
[email protected] | 2ac4673 | 2014-04-03 23:14:31 | [diff] [blame] | 71 | |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 72 | gcm::GCMDriver* GetGCMDriver() const; |
jianli | 7898ae71 | 2015-06-04 19:47:06 | [diff] [blame^] | 73 | instance_id::InstanceIDDriver* GetInstanceIDDriver() const; |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 74 | |
jianli | c5f6bd9 | 2015-05-28 02:23:45 | [diff] [blame] | 75 | private: |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 76 | friend class BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler>; |
| 77 | |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 78 | // ExtensionRegistryObserver implementation. |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 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; |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 87 | |
jianli | 7898ae71 | 2015-06-04 19:47:06 | [diff] [blame^] | 88 | void RemoveInstanceID(const std::string& app_id); |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 89 | void AddDummyAppHandler(); |
| 90 | void RemoveDummyAppHandler(); |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 91 | |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 92 | // BrowserContextKeyedAPI implementation. |
| 93 | static const char* service_name() { return "ExtensionGCMAppHandler"; } |
| 94 | static const bool kServiceIsNULLWhileTesting = true; |
| 95 | |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 96 | Profile* profile_; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 97 | |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 98 | // Listen to extension load, unloaded notifications. |
| 99 | ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 100 | extension_registry_observer_; |
| 101 | |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 102 | scoped_ptr<extensions::GcmJsEventRouter> js_event_router_; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 103 | |
| 104 | base::WeakPtrFactory<ExtensionGCMAppHandler> weak_factory_; |
| 105 | |
| 106 | DISALLOW_COPY_AND_ASSIGN(ExtensionGCMAppHandler); |
| 107 | }; |
| 108 | |
| 109 | } // namespace extensions |
| 110 | |
| 111 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_GCM_APP_HANDLER_H_ |