diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 64a76d57e652..c6943d506e67 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -220,7 +220,8 @@ WebContents::WebContents(content::WebContents* web_contents) embedder_(nullptr), type_(REMOTE), request_id_(0), - background_throttling_(true) { + background_throttling_(true), + is_loading_main_frame_(false) { AttachAsUserData(web_contents); web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent()); } @@ -229,7 +230,8 @@ WebContents::WebContents(v8::Isolate* isolate, const mate::Dictionary& options) : embedder_(nullptr), request_id_(0), - background_throttling_(true) { + background_throttling_(true), + is_loading_main_frame_(false) { // Read options. options.Get("backgroundThrottling", &background_throttling_); @@ -543,12 +545,32 @@ void WebContents::DocumentLoadedInFrame( void WebContents::DidFinishLoad(content::RenderFrameHost* render_frame_host, const GURL& validated_url) { bool is_main_frame = !render_frame_host->GetParent(); + if (is_main_frame) + is_loading_main_frame_ = false; + Emit("did-frame-finish-load", is_main_frame); if (is_main_frame) Emit("did-finish-load"); } +void WebContents::DidStartProvisionalLoadForFrame( + content::RenderFrameHost* render_frame_host, + const GURL& url, + bool is_error_page, + bool is_iframe_srcdoc) { + if (!render_frame_host->GetParent()) + is_loading_main_frame_ = true; +} + +void WebContents::DidCommitProvisionalLoadForFrame( + content::RenderFrameHost* render_frame_host, + const GURL& url, + ui::PageTransition transition_type) { + if (!render_frame_host->GetParent()) + is_loading_main_frame_ = true; +} + void WebContents::DidFailProvisionalLoad( content::RenderFrameHost* render_frame_host, const GURL& url, @@ -556,6 +578,8 @@ void WebContents::DidFailProvisionalLoad( const base::string16& description, bool was_ignored_by_handler) { bool is_main_frame = !render_frame_host->GetParent(); + if (is_main_frame) + is_loading_main_frame_ = false; Emit("did-fail-provisional-load", code, description, url, is_main_frame); Emit("did-fail-load", code, description, url, is_main_frame); } @@ -792,6 +816,10 @@ base::string16 WebContents::GetTitle() const { bool WebContents::IsLoading() const { return web_contents()->IsLoading(); } + +bool WebContents::IsLoadingMainFrame() const { + return is_loading_main_frame_; +} bool WebContents::IsWaitingForResponse() const { return web_contents()->IsWaitingForResponse(); @@ -1189,6 +1217,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, .SetMethod("_getURL", &WebContents::GetURL) .SetMethod("getTitle", &WebContents::GetTitle) .SetMethod("isLoading", &WebContents::IsLoading) + .SetMethod("isLoadingMainFrame", &WebContents::IsLoadingMainFrame) .SetMethod("isWaitingForResponse", &WebContents::IsWaitingForResponse) .SetMethod("_stop", &WebContents::Stop) .SetMethod("_goBack", &WebContents::GoBack) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 0cb2a348e170..7e4bd693536a 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -62,6 +62,7 @@ class WebContents : public mate::TrackableObject, GURL GetURL() const; base::string16 GetTitle() const; bool IsLoading() const; + bool IsLoadingMainFrame() const; bool IsWaitingForResponse() const; void Stop(); void ReloadIgnoringCache(); @@ -228,6 +229,13 @@ class WebContents : public mate::TrackableObject, int error_code, const base::string16& error_description, bool was_ignored_by_handler) override; + void DidStartProvisionalLoadForFrame(content::RenderFrameHost* render_frame_host, + const GURL& validated_url, + bool is_error_page, + bool is_iframe_srcdoc) override; + void DidCommitProvisionalLoadForFrame(content::RenderFrameHost* render_frame_host, + const GURL& url, + ui::PageTransition transition_type) override; void DidStartLoading() override; void DidStopLoading() override; void DidGetResourceResponseStart( @@ -302,6 +310,9 @@ class WebContents : public mate::TrackableObject, // Whether background throttling is disabled. bool background_throttling_; + // Whether the main frame (not just a sub-frame) is currently loading. + bool is_loading_main_frame_; + DISALLOW_COPY_AND_ASSIGN(WebContents); }; diff --git a/lib/browser/api/web-contents.js b/lib/browser/api/web-contents.js index 40efa77cda02..dfacf6a02cf1 100644 --- a/lib/browser/api/web-contents.js +++ b/lib/browser/api/web-contents.js @@ -116,7 +116,7 @@ let wrapWebContents = function (webContents) { callback = hasUserGesture hasUserGesture = false } - if (this.getURL() && !this.isLoading()) { + if (this.getURL() && !this.isLoadingMainFrame()) { return asyncWebFrameMethods.call(this, requestId, 'executeJavaScript', callback, code, hasUserGesture) } else { return this.once('did-finish-load', asyncWebFrameMethods.bind(this, requestId, 'executeJavaScript', callback, code, hasUserGesture)) diff --git a/lib/renderer/web-view/web-view.js b/lib/renderer/web-view/web-view.js index 974d5c6608d6..20f5f07465b5 100644 --- a/lib/renderer/web-view/web-view.js +++ b/lib/renderer/web-view/web-view.js @@ -335,6 +335,7 @@ var registerWebViewElement = function () { 'loadURL', 'getTitle', 'isLoading', + 'isLoadingMainFrame', 'isWaitingForResponse', 'stop', 'reload',