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