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 #include "chrome/browser/extensions/api/copresence_private/copresence_private_api.h"
11 #include "base/guid.h"
12 #include "base/lazy_instance.h"
13 #include "base/stl_util.h"
14 #include "chrome/browser/copresence/chrome_whispernet_client.h"
15 #include "chrome/common/extensions/api/copresence_private.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "media/base/audio_bus.h"
19 using audio_modem::WhispernetClient
;
20 using content::BrowserThread
;
22 namespace extensions
{
24 namespace SendFound
= api::copresence_private::SendFound
;
25 namespace SendSamples
= api::copresence_private::SendSamples
;
26 namespace SendInitialized
= api::copresence_private::SendInitialized
;
30 base::LazyInstance
<BrowserContextKeyedAPIFactory
<CopresencePrivateService
>>
31 g_factory
= LAZY_INSTANCE_INITIALIZER
;
33 void RunInitCallback(WhispernetClient
* client
, bool status
) {
35 audio_modem::SuccessCallback init_callback
=
36 client
->GetInitializedCallback();
37 if (!init_callback
.is_null())
38 init_callback
.Run(status
);
43 CopresencePrivateService::CopresencePrivateService(
44 content::BrowserContext
* context
)
45 : initialized_(false) {}
47 CopresencePrivateService::~CopresencePrivateService() {}
49 const std::string
CopresencePrivateService::RegisterWhispernetClient(
50 WhispernetClient
* client
) {
52 RunInitCallback(client
, true);
54 std::string id
= base::GenerateGUID();
55 whispernet_clients_
[id
] = client
;
60 void CopresencePrivateService::OnWhispernetInitialized(bool success
) {
64 DVLOG(2) << "Notifying " << whispernet_clients_
.size()
65 << " clients that initialization is complete.";
66 for (auto client_entry
: whispernet_clients_
)
67 RunInitCallback(client_entry
.second
, success
);
70 WhispernetClient
* CopresencePrivateService::GetWhispernetClient(
71 const std::string
& id
) {
72 WhispernetClient
* client
= whispernet_clients_
[id
];
78 BrowserContextKeyedAPIFactory
<CopresencePrivateService
>*
79 CopresencePrivateService::GetFactoryInstance() {
80 return g_factory
.Pointer();
84 void BrowserContextKeyedAPIFactory
<CopresencePrivateService
>
85 ::DeclareFactoryDependencies() {
86 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
90 // Copresence Private functions.
92 // CopresenceSendFoundFunction implementation:
93 ExtensionFunction::ResponseAction
CopresencePrivateSendFoundFunction::Run() {
94 scoped_ptr
<SendFound::Params
> params(SendFound::Params::Create(*args_
));
95 EXTENSION_FUNCTION_VALIDATE(params
.get());
97 WhispernetClient
* whispernet_client
=
98 CopresencePrivateService::GetFactoryInstance()->Get(browser_context())
99 ->GetWhispernetClient(params
->client_id
);
100 if (whispernet_client
->GetTokensCallback().is_null())
101 return RespondNow(NoArguments());
103 std::vector
<audio_modem::AudioToken
> tokens
;
104 for (size_t i
= 0; i
< params
->tokens
.size(); ++i
) {
105 tokens
.push_back(audio_modem::AudioToken(params
->tokens
[i
]->token
,
106 params
->tokens
[i
]->audible
));
108 whispernet_client
->GetTokensCallback().Run(tokens
);
109 return RespondNow(NoArguments());
112 // CopresenceSendEncodedFunction implementation:
113 ExtensionFunction::ResponseAction
CopresencePrivateSendSamplesFunction::Run() {
114 scoped_ptr
<SendSamples::Params
> params(SendSamples::Params::Create(*args_
));
115 EXTENSION_FUNCTION_VALIDATE(params
.get());
117 WhispernetClient
* whispernet_client
=
118 CopresencePrivateService::GetFactoryInstance()->Get(browser_context())
119 ->GetWhispernetClient(params
->client_id
);
120 if (whispernet_client
->GetSamplesCallback().is_null())
121 return RespondNow(NoArguments());
123 scoped_refptr
<media::AudioBusRefCounted
> samples
=
124 media::AudioBusRefCounted::Create(1, // Mono
125 params
->samples
.size() / sizeof(float));
127 memcpy(samples
->channel(0), vector_as_array(¶ms
->samples
),
128 params
->samples
.size());
130 whispernet_client
->GetSamplesCallback().Run(
131 params
->token
.audible
? audio_modem::AUDIBLE
: audio_modem::INAUDIBLE
,
132 params
->token
.token
, samples
);
133 return RespondNow(NoArguments());
136 // CopresenceSendInitializedFunction implementation:
137 ExtensionFunction::ResponseAction
138 CopresencePrivateSendInitializedFunction::Run() {
139 scoped_ptr
<SendInitialized::Params
> params(
140 SendInitialized::Params::Create(*args_
));
141 EXTENSION_FUNCTION_VALIDATE(params
.get());
143 CopresencePrivateService::GetFactoryInstance()->Get(browser_context())
144 ->OnWhispernetInitialized(params
->success
);
146 return RespondNow(NoArguments());
149 } // namespace extensions