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_api.h"
6
7#include <vector>
8
9#include "base/lazy_instance.h"
10#include "base/stl_util.h"
11#include "chrome/browser/copresence/chrome_whispernet_client.h"
12#include "chrome/browser/extensions/api/copresence/copresence_api.h"
13#include "chrome/common/extensions/api/copresence_private.h"
14#include "components/copresence/public/whispernet_client.h"
15#include "media/base/audio_bus.h"
16
17namespace extensions {
18
19// Copresence Private functions.
20
21copresence::WhispernetClient* CopresencePrivateFunction::GetWhispernetClient() {
22  CopresenceService* service =
23      CopresenceService::GetFactoryInstance()->Get(browser_context());
24  return service ? service->whispernet_client() : NULL;
25}
26
27// CopresenceSendFoundFunction implementation:
28ExtensionFunction::ResponseAction CopresencePrivateSendFoundFunction::Run() {
29  if (!GetWhispernetClient() ||
30      GetWhispernetClient()->GetTokensCallback().is_null()) {
31    return RespondNow(NoArguments());
32  }
33
34  scoped_ptr<api::copresence_private::SendFound::Params> params(
35      api::copresence_private::SendFound::Params::Create(*args_));
36  EXTENSION_FUNCTION_VALIDATE(params.get());
37  std::vector<copresence::AudioToken> tokens;
38  for (size_t i = 0; i < params->tokens.size(); ++i) {
39    tokens.push_back(copresence::AudioToken(params->tokens[i]->token,
40                                            params->tokens[i]->audible));
41  }
42  GetWhispernetClient()->GetTokensCallback().Run(tokens);
43  return RespondNow(NoArguments());
44}
45
46// CopresenceSendEncodedFunction implementation:
47ExtensionFunction::ResponseAction CopresencePrivateSendSamplesFunction::Run() {
48  if (!GetWhispernetClient() ||
49      GetWhispernetClient()->GetSamplesCallback().is_null()) {
50    return RespondNow(NoArguments());
51  }
52
53  scoped_ptr<api::copresence_private::SendSamples::Params> params(
54      api::copresence_private::SendSamples::Params::Create(*args_));
55  EXTENSION_FUNCTION_VALIDATE(params.get());
56
57  scoped_refptr<media::AudioBusRefCounted> samples =
58      media::AudioBusRefCounted::Create(1,
59                                        params->samples.size() / sizeof(float));
60
61  memcpy(samples->channel(0),
62         string_as_array(&params->samples),
63         params->samples.size());
64
65  GetWhispernetClient()->GetSamplesCallback().Run(
66      params->token.token, params->token.audible, samples);
67  return RespondNow(NoArguments());
68}
69
70// CopresenceSendDetectFunction implementation:
71ExtensionFunction::ResponseAction CopresencePrivateSendDetectFunction::Run() {
72  if (!GetWhispernetClient() ||
73      GetWhispernetClient()->GetDetectBroadcastCallback().is_null()) {
74    return RespondNow(NoArguments());
75  }
76
77  scoped_ptr<api::copresence_private::SendDetect::Params> params(
78      api::copresence_private::SendDetect::Params::Create(*args_));
79  EXTENSION_FUNCTION_VALIDATE(params.get());
80
81  GetWhispernetClient()->GetDetectBroadcastCallback().Run(params->detected);
82  return RespondNow(NoArguments());
83}
84
85// CopresenceSendInitializedFunction implementation:
86ExtensionFunction::ResponseAction
87CopresencePrivateSendInitializedFunction::Run() {
88  if (!GetWhispernetClient() ||
89      GetWhispernetClient()->GetInitializedCallback().is_null()) {
90    return RespondNow(NoArguments());
91  }
92
93  scoped_ptr<api::copresence_private::SendInitialized::Params> params(
94      api::copresence_private::SendInitialized::Params::Create(*args_));
95  EXTENSION_FUNCTION_VALIDATE(params.get());
96
97  GetWhispernetClient()->GetInitializedCallback().Run(params->success);
98  return RespondNow(NoArguments());
99}
100
101}  // namespace extensions
102