OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/audio_modem/audio_modem_api.h" | 5 #include "chrome/browser/extensions/api/audio_modem/audio_modem_api.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 |
8 #include <map> | 9 #include <map> |
| 10 #include <memory> |
9 #include <string> | 11 #include <string> |
10 #include <utility> | 12 #include <utility> |
11 #include <vector> | 13 #include <vector> |
12 | 14 |
13 #include "base/base64.h" | 15 #include "base/base64.h" |
14 #include "base/bind.h" | 16 #include "base/bind.h" |
15 #include "base/guid.h" | 17 #include "base/guid.h" |
16 #include "base/lazy_instance.h" | 18 #include "base/lazy_instance.h" |
17 #include "base/logging.h" | 19 #include "base/logging.h" |
18 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/ptr_util.h" |
19 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
20 #include "base/timer/timer.h" | 22 #include "base/timer/timer.h" |
21 #include "chrome/browser/copresence/chrome_whispernet_client.h" | 23 #include "chrome/browser/copresence/chrome_whispernet_client.h" |
22 #include "chrome/common/extensions/api/audio_modem.h" | 24 #include "chrome/common/extensions/api/audio_modem.h" |
23 #include "extensions/browser/event_router.h" | 25 #include "extensions/browser/event_router.h" |
24 | 26 |
25 // TODO(ckehoe): Implement transmit fail checking. | 27 // TODO(ckehoe): Implement transmit fail checking. |
26 | 28 |
27 namespace extensions { | 29 namespace extensions { |
28 | 30 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 return raw_token; | 99 return raw_token; |
98 } | 100 } |
99 | 101 |
100 } // namespace | 102 } // namespace |
101 | 103 |
102 | 104 |
103 // Public functions. | 105 // Public functions. |
104 | 106 |
105 AudioModemAPI::AudioModemAPI(content::BrowserContext* context) | 107 AudioModemAPI::AudioModemAPI(content::BrowserContext* context) |
106 : AudioModemAPI(context, | 108 : AudioModemAPI(context, |
107 make_scoped_ptr(new ChromeWhispernetClient(context)), | 109 base::WrapUnique(new ChromeWhispernetClient(context)), |
108 audio_modem::Modem::Create()) {} | 110 audio_modem::Modem::Create()) {} |
109 | 111 |
110 AudioModemAPI::AudioModemAPI( | 112 AudioModemAPI::AudioModemAPI( |
111 content::BrowserContext* context, | 113 content::BrowserContext* context, |
112 scoped_ptr<audio_modem::WhispernetClient> whispernet_client, | 114 std::unique_ptr<audio_modem::WhispernetClient> whispernet_client, |
113 scoped_ptr<audio_modem::Modem> modem) | 115 std::unique_ptr<audio_modem::Modem> modem) |
114 : browser_context_(context), | 116 : browser_context_(context), |
115 whispernet_client_(std::move(whispernet_client)), | 117 whispernet_client_(std::move(whispernet_client)), |
116 modem_(std::move(modem)), | 118 modem_(std::move(modem)), |
117 init_failed_(false) { | 119 init_failed_(false) { |
118 // We own these objects, so these callbacks will not outlive us. | 120 // We own these objects, so these callbacks will not outlive us. |
119 whispernet_client_->Initialize( | 121 whispernet_client_->Initialize( |
120 base::Bind(&AudioModemAPI::WhispernetInitComplete, | 122 base::Bind(&AudioModemAPI::WhispernetInitComplete, |
121 base::Unretained(this))); | 123 base::Unretained(this))); |
122 modem_->Initialize(whispernet_client_.get(), | 124 modem_->Initialize(whispernet_client_.get(), |
123 base::Bind(&AudioModemAPI::TokensReceived, | 125 base::Bind(&AudioModemAPI::TokensReceived, |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 // Send events to the appropriate app(s). | 246 // Send events to the appropriate app(s). |
245 for (const auto& app_entry : tokens_by_app) { | 247 for (const auto& app_entry : tokens_by_app) { |
246 const std::string& app_id = app_entry.first; | 248 const std::string& app_id = app_entry.first; |
247 const auto& app_tokens = app_entry.second; | 249 const auto& app_tokens = app_entry.second; |
248 if (app_id.empty()) | 250 if (app_id.empty()) |
249 continue; | 251 continue; |
250 | 252 |
251 // Construct the event arguments by hand because a given token can be | 253 // Construct the event arguments by hand because a given token can be |
252 // present for multiple listeners, so constructing a | 254 // present for multiple listeners, so constructing a |
253 // std::vector<ReceivedToken> for each is inefficient. | 255 // std::vector<ReceivedToken> for each is inefficient. |
254 scoped_ptr<base::ListValue> tokens_value(new base::ListValue()); | 256 std::unique_ptr<base::ListValue> tokens_value(new base::ListValue()); |
255 for (const ReceivedToken* token : app_tokens) | 257 for (const ReceivedToken* token : app_tokens) |
256 tokens_value->Append(token->ToValue()); | 258 tokens_value->Append(token->ToValue()); |
257 scoped_ptr<base::ListValue> args(new base::ListValue()); | 259 std::unique_ptr<base::ListValue> args(new base::ListValue()); |
258 args->Append(std::move(tokens_value)); | 260 args->Append(std::move(tokens_value)); |
259 | 261 |
260 EventRouter::Get(browser_context_) | 262 EventRouter::Get(browser_context_) |
261 ->DispatchEventToExtension( | 263 ->DispatchEventToExtension( |
262 app_id, make_scoped_ptr(new Event(events::AUDIO_MODEM_ON_RECEIVED, | 264 app_id, base::WrapUnique(new Event(events::AUDIO_MODEM_ON_RECEIVED, |
263 OnReceived::kEventName, | 265 OnReceived::kEventName, |
264 std::move(args)))); | 266 std::move(args)))); |
265 } | 267 } |
266 } | 268 } |
267 | 269 |
268 | 270 |
269 // Functions outside the API scope. | 271 // Functions outside the API scope. |
270 | 272 |
271 template <> | 273 template <> |
272 void | 274 void |
273 BrowserContextKeyedAPIFactory<AudioModemAPI>::DeclareFactoryDependencies() { | 275 BrowserContextKeyedAPIFactory<AudioModemAPI>::DeclareFactoryDependencies() { |
274 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 276 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
275 } | 277 } |
276 | 278 |
277 ExtensionFunction::ResponseAction AudioModemTransmitFunction::Run() { | 279 ExtensionFunction::ResponseAction AudioModemTransmitFunction::Run() { |
278 scoped_ptr<Transmit::Params> params(Transmit::Params::Create(*args_)); | 280 std::unique_ptr<Transmit::Params> params(Transmit::Params::Create(*args_)); |
279 EXTENSION_FUNCTION_VALIDATE(params.get()); | 281 EXTENSION_FUNCTION_VALIDATE(params.get()); |
280 AudioModemAPI* api = | 282 AudioModemAPI* api = |
281 AudioModemAPI::GetFactoryInstance()->Get(browser_context()); | 283 AudioModemAPI::GetFactoryInstance()->Get(browser_context()); |
282 if (api->init_failed()) { | 284 if (api->init_failed()) { |
283 return RespondNow(ErrorWithArguments( | 285 return RespondNow(ErrorWithArguments( |
284 Transmit::Results::Create(STATUS_CODERERROR), | 286 Transmit::Results::Create(STATUS_CODERERROR), |
285 kInitFailedError)); | 287 kInitFailedError)); |
286 } | 288 } |
287 | 289 |
288 // Check the token length. | 290 // Check the token length. |
(...skipping 20 matching lines...) Expand all Loading... |
309 } | 311 } |
310 if (timeout_millis > kMaxTransmitTimeout) | 312 if (timeout_millis > kMaxTransmitTimeout) |
311 timeout_millis = kMaxTransmitTimeout; | 313 timeout_millis = kMaxTransmitTimeout; |
312 | 314 |
313 // Start transmission. | 315 // Start transmission. |
314 Status status = api->StartTransmit(extension_id(), params->params, token_str); | 316 Status status = api->StartTransmit(extension_id(), params->params, token_str); |
315 return RespondNow(ArgumentList(Transmit::Results::Create(status))); | 317 return RespondNow(ArgumentList(Transmit::Results::Create(status))); |
316 } | 318 } |
317 | 319 |
318 ExtensionFunction::ResponseAction AudioModemStopTransmitFunction::Run() { | 320 ExtensionFunction::ResponseAction AudioModemStopTransmitFunction::Run() { |
319 scoped_ptr<StopTransmit::Params> params(StopTransmit::Params::Create(*args_)); | 321 std::unique_ptr<StopTransmit::Params> params( |
| 322 StopTransmit::Params::Create(*args_)); |
320 EXTENSION_FUNCTION_VALIDATE(params.get()); | 323 EXTENSION_FUNCTION_VALIDATE(params.get()); |
321 | 324 |
322 Status status = AudioModemAPI::GetFactoryInstance()->Get(browser_context()) | 325 Status status = AudioModemAPI::GetFactoryInstance()->Get(browser_context()) |
323 ->StopTransmit(extension_id(), AudioTypeForBand(params->band)); | 326 ->StopTransmit(extension_id(), AudioTypeForBand(params->band)); |
324 return RespondNow(ArgumentList(StopTransmit::Results::Create(status))); | 327 return RespondNow(ArgumentList(StopTransmit::Results::Create(status))); |
325 } | 328 } |
326 | 329 |
327 ExtensionFunction::ResponseAction AudioModemReceiveFunction::Run() { | 330 ExtensionFunction::ResponseAction AudioModemReceiveFunction::Run() { |
328 scoped_ptr<Receive::Params> params(Receive::Params::Create(*args_)); | 331 std::unique_ptr<Receive::Params> params(Receive::Params::Create(*args_)); |
329 EXTENSION_FUNCTION_VALIDATE(params.get()); | 332 EXTENSION_FUNCTION_VALIDATE(params.get()); |
330 AudioModemAPI* api = | 333 AudioModemAPI* api = |
331 AudioModemAPI::GetFactoryInstance()->Get(browser_context()); | 334 AudioModemAPI::GetFactoryInstance()->Get(browser_context()); |
332 if (api->init_failed()) { | 335 if (api->init_failed()) { |
333 return RespondNow(ErrorWithArguments( | 336 return RespondNow(ErrorWithArguments( |
334 Transmit::Results::Create(STATUS_CODERERROR), | 337 Transmit::Results::Create(STATUS_CODERERROR), |
335 kInitFailedError)); | 338 kInitFailedError)); |
336 } | 339 } |
337 | 340 |
338 // Check the timeout. | 341 // Check the timeout. |
339 int64_t timeout_millis = params->params.timeout_millis; | 342 int64_t timeout_millis = params->params.timeout_millis; |
340 if (timeout_millis <= 0) { | 343 if (timeout_millis <= 0) { |
341 return RespondNow(ErrorWithArguments( | 344 return RespondNow(ErrorWithArguments( |
342 Receive::Results::Create(STATUS_INVALIDREQUEST), | 345 Receive::Results::Create(STATUS_INVALIDREQUEST), |
343 kInvalidTimeoutError)); | 346 kInvalidTimeoutError)); |
344 } | 347 } |
345 if (timeout_millis > kMaxReceiveTimeout) | 348 if (timeout_millis > kMaxReceiveTimeout) |
346 timeout_millis = kMaxReceiveTimeout; | 349 timeout_millis = kMaxReceiveTimeout; |
347 | 350 |
348 // Start receiving. | 351 // Start receiving. |
349 api->StartReceive(extension_id(), params->params); | 352 api->StartReceive(extension_id(), params->params); |
350 return RespondNow(ArgumentList(Receive::Results::Create(STATUS_SUCCESS))); | 353 return RespondNow(ArgumentList(Receive::Results::Create(STATUS_SUCCESS))); |
351 } | 354 } |
352 | 355 |
353 ExtensionFunction::ResponseAction AudioModemStopReceiveFunction::Run() { | 356 ExtensionFunction::ResponseAction AudioModemStopReceiveFunction::Run() { |
354 scoped_ptr<StopReceive::Params> params(StopReceive::Params::Create(*args_)); | 357 std::unique_ptr<StopReceive::Params> params( |
| 358 StopReceive::Params::Create(*args_)); |
355 EXTENSION_FUNCTION_VALIDATE(params.get()); | 359 EXTENSION_FUNCTION_VALIDATE(params.get()); |
356 | 360 |
357 Status status = AudioModemAPI::GetFactoryInstance()->Get(browser_context()) | 361 Status status = AudioModemAPI::GetFactoryInstance()->Get(browser_context()) |
358 ->StopReceive(extension_id(), AudioTypeForBand(params->band)); | 362 ->StopReceive(extension_id(), AudioTypeForBand(params->band)); |
359 return RespondNow(ArgumentList(StopReceive::Results::Create(status))); | 363 return RespondNow(ArgumentList(StopReceive::Results::Create(status))); |
360 } | 364 } |
361 | 365 |
362 } // namespace extensions | 366 } // namespace extensions |
OLD | NEW |