blob: 71d33ec90318317b03c4efa2e66bbf8fe1a40dcc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/copresence_private/copresence_private_api.h"
#include <stddef.h>
#include <map>
#include <string>
#include <vector>
#include "base/guid.h"
#include "base/lazy_instance.h"
#include "chrome/browser/copresence/chrome_whispernet_client.h"
#include "chrome/common/extensions/api/copresence_private.h"
#include "content/public/browser/browser_thread.h"
#include "media/base/audio_bus.h"
using audio_modem::WhispernetClient;
using content::BrowserThread;
namespace extensions {
namespace SendFound = api::copresence_private::SendFound;
namespace SendSamples = api::copresence_private::SendSamples;
namespace SendInitialized = api::copresence_private::SendInitialized;
namespace {
base::LazyInstance<BrowserContextKeyedAPIFactory<CopresencePrivateService>>
g_factory = LAZY_INSTANCE_INITIALIZER;
void RunInitCallback(WhispernetClient* client, bool status) {
DCHECK(client);
audio_modem::SuccessCallback init_callback =
client->GetInitializedCallback();
if (!init_callback.is_null())
init_callback.Run(status);
}
} // namespace
CopresencePrivateService::CopresencePrivateService(
content::BrowserContext* context)
: initialized_(false) {}
CopresencePrivateService::~CopresencePrivateService() {}
const std::string CopresencePrivateService::RegisterWhispernetClient(
WhispernetClient* client) {
if (initialized_)
RunInitCallback(client, true);
std::string id = base::GenerateGUID();
whispernet_clients_[id] = client;
return id;
}
void CopresencePrivateService::OnWhispernetInitialized(bool success) {
if (success)
initialized_ = true;
DVLOG(2) << "Notifying " << whispernet_clients_.size()
<< " clients that initialization is complete.";
for (auto client_entry : whispernet_clients_)
RunInitCallback(client_entry.second, success);
}
WhispernetClient* CopresencePrivateService::GetWhispernetClient(
const std::string& id) {
WhispernetClient* client = whispernet_clients_[id];
DCHECK(client);
return client;
}
// static
BrowserContextKeyedAPIFactory<CopresencePrivateService>*
CopresencePrivateService::GetFactoryInstance() {
return g_factory.Pointer();
}
template <>
void BrowserContextKeyedAPIFactory<CopresencePrivateService>
::DeclareFactoryDependencies() {
DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
}
// Copresence Private functions.
// CopresenceSendFoundFunction implementation:
ExtensionFunction::ResponseAction CopresencePrivateSendFoundFunction::Run() {
scoped_ptr<SendFound::Params> params(SendFound::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
WhispernetClient* whispernet_client =
CopresencePrivateService::GetFactoryInstance()->Get(browser_context())
->GetWhispernetClient(params->client_id);
if (whispernet_client->GetTokensCallback().is_null())
return RespondNow(NoArguments());
std::vector<audio_modem::AudioToken> tokens;
for (size_t i = 0; i < params->tokens.size(); ++i) {
tokens.push_back(audio_modem::AudioToken(params->tokens[i].token,
params->tokens[i].audible));
}
whispernet_client->GetTokensCallback().Run(tokens);
return RespondNow(NoArguments());
}
// CopresenceSendEncodedFunction implementation:
ExtensionFunction::ResponseAction CopresencePrivateSendSamplesFunction::Run() {
scoped_ptr<SendSamples::Params> params(SendSamples::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
WhispernetClient* whispernet_client =
CopresencePrivateService::GetFactoryInstance()->Get(browser_context())
->GetWhispernetClient(params->client_id);
if (whispernet_client->GetSamplesCallback().is_null())
return RespondNow(NoArguments());
scoped_refptr<media::AudioBusRefCounted> samples =
media::AudioBusRefCounted::Create(1, // Mono
params->samples.size() / sizeof(float));
memcpy(samples->channel(0), params->samples.data(), params->samples.size());
whispernet_client->GetSamplesCallback().Run(
params->token.audible ? audio_modem::AUDIBLE : audio_modem::INAUDIBLE,
params->token.token, samples);
return RespondNow(NoArguments());
}
// CopresenceSendInitializedFunction implementation:
ExtensionFunction::ResponseAction
CopresencePrivateSendInitializedFunction::Run() {
scoped_ptr<SendInitialized::Params> params(
SendInitialized::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
CopresencePrivateService::GetFactoryInstance()->Get(browser_context())
->OnWhispernetInitialized(params->success);
return RespondNow(NoArguments());
}
} // namespace extensions
|