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