1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
// Copyright 2013 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.
#ifndef CHROME_BROWSER_EXTENSIONS_API_WEBRTC_AUDIO_PRIVATE_WEBRTC_AUDIO_PRIVATE_API_H_
#define CHROME_BROWSER_EXTENSIONS_API_WEBRTC_AUDIO_PRIVATE_WEBRTC_AUDIO_PRIVATE_API_H_
#include <stddef.h>
#include <string>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/system/system_monitor.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/common/extensions/api/webrtc_audio_private.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/resource_context.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "media/audio/audio_device_description.h"
namespace media {
class AudioSystem;
}
namespace extensions {
// Listens for device changes and forwards as an extension event.
class WebrtcAudioPrivateEventService
: public BrowserContextKeyedAPI,
public base::SystemMonitor::DevicesChangedObserver {
public:
explicit WebrtcAudioPrivateEventService(content::BrowserContext* context);
~WebrtcAudioPrivateEventService() override;
// BrowserContextKeyedAPI implementation.
void Shutdown() override;
static BrowserContextKeyedAPIFactory<WebrtcAudioPrivateEventService>*
GetFactoryInstance();
static const char* service_name();
// base::SystemMonitor::DevicesChangedObserver implementation.
void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override;
private:
friend class BrowserContextKeyedAPIFactory<WebrtcAudioPrivateEventService>;
void SignalEvent();
content::BrowserContext* browser_context_;
};
// Common base for WebrtcAudioPrivate functions, that provides a
// couple of optionally-used common implementations.
class WebrtcAudioPrivateFunction : public ChromeAsyncExtensionFunction {
protected:
WebrtcAudioPrivateFunction();
~WebrtcAudioPrivateFunction() override;
protected:
// Calculates a single HMAC, using the extension ID as the security origin.
std::string CalculateHMAC(const std::string& raw_id);
// Initializes |device_id_salt_|. Must be called on the UI thread,
// before any calls to |device_id_salt()|.
void InitDeviceIDSalt();
// Callable from any thread. Must previously have called
// |InitDeviceIDSalt()|.
std::string device_id_salt() const;
media::AudioSystem* GetAudioSystem();
// Returns the RenderProcessHost associated with the given |request|
// authorized by the |security_origin|. Returns null if unauthorized or
// the RPH does not exist.
content::RenderProcessHost* GetRenderProcessHostFromRequest(
const api::webrtc_audio_private::RequestInfo& request,
const std::string& security_origin);
private:
std::string device_id_salt_;
std::unique_ptr<media::AudioSystem> audio_system_;
DISALLOW_COPY_AND_ASSIGN(WebrtcAudioPrivateFunction);
};
class WebrtcAudioPrivateGetSinksFunction : public WebrtcAudioPrivateFunction {
protected:
~WebrtcAudioPrivateGetSinksFunction() override {}
private:
using SinkInfoVector = std::vector<api::webrtc_audio_private::SinkInfo>;
DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.getSinks",
WEBRTC_AUDIO_PRIVATE_GET_SINKS);
bool RunAsync() override;
// Receives output device descriptions, calculates HMACs for them and sends
// the response.
void ReceiveOutputDeviceDescriptions(
media::AudioDeviceDescriptions sink_devices);
};
class WebrtcAudioPrivateGetAssociatedSinkFunction
: public WebrtcAudioPrivateFunction {
public:
WebrtcAudioPrivateGetAssociatedSinkFunction();
protected:
~WebrtcAudioPrivateGetAssociatedSinkFunction() override;
private:
DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.getAssociatedSink",
WEBRTC_AUDIO_PRIVATE_GET_ASSOCIATED_SINK);
// UI thread: Entry point, posts GetInputDeviceDescriptions() to IO thread.
bool RunAsync() override;
// Receives the input device descriptions, looks up the raw source device ID
// basing on |params|, and requests the associated raw sink ID for it.
void ReceiveInputDeviceDescriptions(
media::AudioDeviceDescriptions source_devices);
// Receives the raw sink ID, calculates HMAC and calls Reply().
void CalculateHMACAndReply(const base::Optional<std::string>& raw_sink_id);
// Receives the associated sink ID as HMAC and sends the response.
void Reply(const std::string& hmac);
std::unique_ptr<api::webrtc_audio_private::GetAssociatedSink::Params> params_;
};
class WebrtcAudioPrivateSetAudioExperimentsFunction
: public WebrtcAudioPrivateFunction {
public:
WebrtcAudioPrivateSetAudioExperimentsFunction();
protected:
~WebrtcAudioPrivateSetAudioExperimentsFunction() override;
private:
DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.setAudioExperiments",
WEBRTC_AUDIO_PRIVATE_SET_AUDIO_EXPERIMENTS);
bool RunAsync() override;
// Must be called on the UI thread.
void FireCallback(bool success, const std::string& error_message);
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_WEBRTC_AUDIO_PRIVATE_WEBRTC_AUDIO_PRIVATE_API_H_
|