[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 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 8 | #include <memory> |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 9 | #include <string> |
| 10 | |
| 11 | #include "base/compiler_specific.h" |
| 12 | #include "base/macros.h" |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 14 | #include "base/scoped_observer.h" |
Rayan Kanso | 7b3b5ea | 2019-05-08 18:15:00 | [diff] [blame] | 15 | #include "components/gcm_driver/common/gcm_message.h" |
[email protected] | 446f73c2 | 2014-05-14 20:47:18 | [diff] [blame] | 16 | #include "components/gcm_driver/gcm_app_handler.h" |
[email protected] | cd57f37 | 2014-06-09 17:13:06 | [diff] [blame] | 17 | #include "components/gcm_driver/gcm_client.h" |
jianli | 7898ae71 | 2015-06-04 19:47:06 | [diff] [blame] | 18 | #include "components/gcm_driver/instance_id/instance_id.h" |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 19 | #include "extensions/browser/browser_context_keyed_api_factory.h" |
Evan Stade | 75872a6 | 2019-09-06 21:17:38 | [diff] [blame] | 20 | #include "extensions/browser/extension_registry.h" |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 21 | #include "extensions/browser/extension_registry_observer.h" |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 22 | |
| 23 | class Profile; |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 24 | |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 25 | namespace content { |
| 26 | class BrowserContext; |
| 27 | } |
[email protected] | df84c53 | 2014-04-25 15:36:54 | [diff] [blame] | 28 | |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 29 | namespace gcm { |
[email protected] | 9d7e5c0 | 2014-05-21 03:09:03 | [diff] [blame] | 30 | class GCMDriver; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 31 | } |
jianli | 7898ae71 | 2015-06-04 19:47:06 | [diff] [blame] | 32 | namespace instance_id { |
| 33 | class InstanceIDDriver; |
| 34 | } |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 35 | |
| 36 | namespace extensions { |
| 37 | |
| 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(); |
sense | 383ce0f | 2017-03-24 04:06:43 | [diff] [blame] | 51 | void Shutdown() override; |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 52 | |
| 53 | // gcm::GCMAppHandler implementation. |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 54 | void ShutdownHandler() override; |
johnme | 93ec793 | 2016-11-17 14:26:58 | [diff] [blame] | 55 | void OnStoreReset() override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 56 | void OnMessage(const std::string& app_id, |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 57 | const gcm::IncomingMessage& message) override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 58 | void OnMessagesDeleted(const std::string& app_id) override; |
| 59 | void OnSendError( |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 60 | const std::string& app_id, |
mostynb | a15bee1 | 2014-10-04 00:40:32 | [diff] [blame] | 61 | const gcm::GCMClient::SendErrorDetails& send_error_details) override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 62 | void OnSendAcknowledged(const std::string& app_id, |
| 63 | const std::string& message_id) override; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 64 | |
[email protected] | 2ac4673 | 2014-04-03 23:14:31 | [diff] [blame] | 65 | protected: |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 66 | // Could be overridden by testing purpose. |
[email protected] | 2ac4673 | 2014-04-03 23:14:31 | [diff] [blame] | 67 | virtual void OnUnregisterCompleted(const std::string& app_id, |
| 68 | gcm::GCMClient::Result result); |
jianli | 7898ae71 | 2015-06-04 19:47:06 | [diff] [blame] | 69 | virtual void OnDeleteIDCompleted(const std::string& app_id, |
| 70 | instance_id::InstanceID::Result result); |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 71 | virtual void AddAppHandler(const std::string& app_id); |
| 72 | virtual void RemoveAppHandler(const std::string& app_id); |
[email protected] | 2ac4673 | 2014-04-03 23:14:31 | [diff] [blame] | 73 | |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 74 | gcm::GCMDriver* GetGCMDriver() const; |
jianli | 7898ae71 | 2015-06-04 19:47:06 | [diff] [blame] | 75 | instance_id::InstanceIDDriver* GetInstanceIDDriver() const; |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 76 | |
jianli | c5f6bd9 | 2015-05-28 02:23:45 | [diff] [blame] | 77 | private: |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 78 | friend class BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler>; |
| 79 | |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 80 | // ExtensionRegistryObserver implementation. |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 81 | void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 82 | const Extension* extension) override; |
| 83 | void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 84 | const Extension* extension, |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 85 | UnloadedExtensionReason reason) override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 86 | void OnExtensionUninstalled(content::BrowserContext* browser_context, |
| 87 | const Extension* extension, |
| 88 | extensions::UninstallReason reason) override; |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 89 | |
jianli | 7898ae71 | 2015-06-04 19:47:06 | [diff] [blame] | 90 | void RemoveInstanceID(const std::string& app_id); |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 91 | void AddDummyAppHandler(); |
| 92 | void RemoveDummyAppHandler(); |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 93 | |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 94 | // BrowserContextKeyedAPI implementation. |
| 95 | static const char* service_name() { return "ExtensionGCMAppHandler"; } |
| 96 | static const bool kServiceIsNULLWhileTesting = true; |
| 97 | |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 98 | Profile* profile_; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 99 | |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 100 | // Listen to extension load, unloaded notifications. |
| 101 | ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
Evan Stade | 75872a6 | 2019-09-06 21:17:38 | [diff] [blame] | 102 | extension_registry_observer_{this}; |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 103 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 104 | std::unique_ptr<extensions::GcmJsEventRouter> js_event_router_; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 105 | |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 106 | base::WeakPtrFactory<ExtensionGCMAppHandler> weak_factory_{this}; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 107 | |
| 108 | DISALLOW_COPY_AND_ASSIGN(ExtensionGCMAppHandler); |
| 109 | }; |
| 110 | |
| 111 | } // namespace extensions |
| 112 | |
| 113 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_GCM_APP_HANDLER_H_ |