From 4fcd178c368e67e543ee10ee84a0947342412a4c Mon Sep 17 00:00:00 2001 From: bughit Date: Thu, 26 Apr 2018 06:17:55 -0400 Subject: [PATCH] expose WebFrame#routingId (#12614) * expose WebFrame#routingId and pass it to WebContents frame specific events along with frameProcessId; add WebContets.did-start-navigation event * fix compilation error on ia32 Windows --- atom/browser/api/atom_api_web_contents.cc | 93 ++++++++++++++++++-- atom/browser/api/atom_api_web_contents.h | 2 + atom/renderer/api/atom_api_web_frame.cc | 22 ++++- atom/renderer/api/atom_api_web_frame.h | 2 + lib/browser/guest-view-manager.js | 1 + lib/renderer/web-view/guest-view-internal.js | 7 +- 6 files changed, 116 insertions(+), 11 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index f89cc86fdd6c..be1af9d3b330 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -7,6 +7,16 @@ #include #include +// We have problems with redefinition of ssize_t between node.h and +// port_chromium.h, and the latter was introduced by leveldb.mojom.h. +// The best solution is to not include content/browser/frame_host/ headers +// and node.h in the same file, but for now I'm just working around the +// problem. +#if defined(OS_WIN) +#define COMPONENTS_SERVICES_LEVELDB_PUBLIC_INTERFACES_LEVELDB_MOJOM_H_ +#define COMPONENTS_LEVELDB_PUBLIC_INTERFACES_LEVELDB_MOJOM_H_ +#endif + #include "atom/browser/api/atom_api_browser_window.h" #include "atom/browser/api/atom_api_debugger.h" #include "atom/browser/api/atom_api_session.h" @@ -55,6 +65,8 @@ #include "chrome/browser/printing/print_preview_message_handler.h" #include "chrome/browser/printing/print_view_manager_basic.h" #include "chrome/browser/ssl/security_state_tab_helper.h" +#include "content/browser/frame_host/frame_tree_node.h" +#include "content/browser/frame_host/render_frame_host_manager.h" #include "content/browser/renderer_host/render_widget_host_impl.h" #include "content/browser/renderer_host/render_widget_host_view_base.h" #include "content/common/view_messages.h" @@ -840,7 +852,12 @@ void WebContents::DocumentLoadedInFrame( void WebContents::DidFinishLoad(content::RenderFrameHost* render_frame_host, const GURL& validated_url) { bool is_main_frame = !render_frame_host->GetParent(); - Emit("did-frame-finish-load", is_main_frame); + int frame_process_id = render_frame_host->GetProcess()->GetID(); + int frame_routing_id = render_frame_host->GetRoutingID(); + Emit("did-frame-finish-load", + is_main_frame, + frame_process_id, + frame_routing_id); if (is_main_frame) Emit("did-finish-load"); @@ -851,7 +868,15 @@ void WebContents::DidFailLoad(content::RenderFrameHost* render_frame_host, int error_code, const base::string16& error_description) { bool is_main_frame = !render_frame_host->GetParent(); - Emit("did-fail-load", error_code, error_description, url, is_main_frame); + int frame_process_id = render_frame_host->GetProcess()->GetID(); + int frame_routing_id = render_frame_host->GetRoutingID(); + Emit("did-fail-load", + error_code, + error_description, + url, + is_main_frame, + frame_process_id, + frame_routing_id); } void WebContents::DidStartLoading() { @@ -886,26 +911,80 @@ void WebContents::DidGetRedirectForResourceRequest( details.headers.get()); } -void WebContents::DidFinishNavigation( +void WebContents::DidStartNavigation( content::NavigationHandle* navigation_handle) { bool is_main_frame = navigation_handle->IsInMainFrame(); - if (navigation_handle->HasCommitted() && !navigation_handle->IsErrorPage()) { + int frame_tree_node_id = navigation_handle->GetFrameTreeNodeId(); + content::FrameTreeNode* frame_tree_node = + content::FrameTreeNode::GloballyFindByID(frame_tree_node_id); + content::RenderFrameHostManager* render_manager = + frame_tree_node->render_manager(); + content::RenderFrameHost* frame_host = nullptr; + if (render_manager) { + frame_host = render_manager->speculative_frame_host(); + if (!frame_host) + frame_host = render_manager->current_frame_host(); + } + int frame_process_id = -1, frame_routing_id = -1; + if (frame_host) { + frame_process_id = frame_host->GetProcess()->GetID(); + frame_routing_id = frame_host->GetRoutingID(); + } + bool is_same_document = navigation_handle->IsSameDocument(); + auto url = navigation_handle->GetURL(); + Emit("did-start-navigation", + url, + is_same_document, + is_main_frame, + frame_process_id, + frame_routing_id); +} + +void WebContents::DidFinishNavigation( + content::NavigationHandle* navigation_handle) { + if (!navigation_handle->HasCommitted()) + return; + bool is_main_frame = navigation_handle->IsInMainFrame(); + content::RenderFrameHost* frame_host = + navigation_handle->GetRenderFrameHost(); + int frame_process_id = -1, frame_routing_id = -1; + if (frame_host) { + frame_process_id = frame_host->GetProcess()->GetID(); + frame_routing_id = frame_host->GetRoutingID(); + } + 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) { - Emit("did-navigate-in-page", url, is_main_frame); + Emit("did-navigate-in-page", + url, + is_main_frame, + frame_process_id, + frame_routing_id); } } else { auto url = navigation_handle->GetURL(); int code = navigation_handle->GetNetErrorCode(); auto description = net::ErrorToShortString(code); - Emit("did-fail-provisional-load", code, description, url, is_main_frame); + Emit("did-fail-provisional-load", + code, + description, + url, + is_main_frame, + frame_process_id, + frame_routing_id); // Do not emit "did-fail-load" for canceled requests. if (code != net::ERR_ABORTED) - Emit("did-fail-load", code, description, url, is_main_frame); + Emit("did-fail-load", + code, + description, + url, + is_main_frame, + frame_process_id, + frame_routing_id); } } diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 0724e3456f4d..84c4e3eccba5 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -355,6 +355,8 @@ class WebContents : public mate::TrackableObject, const content::ResourceRequestDetails& details) override; void DidGetRedirectForResourceRequest( const content::ResourceRedirectDetails& details) override; + void DidStartNavigation( + content::NavigationHandle* navigation_handle) override; void DidFinishNavigation( content::NavigationHandle* navigation_handle) override; bool OnMessageReceived(const IPC::Message& message) override; diff --git a/atom/renderer/api/atom_api_web_frame.cc b/atom/renderer/api/atom_api_web_frame.cc index d2108235b43e..c4b23d9164e0 100644 --- a/atom/renderer/api/atom_api_web_frame.cc +++ b/atom/renderer/api/atom_api_web_frame.cc @@ -440,6 +440,24 @@ v8::Local WebFrame::FindFrameByName(const std::string& name) const { return v8::Null(isolate()); } +v8::Local WebFrame::FindFrameByRoutingId(int routing_id) const { + content::RenderFrame* render_frame = + content::RenderFrame::FromRoutingID(routing_id); + blink::WebLocalFrame* local_frame = nullptr; + if (render_frame) + local_frame = render_frame->GetWebFrame(); + if (local_frame) + return mate::CreateHandle(isolate(), + new WebFrame(isolate(), local_frame)).ToV8(); + else + return v8::Null(isolate()); +} + +v8::Local WebFrame::RoutingId() const { + int routing_id = content::RenderFrame::GetRoutingIdForWebFrame(web_frame_); + return v8::Number::New(isolate(), routing_id); +} + // static void WebFrame::BuildPrototype(v8::Isolate* isolate, v8::Local prototype) { @@ -486,7 +504,9 @@ void WebFrame::BuildPrototype(v8::Isolate* isolate, .SetProperty("parent", &WebFrame::Parent) .SetProperty("top", &WebFrame::Top) .SetProperty("firstChild", &WebFrame::FirstChild) - .SetProperty("nextSibling", &WebFrame::NextSibling); + .SetProperty("nextSibling", &WebFrame::NextSibling) + .SetProperty("routingId", &WebFrame::RoutingId) + .SetMethod("findFrameByRoutingId", &WebFrame::FindFrameByRoutingId); } } // namespace api diff --git a/atom/renderer/api/atom_api_web_frame.h b/atom/renderer/api/atom_api_web_frame.h index 54cf849009f9..1e39c544d7bf 100644 --- a/atom/renderer/api/atom_api_web_frame.h +++ b/atom/renderer/api/atom_api_web_frame.h @@ -102,6 +102,8 @@ class WebFrame : public mate::Wrappable { v8::Local NextSibling() const; v8::Local GetFrameForSelector(const std::string& selector) const; v8::Local FindFrameByName(const std::string& name) const; + v8::Local FindFrameByRoutingId(int routing_id) const; + v8::Local RoutingId() const; std::unique_ptr spell_check_client_; diff --git a/lib/browser/guest-view-manager.js b/lib/browser/guest-view-manager.js index 2a8e6c4e4e93..16eff9117bd0 100644 --- a/lib/browser/guest-view-manager.js +++ b/lib/browser/guest-view-manager.js @@ -24,6 +24,7 @@ const supportedWebViewEvents = [ 'devtools-focused', 'new-window', 'will-navigate', + 'did-start-navigation', 'did-navigate', 'did-navigate-in-page', 'close', diff --git a/lib/renderer/web-view/guest-view-internal.js b/lib/renderer/web-view/guest-view-internal.js index 043f1bf913c0..7ea5c30538f4 100644 --- a/lib/renderer/web-view/guest-view-internal.js +++ b/lib/renderer/web-view/guest-view-internal.js @@ -8,8 +8,8 @@ const WEB_VIEW_EVENTS = { 'load-commit': ['url', 'isMainFrame'], 'did-attach': [], 'did-finish-load': [], - 'did-fail-load': ['errorCode', 'errorDescription', 'validatedURL', 'isMainFrame'], - 'did-frame-finish-load': ['isMainFrame'], + 'did-fail-load': ['errorCode', 'errorDescription', 'validatedURL', 'isMainFrame', 'frameProcessId', 'frameRoutingId'], + 'did-frame-finish-load': ['isMainFrame', 'frameProcessId', 'frameRoutingId'], 'did-start-loading': [], 'did-stop-loading': [], '-did-get-response-details': ['status', 'newURL', 'originalURL', 'httpResponseCode', 'requestMethod', 'referrer', 'headers', 'resourceType'], @@ -22,8 +22,9 @@ const WEB_VIEW_EVENTS = { 'devtools-focused': [], 'new-window': ['url', 'frameName', 'disposition', 'options'], 'will-navigate': ['url'], + 'did-start-navigation': ['url', 'isInPlace', 'isMainFrame', 'frameProcessId', 'frameRoutingId'], 'did-navigate': ['url'], - 'did-navigate-in-page': ['url', 'isMainFrame'], + 'did-navigate-in-page': ['url', 'isMainFrame', 'frameProcessId', 'frameRoutingId'], 'close': [], 'crashed': [], 'gpu-crashed': [],