[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" |
Sigurdur Asgeirsson | 2c442dc | 2021-03-25 13:41:20 | [diff] [blame] | 14 | #include "base/scoped_observation.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); |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame^] | 46 | |
| 47 | ExtensionGCMAppHandler(const ExtensionGCMAppHandler&) = delete; |
| 48 | ExtensionGCMAppHandler& operator=(const ExtensionGCMAppHandler&) = delete; |
| 49 | |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 50 | ~ExtensionGCMAppHandler() override; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 51 | |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 52 | // BrowserContextKeyedAPI implementation. |
| 53 | static BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler>* |
| 54 | GetFactoryInstance(); |
sense | 383ce0f | 2017-03-24 04:06:43 | [diff] [blame] | 55 | void Shutdown() override; |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 56 | |
| 57 | // gcm::GCMAppHandler implementation. |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 58 | void ShutdownHandler() override; |
johnme | 93ec793 | 2016-11-17 14:26:58 | [diff] [blame] | 59 | void OnStoreReset() override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 60 | void OnMessage(const std::string& app_id, |
mvanouwerkerk | f8633deb | 2015-07-13 11:04:06 | [diff] [blame] | 61 | const gcm::IncomingMessage& message) override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 62 | void OnMessagesDeleted(const std::string& app_id) override; |
| 63 | void OnSendError( |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 64 | const std::string& app_id, |
mostynb | a15bee1 | 2014-10-04 00:40:32 | [diff] [blame] | 65 | const gcm::GCMClient::SendErrorDetails& send_error_details) override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 66 | void OnSendAcknowledged(const std::string& app_id, |
| 67 | const std::string& message_id) override; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 68 | |
[email protected] | 2ac4673 | 2014-04-03 23:14:31 | [diff] [blame] | 69 | protected: |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 70 | // Could be overridden by testing purpose. |
[email protected] | 2ac4673 | 2014-04-03 23:14:31 | [diff] [blame] | 71 | virtual void OnUnregisterCompleted(const std::string& app_id, |
| 72 | gcm::GCMClient::Result result); |
jianli | 7898ae71 | 2015-06-04 19:47:06 | [diff] [blame] | 73 | virtual void OnDeleteIDCompleted(const std::string& app_id, |
| 74 | instance_id::InstanceID::Result result); |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 75 | virtual void AddAppHandler(const std::string& app_id); |
| 76 | virtual void RemoveAppHandler(const std::string& app_id); |
[email protected] | 2ac4673 | 2014-04-03 23:14:31 | [diff] [blame] | 77 | |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 78 | gcm::GCMDriver* GetGCMDriver() const; |
jianli | 7898ae71 | 2015-06-04 19:47:06 | [diff] [blame] | 79 | instance_id::InstanceIDDriver* GetInstanceIDDriver() const; |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 80 | |
jianli | c5f6bd9 | 2015-05-28 02:23:45 | [diff] [blame] | 81 | private: |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 82 | friend class BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler>; |
| 83 | |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 84 | // ExtensionRegistryObserver implementation. |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 85 | void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 86 | const Extension* extension) override; |
| 87 | void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 88 | const Extension* extension, |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 89 | UnloadedExtensionReason reason) override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 90 | void OnExtensionUninstalled(content::BrowserContext* browser_context, |
| 91 | const Extension* extension, |
| 92 | extensions::UninstallReason reason) override; |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 93 | |
jianli | 7898ae71 | 2015-06-04 19:47:06 | [diff] [blame] | 94 | void RemoveInstanceID(const std::string& app_id); |
[email protected] | 1182aa1b | 2014-06-17 21:19:51 | [diff] [blame] | 95 | void AddDummyAppHandler(); |
| 96 | void RemoveDummyAppHandler(); |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 97 | |
[email protected] | 2f361704 | 2014-03-21 22:44:36 | [diff] [blame] | 98 | // BrowserContextKeyedAPI implementation. |
| 99 | static const char* service_name() { return "ExtensionGCMAppHandler"; } |
| 100 | static const bool kServiceIsNULLWhileTesting = true; |
| 101 | |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 102 | Profile* profile_; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 103 | |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 104 | // Listen to extension load, unloaded notifications. |
Sigurdur Asgeirsson | 2c442dc | 2021-03-25 13:41:20 | [diff] [blame] | 105 | base::ScopedObservation<ExtensionRegistry, ExtensionRegistryObserver> |
| 106 | extension_registry_observation_{this}; |
[email protected] | 96ac596 | 2014-04-22 19:49:58 | [diff] [blame] | 107 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 108 | std::unique_ptr<extensions::GcmJsEventRouter> js_event_router_; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 109 | |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 110 | base::WeakPtrFactory<ExtensionGCMAppHandler> weak_factory_{this}; |
[email protected] | 240fc7e | 2014-03-20 06:52:20 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | } // namespace extensions |
| 114 | |
| 115 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_GCM_APP_HANDLER_H_ |