Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / webrtc_audio_private / webrtc_audio_private_api.h
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.
4
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_
7
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_view_host.h"
13 #include "extensions/browser/browser_context_keyed_api_factory.h"
14 #include "media/audio/audio_device_name.h"
15 #include "url/gurl.h"
16
17 namespace content {
18 class ResourceContext;
19 }
20
21 namespace extensions {
22
23 // Listens for device changes and forwards as an extension event.
24 class WebrtcAudioPrivateEventService
25     : public BrowserContextKeyedAPI,
26       public base::SystemMonitor::DevicesChangedObserver {
27  public:
28   explicit WebrtcAudioPrivateEventService(content::BrowserContext* context);
29   ~WebrtcAudioPrivateEventService() override;
30
31   // BrowserContextKeyedAPI implementation.
32   void Shutdown() override;
33   static BrowserContextKeyedAPIFactory<WebrtcAudioPrivateEventService>*
34       GetFactoryInstance();
35   static const char* service_name();
36
37   // base::SystemMonitor::DevicesChangedObserver implementation.
38   void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override;
39
40  private:
41   friend class BrowserContextKeyedAPIFactory<WebrtcAudioPrivateEventService>;
42
43   void SignalEvent();
44
45   content::BrowserContext* browser_context_;
46 };
47
48 // Common base for WebrtcAudioPrivate functions, that provides a
49 // couple of optionally-used common implementations.
50 class WebrtcAudioPrivateFunction : public ChromeAsyncExtensionFunction {
51  protected:
52   WebrtcAudioPrivateFunction();
53   ~WebrtcAudioPrivateFunction() override;
54
55  protected:
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();
59
60   // Must override this if you call GetOutputDeviceNames. Called on IO thread.
61   virtual void OnOutputDeviceNames(
62       scoped_ptr<media::AudioDeviceNames> device_names);
63
64   // Retrieve the list of AudioOutputController objects. Calls back
65   // via OnControllerList.
66   //
67   // Returns false on error, in which case it has set |error_| and the
68   // entire function should fail.
69   //
70   // Call from any thread. Callback will occur on originating thread.
71   bool GetControllerList(int tab_id);
72
73   // Must override this if you call GetControllerList.
74   virtual void OnControllerList(
75       const content::RenderViewHost::AudioOutputControllerList& list);
76
77   // Calculates a single HMAC. Call from any thread. Calls back via
78   // OnHMACCalculated on UI thread.
79   //
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);
85
86   // Must override this if you call CalculateHMAC.
87   virtual void OnHMACCalculated(const std::string& hmac);
88
89   // Calculates a single HMAC, using the extension ID as the security origin.
90   //
91   // Call only on IO thread.
92   std::string CalculateHMACImpl(const std::string& raw_id);
93
94   // Initializes |resource_context_|. Must be called on the UI thread,
95   // before any calls to |resource_context()|.
96   void InitResourceContext();
97
98   // Callable from any thread. Must previously have called
99   // |InitResourceContext()|.
100   content::ResourceContext* resource_context() const;
101
102  private:
103   content::ResourceContext* resource_context_;
104
105   DISALLOW_COPY_AND_ASSIGN(WebrtcAudioPrivateFunction);
106 };
107
108 class WebrtcAudioPrivateGetSinksFunction : public WebrtcAudioPrivateFunction {
109  protected:
110   ~WebrtcAudioPrivateGetSinksFunction() override {}
111
112  private:
113   DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.getSinks",
114                              WEBRTC_AUDIO_PRIVATE_GET_SINKS);
115
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;
120   void DoQuery();
121   void OnOutputDeviceNames(
122       scoped_ptr<media::AudioDeviceNames> raw_ids) override;
123   void DoneOnUIThread();
124 };
125
126 class WebrtcAudioPrivateGetActiveSinkFunction
127     : public WebrtcAudioPrivateFunction {
128  protected:
129   ~WebrtcAudioPrivateGetActiveSinkFunction() override {}
130
131  private:
132   DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.getActiveSink",
133                              WEBRTC_AUDIO_PRIVATE_GET_ACTIVE_SINK);
134
135   bool RunAsync() override;
136   void OnControllerList(
137       const content::RenderViewHost::AudioOutputControllerList& controllers)
138       override;
139   void OnHMACCalculated(const std::string& hmac) override;
140 };
141
142 class WebrtcAudioPrivateSetActiveSinkFunction
143     : public WebrtcAudioPrivateFunction {
144  public:
145   WebrtcAudioPrivateSetActiveSinkFunction();
146
147  protected:
148   ~WebrtcAudioPrivateSetActiveSinkFunction() override;
149
150  private:
151   DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.setActiveSink",
152                              WEBRTC_AUDIO_PRIVATE_SET_ACTIVE_SINK);
153
154   bool RunAsync() override;
155   void OnControllerList(
156       const content::RenderViewHost::AudioOutputControllerList& controllers)
157       override;
158   void OnOutputDeviceNames(
159       scoped_ptr<media::AudioDeviceNames> device_names) override;
160   void SwitchDone();
161   void DoneOnUIThread();
162
163   int tab_id_;
164   std::string sink_id_;
165
166   // Filled in by OnControllerList.
167   content::RenderViewHost::AudioOutputControllerList controllers_;
168
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_;
172 };
173
174 class WebrtcAudioPrivateGetAssociatedSinkFunction
175     : public WebrtcAudioPrivateFunction {
176  public:
177   WebrtcAudioPrivateGetAssociatedSinkFunction();
178
179  protected:
180   ~WebrtcAudioPrivateGetAssociatedSinkFunction() override;
181
182  private:
183   DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.getAssociatedSink",
184                              WEBRTC_AUDIO_PRIVATE_GET_ASSOCIATED_SINK);
185
186   bool RunAsync() override;
187
188   // This implementation is slightly complicated because of different
189   // thread requirements for the various functions we need to invoke.
190   //
191   // Each worker function will post a task to the appropriate thread
192   // for the next one.
193   //
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
200   //    device thread.
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.
203
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();
208
209   // Takes the parameters of the function, retrieves the raw source
210   // device ID, or the empty string if none.
211   void GetRawSourceIDOnIOThread();
212
213   // Gets the raw sink ID for a raw source ID. Sends it to |CalculateHMAC|.
214   void GetAssociatedSinkOnDeviceThread(const std::string& raw_source_id);
215
216   // Receives the associated sink ID after its HMAC is calculated.
217   void OnHMACCalculated(const std::string& hmac) override;
218
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_;
222
223   // Audio sources (input devices). Filled in by DoWorkOnDeviceThread.
224   media::AudioDeviceNames source_devices_;
225 };
226
227 }  // namespace extensions
228
229 #endif  // CHROME_BROWSER_EXTENSIONS_API_WEBRTC_AUDIO_PRIVATE_WEBRTC_AUDIO_PRIVATE_API_H_