Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / media / media_capture_devices_dispatcher.h
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright (c) 2013 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #ifndef XWALK_RUNTIME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_
7 #define XWALK_RUNTIME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_
8
9 #include <string>
10
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/singleton.h"
14 #include "base/observer_list.h"
15 #include "content/public/browser/media_observer.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/media_stream_request.h"
18
19 // This singleton is used to receive updates about media events from the content
20 // layer. Based on chrome/browser/media/media_capture_devices_dispatcher.[h|cc].
21 class XWalkMediaCaptureDevicesDispatcher : public content::MediaObserver {
22  public:
23   class Observer {
24    public:
25     // Handle an information update consisting of a up-to-date audio capture
26     // device lists. This happens when a microphone is plugged in or unplugged.
27     virtual void OnUpdateAudioDevices(
28         const content::MediaStreamDevices& devices) {}
29
30     // Handle an information update consisting of a up-to-date video capture
31     // device lists. This happens when a camera is plugged in or unplugged.
32     virtual void OnUpdateVideoDevices(
33         const content::MediaStreamDevices& devices) {}
34
35     // Handle an information update related to a media stream request.
36     virtual void OnRequestUpdate(
37         int render_process_id,
38         int render_frame_id,
39         content::MediaStreamType stream_type,
40         const content::MediaRequestState state) {}
41
42     virtual ~Observer() {}
43   };
44
45   static XWalkMediaCaptureDevicesDispatcher* GetInstance();
46
47   static void RunRequestMediaAccessPermission(
48       content::WebContents* web_contents,
49       const content::MediaStreamRequest& request,
50       const content::MediaResponseCallback& callback);
51
52   // Methods for observers. Called on UI thread.
53   // Observers should add themselves on construction and remove themselves
54   // on destruction.
55   void AddObserver(Observer* observer);
56   void RemoveObserver(Observer* observer);
57   const content::MediaStreamDevices& GetAudioCaptureDevices();
58   const content::MediaStreamDevices& GetVideoCaptureDevices();
59
60   // Helper for picking the device that was requested for an OpenDevice request.
61   // If the device requested is not available it will revert to using the first
62   // available one instead or will return an empty list if no devices of the
63   // requested kind are present.
64   void GetRequestedDevice(const std::string& requested_audio_device_id,
65                           const std::string& requested_video_device_id,
66                           bool audio,
67                           bool video,
68                           content::MediaStreamDevices* devices);
69
70   // Overridden from content::MediaObserver:
71   void OnAudioCaptureDevicesChanged() override;
72   void OnVideoCaptureDevicesChanged() override;
73   void OnMediaRequestStateChanged(
74       int render_process_id,
75       int render_frame_id,
76       int page_request_id,
77       const GURL& security_origin,
78       content::MediaStreamType stream_type,
79       content::MediaRequestState state) override;
80   void OnCreatingAudioStream(int render_process_id,
81                              int render_view_id) override {}
82
83   // Only for testing.
84   void SetTestAudioCaptureDevices(const content::MediaStreamDevices& devices);
85   void SetTestVideoCaptureDevices(const content::MediaStreamDevices& devices);
86
87  private:
88   friend struct DefaultSingletonTraits<XWalkMediaCaptureDevicesDispatcher>;
89
90   XWalkMediaCaptureDevicesDispatcher();
91   virtual ~XWalkMediaCaptureDevicesDispatcher();
92
93   // Called by the MediaObserver() functions, executed on UI thread.
94   void NotifyAudioDevicesChangedOnUIThread();
95   void NotifyVideoDevicesChangedOnUIThread();
96   void UpdateMediaReqStateOnUIThread(
97       int render_process_id,
98       int render_frame_id,
99       const GURL& security_origin,
100       content::MediaStreamType stream_type,
101       content::MediaRequestState state);
102
103   // Only for testing, a list of cached audio capture devices.
104   content::MediaStreamDevices test_audio_devices_;
105
106   // Only for testing, a list of video audio capture devices.
107   content::MediaStreamDevices test_video_devices_;
108
109   // A list of observers for the device update notifications.
110   ObserverList<Observer> observers_;
111 };
112
113 #endif  // XWALK_RUNTIME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_