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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__
6 #define CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__
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 "chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher.h"
15 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler_delegate.h"
16 #include "chrome/browser/extensions/chrome_extension_function.h"
17 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "extensions/browser/browser_context_keyed_api_factory.h"
21 #include "google_apis/gaia/google_service_auth_error.h"
22 #include "google_apis/gaia/oauth2_token_service.h"
30 namespace extensions {
32 class PushMessagingInvalidationMapper;
34 // Observes a single InvalidationHandler and generates onMessage events.
35 class PushMessagingEventRouter
36 : public PushMessagingInvalidationHandlerDelegate {
38 explicit PushMessagingEventRouter(Profile* profile);
39 virtual ~PushMessagingEventRouter();
41 // For testing purposes.
42 void TriggerMessageForTest(const std::string& extension_id,
44 const std::string& payload);
47 // InvalidationHandlerDelegate implementation.
48 virtual void OnMessage(const std::string& extension_id,
50 const std::string& payload) OVERRIDE;
52 Profile* const profile_;
54 DISALLOW_COPY_AND_ASSIGN(PushMessagingEventRouter);
57 class PushMessagingGetChannelIdFunction
58 : public ChromeAsyncExtensionFunction,
59 public ObfuscatedGaiaIdFetcher::Delegate,
60 public OAuth2TokenService::Observer,
61 public OAuth2TokenService::Consumer {
63 PushMessagingGetChannelIdFunction();
66 virtual ~PushMessagingGetChannelIdFunction();
69 virtual bool RunAsync() OVERRIDE;
70 DECLARE_EXTENSION_FUNCTION("pushMessaging.getChannelId",
71 PUSHMESSAGING_GETCHANNELID)
74 void ReportResult(const std::string& gaia_id,
75 const std::string& error_message);
77 void BuildAndSendResult(const std::string& gaia_id,
78 const std::string& error_message);
80 // Begin the async fetch of the Gaia ID.
81 void StartGaiaIdFetch(const std::string& access_token);
83 // Begin the async fetch of the access token for Gaia ID fetcher.
84 void StartAccessTokenFetch();
86 // OAuth2TokenService::Observer implementation.
87 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
89 // OAuth2TokenService::Consumer implementation.
90 virtual void OnGetTokenSuccess(
91 const OAuth2TokenService::Request* request,
92 const std::string& access_token,
93 const base::Time& expiration_time) OVERRIDE;
94 virtual void OnGetTokenFailure(
95 const OAuth2TokenService::Request* request,
96 const GoogleServiceAuthError& error) OVERRIDE;
98 // ObfuscatedGiaiaIdFetcher::Delegate implementation.
99 virtual void OnObfuscatedGaiaIdFetchSuccess(const std::string& gaia_id)
101 virtual void OnObfuscatedGaiaIdFetchFailure(
102 const GoogleServiceAuthError& error) OVERRIDE;
104 scoped_ptr<ObfuscatedGaiaIdFetcher> fetcher_;
106 scoped_ptr<OAuth2TokenService::Request> fetcher_access_token_request_;
108 DISALLOW_COPY_AND_ASSIGN(PushMessagingGetChannelIdFunction);
111 class PushMessagingAPI : public BrowserContextKeyedAPI,
112 public content::NotificationObserver {
114 explicit PushMessagingAPI(content::BrowserContext* context);
115 virtual ~PushMessagingAPI();
117 // Convenience method to get the PushMessagingAPI for a profile.
118 static PushMessagingAPI* Get(content::BrowserContext* context);
120 // KeyedService implementation.
121 virtual void Shutdown() OVERRIDE;
123 // BrowserContextKeyedAPI implementation.
124 static BrowserContextKeyedAPIFactory<PushMessagingAPI>* GetFactoryInstance();
126 // For testing purposes.
127 PushMessagingEventRouter* GetEventRouterForTest() const {
128 return event_router_.get();
130 PushMessagingInvalidationMapper* GetMapperForTest() const {
131 return handler_.get();
133 void SetMapperForTest(scoped_ptr<PushMessagingInvalidationMapper> mapper);
136 friend class BrowserContextKeyedAPIFactory<PushMessagingAPI>;
138 // BrowserContextKeyedAPI implementation.
139 static const char* service_name() {
140 return "PushMessagingAPI";
142 static const bool kServiceIsNULLWhileTesting = true;
144 // content::NotificationDelegate implementation.
145 virtual void Observe(int type,
146 const content::NotificationSource& source,
147 const content::NotificationDetails& details) OVERRIDE;
149 // Created lazily when an app or extension with the push messaging permission
151 scoped_ptr<PushMessagingEventRouter> event_router_;
152 scoped_ptr<PushMessagingInvalidationMapper> handler_;
154 content::NotificationRegistrar registrar_;
158 DISALLOW_COPY_AND_ASSIGN(PushMessagingAPI);
162 void BrowserContextKeyedAPIFactory<
163 PushMessagingAPI>::DeclareFactoryDependencies();
165 } // namespace extensions
167 #endif // CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__