Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
mcasas | 6b8c0c8 | 2016-07-29 23:04:35 | [diff] [blame] | 5 | #include "content/browser/webrtc/webrtc_internals_message_handler.h" |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 6 | |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 7 | #include "base/functional/bind.h" |
| 8 | #include "base/functional/callback_helpers.h" |
Antonio Gomes | bc917af | 2019-10-20 08:01:54 | [diff] [blame] | 9 | #include "content/browser/renderer_host/media/peer_connection_tracker_host.h" |
mcasas | 6b8c0c8 | 2016-07-29 23:04:35 | [diff] [blame] | 10 | #include "content/browser/webrtc/webrtc_internals.h" |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 11 | #include "content/public/browser/browser_thread.h" |
[email protected] | f13ab89 | 2014-03-12 06:48:52 | [diff] [blame] | 12 | #include "content/public/browser/render_frame_host.h" |
[email protected] | 6efd5f2 | 2013-02-06 19:20:37 | [diff] [blame] | 13 | #include "content/public/browser/render_process_host.h" |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 14 | #include "content/public/browser/web_contents.h" |
| 15 | #include "content/public/browser/web_ui.h" |
tommi | 61cc0c0 | 2015-10-26 12:42:24 | [diff] [blame] | 16 | #include "content/public/common/url_constants.h" |
Philip Eliasson | 88d9bbcf | 2025-05-14 12:55:24 | [diff] [blame] | 17 | #include "third_party/blink/public/common/features.h" |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 18 | |
[email protected] | 64ccac3 | 2013-01-29 21:40:04 | [diff] [blame] | 19 | namespace content { |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 20 | |
maxmorin | c369574d | 2016-07-25 15:28:17 | [diff] [blame] | 21 | WebRTCInternalsMessageHandler::WebRTCInternalsMessageHandler() |
| 22 | : WebRTCInternalsMessageHandler(WebRTCInternals::GetInstance()) {} |
| 23 | |
| 24 | WebRTCInternalsMessageHandler::WebRTCInternalsMessageHandler( |
| 25 | WebRTCInternals* webrtc_internals) |
| 26 | : webrtc_internals_(webrtc_internals) { |
Elad Alon | a619d556 | 2018-01-13 01:30:15 | [diff] [blame] | 27 | DCHECK(webrtc_internals); |
maxmorin | c369574d | 2016-07-25 15:28:17 | [diff] [blame] | 28 | webrtc_internals_->AddObserver(this); |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | WebRTCInternalsMessageHandler::~WebRTCInternalsMessageHandler() { |
maxmorin | c369574d | 2016-07-25 15:28:17 | [diff] [blame] | 32 | webrtc_internals_->RemoveObserver(this); |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void WebRTCInternalsMessageHandler::RegisterMessages() { |
Maks Orlovich | b3c5128e | 2022-07-13 18:26:02 | [diff] [blame] | 36 | web_ui()->RegisterMessageCallback( |
Henrik Boström | 33ece3e9 | 2019-04-23 10:08:53 | [diff] [blame] | 37 | "getStandardStats", |
| 38 | base::BindRepeating(&WebRTCInternalsMessageHandler::OnGetStandardStats, |
| 39 | base::Unretained(this))); |
[email protected] | ce34e101 | 2013-05-02 04:55:46 | [diff] [blame] | 40 | |
Maks Orlovich | b3c5128e | 2022-07-13 18:26:02 | [diff] [blame] | 41 | web_ui()->RegisterMessageCallback( |
Avi Drissman | 5e5875b | 2018-03-24 01:39:47 | [diff] [blame] | 42 | "enableAudioDebugRecordings", |
| 43 | base::BindRepeating( |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 44 | &WebRTCInternalsMessageHandler::OnSetAudioDebugRecordingsEnabled, |
Avi Drissman | 5e5875b | 2018-03-24 01:39:47 | [diff] [blame] | 45 | base::Unretained(this), true)); |
[email protected] | bf3657b | 2013-12-17 12:36:17 | [diff] [blame] | 46 | |
Maks Orlovich | b3c5128e | 2022-07-13 18:26:02 | [diff] [blame] | 47 | web_ui()->RegisterMessageCallback( |
Avi Drissman | 5e5875b | 2018-03-24 01:39:47 | [diff] [blame] | 48 | "disableAudioDebugRecordings", |
| 49 | base::BindRepeating( |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 50 | &WebRTCInternalsMessageHandler::OnSetAudioDebugRecordingsEnabled, |
Avi Drissman | 5e5875b | 2018-03-24 01:39:47 | [diff] [blame] | 51 | base::Unretained(this), false)); |
[email protected] | bf3657b | 2013-12-17 12:36:17 | [diff] [blame] | 52 | |
Maks Orlovich | b3c5128e | 2022-07-13 18:26:02 | [diff] [blame] | 53 | web_ui()->RegisterMessageCallback( |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 54 | "enableEventLogRecordings", |
Avi Drissman | 5e5875b | 2018-03-24 01:39:47 | [diff] [blame] | 55 | base::BindRepeating( |
| 56 | &WebRTCInternalsMessageHandler::OnSetEventLogRecordingsEnabled, |
| 57 | base::Unretained(this), true)); |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 58 | |
Maks Orlovich | b3c5128e | 2022-07-13 18:26:02 | [diff] [blame] | 59 | web_ui()->RegisterMessageCallback( |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 60 | "disableEventLogRecordings", |
Avi Drissman | 5e5875b | 2018-03-24 01:39:47 | [diff] [blame] | 61 | base::BindRepeating( |
| 62 | &WebRTCInternalsMessageHandler::OnSetEventLogRecordingsEnabled, |
| 63 | base::Unretained(this), false)); |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 64 | |
Maks Orlovich | b3c5128e | 2022-07-13 18:26:02 | [diff] [blame] | 65 | web_ui()->RegisterMessageCallback( |
Philip Eliasson | 9b5072d4 | 2025-05-13 13:13:49 | [diff] [blame] | 66 | "enableDataChannelRecordings", |
| 67 | base::BindRepeating( |
| 68 | &WebRTCInternalsMessageHandler::OnSetDataChannelRecordingsEnabled, |
| 69 | base::Unretained(this), true)); |
| 70 | |
| 71 | web_ui()->RegisterMessageCallback( |
| 72 | "disableDataChannelRecordings", |
| 73 | base::BindRepeating( |
| 74 | &WebRTCInternalsMessageHandler::OnSetDataChannelRecordingsEnabled, |
| 75 | base::Unretained(this), false)); |
| 76 | |
| 77 | web_ui()->RegisterMessageCallback( |
Avi Drissman | 5e5875b | 2018-03-24 01:39:47 | [diff] [blame] | 78 | "finishedDOMLoad", |
| 79 | base::BindRepeating(&WebRTCInternalsMessageHandler::OnDOMLoadDone, |
| 80 | base::Unretained(this))); |
[email protected] | c054f09 | 2013-01-27 08:12:49 | [diff] [blame] | 81 | } |
| 82 | |
rbpotter | 6353cbe | 2021-04-22 15:48:28 | [diff] [blame] | 83 | RenderFrameHost* WebRTCInternalsMessageHandler::GetWebRTCInternalsHost() { |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 84 | RenderFrameHost* host = web_ui()->GetWebContents()->GetPrimaryMainFrame(); |
tommi | 61cc0c0 | 2015-10-26 12:42:24 | [diff] [blame] | 85 | if (host) { |
| 86 | // Make sure we only ever execute the script in the webrtc-internals page. |
| 87 | const GURL url(host->GetLastCommittedURL()); |
| 88 | if (!url.SchemeIs(kChromeUIScheme) || |
| 89 | url.host() != kChromeUIWebRTCInternalsHost) { |
| 90 | // Some other page is currently loaded even though we might be in the |
| 91 | // process of loading webrtc-internals. So, the current RFH is not the |
| 92 | // one we're waiting for. |
| 93 | host = nullptr; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return host; |
| 98 | } |
| 99 | |
Henrik Boström | 33ece3e9 | 2019-04-23 10:08:53 | [diff] [blame] | 100 | void WebRTCInternalsMessageHandler::OnGetStandardStats( |
Maks Orlovich | b3c5128e | 2022-07-13 18:26:02 | [diff] [blame] | 101 | const base::Value::List& /* unused_list */) { |
Tal Pressman | d7f6d6f | 2021-03-02 00:10:04 | [diff] [blame] | 102 | for (auto* host : PeerConnectionTrackerHost::GetAllHosts()) { |
| 103 | host->GetStandardStats(); |
Henrik Boström | 33ece3e9 | 2019-04-23 10:08:53 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 107 | void WebRTCInternalsMessageHandler::OnSetAudioDebugRecordingsEnabled( |
Maks Orlovich | b3c5128e | 2022-07-13 18:26:02 | [diff] [blame] | 108 | bool enable, |
| 109 | const base::Value::List& /* unused_list */) { |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 110 | if (enable) { |
maxmorin | c369574d | 2016-07-25 15:28:17 | [diff] [blame] | 111 | webrtc_internals_->EnableAudioDebugRecordings(web_ui()->GetWebContents()); |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 112 | } else { |
maxmorin | c369574d | 2016-07-25 15:28:17 | [diff] [blame] | 113 | webrtc_internals_->DisableAudioDebugRecordings(); |
grunell | 7566ffc | 2015-09-07 07:39:44 | [diff] [blame] | 114 | } |
[email protected] | bf3657b | 2013-12-17 12:36:17 | [diff] [blame] | 115 | } |
| 116 | |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 117 | void WebRTCInternalsMessageHandler::OnSetEventLogRecordingsEnabled( |
| 118 | bool enable, |
Maks Orlovich | b3c5128e | 2022-07-13 18:26:02 | [diff] [blame] | 119 | const base::Value::List& /* unused_list */) { |
Elad Alon | c0928bf1 | 2018-02-28 19:35:17 | [diff] [blame] | 120 | if (!webrtc_internals_->CanToggleEventLogRecordings()) { |
| 121 | LOG(WARNING) << "Cannot toggle WebRTC event logging."; |
| 122 | return; |
| 123 | } |
| 124 | |
ivoc | cf0887d | 2016-07-07 18:23:53 | [diff] [blame] | 125 | if (enable) { |
Elad Alon | 2afe249 | 2017-12-01 15:11:46 | [diff] [blame] | 126 | webrtc_internals_->EnableLocalEventLogRecordings( |
| 127 | web_ui()->GetWebContents()); |
ivoc | cf0887d | 2016-07-07 18:23:53 | [diff] [blame] | 128 | } else { |
Elad Alon | 2afe249 | 2017-12-01 15:11:46 | [diff] [blame] | 129 | webrtc_internals_->DisableLocalEventLogRecordings(); |
ivoc | cf0887d | 2016-07-07 18:23:53 | [diff] [blame] | 130 | } |
ivoc | add54f0d | 2015-12-18 23:17:05 | [diff] [blame] | 131 | } |
| 132 | |
Philip Eliasson | 9b5072d4 | 2025-05-13 13:13:49 | [diff] [blame] | 133 | void WebRTCInternalsMessageHandler::OnSetDataChannelRecordingsEnabled( |
| 134 | bool enable, |
| 135 | const base::Value::List& /* unused_list */) { |
Philip Eliasson | 88d9bbcf | 2025-05-14 12:55:24 | [diff] [blame] | 136 | if (!base::FeatureList::IsEnabled( |
| 137 | blink::features::kWebRtcAllowDataChannelRecordingInWebrtcInternals)) { |
| 138 | return; |
| 139 | } |
Philip Eliasson | 9b5072d4 | 2025-05-13 13:13:49 | [diff] [blame] | 140 | if (enable) { |
| 141 | webrtc_internals_->EnableDataChannelRecordings(web_ui()->GetWebContents()); |
| 142 | } else { |
| 143 | webrtc_internals_->DisableDataChannelRecordings(); |
| 144 | } |
| 145 | } |
| 146 | |
Maks Orlovich | b3c5128e | 2022-07-13 18:26:02 | [diff] [blame] | 147 | void WebRTCInternalsMessageHandler::OnDOMLoadDone( |
| 148 | const base::Value::List& args_list) { |
Maks Orlovich | 266c5dd | 2021-12-02 23:06:41 | [diff] [blame] | 149 | CHECK_GE(args_list.size(), 1u); |
| 150 | |
| 151 | const std::string callback_id = args_list[0].GetString(); |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 152 | AllowJavascript(); |
| 153 | |
maxmorin | c369574d | 2016-07-25 15:28:17 | [diff] [blame] | 154 | webrtc_internals_->UpdateObserver(this); |
[email protected] | bf3657b | 2013-12-17 12:36:17 | [diff] [blame] | 155 | |
Maks Orlovich | b3c5128e | 2022-07-13 18:26:02 | [diff] [blame] | 156 | base::Value::Dict params; |
| 157 | params.Set("audioDebugRecordingsEnabled", |
| 158 | webrtc_internals_->IsAudioDebugRecordingsEnabled()); |
| 159 | params.Set("eventLogRecordingsEnabled", |
| 160 | webrtc_internals_->IsEventLogRecordingsEnabled()); |
| 161 | params.Set("eventLogRecordingsToggleable", |
| 162 | webrtc_internals_->CanToggleEventLogRecordings()); |
Philip Eliasson | 9b5072d4 | 2025-05-13 13:13:49 | [diff] [blame] | 163 | params.Set("dataChannelRecordingsEnabled", |
| 164 | webrtc_internals_->IsDataChannelRecordingsEnabled()); |
tommi | 61cc0c0 | 2015-10-26 12:42:24 | [diff] [blame] | 165 | |
Philipp Hancke | a9abeab5 | 2023-05-16 06:43:41 | [diff] [blame] | 166 | for (auto* host : PeerConnectionTrackerHost::GetAllHosts()) { |
| 167 | host->GetCurrentState(); |
| 168 | } |
Claudio DeSouza | e80c81e9 | 2022-08-15 22:09:33 | [diff] [blame] | 169 | ResolveJavascriptCallback(base::Value(callback_id), params); |
[email protected] | bf3657b | 2013-12-17 12:36:17 | [diff] [blame] | 170 | } |
| 171 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 172 | void WebRTCInternalsMessageHandler::OnUpdate(const std::string& event_name, |
| 173 | const base::Value* event_data) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 174 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Tony Herre | 6bd89b4f | 2021-10-07 14:27:41 | [diff] [blame] | 175 | if (!IsJavascriptAllowed()) { |
| 176 | // Javascript is disallowed, either due to the page still loading, or in the |
| 177 | // process of being unloaded. Skip this update. |
| 178 | return; |
| 179 | } |
tommi | 61cc0c0 | 2015-10-26 12:42:24 | [diff] [blame] | 180 | |
| 181 | RenderFrameHost* host = GetWebRTCInternalsHost(); |
| 182 | if (!host) |
| 183 | return; |
| 184 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 185 | if (event_data) { |
| 186 | FireWebUIListener(event_name, *event_data); |
| 187 | } else { |
| 188 | FireWebUIListener(event_name, base::Value()); |
| 189 | } |
[email protected] | 94fb510 | 2013-01-23 09:00:47 | [diff] [blame] | 190 | } |
[email protected] | 64ccac3 | 2013-01-29 21:40:04 | [diff] [blame] | 191 | |
| 192 | } // namespace content |