Upstream version 5.34.104.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/api/profile_keyed_api_factory.h"
11 #include "chrome/browser/extensions/chrome_extension_function.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/extensions/api/webrtc_audio_private.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "media/audio/audio_device_name.h"
16 #include "url/gurl.h"
17
18 namespace extensions {
19
20 // Listens for device changes and forwards as an extension event.
21 class WebrtcAudioPrivateEventService
22     : public ProfileKeyedAPI,
23       public base::SystemMonitor::DevicesChangedObserver {
24  public:
25   explicit WebrtcAudioPrivateEventService(Profile* profile);
26   virtual ~WebrtcAudioPrivateEventService();
27
28   // ProfileKeyedAPI implementation.
29   virtual void Shutdown() OVERRIDE;
30   static ProfileKeyedAPIFactory<WebrtcAudioPrivateEventService>*
31       GetFactoryInstance();
32   static const char* service_name();
33
34   // base::SystemMonitor::DevicesChangedObserver implementation.
35   virtual void OnDevicesChanged(
36       base::SystemMonitor::DeviceType device_type) OVERRIDE;
37
38  private:
39   friend class ProfileKeyedAPIFactory<WebrtcAudioPrivateEventService>;
40
41   void SignalEvent();
42
43   Profile* profile_;
44 };
45
46 // Common base for WebrtcAudioPrivate functions, that provides a
47 // couple of optionally-used common implementations.
48 class WebrtcAudioPrivateFunction : public ChromeAsyncExtensionFunction {
49  protected:
50   WebrtcAudioPrivateFunction();
51   virtual ~WebrtcAudioPrivateFunction();
52
53  protected:
54   // Retrieves the list of output device names on the appropriate
55   // thread. Call from UI thread, callback will occur on IO thread.
56   void GetOutputDeviceNames();
57
58   // Must override this if you call GetOutputDeviceNames. Called on IO thread.
59   virtual void OnOutputDeviceNames(
60       scoped_ptr<media::AudioDeviceNames> device_names);
61
62   // Retrieve the list of AudioOutputController objects. Calls back
63   // via OnControllerList.
64   //
65   // Returns false on error, in which case it has set |error_| and the
66   // entire function should fail.
67   //
68   // Call from any thread. Callback will occur on originating thread.
69   bool GetControllerList(int tab_id);
70
71   // Must override this if you call GetControllerList.
72   virtual void OnControllerList(
73       const content::RenderViewHost::AudioOutputControllerList& list);
74
75   // Calculates a single HMAC. Call from any thread. Calls back via
76   // OnHMACCalculated on UI thread.
77   //
78   // This function, and device ID HMACs in this API in general use the
79   // calling extension's ID as the security origin. The only exception
80   // to this rule is when calculating the input device ID HMAC in
81   // getAssociatedSink, where we use the provided |securityOrigin|.
82   void CalculateHMAC(const std::string& raw_id);
83
84   // Must override this if you call CalculateHMAC.
85   virtual void OnHMACCalculated(const std::string& hmac);
86
87   // Calculates a single HMAC, using the extension ID as the security origin.
88   //
89   // Call only on IO thread.
90   std::string CalculateHMACImpl(const std::string& raw_id);
91
92   // Initializes |resource_context_|. Must be called on the UI thread,
93   // before any calls to |resource_context()|.
94   void InitResourceContext();
95
96   // Callable from any thread. Must previously have called
97   // |InitResourceContext()|.
98   content::ResourceContext* resource_context() const;
99
100  private:
101   content::ResourceContext* resource_context_;
102
103   DISALLOW_COPY_AND_ASSIGN(WebrtcAudioPrivateFunction);
104 };
105
106 class WebrtcAudioPrivateGetSinksFunction : public WebrtcAudioPrivateFunction {
107  protected:
108   virtual ~WebrtcAudioPrivateGetSinksFunction() {}
109
110  private:
111   DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.getSinks",
112                              WEBRTC_AUDIO_PRIVATE_GET_SINKS);
113
114   // Sequence of events is that we query the list of sinks on the
115   // AudioManager's thread, then calculate HMACs on the IO thread,
116   // then finish on the UI thread.
117   virtual bool RunImpl() OVERRIDE;
118   void DoQuery();
119   virtual void OnOutputDeviceNames(
120       scoped_ptr<media::AudioDeviceNames> raw_ids) OVERRIDE;
121   void DoneOnUIThread();
122 };
123
124 class WebrtcAudioPrivateGetActiveSinkFunction
125     : public WebrtcAudioPrivateFunction {
126  protected:
127   virtual ~WebrtcAudioPrivateGetActiveSinkFunction() {}
128
129  private:
130   DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.getActiveSink",
131                              WEBRTC_AUDIO_PRIVATE_GET_ACTIVE_SINK);
132
133   virtual bool RunImpl() OVERRIDE;
134   virtual void OnControllerList(
135       const content::RenderViewHost::AudioOutputControllerList&
136       controllers) OVERRIDE;
137   virtual void OnHMACCalculated(const std::string& hmac) OVERRIDE;
138 };
139
140 class WebrtcAudioPrivateSetActiveSinkFunction
141     : public WebrtcAudioPrivateFunction {
142  public:
143   WebrtcAudioPrivateSetActiveSinkFunction();
144
145  protected:
146   virtual ~WebrtcAudioPrivateSetActiveSinkFunction();
147
148  private:
149   DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.setActiveSink",
150                              WEBRTC_AUDIO_PRIVATE_SET_ACTIVE_SINK);
151
152   virtual bool RunImpl() OVERRIDE;
153   virtual void OnControllerList(
154       const content::RenderViewHost::AudioOutputControllerList&
155       controllers) OVERRIDE;
156   virtual void OnOutputDeviceNames(
157       scoped_ptr<media::AudioDeviceNames> device_names) OVERRIDE;
158   void SwitchDone();
159   void DoneOnUIThread();
160
161   int tab_id_;
162   std::string sink_id_;
163
164   // Filled in by OnControllerList.
165   content::RenderViewHost::AudioOutputControllerList controllers_;
166
167   // Number of sink IDs we are still waiting for. Can become greater
168   // than 0 in OnControllerList, decreases on every OnSinkId call.
169   size_t num_remaining_sink_ids_;
170 };
171
172 class WebrtcAudioPrivateGetAssociatedSinkFunction
173     : public WebrtcAudioPrivateFunction {
174  public:
175   WebrtcAudioPrivateGetAssociatedSinkFunction();
176
177  protected:
178   virtual ~WebrtcAudioPrivateGetAssociatedSinkFunction();
179
180  private:
181   DECLARE_EXTENSION_FUNCTION("webrtcAudioPrivate.getAssociatedSink",
182                              WEBRTC_AUDIO_PRIVATE_GET_ASSOCIATED_SINK);
183
184   virtual bool RunImpl() OVERRIDE;
185
186   // This implementation is slightly complicated because of different
187   // thread requirements for the various functions we need to invoke.
188   //
189   // Each worker function will post a task to the appropriate thread
190   // for the next one.
191   //
192   // The sequence of events is:
193   // 1. Get the list of source devices on the device thread.
194   // 2. Given a source ID for an origin and that security origin, find
195   //    the raw source ID. This needs to happen on the IO thread since
196   //    we will be using the ResourceContext.
197   // 3. Given a raw source ID, get the raw associated sink ID on the
198   //    device thread.
199   // 4. Given the raw associated sink ID, get its HMAC on the IO thread.
200   // 5. Respond with the HMAC of the associated sink ID on the UI thread.
201
202   // Fills in |source_devices_|. Note that these are input devices,
203   // not output devices, so don't use
204   // |WebrtcAudioPrivateFunction::GetOutputDeviceNames|.
205   void GetDevicesOnDeviceThread();
206
207   // Takes the parameters of the function, retrieves the raw source
208   // device ID, or the empty string if none.
209   void GetRawSourceIDOnIOThread();
210
211   // Gets the raw sink ID for a raw source ID. Sends it to |CalculateHMAC|.
212   void GetAssociatedSinkOnDeviceThread(const std::string& raw_source_id);
213
214   // Receives the associated sink ID after its HMAC is calculated.
215   virtual void OnHMACCalculated(const std::string& hmac) OVERRIDE;
216
217   // Accessed from UI thread and device thread, but only on one at a
218   // time, no locking needed.
219   scoped_ptr<api::webrtc_audio_private::GetAssociatedSink::Params> params_;
220
221   // Audio sources (input devices). Filled in by DoWorkOnDeviceThread.
222   media::AudioDeviceNames source_devices_;
223 };
224
225 }  // namespace extensions
226
227 #endif  // CHROME_BROWSER_EXTENSIONS_API_WEBRTC_AUDIO_PRIVATE_WEBRTC_AUDIO_PRIVATE_API_H_