diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index dd69a4ee6..c0d4dd615 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -11,10 +11,12 @@ #include "atom/common/api/api_messages.h" #include "atom/common/native_mate_converters/gfx_converter.h" #include "atom/common/native_mate_converters/gurl_converter.h" +#include "atom/common/native_mate_converters/image_converter.h" #include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/native_mate_converters/value_converter.h" #include "base/strings/utf_string_conversions.h" #include "brightray/browser/inspectable_web_contents.h" +#include "content/public/browser/favicon_status.h" #include "content/public/browser/navigation_details.h" #include "content/public/browser/navigation_entry.h" #include "content/public/browser/render_frame_host.h" @@ -266,7 +268,7 @@ void WebContents::DidUpdateFaviconURL( if (url.is_valid()) unique_urls.insert(url); } - Emit("page-favicon-set", unique_urls); + Emit("page-favicon-updated", unique_urls); } bool WebContents::OnMessageReceived(const IPC::Message& message) { @@ -308,6 +310,8 @@ void WebContents::WebContentsDestroyed() { void WebContents::NavigationEntryCommitted( const content::LoadCommittedDetails& load_details) { + if (load_details.is_navigation_to_different_page()) + Emit("did-navigate"); auto entry = web_contents()->GetController().GetLastCommittedEntry(); entry->SetVirtualURL(load_details.entry->GetOriginalRequestURL()); } @@ -392,6 +396,14 @@ base::string16 WebContents::GetTitle() const { return web_contents()->GetTitle(); } +gfx::Image WebContents::GetFavicon() const { + auto entry = web_contents()->GetController().GetLastCommittedEntry(); + if (!entry) + return gfx::Image(); + auto favicon_status = entry->GetFavicon(); + return favicon_status.image; +} + bool WebContents::IsLoading() const { return web_contents()->IsLoading(); } @@ -583,6 +595,7 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( .SetMethod("_loadUrl", &WebContents::LoadURL) .SetMethod("getUrl", &WebContents::GetURL) .SetMethod("getTitle", &WebContents::GetTitle) + .SetMethod("getFavicon", &WebContents::GetFavicon) .SetMethod("isLoading", &WebContents::IsLoading) .SetMethod("isWaitingForResponse", &WebContents::IsWaitingForResponse) .SetMethod("stop", &WebContents::Stop) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 384cf29ad..484501df4 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -16,6 +16,7 @@ #include "content/public/browser/web_contents_delegate.h" #include "content/public/browser/web_contents_observer.h" #include "native_mate/handle.h" +#include "ui/gfx/image/image.h" namespace brightray { class InspectableWebContents; @@ -49,6 +50,7 @@ class WebContents : public mate::EventEmitter, void LoadURL(const GURL& url, const mate::Dictionary& options); GURL GetURL() const; base::string16 GetTitle() const; + gfx::Image GetFavicon() const; bool IsLoading() const; bool IsWaitingForResponse() const; void Stop(); diff --git a/atom/browser/lib/guest-view-manager.coffee b/atom/browser/lib/guest-view-manager.coffee index a949617da..255349082 100644 --- a/atom/browser/lib/guest-view-manager.coffee +++ b/atom/browser/lib/guest-view-manager.coffee @@ -6,6 +6,7 @@ supportedWebViewEvents = [ 'did-finish-load' 'did-fail-load' 'did-frame-finish-load' + 'did-navigate' 'did-start-loading' 'did-stop-loading' 'did-get-response-details' @@ -16,7 +17,7 @@ supportedWebViewEvents = [ 'crashed' 'destroyed' 'page-title-set' - 'page-favicon-set' + 'page-favicon-updated' ] nextInstanceId = 0 diff --git a/atom/renderer/lib/web-view/guest-view-internal.coffee b/atom/renderer/lib/web-view/guest-view-internal.coffee index 19cca6a83..004dfa98f 100644 --- a/atom/renderer/lib/web-view/guest-view-internal.coffee +++ b/atom/renderer/lib/web-view/guest-view-internal.coffee @@ -7,6 +7,7 @@ WEB_VIEW_EVENTS = 'did-finish-load': [] 'did-fail-load': ['errorCode', 'errorDescription'] 'did-frame-finish-load': ['isMainFrame'] + 'did-navigate': [] 'did-start-loading': [] 'did-stop-loading': [] 'did-get-response-details': ['status', 'newUrl', 'originalUrl', @@ -18,7 +19,7 @@ WEB_VIEW_EVENTS = 'crashed': [] 'destroyed': [] 'page-title-set': ['title', 'explicitSet'] - 'page-favicon-set': ['favicons'] + 'page-favicon-updated': ['favicons'] dispatchEvent = (webView, event, args...) -> throw new Error("Unkown event #{event}") unless WEB_VIEW_EVENTS[event]? diff --git a/atom/renderer/lib/web-view/web-view-attributes.coffee b/atom/renderer/lib/web-view/web-view-attributes.coffee index eb801eb97..fb0c0a1b8 100644 --- a/atom/renderer/lib/web-view/web-view-attributes.coffee +++ b/atom/renderer/lib/web-view/web-view-attributes.coffee @@ -154,6 +154,7 @@ class SrcAttribute extends WebViewAttribute not @.getValue() return + # Allow users to cancel webview navigation. domEvent = new Event('will-navigate') domEvent['url'] = @getValue() domEvent.cancelable = true diff --git a/atom/renderer/lib/web-view/web-view.coffee b/atom/renderer/lib/web-view/web-view.coffee index 3058863f7..51faeb796 100644 --- a/atom/renderer/lib/web-view/web-view.coffee +++ b/atom/renderer/lib/web-view/web-view.coffee @@ -236,6 +236,7 @@ registerWebViewElement = -> methods = [ "getUrl" "getTitle" + "getFavicon" "isLoading" "isWaitingForResponse" "stop" diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 62eeb332e..9a4bd9fc8 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -632,6 +632,12 @@ cancelled, e.g. `window.stop()` is invoked. Emitted when a frame has done navigation. +### Event: 'did-navigate' + +* `event` Event + +Emitted when a frame navigation has been committed to history. + ### Event: 'did-start-loading' Corresponds to the points in time when the spinner of the tab starts spinning. @@ -662,7 +668,7 @@ Emitted when details regarding a requested resource is available. Emitted when a redirect was received while requesting a resource. -### Event: 'page-favicon-set' +### Event: 'page-favicon-updated' * `event` Event * `favicons` [String] @@ -720,6 +726,10 @@ Returns URL of current web page. Returns the title of web page. +### WebContents.getFavicon() + +Returns the favicon of web page as `nativeImage`. + ### WebContents.isLoading() Returns whether web page is still loading resources. diff --git a/docs/api/web-view-tag.md b/docs/api/web-view-tag.md index b7572eefa..01be3dca0 100644 --- a/docs/api/web-view-tag.md +++ b/docs/api/web-view-tag.md @@ -130,6 +130,10 @@ Returns URL of guest page. Returns the title of guest page. +### ``.getFavicon() + +Returns the favicon of guest page as `nativeImage`. + ### ``.isLoading() Returns whether guest page is still loading resources. @@ -301,6 +305,10 @@ cancelled, e.g. `window.stop()` is invoked. Fired when a frame has done navigation. +### did-navigate + +Fired when a frame navigation has been committed to history. + ### did-start-loading Corresponds to the points in time when the spinner of the tab starts spinning. @@ -337,7 +345,7 @@ Fired when a redirect was received while requesting a resource. Fired when page title is set during navigation. `explicitSet` is false when title is synthesised from file url. -### page-favicon-set +### page-favicon-updated * `favicons` [String] diff --git a/spec/webview-spec.coffee b/spec/webview-spec.coffee index 3ac34fdf9..402d80942 100644 --- a/spec/webview-spec.coffee +++ b/spec/webview-spec.coffee @@ -154,16 +154,20 @@ describe ' tag', -> describe 'will-navigate event', -> it 'is emitted before navigation', (done) -> - webview.addEventListener 'will-navigate', (e) -> + view = new WebView + view.addEventListener 'will-navigate', (e) -> e.preventDefault() assert.equal webview.src, '' + view.addEventListener 'did-navigate', (e) -> + document.body.removeChild view done() - webview.src = "file://#{fixtures}/pages/a.html" + view.src = "file://#{fixtures}/pages/a.html" + document.body.appendChild view document.body.appendChild webview - describe 'page-favicon-set event', -> - it 'emits when favicon is set', (done) -> - webview.addEventListener 'page-favicon-set', (e) -> + describe 'page-favicon-updated event', -> + it 'emits when favicon urls are received', (done) -> + webview.addEventListener 'page-favicon-updated', (e) -> assert.equal e.favicons.length, 2 assert.equal e.favicons[0], 'file:///favicon.png' done()