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