added did-navigate event, getFavicon api and workaround webview spec
authordeepak1556 <hop2deep@gmail.com>
Mon, 20 Apr 2015 06:50:04 +0000 (12:20 +0530)
committerdeepak1556 <hop2deep@gmail.com>
Sat, 25 Apr 2015 04:20:28 +0000 (09:50 +0530)
atom/browser/api/atom_api_web_contents.cc
atom/browser/api/atom_api_web_contents.h
atom/browser/lib/guest-view-manager.coffee
atom/renderer/lib/web-view/guest-view-internal.coffee
atom/renderer/lib/web-view/web-view-attributes.coffee
atom/renderer/lib/web-view/web-view.coffee
docs/api/browser-window.md
docs/api/web-view-tag.md
spec/webview-spec.coffee

index dd69a4e..c0d4dd6 100644 (file)
 #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)
index 384cf29..484501d 100644 (file)
@@ -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();
index a949617..2553490 100644 (file)
@@ -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
index 19cca6a..004dfa9 100644 (file)
@@ -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]?
index eb801eb..fb0c0a1 100644 (file)
@@ -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
index 3058863..51faeb7 100644 (file)
@@ -236,6 +236,7 @@ registerWebViewElement = ->
   methods = [
     "getUrl"
     "getTitle"
+    "getFavicon"
     "isLoading"
     "isWaitingForResponse"
     "stop"
index 62eeb33..9a4bd9f 100644 (file)
@@ -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.
index b7572ee..01be3dc 100644 (file)
@@ -130,6 +130,10 @@ Returns URL of guest page.
 
 Returns the title of guest page.
 
+### `<webview>`.getFavicon()
+
+Returns the favicon of guest page as `nativeImage`.
+
 ### `<webview>`.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]
 
index 3ac34fd..402d809 100644 (file)
@@ -154,16 +154,20 @@ describe '<webview> 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()