Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / push_messaging / push_messaging_api.h
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 "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"
23
24 class Profile;
25
26 namespace content {
27 class BrowserContext;
28 }
29
30 namespace extensions {
31
32 class PushMessagingInvalidationMapper;
33
34 // Observes a single InvalidationHandler and generates onMessage events.
35 class PushMessagingEventRouter
36     : public PushMessagingInvalidationHandlerDelegate {
37  public:
38   explicit PushMessagingEventRouter(Profile* profile);
39   virtual ~PushMessagingEventRouter();
40
41   // For testing purposes.
42   void TriggerMessageForTest(const std::string& extension_id,
43                              int subchannel,
44                              const std::string& payload);
45
46  private:
47   // InvalidationHandlerDelegate implementation.
48   virtual void OnMessage(const std::string& extension_id,
49                          int subchannel,
50                          const std::string& payload) OVERRIDE;
51
52   Profile* const profile_;
53
54   DISALLOW_COPY_AND_ASSIGN(PushMessagingEventRouter);
55 };
56
57 class PushMessagingGetChannelIdFunction
58     : public ChromeAsyncExtensionFunction,
59       public ObfuscatedGaiaIdFetcher::Delegate,
60       public OAuth2TokenService::Observer,
61       public OAuth2TokenService::Consumer {
62  public:
63   PushMessagingGetChannelIdFunction();
64
65  protected:
66   virtual ~PushMessagingGetChannelIdFunction();
67
68   // ExtensionFunction:
69   virtual bool RunAsync() OVERRIDE;
70   DECLARE_EXTENSION_FUNCTION("pushMessaging.getChannelId",
71                              PUSHMESSAGING_GETCHANNELID)
72
73  private:
74   void ReportResult(const std::string& gaia_id,
75                     const std::string& error_message);
76
77   void BuildAndSendResult(const std::string& gaia_id,
78                           const std::string& error_message);
79
80   // Begin the async fetch of the Gaia ID.
81   void StartGaiaIdFetch(const std::string& access_token);
82
83   // Begin the async fetch of the access token for Gaia ID fetcher.
84   void StartAccessTokenFetch();
85
86   // OAuth2TokenService::Observer implementation.
87   virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
88
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;
97
98   // ObfuscatedGiaiaIdFetcher::Delegate implementation.
99   virtual void OnObfuscatedGaiaIdFetchSuccess(const std::string& gaia_id)
100       OVERRIDE;
101   virtual void OnObfuscatedGaiaIdFetchFailure(
102       const GoogleServiceAuthError& error) OVERRIDE;
103
104   scoped_ptr<ObfuscatedGaiaIdFetcher> fetcher_;
105   bool interactive_;
106   scoped_ptr<OAuth2TokenService::Request> fetcher_access_token_request_;
107
108   DISALLOW_COPY_AND_ASSIGN(PushMessagingGetChannelIdFunction);
109 };
110
111 class PushMessagingAPI : public BrowserContextKeyedAPI,
112                          public content::NotificationObserver {
113  public:
114   explicit PushMessagingAPI(content::BrowserContext* context);
115   virtual ~PushMessagingAPI();
116
117   // Convenience method to get the PushMessagingAPI for a profile.
118   static PushMessagingAPI* Get(content::BrowserContext* context);
119
120   // KeyedService implementation.
121   virtual void Shutdown() OVERRIDE;
122
123   // BrowserContextKeyedAPI implementation.
124   static BrowserContextKeyedAPIFactory<PushMessagingAPI>* GetFactoryInstance();
125
126   // For testing purposes.
127   PushMessagingEventRouter* GetEventRouterForTest() const {
128   return event_router_.get();
129   }
130   PushMessagingInvalidationMapper* GetMapperForTest() const {
131     return handler_.get();
132   }
133   void SetMapperForTest(scoped_ptr<PushMessagingInvalidationMapper> mapper);
134
135  private:
136   friend class BrowserContextKeyedAPIFactory<PushMessagingAPI>;
137
138   // BrowserContextKeyedAPI implementation.
139   static const char* service_name() {
140     return "PushMessagingAPI";
141   }
142   static const bool kServiceIsNULLWhileTesting = true;
143
144   // content::NotificationDelegate implementation.
145   virtual void Observe(int type,
146                        const content::NotificationSource& source,
147                        const content::NotificationDetails& details) OVERRIDE;
148
149   // Created lazily when an app or extension with the push messaging permission
150   // is loaded.
151   scoped_ptr<PushMessagingEventRouter> event_router_;
152   scoped_ptr<PushMessagingInvalidationMapper> handler_;
153
154   content::NotificationRegistrar registrar_;
155
156   Profile* profile_;
157
158   DISALLOW_COPY_AND_ASSIGN(PushMessagingAPI);
159 };
160
161 template <>
162 void BrowserContextKeyedAPIFactory<
163     PushMessagingAPI>::DeclareFactoryDependencies();
164
165 }  // namespace extensions
166
167 #endif  // CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__