From 55a7f6f0ce8f6ceda70925c84fb720e8c43261c1 Mon Sep 17 00:00:00 2001 From: bughit Date: Tue, 1 May 2018 00:34:41 -0400 Subject: [PATCH] add did-frame-navigate event to WebContents (#12723) * add did-frame-navigate event to WebContents, pass http response code to it and did-navigate * docs for frame routing id related api changes on WebFrame and WebContents --- atom/browser/api/atom_api_web_contents.cc | 23 +++++++-- docs/api/web-contents.md | 52 ++++++++++++++++++-- docs/api/web-frame.md | 16 +++++- lib/browser/guest-view-manager.js | 1 + lib/renderer/web-view/guest-view-internal.js | 3 +- 5 files changed, 87 insertions(+), 8 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index be1af9d3b3..e51792d1f5 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -955,14 +955,31 @@ void WebContents::DidFinishNavigation( if (!navigation_handle->IsErrorPage()) { auto url = navigation_handle->GetURL(); bool is_same_document = navigation_handle->IsSameDocument(); - if (is_main_frame && !is_same_document) { - Emit("did-navigate", url); - } else if (is_same_document) { + if (is_same_document) { Emit("did-navigate-in-page", + url, + is_main_frame, + frame_process_id, + frame_routing_id); + } else { + const net::HttpResponseHeaders* http_response + = navigation_handle->GetResponseHeaders(); + std::string http_status_text; + int http_response_code = -1; + if (http_response) { + http_status_text = http_response->GetStatusText(); + http_response_code = http_response->response_code(); + } + Emit("did-frame-navigate", url, + http_response_code, + http_status_text, is_main_frame, frame_process_id, frame_routing_id); + if (is_main_frame) { + Emit("did-navigate", url, http_response_code, http_status_text); + } } } else { auto url = navigation_handle->GetURL(); diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index b49d274b23..94e9cc333e 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -67,6 +67,8 @@ Returns: * `errorDescription` String * `validatedURL` String * `isMainFrame` Boolean +* `frameProcessId` Integer +* `frameRoutingId` Integer This event is like `did-finish-load` but emitted when the load failed or was cancelled, e.g. `window.stop()` is invoked. @@ -78,6 +80,8 @@ Returns: * `event` Event * `isMainFrame` Boolean +* `frameProcessId` Integer +* `frameRoutingId` Integer Emitted when a frame has done navigation. @@ -195,14 +199,47 @@ this purpose. Calling `event.preventDefault()` will prevent the navigation. +#### Event: 'did-start-navigation' + +Returns: + +* `url` String +* `isInPlace` Boolean +* `isMainFrame` Boolean +* `frameProcessId` Integer +* `frameRoutingId` Integer + +Emitted when any frame (including main) starts navigating. `isInplace` will be +`true` for in-page navigations. + #### Event: 'did-navigate' Returns: * `event` Event * `url` String +* `httpResponseCode` Integer - -1 for non HTTP navigations +* `httpStatusText` String - empty for non HTTP navigations -Emitted when a navigation is done. +Emitted when a main frame navigation is done. + +This event is not emitted for in-page navigations, such as clicking anchor links +or updating the `window.location.hash`. Use `did-navigate-in-page` event for +this purpose. + +#### Event: 'did-frame-navigate' + +Returns: + +* `event` Event +* `url` String +* `httpResponseCode` Integer - -1 for non HTTP navigations +* `httpStatusText` String - empty for non HTTP navigations, +* `isMainFrame` Boolean +* `frameProcessId` Integer +* `frameRoutingId` Integer + +Emitted when any frame navigation is done. This event is not emitted for in-page navigations, such as clicking anchor links or updating the `window.location.hash`. Use `did-navigate-in-page` event for @@ -215,8 +252,10 @@ Returns: * `event` Event * `url` String * `isMainFrame` Boolean +* `frameProcessId` Integer +* `frameRoutingId` Integer -Emitted when an in-page navigation happened. +Emitted when an in-page navigation happened in any frame. When in-page navigation happens, the page URL changes but does not cause navigation outside of the page. Examples of this occurring are when anchor links @@ -1439,7 +1478,14 @@ more details. #### `contents.getOSProcessId()` -Returns `Integer` - The `pid` of the associated renderer process. +Returns `Integer` - The operating system `pid` of the associated renderer +process. + +#### `contents.getProcessId()` + +Returns `Integer` - The chromium internal `pid` of the associated renderer. Can +be compared to the `frameProcessId` passed by frame specific navigation events +(e.g. `did-frame-navigate`) ### Instance Properties diff --git a/docs/api/web-frame.md b/docs/api/web-frame.md index cc7ab36060..20eaea77a9 100644 --- a/docs/api/web-frame.md +++ b/docs/api/web-frame.md @@ -235,6 +235,15 @@ Returns `WebFrame` - A child of `webFrame` with the supplied `name`, `null` would be returned if there's no such frame or if the frame is not in the current renderer process. +### `webFrame.findFrameByRoutingId(routingId)` + +* `routingId` Integer - A unique frame id in the current renderer process. + Routing IDs can be retrieved from `WebFrame` instances (`webFrame.routingId`) + and are also passed by frame specific `WebContents` navigation events (e.g. + `did-frame-navigate`) + +Returns `WebFrame` - that has the supplied `routingId`, `null` if not found. + ## Properties ### `webFrame.top` @@ -262,5 +271,10 @@ current renderer process. ### `webFrame.nextSibling` A `WebFrame` representing next sibling frame, the property would be `null` if -`webFrame` is the last frame its parent or if the next sibling is not in the +`webFrame` is the last frame in its parent or if the next sibling is not in the current renderer process. + +### `webFrame.routingId` + +A unique frame id in the current renderer process. Distinct WebFrame instances +that refer to the same underlying frame will have the same `routingId`. \ No newline at end of file diff --git a/lib/browser/guest-view-manager.js b/lib/browser/guest-view-manager.js index 16eff9117b..e7f620a6e2 100644 --- a/lib/browser/guest-view-manager.js +++ b/lib/browser/guest-view-manager.js @@ -26,6 +26,7 @@ const supportedWebViewEvents = [ 'will-navigate', 'did-start-navigation', 'did-navigate', + 'did-frame-navigate', 'did-navigate-in-page', 'close', 'crashed', diff --git a/lib/renderer/web-view/guest-view-internal.js b/lib/renderer/web-view/guest-view-internal.js index 7ea5c30538..52ef64cd77 100644 --- a/lib/renderer/web-view/guest-view-internal.js +++ b/lib/renderer/web-view/guest-view-internal.js @@ -23,7 +23,8 @@ const WEB_VIEW_EVENTS = { 'new-window': ['url', 'frameName', 'disposition', 'options'], 'will-navigate': ['url'], 'did-start-navigation': ['url', 'isInPlace', 'isMainFrame', 'frameProcessId', 'frameRoutingId'], - 'did-navigate': ['url'], + 'did-navigate': ['url', 'httpResponseCode', 'httpStatusText'], + 'did-frame-navigate': ['url', 'httpResponseCode', 'httpStatusText', 'isMainFrame', 'frameProcessId', 'frameRoutingId'], 'did-navigate-in-page': ['url', 'isMainFrame', 'frameProcessId', 'frameRoutingId'], 'close': [], 'crashed': [],