feat: add frame and webContents to webRequest details (#27078)

* feat: add frame and webContents to webRequest details

* chore: use frame_converter.h

Co-authored-by: Cheng Zhao <zcbenz@gmail.com>
This commit is contained in:
Jeremy Rose 2021-01-10 19:20:43 -08:00 committed by GitHub
parent 81dc8a0d04
commit 088f2e625f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 6 deletions

View file

@ -51,6 +51,8 @@ The following methods are available on instances of `WebRequest`:
* `url` String * `url` String
* `method` String * `method` String
* `webContentsId` Integer (optional) * `webContentsId` Integer (optional)
* `webContents` WebContents (optional)
* `frame` WebFrameMain (optional)
* `resourceType` String * `resourceType` String
* `referrer` String * `referrer` String
* `timestamp` Double * `timestamp` Double
@ -94,6 +96,8 @@ Some examples of valid `urls`:
* `url` String * `url` String
* `method` String * `method` String
* `webContentsId` Integer (optional) * `webContentsId` Integer (optional)
* `webContents` WebContents (optional)
* `frame` WebFrameMain (optional)
* `resourceType` String * `resourceType` String
* `referrer` String * `referrer` String
* `timestamp` Double * `timestamp` Double
@ -121,6 +125,8 @@ The `callback` has to be called with a `response` object.
* `url` String * `url` String
* `method` String * `method` String
* `webContentsId` Integer (optional) * `webContentsId` Integer (optional)
* `webContents` WebContents (optional)
* `frame` WebFrameMain (optional)
* `resourceType` String * `resourceType` String
* `referrer` String * `referrer` String
* `timestamp` Double * `timestamp` Double
@ -141,6 +147,8 @@ response are visible by the time this listener is fired.
* `url` String * `url` String
* `method` String * `method` String
* `webContentsId` Integer (optional) * `webContentsId` Integer (optional)
* `webContents` WebContents (optional)
* `frame` WebFrameMain (optional)
* `resourceType` String * `resourceType` String
* `referrer` String * `referrer` String
* `timestamp` Double * `timestamp` Double
@ -173,6 +181,8 @@ The `callback` has to be called with a `response` object.
* `url` String * `url` String
* `method` String * `method` String
* `webContentsId` Integer (optional) * `webContentsId` Integer (optional)
* `webContents` WebContents (optional)
* `frame` WebFrameMain (optional)
* `resourceType` String * `resourceType` String
* `referrer` String * `referrer` String
* `timestamp` Double * `timestamp` Double
@ -197,6 +207,8 @@ and response headers are available.
* `url` String * `url` String
* `method` String * `method` String
* `webContentsId` Integer (optional) * `webContentsId` Integer (optional)
* `webContents` WebContents (optional)
* `frame` WebFrameMain (optional)
* `resourceType` String * `resourceType` String
* `referrer` String * `referrer` String
* `timestamp` Double * `timestamp` Double
@ -222,6 +234,8 @@ redirect is about to occur.
* `url` String * `url` String
* `method` String * `method` String
* `webContentsId` Integer (optional) * `webContentsId` Integer (optional)
* `webContents` WebContents (optional)
* `frame` WebFrameMain (optional)
* `resourceType` String * `resourceType` String
* `referrer` String * `referrer` String
* `timestamp` Double * `timestamp` Double
@ -245,6 +259,8 @@ completed.
* `url` String * `url` String
* `method` String * `method` String
* `webContentsId` Integer (optional) * `webContentsId` Integer (optional)
* `webContents` WebContents (optional)
* `frame` WebFrameMain (optional)
* `resourceType` String * `resourceType` String
* `referrer` String * `referrer` String
* `timestamp` Double * `timestamp` Double

View file

@ -17,9 +17,11 @@
#include "net/http/http_content_disposition.h" #include "net/http/http_content_disposition.h"
#include "shell/browser/api/electron_api_session.h" #include "shell/browser/api/electron_api_session.h"
#include "shell/browser/api/electron_api_web_contents.h" #include "shell/browser/api/electron_api_web_contents.h"
#include "shell/browser/api/electron_api_web_frame_main.h"
#include "shell/browser/electron_browser_context.h" #include "shell/browser/electron_browser_context.h"
#include "shell/browser/javascript_environment.h" #include "shell/browser/javascript_environment.h"
#include "shell/common/gin_converters/callback_converter.h" #include "shell/common/gin_converters/callback_converter.h"
#include "shell/common/gin_converters/frame_converter.h"
#include "shell/common/gin_converters/gurl_converter.h" #include "shell/common/gin_converters/gurl_converter.h"
#include "shell/common/gin_converters/net_converter.h" #include "shell/common/gin_converters/net_converter.h"
#include "shell/common/gin_converters/std_converter.h" #include "shell/common/gin_converters/std_converter.h"
@ -156,12 +158,18 @@ void ToDictionary(gin::Dictionary* details, extensions::WebRequestInfo* info) {
HttpResponseHeadersToV8(info->response_headers.get())); HttpResponseHeadersToV8(info->response_headers.get()));
} }
auto* web_contents = content::WebContents::FromRenderFrameHost( auto* render_frame_host =
content::RenderFrameHost::FromID(info->render_process_id, content::RenderFrameHost::FromID(info->render_process_id, info->frame_id);
info->frame_id)); if (render_frame_host) {
details->Set("frame", render_frame_host);
auto* web_contents =
content::WebContents::FromRenderFrameHost(render_frame_host);
auto* api_web_contents = WebContents::From(web_contents); auto* api_web_contents = WebContents::From(web_contents);
if (api_web_contents) if (api_web_contents) {
details->Set("webContents", api_web_contents);
details->Set("webContentsId", api_web_contents->ID()); details->Set("webContentsId", api_web_contents->ID());
}
}
} }
void ToDictionary(gin::Dictionary* details, void ToDictionary(gin::Dictionary* details,

View file

@ -90,6 +90,9 @@ describe('webRequest module', () => {
expect(details.id).to.be.a('number'); expect(details.id).to.be.a('number');
expect(details.timestamp).to.be.a('number'); expect(details.timestamp).to.be.a('number');
expect(details.webContentsId).to.be.a('number'); expect(details.webContentsId).to.be.a('number');
expect(details.webContents).to.be.an('object');
expect(details.webContents!.id).to.equal(details.webContentsId);
expect(details.frame).to.be.an('object');
expect(details.url).to.be.a('string').that.is.equal(defaultURL); expect(details.url).to.be.a('string').that.is.equal(defaultURL);
expect(details.method).to.be.a('string').that.is.equal('GET'); expect(details.method).to.be.a('string').that.is.equal('GET');
expect(details.resourceType).to.be.a('string').that.is.equal('xhr'); expect(details.resourceType).to.be.a('string').that.is.equal('xhr');