feat: add setter and getter apis to specify udp port range for webrtc (#39046)

* feat:Add setter and getter apis to specify udp port range for webrtc (issue#9042)

* lint error fix for PR#39046

* feat: add setter and getter apis to specify udp port range for webrtc (issue#9042) , changed for codereview

* fix lint error

* fix lint errors in file api-web-contents-spec.ts

* feat: add setter and getter apis to specify udp port range for webrtc (issue#9042) , changed for review from itsananderson

* feat: add setter and getter apis to specify udp port range for webrtc (issue#9042) , changed for review from jkleinsc

* fix lint error

* feat: add setter and getter apis to specify udp port range for webrtc (issue#9042) , changed for review from codebyter
This commit is contained in:
wanted002 2023-08-25 05:21:22 +08:00 committed by GitHub
parent 33000c4b42
commit e14964ccd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 109 additions and 0 deletions

View file

@ -2069,6 +2069,24 @@ Setting the WebRTC IP handling policy allows you to control which IPs are
exposed via WebRTC. See [BrowserLeaks](https://browserleaks.com/webrtc) for
more details.
#### `contents.getWebRTCUDPPortRange()`
Returns `Object`:
* `min` Integer - The minimum UDP port number that WebRTC should use.
* `max` Integer - The maximum UDP port number that WebRTC should use.
By default this value is `{ min: 0, max: 0 }` , which would apply no restriction on the udp port range.
#### `contents.setWebRTCUDPPortRange(udpPortRange)`
* `udpPortRange` Object
* `min` Integer - The minimum UDP port number that WebRTC should use.
* `max` Integer - The maximum UDP port number that WebRTC should use.
Setting the WebRTC UDP Port Range allows you to restrict the udp port range used by WebRTC. By default the port range is unrestricted.
**Note:** To reset to an unrestricted port range this value should be set to `{ min: 0, max: 0 }`.
#### `contents.getMediaSourceId(requestWebContents)`
* `requestWebContents` WebContents - Web contents that the id will be registered to.

View file

@ -2552,6 +2552,52 @@ void WebContents::SetWebRTCIPHandlingPolicy(
web_contents()->SyncRendererPrefs();
}
v8::Local<v8::Value> WebContents::GetWebRTCUDPPortRange(
v8::Isolate* isolate) const {
auto* prefs = web_contents()->GetMutableRendererPrefs();
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
dict.Set("min", static_cast<uint32_t>(prefs->webrtc_udp_min_port));
dict.Set("max", static_cast<uint32_t>(prefs->webrtc_udp_max_port));
return dict.GetHandle();
}
void WebContents::SetWebRTCUDPPortRange(gin::Arguments* args) {
uint32_t min = 0, max = 0;
gin_helper::Dictionary range;
if (!args->GetNext(&range) || !range.Get("min", &min) ||
!range.Get("max", &max)) {
gin_helper::ErrorThrower(args->isolate())
.ThrowError("'min' and 'max' are both required");
return;
}
if ((0 == min && 0 != max) || max > UINT16_MAX) {
gin_helper::ErrorThrower(args->isolate())
.ThrowError(
"'min' and 'max' must be in the (0, 65535] range or [0, 0]");
return;
}
if (min > max) {
gin_helper::ErrorThrower(args->isolate())
.ThrowError("'max' must be greater than or equal to 'min'");
return;
}
auto* prefs = web_contents()->GetMutableRendererPrefs();
if (prefs->webrtc_udp_min_port == static_cast<uint16_t>(min) &&
prefs->webrtc_udp_max_port == static_cast<uint16_t>(max)) {
return;
}
prefs->webrtc_udp_min_port = min;
prefs->webrtc_udp_max_port = max;
web_contents()->SyncRendererPrefs();
}
std::string WebContents::GetMediaSourceID(
content::WebContents* request_web_contents) {
auto* frame_host = web_contents()->GetPrimaryMainFrame();
@ -4319,9 +4365,11 @@ void WebContents::FillObjectTemplate(v8::Isolate* isolate,
.SetMethod("isBeingCaptured", &WebContents::IsBeingCaptured)
.SetMethod("setWebRTCIPHandlingPolicy",
&WebContents::SetWebRTCIPHandlingPolicy)
.SetMethod("setWebRTCUDPPortRange", &WebContents::SetWebRTCUDPPortRange)
.SetMethod("getMediaSourceId", &WebContents::GetMediaSourceID)
.SetMethod("getWebRTCIPHandlingPolicy",
&WebContents::GetWebRTCIPHandlingPolicy)
.SetMethod("getWebRTCUDPPortRange", &WebContents::GetWebRTCUDPPortRange)
.SetMethod("takeHeapSnapshot", &WebContents::TakeHeapSnapshot)
.SetMethod("setImageAnimationPolicy",
&WebContents::SetImageAnimationPolicy)

View file

@ -189,6 +189,8 @@ class WebContents : public ExclusiveAccessContext,
int GetHistoryLength() const;
const std::string GetWebRTCIPHandlingPolicy() const;
void SetWebRTCIPHandlingPolicy(const std::string& webrtc_ip_handling_policy);
v8::Local<v8::Value> GetWebRTCUDPPortRange(v8::Isolate* isolate) const;
void SetWebRTCUDPPortRange(gin::Arguments* args);
std::string GetMediaSourceID(content::WebContents* request_web_contents);
bool IsCrashed() const;
void ForcefullyCrashRenderer();

View file

@ -1309,6 +1309,47 @@ describe('webContents module', () => {
});
});
describe('webrtc udp port range policy api', () => {
let w: BrowserWindow;
beforeEach(() => {
w = new BrowserWindow({ show: false });
});
afterEach(closeAllWindows);
it('check default webrtc udp port range is { min: 0, max: 0 }', () => {
const settings = w.webContents.getWebRTCUDPPortRange();
expect(settings).to.deep.equal({ min: 0, max: 0 });
});
it('can set and get webrtc udp port range policy with correct arguments', () => {
w.webContents.setWebRTCUDPPortRange({ min: 1, max: 65535 });
const settings = w.webContents.getWebRTCUDPPortRange();
expect(settings).to.deep.equal({ min: 1, max: 65535 });
});
it('can not set webrtc udp port range policy with invalid arguments', () => {
expect(() => {
w.webContents.setWebRTCUDPPortRange({ min: 0, max: 65535 });
}).to.throw("'min' and 'max' must be in the (0, 65535] range or [0, 0]");
expect(() => {
w.webContents.setWebRTCUDPPortRange({ min: 1, max: 65536 });
}).to.throw("'min' and 'max' must be in the (0, 65535] range or [0, 0]");
expect(() => {
w.webContents.setWebRTCUDPPortRange({ min: 60000, max: 56789 });
}).to.throw("'max' must be greater than or equal to 'min'");
});
it('can reset webrtc udp port range policy to default with { min: 0, max: 0 }', () => {
w.webContents.setWebRTCUDPPortRange({ min: 1, max: 65535 });
const settings = w.webContents.getWebRTCUDPPortRange();
expect(settings).to.deep.equal({ min: 1, max: 65535 });
w.webContents.setWebRTCUDPPortRange({ min: 0, max: 0 });
const defaultSetting = w.webContents.getWebRTCUDPPortRange();
expect(defaultSetting).to.deep.equal({ min: 0, max: 0 });
});
});
describe('opener api', () => {
afterEach(closeAllWindows);
it('can get opener with window.open()', async () => {