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