1 // Copyright 2013 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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_WEBRTC_AUDIO_PRIVATE_WEBRTC_AUDIO_PRIVATE_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_WEBRTC_AUDIO_PRIVATE_WEBRTC_AUDIO_PRIVATE_API_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/system_monitor/system_monitor.h"
10 #include "chrome/browser/extensions/chrome_extension_function.h"
11 #include "chrome/common/extensions/api/webrtc_audio_private.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "extensions/browser/browser_context_keyed_api_factory.h"
14 #include "media/audio/audio_device_name.h"
18 class ResourceContext
;
21 namespace extensions
{
23 // Listens for device changes and forwards as an extension event.
24 class WebrtcAudioPrivateEventService
25 : public BrowserContextKeyedAPI
,
26 public base::SystemMonitor::DevicesChangedObserver
{
28 explicit WebrtcAudioPrivateEventService(content::BrowserContext
* context
);
29 ~WebrtcAudioPrivateEventService() override
;
31 // BrowserContextKeyedAPI implementation.
32 void Shutdown() override
;
33 static BrowserContextKeyedAPIFactory
<WebrtcAudioPrivateEventService
>*
35 static const char* service_name();
37 // base::SystemMonitor::DevicesChangedObserver implementation.
38 void OnDevicesChanged(base::SystemMonitor::DeviceType device_type
) override
;
41 friend class BrowserContextKeyedAPIFactory
<WebrtcAudioPrivateEventService
>;
45 content::BrowserContext
* browser_context_
;
48 // Common base for WebrtcAudioPrivate functions, that provides a
49 // couple of optionally-used common implementations.
50 class WebrtcAudioPrivateFunction
: public ChromeAsyncExtensionFunction
{
52 WebrtcAudioPrivateFunction();
53 ~WebrtcAudioPrivateFunction() override
;
56 // Retrieves the list of output device names on the appropriate
57 // thread. Call from UI thread, callback will occur on IO thread.
58 void GetOutputDeviceNames();
60 // Must override this if you call GetOutputDeviceNames. Called on IO thread.
61 virtual void OnOutputDeviceNames(
62 scoped_ptr
<media::AudioDeviceNames
> device_names
);
64 // Retrieve the list of AudioOutputController objects. Calls back
65 // via OnControllerList.
67 // Returns false on error, in which case it has set |error_| and the
68 // entire function should fail.
70 // Call from any thread. Callback will occur on originating thread.
71 bool GetControllerList(const api::webrtc_audio_private::RequestInfo
& request
);
73 // Must override this if you call GetControllerList.
74 virtual void OnControllerList(
75 const content::RenderProcessHost::AudioOutputControllerList
& list
);
77 // Calculates a single HMAC. Call from any thread. Calls back via
78 // OnHMACCalculated on UI thread.
80 // This function, and device ID HMACs in this API in general use the
81 // calling extension's ID as the security origin. The only exception
82 // to this rule is when calculating the input device ID HMAC in
83 // getAssociatedSink, where we use the provided |securityOrigin|.
84 void CalculateHMAC(const std::string
& raw_id
);
86 // Must override this if you call CalculateHMAC.
87 virtual void OnHMACCalculated(const std::string
& hmac
);
89 // Calculates a single HMAC, using the extension ID as the security origin.
91 // Call only on IO thread.
92 std::string
CalculateHMACImpl(const std::string
& raw_id
);
94 // Initializes |resource_context_|. Must be called on the UI thread,
95 // before any calls to |resource_context()|.
96 void InitResourceContext();
98 // Callable from any thread. Must previously have called
99 // |InitResourceContext()|.
100 content::ResourceContext
* resource_context() const;
103 content::ResourceContext
* resource_context_
;
105 DISALLOW_COPY_AND_ASSIGN(WebrtcAudioPrivateFunction
);
108 class WebrtcAudioPrivateGetSinksFunction
: public WebrtcAudioPrivateFunction
{
110 ~WebrtcAudioPrivateGetSinksFunction() override
{}
113 DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.getSinks",
114 WEBRTC_AUDIO_PRIVATE_GET_SINKS
);
116 // Sequence of events is that we query the list of sinks on the
117 // AudioManager's thread, then calculate HMACs on the IO thread,
118 // then finish on the UI thread.
119 bool RunAsync() override
;
121 void OnOutputDeviceNames(
122 scoped_ptr
<media::AudioDeviceNames
> raw_ids
) override
;
123 void DoneOnUIThread();
126 class WebrtcAudioPrivateGetActiveSinkFunction
127 : public WebrtcAudioPrivateFunction
{
129 ~WebrtcAudioPrivateGetActiveSinkFunction() override
{}
132 DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.getActiveSink",
133 WEBRTC_AUDIO_PRIVATE_GET_ACTIVE_SINK
);
135 bool RunAsync() override
;
136 void OnControllerList(
137 const content::RenderProcessHost::AudioOutputControllerList
& controllers
)
139 void OnHMACCalculated(const std::string
& hmac
) override
;
142 class WebrtcAudioPrivateSetActiveSinkFunction
143 : public WebrtcAudioPrivateFunction
{
145 WebrtcAudioPrivateSetActiveSinkFunction();
148 ~WebrtcAudioPrivateSetActiveSinkFunction() override
;
151 DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.setActiveSink",
152 WEBRTC_AUDIO_PRIVATE_SET_ACTIVE_SINK
);
154 bool RunAsync() override
;
155 void OnControllerList(
156 const content::RenderProcessHost::AudioOutputControllerList
& controllers
)
158 void OnOutputDeviceNames(
159 scoped_ptr
<media::AudioDeviceNames
> device_names
) override
;
161 void DoneOnUIThread();
163 api::webrtc_audio_private::RequestInfo request_info_
;
164 std::string sink_id_
;
166 // Filled in by OnControllerList.
167 content::RenderProcessHost::AudioOutputControllerList controllers_
;
169 // Number of sink IDs we are still waiting for. Can become greater
170 // than 0 in OnControllerList, decreases on every OnSinkId call.
171 size_t num_remaining_sink_ids_
;
174 class WebrtcAudioPrivateGetAssociatedSinkFunction
175 : public WebrtcAudioPrivateFunction
{
177 WebrtcAudioPrivateGetAssociatedSinkFunction();
180 ~WebrtcAudioPrivateGetAssociatedSinkFunction() override
;
183 DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.getAssociatedSink",
184 WEBRTC_AUDIO_PRIVATE_GET_ASSOCIATED_SINK
);
186 bool RunAsync() override
;
188 // This implementation is slightly complicated because of different
189 // thread requirements for the various functions we need to invoke.
191 // Each worker function will post a task to the appropriate thread
194 // The sequence of events is:
195 // 1. Get the list of source devices on the device thread.
196 // 2. Given a source ID for an origin and that security origin, find
197 // the raw source ID. This needs to happen on the IO thread since
198 // we will be using the ResourceContext.
199 // 3. Given a raw source ID, get the raw associated sink ID on the
201 // 4. Given the raw associated sink ID, get its HMAC on the IO thread.
202 // 5. Respond with the HMAC of the associated sink ID on the UI thread.
204 // Fills in |source_devices_|. Note that these are input devices,
205 // not output devices, so don't use
206 // |WebrtcAudioPrivateFunction::GetOutputDeviceNames|.
207 void GetDevicesOnDeviceThread();
209 // Takes the parameters of the function, retrieves the raw source
210 // device ID, or the empty string if none.
211 void GetRawSourceIDOnIOThread();
213 // Gets the raw sink ID for a raw source ID. Sends it to |CalculateHMAC|.
214 void GetAssociatedSinkOnDeviceThread(const std::string
& raw_source_id
);
216 // Receives the associated sink ID after its HMAC is calculated.
217 void OnHMACCalculated(const std::string
& hmac
) override
;
219 // Accessed from UI thread and device thread, but only on one at a
220 // time, no locking needed.
221 scoped_ptr
<api::webrtc_audio_private::GetAssociatedSink::Params
> params_
;
223 // Audio sources (input devices). Filled in by DoWorkOnDeviceThread.
224 media::AudioDeviceNames source_devices_
;
227 } // namespace extensions
229 #endif // CHROME_BROWSER_EXTENSIONS_API_WEBRTC_AUDIO_PRIVATE_WEBRTC_AUDIO_PRIVATE_API_H_