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 audio_modem
{
23 class WhispernetClient
;
26 namespace copresence
{
27 class CopresenceManager
;
34 namespace user_prefs
{
35 class PrefRegistrySyncable
;
38 namespace extensions
{
40 class CopresenceService final
: public BrowserContextKeyedAPI
,
41 public copresence::CopresenceDelegate
{
43 explicit CopresenceService(content::BrowserContext
* context
);
44 ~CopresenceService() override
;
46 // BrowserContextKeyedAPI implementation.
47 static const bool kServiceHasOwnInstanceInIncognito
= true;
48 void Shutdown() override
;
50 // These accessors will always return an object (except during shutdown).
51 // If the object doesn't exist, they will create one first.
52 copresence::CopresenceManager
* manager();
54 // A registry containing the app id's associated with every subscription.
55 SubscriptionToAppMap
& apps_by_subscription_id() {
56 return apps_by_subscription_id_
;
59 const std::string
auth_token(const std::string
& app_id
) const;
61 void set_api_key(const std::string
& app_id
,
62 const std::string
& api_key
);
64 void set_auth_token(const std::string
& app_id
,
65 const std::string
& token
);
67 // Delete all current copresence data, including stored device IDs.
70 // Manager override for testing.
71 void set_manager_for_testing(
72 scoped_ptr
<copresence::CopresenceManager
> manager
);
74 // Registers the preference for saving our device IDs.
75 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
77 // BrowserContextKeyedAPI implementation.
78 static BrowserContextKeyedAPIFactory
<CopresenceService
>* GetFactoryInstance();
81 friend class BrowserContextKeyedAPIFactory
<CopresenceService
>;
83 // CopresenceDelegate implementation
84 void HandleMessages(const std::string
& app_id
,
85 const std::string
& subscription_id
,
86 const std::vector
<copresence::Message
>& message
) override
;
87 void HandleStatusUpdate(copresence::CopresenceStatus status
) override
;
88 net::URLRequestContextGetter
* GetRequestContext() const override
;
89 const std::string
GetPlatformVersionString() const override
;
90 const std::string
GetAPIKey(const std::string
& app_id
) const override
;
91 audio_modem::WhispernetClient
* GetWhispernetClient() override
;
92 gcm::GCMDriver
* GetGCMDriver() override
;
93 const std::string
GetDeviceId(bool authenticated
) override
;
94 void SaveDeviceId(bool authenticated
, const std::string
& device_id
) override
;
96 // BrowserContextKeyedAPI implementation.
97 static const char* service_name() { return "CopresenceService"; }
99 PrefService
* GetPrefService();
101 bool is_shutting_down_
;
102 content::BrowserContext
* const browser_context_
;
104 std::map
<std::string
, std::string
> apps_by_subscription_id_
;
106 std::map
<std::string
, std::string
> api_keys_by_app_
;
107 std::map
<std::string
, std::string
> auth_tokens_by_app_
;
109 scoped_ptr
<audio_modem::WhispernetClient
> whispernet_client_
;
110 scoped_ptr
<copresence::CopresenceManager
> manager_
;
112 DISALLOW_COPY_AND_ASSIGN(CopresenceService
);
116 void BrowserContextKeyedAPIFactory
<
117 CopresenceService
>::DeclareFactoryDependencies();
119 class CopresenceExecuteFunction
: public ChromeUIThreadExtensionFunction
{
121 DECLARE_EXTENSION_FUNCTION("copresence.execute", COPRESENCE_EXECUTE
);
124 ~CopresenceExecuteFunction() override
{}
125 ExtensionFunction::ResponseAction
Run() override
;
128 void SendResult(copresence::CopresenceStatus status
);
131 // TODO(ckehoe): Remove this function.
132 class CopresenceSetApiKeyFunction
: public ChromeUIThreadExtensionFunction
{
134 DECLARE_EXTENSION_FUNCTION("copresence.setApiKey", COPRESENCE_SETAPIKEY
);
137 ~CopresenceSetApiKeyFunction() override
{}
138 ExtensionFunction::ResponseAction
Run() override
;
141 class CopresenceSetAuthTokenFunction
: public ChromeUIThreadExtensionFunction
{
143 DECLARE_EXTENSION_FUNCTION("copresence.setAuthToken",
144 COPRESENCE_SETAUTHTOKEN
);
147 ~CopresenceSetAuthTokenFunction() override
{}
148 ExtensionFunction::ResponseAction
Run() override
;
151 } // namespace extensions
153 #endif // CHROME_BROWSER_EXTENSIONS_API_COPRESENCE_COPRESENCE_API_H_