Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / media / webrtc_internals.h
1 // Copyright (c) 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 CONTENT_BROWSER_MEDIA_WEBRTC_INTERNALS_H_
6 #define CONTENT_BROWSER_MEDIA_WEBRTC_INTERNALS_H_
7
8 #include "base/gtest_prod_util.h"
9 #include "base/memory/singleton.h"
10 #include "base/observer_list.h"
11 #include "base/process/process.h"
12 #include "base/values.h"
13 #include "content/common/content_export.h"
14 #include "content/public/browser/browser_child_process_observer.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "ui/shell_dialogs/select_file_dialog.h"
18
19 namespace content {
20 class WebContents;
21 class WebRTCInternalsUIObserver;
22
23 // This is a singleton class running in the browser UI thread.
24 // It collects peer connection infomation from the renderers,
25 // forwards the data to WebRTCInternalsUIObserver and
26 // sends data collecting commands to the renderers.
27 class CONTENT_EXPORT WebRTCInternals : public BrowserChildProcessObserver,
28                                        public NotificationObserver,
29                                        public ui::SelectFileDialog::Listener {
30  public:
31   static WebRTCInternals* GetInstance();
32
33   // This method is called when a PeerConnection is created.
34   // |render_process_id| is the id of the render process (not OS pid), which is
35   // needed because we might not be able to get the OS process id when the
36   // render process terminates and we want to clean up.
37   // |pid| is the renderer process id, |lid| is the renderer local id used to
38   // identify a PeerConnection, |url| is the url of the tab owning the
39   // PeerConnection, |servers| is the servers configuration, |constraints| is
40   // the media constraints used to initialize the PeerConnection.
41   void OnAddPeerConnection(int render_process_id,
42                            base::ProcessId pid,
43                            int lid,
44                            const std::string& url,
45                            const std::string& servers,
46                            const std::string& constraints);
47
48   // This method is called when PeerConnection is destroyed.
49   // |pid| is the renderer process id, |lid| is the renderer local id.
50   void OnRemovePeerConnection(base::ProcessId pid, int lid);
51
52   // This method is called when a PeerConnection is updated.
53   // |pid| is the renderer process id, |lid| is the renderer local id,
54   // |type| is the update type, |value| is the detail of the update.
55   void OnUpdatePeerConnection(base::ProcessId pid,
56                               int lid,
57                               const std::string& type,
58                               const std::string& value);
59
60   // This method is called when results from PeerConnectionInterface::GetStats
61   // are available. |pid| is the renderer process id, |lid| is the renderer
62   // local id, |value| is the list of stats reports.
63   void OnAddStats(base::ProcessId pid, int lid, const base::ListValue& value);
64
65   // This method is called when getUserMedia is called. |render_process_id| is
66   // the id of the render process (not OS pid), which is needed because we might
67   // not be able to get the OS process id when the render process terminates and
68   // we want to clean up. |pid| is the renderer OS process id, |origin| is the
69   // security origin of the getUserMedia call, |audio| is true if audio stream
70   // is requested, |video| is true if the video stream is requested,
71   // |audio_constraints| is the constraints for the audio, |video_constraints|
72   // is the constraints for the video.
73   void OnGetUserMedia(int render_process_id,
74                       base::ProcessId pid,
75                       const std::string& origin,
76                       bool audio,
77                       bool video,
78                       const std::string& audio_constraints,
79                       const std::string& video_constraints);
80
81   // Methods for adding or removing WebRTCInternalsUIObserver.
82   void AddObserver(WebRTCInternalsUIObserver *observer);
83   void RemoveObserver(WebRTCInternalsUIObserver *observer);
84
85   // Sends all update data to |observer|.
86   void UpdateObserver(WebRTCInternalsUIObserver* observer);
87
88   // Enables or disables AEC dump (diagnostic echo canceller recording).
89   void EnableAecDump(content::WebContents* web_contents);
90   void DisableAecDump();
91
92   bool aec_dump_enabled() {
93     return aec_dump_enabled_;
94   }
95
96   base::FilePath aec_dump_file_path() {
97     return aec_dump_file_path_;
98   }
99
100   void ResetForTesting();
101
102  private:
103   friend struct DefaultSingletonTraits<WebRTCInternals>;
104   FRIEND_TEST_ALL_PREFIXES(WebRtcBrowserTest, CallWithAecDump);
105   FRIEND_TEST_ALL_PREFIXES(WebRtcBrowserTest,
106                            CallWithAecDumpEnabledThenDisabled);
107
108   WebRTCInternals();
109   virtual ~WebRTCInternals();
110
111   void SendUpdate(const std::string& command, base::Value* value);
112
113   // BrowserChildProcessObserver implementation.
114   virtual void BrowserChildProcessCrashed(
115       const ChildProcessData& data) OVERRIDE;
116
117   // NotificationObserver implementation.
118   virtual void Observe(int type,
119                        const NotificationSource& source,
120                        const NotificationDetails& details) OVERRIDE;
121
122   // ui::SelectFileDialog::Listener implementation.
123   virtual void FileSelected(const base::FilePath& path,
124                             int index,
125                             void* unused_params) OVERRIDE;
126
127   // Called when a renderer exits (including crashes).
128   void OnRendererExit(int render_process_id);
129
130 #if defined(ENABLE_WEBRTC)
131   // Enables AEC dump on all render process hosts using |aec_dump_file_path_|.
132   void EnableAecDumpOnAllRenderProcessHosts();
133 #endif
134
135   ObserverList<WebRTCInternalsUIObserver> observers_;
136
137   // |peer_connection_data_| is a list containing all the PeerConnection
138   // updates.
139   // Each item of the list represents the data for one PeerConnection, which
140   // contains these fields:
141   // "rid" -- the renderer id.
142   // "pid" -- OS process id of the renderer that creates the PeerConnection.
143   // "lid" -- local Id assigned to the PeerConnection.
144   // "url" -- url of the web page that created the PeerConnection.
145   // "servers" and "constraints" -- server configuration and media constraints
146   // used to initialize the PeerConnection respectively.
147   // "log" -- a ListValue contains all the updates for the PeerConnection. Each
148   // list item is a DictionaryValue containing "type" and "value", both of which
149   // are strings.
150   base::ListValue peer_connection_data_;
151
152   // A list of getUserMedia requests. Each item is a DictionaryValue that
153   // contains these fields:
154   // "rid" -- the renderer id.
155   // "pid" -- proceddId of the renderer.
156   // "origin" -- the security origin of the request.
157   // "audio" -- the serialized audio constraints if audio is requested.
158   // "video" -- the serialized video constraints if video is requested.
159   base::ListValue get_user_media_requests_;
160
161   NotificationRegistrar registrar_;
162
163   // For managing select file dialog.
164   scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
165
166   // AEC dump (diagnostic echo canceller recording) state.
167   bool aec_dump_enabled_;
168   base::FilePath aec_dump_file_path_;
169 };
170
171 }  // namespace content
172
173 #endif  // CONTENT_BROWSER_MEDIA_WEBRTC_INTERNALS_H_