/chromium-webcl/src/chrome/browser/extensions/api/push_messaging/push_messaging_api.h

https://bitbucket.org/peixuan/chromium_r197479_base · C Header · 155 lines · 98 code · 37 blank · 20 comment · 0 complexity · ed33aa7a09a8b0682baac9eb3c07c129 MD5 · raw file

  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. #ifndef CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__
  5. #define CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__
  6. #include <string>
  7. #include "base/basictypes.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/gtest_prod_util.h"
  10. #include "base/memory/scoped_ptr.h"
  11. #include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
  12. #include "chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher.h"
  13. #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidation_handler_delegate.h"
  14. #include "chrome/browser/extensions/extension_function.h"
  15. #include "chrome/browser/ui/webui/signin/login_ui_service.h"
  16. #include "content/public/browser/notification_observer.h"
  17. #include "content/public/browser/notification_registrar.h"
  18. #include "google_apis/gaia/google_service_auth_error.h"
  19. class Profile;
  20. namespace extensions {
  21. class PushMessagingInvalidationMapper;
  22. class ObfuscatedGaiaIdFetcher;
  23. // Observes a single InvalidationHandler and generates onMessage events.
  24. class PushMessagingEventRouter
  25. : public PushMessagingInvalidationHandlerDelegate {
  26. public:
  27. explicit PushMessagingEventRouter(Profile* profile);
  28. virtual ~PushMessagingEventRouter();
  29. // For testing purposes.
  30. void TriggerMessageForTest(const std::string& extension_id,
  31. int subchannel,
  32. const std::string& payload);
  33. private:
  34. // InvalidationHandlerDelegate implementation.
  35. virtual void OnMessage(const std::string& extension_id,
  36. int subchannel,
  37. const std::string& payload) OVERRIDE;
  38. Profile* const profile_;
  39. DISALLOW_COPY_AND_ASSIGN(PushMessagingEventRouter);
  40. };
  41. class PushMessagingGetChannelIdFunction
  42. : public AsyncExtensionFunction,
  43. public ObfuscatedGaiaIdFetcher::Delegate,
  44. public content::NotificationObserver {
  45. public:
  46. PushMessagingGetChannelIdFunction();
  47. protected:
  48. virtual ~PushMessagingGetChannelIdFunction();
  49. // ExtensionFunction:
  50. virtual bool RunImpl() OVERRIDE;
  51. DECLARE_EXTENSION_FUNCTION("pushMessaging.getChannelId",
  52. PUSHMESSAGING_GETCHANNELID)
  53. private:
  54. void ReportResult(const std::string& gaia_id,
  55. const std::string& error_message);
  56. void BuildAndSendResult(const std::string& gaia_id,
  57. const std::string& error_message);
  58. // Begin the async fetch of the Gaia ID.
  59. bool StartGaiaIdFetch();
  60. // content::NotificationObserver implementation.
  61. virtual void Observe(int type,
  62. const content::NotificationSource& source,
  63. const content::NotificationDetails& details) OVERRIDE;
  64. // Check if the user is signed into chrome.
  65. bool IsUserLoggedIn() const;
  66. // ObfuscatedGiaiaIdFetcher::Delegate implementation.
  67. virtual void OnObfuscatedGaiaIdFetchSuccess(const std::string& gaia_id)
  68. OVERRIDE;
  69. virtual void OnObfuscatedGaiaIdFetchFailure(
  70. const GoogleServiceAuthError& error) OVERRIDE;
  71. scoped_ptr<ObfuscatedGaiaIdFetcher> fetcher_;
  72. bool interactive_;
  73. // We use this to register for notifications if the logon attept succeeds.
  74. content::NotificationRegistrar registrar_;
  75. DISALLOW_COPY_AND_ASSIGN(PushMessagingGetChannelIdFunction);
  76. };
  77. class PushMessagingAPI : public ProfileKeyedAPI,
  78. public content::NotificationObserver {
  79. public:
  80. explicit PushMessagingAPI(Profile* profile);
  81. virtual ~PushMessagingAPI();
  82. // Convenience method to get the PushMessagingAPI for a profile.
  83. static PushMessagingAPI* Get(Profile* profile);
  84. // ProfileKeyedService implementation.
  85. virtual void Shutdown() OVERRIDE;
  86. // ProfileKeyedAPI implementation.
  87. static ProfileKeyedAPIFactory<PushMessagingAPI>* GetFactoryInstance();
  88. // For testing purposes.
  89. PushMessagingEventRouter* GetEventRouterForTest() const {
  90. return event_router_.get();
  91. }
  92. PushMessagingInvalidationMapper* GetMapperForTest() const {
  93. return handler_.get();
  94. }
  95. void SetMapperForTest(scoped_ptr<PushMessagingInvalidationMapper> mapper);
  96. private:
  97. friend class ProfileKeyedAPIFactory<PushMessagingAPI>;
  98. // ProfileKeyedAPI implementation.
  99. static const char* service_name() {
  100. return "PushMessagingAPI";
  101. }
  102. static const bool kServiceIsNULLWhileTesting = true;
  103. // content::NotificationDelegate implementation.
  104. virtual void Observe(int type,
  105. const content::NotificationSource& source,
  106. const content::NotificationDetails& details) OVERRIDE;
  107. // Created lazily when an app or extension with the push messaging permission
  108. // is loaded.
  109. scoped_ptr<PushMessagingEventRouter> event_router_;
  110. scoped_ptr<PushMessagingInvalidationMapper> handler_;
  111. content::NotificationRegistrar registrar_;
  112. Profile* profile_;
  113. DISALLOW_COPY_AND_ASSIGN(PushMessagingAPI);
  114. };
  115. template <>
  116. void ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies();
  117. } // namespace extensions
  118. #endif // CHROME_BROWSER_EXTENSIONS_API_PUSH_MESSAGING_PUSH_MESSAGING_API_H__