Chromium Code Reviews| Index: chrome/browser/extensions/api/copresence_private/copresence_private_api.cc |
| diff --git a/chrome/browser/extensions/api/copresence_private/copresence_private_api.cc b/chrome/browser/extensions/api/copresence_private/copresence_private_api.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bd22edabeb200fa8d649b582a36f9db5fee91143 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/copresence_private/copresence_private_api.cc |
| @@ -0,0 +1,111 @@ |
| +// 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 "base/lazy_instance.h" |
| +#include "base/stl_util.h" |
| +#include "chrome/browser/copresence/chrome_whispernet_client.h" |
| +#include "chrome/common/extensions/api/copresence_private.h" |
| +#include "components/copresence/public/whispernet_client.h" |
| +#include "media/base/audio_bus.h" |
| + |
| +namespace extensions { |
| + |
| +// This code is only for testing while we don't have the rest of the |
| +// CopresenceAPI service which will actually give us the whispernet client. |
| +// Once we add that code, this entire namespace will go away. |
|
not at google - send to devlin
2014/07/31 23:31:18
"this entire namespace will go away" - what namesp
rkc
2014/08/01 19:20:00
This was referring to the util namespace, that I r
|
| +copresence::WhispernetClient* g_whispernet_client = NULL; |
| + |
| +copresence::WhispernetClient* GetWhispernetClient( |
| + content::BrowserContext* context) { |
| + // This is temporary code, this needs to be replaced by the real |
| + // GetWhispernetClient code from c/b/e/api/copresence/copresence_util.h |
| + DCHECK(g_whispernet_client); |
| + return g_whispernet_client; |
| +} |
| + |
| +// Copresence Private functions. |
| + |
| +// CopresenceSendFoundFunction implementation: |
| +ExtensionFunction::ResponseAction CopresencePrivateSendFoundFunction::Run() { |
| + if (!GetWhispernetClient(browser_context()) || |
| + GetWhispernetClient(browser_context())->GetTokensCallback().is_null()) { |
| + return RespondNow(ArgumentList(results_.Pass())); |
|
not at google - send to devlin
2014/07/31 23:31:18
given there are actually no results (and no result
rkc
2014/08/01 19:20:00
Done.
|
| + } |
| + |
| + scoped_ptr<api::copresence_private::SendFound::Params> params( |
| + api::copresence_private::SendFound::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params.get()); |
| + GetWhispernetClient(browser_context())->GetTokensCallback().Run( |
| + params->tokens); |
| + return RespondNow(ArgumentList(results_.Pass())); |
| +} |
| + |
| +// CopresenceSendEncodedFunction implementation: |
| +ExtensionFunction::ResponseAction CopresencePrivateSendSamplesFunction::Run() { |
| + if (!GetWhispernetClient(browser_context()) || |
| + GetWhispernetClient(browser_context())->GetSamplesCallback().is_null()) { |
| + return RespondNow(ArgumentList(results_.Pass())); |
| + } |
| + |
| + scoped_ptr<api::copresence_private::SendSamples::Params> params( |
| + api::copresence_private::SendSamples::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params.get()); |
| + |
| + scoped_refptr<media::AudioBusRefCounted> samples = |
| + media::AudioBusRefCounted::Create(1, |
| + params->samples.size() / sizeof(float)); |
| + |
| + memcpy(samples->channel(0), |
| + string_as_array(¶ms->samples), |
| + params->samples.size()); |
| + |
| + GetWhispernetClient(browser_context())->GetSamplesCallback().Run( |
| + params->token, samples); |
| + return RespondNow(ArgumentList(results_.Pass())); |
| +} |
| + |
| +// CopresenceSendDetectFunction implementation: |
| +ExtensionFunction::ResponseAction CopresencePrivateSendDetectFunction::Run() { |
| + if (!GetWhispernetClient(browser_context()) || |
| + GetWhispernetClient(browser_context()) |
| + ->GetDetectBroadcastCallback() |
| + .is_null()) { |
| + return RespondNow(ArgumentList(results_.Pass())); |
| + } |
| + |
| + scoped_ptr<api::copresence_private::SendDetect::Params> params( |
| + api::copresence_private::SendDetect::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params.get()); |
| + |
| + GetWhispernetClient(browser_context())->GetDetectBroadcastCallback().Run( |
| + params->detected); |
| + return RespondNow(ArgumentList(results_.Pass())); |
| +} |
| + |
| +// CopresenceSendInitializedFunction implementation: |
| +ExtensionFunction::ResponseAction |
| +CopresencePrivateSendInitializedFunction::Run() { |
| + if (!GetWhispernetClient(browser_context()) || |
| + GetWhispernetClient(browser_context()) |
| + ->GetInitializedCallback() |
| + .is_null()) { |
| + return RespondNow(ArgumentList(results_.Pass())); |
| + } |
| + |
| + scoped_ptr<api::copresence_private::SendInitialized::Params> params( |
| + api::copresence_private::SendInitialized::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params.get()); |
| + |
| + GetWhispernetClient(browser_context())->GetInitializedCallback().Run( |
| + params->success); |
| + return RespondNow(ArgumentList(results_.Pass())); |
| +} |
| + |
| +void SetWhispernetClientForTesting(copresence::WhispernetClient* client) { |
| + g_whispernet_client = client; |
| +} |
| + |
| +} // namespace extensions |