Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / copresence_private / copresence_private_api.cc
blobb69384307f37689c0e6afdaf5aa24e673e56fab6
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"
7 #include <map>
8 #include <string>
9 #include <vector>
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;
28 namespace {
30 base::LazyInstance<BrowserContextKeyedAPIFactory<CopresencePrivateService>>
31 g_factory = LAZY_INSTANCE_INITIALIZER;
33 void RunInitCallback(WhispernetClient* client, bool status) {
34 DCHECK(client);
35 audio_modem::SuccessCallback init_callback =
36 client->GetInitializedCallback();
37 if (!init_callback.is_null())
38 init_callback.Run(status);
41 } // namespace
43 CopresencePrivateService::CopresencePrivateService(
44 content::BrowserContext* context)
45 : initialized_(false) {}
47 CopresencePrivateService::~CopresencePrivateService() {}
49 const std::string CopresencePrivateService::RegisterWhispernetClient(
50 WhispernetClient* client) {
51 if (initialized_)
52 RunInitCallback(client, true);
54 std::string id = base::GenerateGUID();
55 whispernet_clients_[id] = client;
57 return id;
60 void CopresencePrivateService::OnWhispernetInitialized(bool success) {
61 if (success)
62 initialized_ = true;
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];
73 DCHECK(client);
74 return client;
77 // static
78 BrowserContextKeyedAPIFactory<CopresencePrivateService>*
79 CopresencePrivateService::GetFactoryInstance() {
80 return g_factory.Pointer();
83 template <>
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(&params->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