| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__ | 6 #define CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 namespace extensions { | 28 namespace extensions { |
| 29 class ExtensionRegistry; | 29 class ExtensionRegistry; |
| 30 class PushMessagingInvalidationMapper; | 30 class PushMessagingInvalidationMapper; |
| 31 | 31 |
| 32 // Observes a single InvalidationHandler and generates onMessage events. | 32 // Observes a single InvalidationHandler and generates onMessage events. |
| 33 class PushMessagingEventRouter | 33 class PushMessagingEventRouter |
| 34 : public PushMessagingInvalidationHandlerDelegate { | 34 : public PushMessagingInvalidationHandlerDelegate { |
| 35 public: | 35 public: |
| 36 explicit PushMessagingEventRouter(content::BrowserContext* context); | 36 explicit PushMessagingEventRouter(content::BrowserContext* context); |
| 37 virtual ~PushMessagingEventRouter(); | 37 ~PushMessagingEventRouter() override; |
| 38 | 38 |
| 39 // For testing purposes. | 39 // For testing purposes. |
| 40 void TriggerMessageForTest(const std::string& extension_id, | 40 void TriggerMessageForTest(const std::string& extension_id, |
| 41 int subchannel, | 41 int subchannel, |
| 42 const std::string& payload); | 42 const std::string& payload); |
| 43 | 43 |
| 44 private: | 44 private: |
| 45 // InvalidationHandlerDelegate implementation. | 45 // InvalidationHandlerDelegate implementation. |
| 46 virtual void OnMessage(const std::string& extension_id, | 46 void OnMessage(const std::string& extension_id, |
| 47 int subchannel, | 47 int subchannel, |
| 48 const std::string& payload) override; | 48 const std::string& payload) override; |
| 49 | 49 |
| 50 content::BrowserContext* const browser_context_; | 50 content::BrowserContext* const browser_context_; |
| 51 | 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(PushMessagingEventRouter); | 52 DISALLOW_COPY_AND_ASSIGN(PushMessagingEventRouter); |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 class PushMessagingGetChannelIdFunction | 55 class PushMessagingGetChannelIdFunction |
| 56 : public ChromeAsyncExtensionFunction, | 56 : public ChromeAsyncExtensionFunction, |
| 57 public ObfuscatedGaiaIdFetcher::Delegate, | 57 public ObfuscatedGaiaIdFetcher::Delegate, |
| 58 public OAuth2TokenService::Observer, | 58 public OAuth2TokenService::Observer, |
| 59 public OAuth2TokenService::Consumer { | 59 public OAuth2TokenService::Consumer { |
| 60 public: | 60 public: |
| 61 PushMessagingGetChannelIdFunction(); | 61 PushMessagingGetChannelIdFunction(); |
| 62 | 62 |
| 63 protected: | 63 protected: |
| 64 virtual ~PushMessagingGetChannelIdFunction(); | 64 ~PushMessagingGetChannelIdFunction() override; |
| 65 | 65 |
| 66 // ExtensionFunction: | 66 // ExtensionFunction: |
| 67 virtual bool RunAsync() override; | 67 bool RunAsync() override; |
| 68 DECLARE_EXTENSION_FUNCTION("pushMessaging.getChannelId", | 68 DECLARE_EXTENSION_FUNCTION("pushMessaging.getChannelId", |
| 69 PUSHMESSAGING_GETCHANNELID) | 69 PUSHMESSAGING_GETCHANNELID) |
| 70 | 70 |
| 71 private: | 71 private: |
| 72 void ReportResult(const std::string& gaia_id, | 72 void ReportResult(const std::string& gaia_id, |
| 73 const std::string& error_message); | 73 const std::string& error_message); |
| 74 | 74 |
| 75 void BuildAndSendResult(const std::string& gaia_id, | 75 void BuildAndSendResult(const std::string& gaia_id, |
| 76 const std::string& error_message); | 76 const std::string& error_message); |
| 77 | 77 |
| 78 // Begin the async fetch of the Gaia ID. | 78 // Begin the async fetch of the Gaia ID. |
| 79 void StartGaiaIdFetch(const std::string& access_token); | 79 void StartGaiaIdFetch(const std::string& access_token); |
| 80 | 80 |
| 81 // Begin the async fetch of the access token for Gaia ID fetcher. | 81 // Begin the async fetch of the access token for Gaia ID fetcher. |
| 82 void StartAccessTokenFetch(); | 82 void StartAccessTokenFetch(); |
| 83 | 83 |
| 84 // OAuth2TokenService::Observer implementation. | 84 // OAuth2TokenService::Observer implementation. |
| 85 virtual void OnRefreshTokenAvailable(const std::string& account_id) override; | 85 void OnRefreshTokenAvailable(const std::string& account_id) override; |
| 86 | 86 |
| 87 // OAuth2TokenService::Consumer implementation. | 87 // OAuth2TokenService::Consumer implementation. |
| 88 virtual void OnGetTokenSuccess( | 88 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 89 const OAuth2TokenService::Request* request, | 89 const std::string& access_token, |
| 90 const std::string& access_token, | 90 const base::Time& expiration_time) override; |
| 91 const base::Time& expiration_time) override; | 91 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 92 virtual void OnGetTokenFailure( | 92 const GoogleServiceAuthError& error) override; |
| 93 const OAuth2TokenService::Request* request, | |
| 94 const GoogleServiceAuthError& error) override; | |
| 95 | 93 |
| 96 // ObfuscatedGiaiaIdFetcher::Delegate implementation. | 94 // ObfuscatedGiaiaIdFetcher::Delegate implementation. |
| 97 virtual void OnObfuscatedGaiaIdFetchSuccess(const std::string& gaia_id) | 95 void OnObfuscatedGaiaIdFetchSuccess(const std::string& gaia_id) override; |
| 98 override; | 96 void OnObfuscatedGaiaIdFetchFailure( |
| 99 virtual void OnObfuscatedGaiaIdFetchFailure( | |
| 100 const GoogleServiceAuthError& error) override; | 97 const GoogleServiceAuthError& error) override; |
| 101 | 98 |
| 102 scoped_ptr<ObfuscatedGaiaIdFetcher> fetcher_; | 99 scoped_ptr<ObfuscatedGaiaIdFetcher> fetcher_; |
| 103 bool interactive_; | 100 bool interactive_; |
| 104 scoped_ptr<OAuth2TokenService::Request> fetcher_access_token_request_; | 101 scoped_ptr<OAuth2TokenService::Request> fetcher_access_token_request_; |
| 105 | 102 |
| 106 DISALLOW_COPY_AND_ASSIGN(PushMessagingGetChannelIdFunction); | 103 DISALLOW_COPY_AND_ASSIGN(PushMessagingGetChannelIdFunction); |
| 107 }; | 104 }; |
| 108 | 105 |
| 109 class PushMessagingAPI : public BrowserContextKeyedAPI, | 106 class PushMessagingAPI : public BrowserContextKeyedAPI, |
| 110 public ExtensionRegistryObserver { | 107 public ExtensionRegistryObserver { |
| 111 public: | 108 public: |
| 112 explicit PushMessagingAPI(content::BrowserContext* context); | 109 explicit PushMessagingAPI(content::BrowserContext* context); |
| 113 virtual ~PushMessagingAPI(); | 110 ~PushMessagingAPI() override; |
| 114 | 111 |
| 115 // Convenience method to get the PushMessagingAPI for a BrowserContext. | 112 // Convenience method to get the PushMessagingAPI for a BrowserContext. |
| 116 static PushMessagingAPI* Get(content::BrowserContext* context); | 113 static PushMessagingAPI* Get(content::BrowserContext* context); |
| 117 | 114 |
| 118 // KeyedService implementation. | 115 // KeyedService implementation. |
| 119 virtual void Shutdown() override; | 116 void Shutdown() override; |
| 120 | 117 |
| 121 // BrowserContextKeyedAPI implementation. | 118 // BrowserContextKeyedAPI implementation. |
| 122 static BrowserContextKeyedAPIFactory<PushMessagingAPI>* GetFactoryInstance(); | 119 static BrowserContextKeyedAPIFactory<PushMessagingAPI>* GetFactoryInstance(); |
| 123 | 120 |
| 124 // For testing purposes. | 121 // For testing purposes. |
| 125 PushMessagingEventRouter* GetEventRouterForTest() const { | 122 PushMessagingEventRouter* GetEventRouterForTest() const { |
| 126 return event_router_.get(); | 123 return event_router_.get(); |
| 127 } | 124 } |
| 128 PushMessagingInvalidationMapper* GetMapperForTest() const { | 125 PushMessagingInvalidationMapper* GetMapperForTest() const { |
| 129 return handler_.get(); | 126 return handler_.get(); |
| 130 } | 127 } |
| 131 void SetMapperForTest(scoped_ptr<PushMessagingInvalidationMapper> mapper); | 128 void SetMapperForTest(scoped_ptr<PushMessagingInvalidationMapper> mapper); |
| 132 | 129 |
| 133 private: | 130 private: |
| 134 friend class BrowserContextKeyedAPIFactory<PushMessagingAPI>; | 131 friend class BrowserContextKeyedAPIFactory<PushMessagingAPI>; |
| 135 | 132 |
| 136 // BrowserContextKeyedAPI implementation. | 133 // BrowserContextKeyedAPI implementation. |
| 137 static const char* service_name() { | 134 static const char* service_name() { |
| 138 return "PushMessagingAPI"; | 135 return "PushMessagingAPI"; |
| 139 } | 136 } |
| 140 static const bool kServiceIsNULLWhileTesting = true; | 137 static const bool kServiceIsNULLWhileTesting = true; |
| 141 | 138 |
| 142 // Overridden from ExtensionRegistryObserver. | 139 // Overridden from ExtensionRegistryObserver. |
| 143 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, | 140 void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 144 const Extension* extension) override; | 141 const Extension* extension) override; |
| 145 virtual void OnExtensionUnloaded( | 142 void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 146 content::BrowserContext* browser_context, | 143 const Extension* extension, |
| 147 const Extension* extension, | 144 UnloadedExtensionInfo::Reason reason) override; |
| 148 UnloadedExtensionInfo::Reason reason) override; | 145 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context, |
| 149 virtual void OnExtensionWillBeInstalled( | 146 const Extension* extension, |
| 150 content::BrowserContext* browser_context, | 147 bool is_update, |
| 151 const Extension* extension, | 148 bool from_ephemeral, |
| 152 bool is_update, | 149 const std::string& old_name) override; |
| 153 bool from_ephemeral, | |
| 154 const std::string& old_name) override; | |
| 155 | 150 |
| 156 // Initialize |event_router_| and |handler_|. | 151 // Initialize |event_router_| and |handler_|. |
| 157 bool InitEventRouterAndHandler(); | 152 bool InitEventRouterAndHandler(); |
| 158 | 153 |
| 159 // Created lazily when an app or extension with the push messaging permission | 154 // Created lazily when an app or extension with the push messaging permission |
| 160 // is loaded. | 155 // is loaded. |
| 161 scoped_ptr<PushMessagingEventRouter> event_router_; | 156 scoped_ptr<PushMessagingEventRouter> event_router_; |
| 162 scoped_ptr<PushMessagingInvalidationMapper> handler_; | 157 scoped_ptr<PushMessagingInvalidationMapper> handler_; |
| 163 | 158 |
| 164 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 159 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 165 extension_registry_observer_; | 160 extension_registry_observer_; |
| 166 | 161 |
| 167 content::BrowserContext* browser_context_; | 162 content::BrowserContext* browser_context_; |
| 168 | 163 |
| 169 DISALLOW_COPY_AND_ASSIGN(PushMessagingAPI); | 164 DISALLOW_COPY_AND_ASSIGN(PushMessagingAPI); |
| 170 }; | 165 }; |
| 171 | 166 |
| 172 template <> | 167 template <> |
| 173 void BrowserContextKeyedAPIFactory< | 168 void BrowserContextKeyedAPIFactory< |
| 174 PushMessagingAPI>::DeclareFactoryDependencies(); | 169 PushMessagingAPI>::DeclareFactoryDependencies(); |
| 175 | 170 |
| 176 } // namespace extensions | 171 } // namespace extensions |
| 177 | 172 |
| 178 #endif // CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__ | 173 #endif // CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__ |
| OLD | NEW |