1 // Copyright 2014 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_COPRESENCE_COPRESENCE_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_COPRESENCE_COPRESENCE_API_H_
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "chrome/browser/extensions/api/copresence/copresence_translations.h"
15 #include "chrome/browser/extensions/chrome_extension_function.h"
16 #include "chrome/common/extensions/api/copresence.h"
17 #include "components/copresence/public/copresence_delegate.h"
18 #include "extensions/browser/browser_context_keyed_api_factory.h"
20 class ChromeWhispernetClient;
22 namespace copresence {
23 class CopresenceManager;
24 class WhispernetClient;
31 namespace extensions {
33 class CopresenceService : public BrowserContextKeyedAPI,
34 public copresence::CopresenceDelegate {
36 explicit CopresenceService(content::BrowserContext* context);
37 ~CopresenceService() override;
39 // BrowserContextKeyedAPI implementation.
40 void Shutdown() override;
42 // These accessors will always return an object (except during shutdown).
43 // If the object doesn't exist, they will create one first.
44 copresence::CopresenceManager* manager();
45 copresence::WhispernetClient* whispernet_client();
47 // A registry containing the app id's associated with every subscription.
48 SubscriptionToAppMap& apps_by_subscription_id() {
49 return apps_by_subscription_id_;
52 void set_api_key(const std::string& app_id,
53 const std::string& api_key);
55 std::string auth_token() const {
59 void set_auth_token(const std::string& token);
61 // Manager override for testing.
62 void set_manager_for_testing(
63 scoped_ptr<copresence::CopresenceManager> manager);
65 // BrowserContextKeyedAPI implementation.
66 static BrowserContextKeyedAPIFactory<CopresenceService>* GetFactoryInstance();
69 friend class BrowserContextKeyedAPIFactory<CopresenceService>;
71 // CopresenceDelegate implementation
72 void HandleMessages(const std::string& app_id,
73 const std::string& subscription_id,
74 const std::vector<copresence::Message>& message) override;
75 void HandleStatusUpdate(copresence::CopresenceStatus status) override;
76 net::URLRequestContextGetter* GetRequestContext() const override;
77 const std::string GetPlatformVersionString() const override;
78 const std::string GetAPIKey(const std::string& app_id) const override;
79 copresence::WhispernetClient* GetWhispernetClient() override;
80 gcm::GCMDriver* GetGCMDriver() override;
82 // BrowserContextKeyedAPI implementation.
83 static const char* service_name() { return "CopresenceService"; }
85 bool is_shutting_down_;
86 std::map<std::string, std::string> apps_by_subscription_id_;
88 content::BrowserContext* const browser_context_;
89 std::map<std::string, std::string> api_keys_by_app_;
91 // TODO(ckehoe): This is a temporary hack.
92 // Auth tokens from different apps needs to be separated properly.
93 std::string auth_token_;
95 scoped_ptr<copresence::CopresenceManager> manager_;
96 scoped_ptr<ChromeWhispernetClient> whispernet_client_;
98 DISALLOW_COPY_AND_ASSIGN(CopresenceService);
102 void BrowserContextKeyedAPIFactory<
103 CopresenceService>::DeclareFactoryDependencies();
105 class CopresenceExecuteFunction : public ChromeUIThreadExtensionFunction {
107 DECLARE_EXTENSION_FUNCTION("copresence.execute", COPRESENCE_EXECUTE);
110 ~CopresenceExecuteFunction() override {}
111 ExtensionFunction::ResponseAction Run() override;
114 void SendResult(copresence::CopresenceStatus status);
117 class CopresenceSetApiKeyFunction : public ChromeUIThreadExtensionFunction {
119 DECLARE_EXTENSION_FUNCTION("copresence.setApiKey", COPRESENCE_SETAPIKEY);
122 ~CopresenceSetApiKeyFunction() override {}
123 ExtensionFunction::ResponseAction Run() override;
126 class CopresenceSetAuthTokenFunction : public ChromeUIThreadExtensionFunction {
128 DECLARE_EXTENSION_FUNCTION("copresence.setAuthToken",
129 COPRESENCE_SETAUTHTOKEN);
132 virtual ~CopresenceSetAuthTokenFunction() {}
133 ExtensionFunction::ResponseAction Run() override;
136 } // namespace extensions
138 #endif // CHROME_BROWSER_EXTENSIONS_API_COPRESENCE_COPRESENCE_API_H_