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
This commit is contained in:
bughit 2018-04-26 06:17:55 -04:00 committed by Cheng Zhao
parent 152422af81
commit 4fcd178c36
6 changed files with 116 additions and 11 deletions

View file

@ -7,6 +7,16 @@
#include <set>
#include <string>
// 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);
}
}

View file

@ -355,6 +355,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
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;

View file

@ -440,6 +440,24 @@ v8::Local<v8::Value> WebFrame::FindFrameByName(const std::string& name) const {
return v8::Null(isolate());
}
v8::Local<v8::Value> 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<v8::Value> 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<v8::FunctionTemplate> 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

View file

@ -102,6 +102,8 @@ class WebFrame : public mate::Wrappable<WebFrame> {
v8::Local<v8::Value> NextSibling() const;
v8::Local<v8::Value> GetFrameForSelector(const std::string& selector) const;
v8::Local<v8::Value> FindFrameByName(const std::string& name) const;
v8::Local<v8::Value> FindFrameByRoutingId(int routing_id) const;
v8::Local<v8::Value> RoutingId() const;
std::unique_ptr<SpellCheckClient> spell_check_client_;

View file

@ -24,6 +24,7 @@ const supportedWebViewEvents = [
'devtools-focused',
'new-window',
'will-navigate',
'did-start-navigation',
'did-navigate',
'did-navigate-in-page',
'close',

View file

@ -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': [],