Merge pull request #1438 from deepak1556/api_web_view_patch

webview: page-favicon-updated navigation event and getFavicon api
This commit is contained in:
Cheng Zhao 2015-04-25 13:04:43 +08:00
commit 1649d8f900
9 changed files with 72 additions and 0 deletions

View file

@ -4,6 +4,8 @@
#include "atom/browser/api/atom_api_web_contents.h" #include "atom/browser/api/atom_api_web_contents.h"
#include <set>
#include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_browser_context.h"
#include "atom/browser/native_window.h" #include "atom/browser/native_window.h"
#include "atom/browser/web_dialog_helper.h" #include "atom/browser/web_dialog_helper.h"
@ -11,10 +13,12 @@
#include "atom/common/api/api_messages.h" #include "atom/common/api/api_messages.h"
#include "atom/common/native_mate_converters/gfx_converter.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/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/string16_converter.h"
#include "atom/common/native_mate_converters/value_converter.h" #include "atom/common/native_mate_converters/value_converter.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "brightray/browser/inspectable_web_contents.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_details.h"
#include "content/public/browser/navigation_entry.h" #include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_frame_host.h"
@ -256,6 +260,19 @@ void WebContents::TitleWasSet(content::NavigationEntry* entry,
Emit("page-title-set", entry->GetTitle(), explicit_set); Emit("page-title-set", entry->GetTitle(), explicit_set);
} }
void WebContents::DidUpdateFaviconURL(
const std::vector<content::FaviconURL>& urls) {
std::set<GURL> unique_urls;
for (auto iter = urls.begin(); iter != urls.end(); ++iter) {
if (iter->icon_type != content::FaviconURL::FAVICON)
continue;
const GURL& url = iter->icon_url;
if (url.is_valid())
unique_urls.insert(url);
}
Emit("page-favicon-updated", unique_urls);
}
bool WebContents::OnMessageReceived(const IPC::Message& message) { bool WebContents::OnMessageReceived(const IPC::Message& message) {
bool handled = true; bool handled = true;
IPC_BEGIN_MESSAGE_MAP(WebContents, message) IPC_BEGIN_MESSAGE_MAP(WebContents, message)
@ -379,6 +396,13 @@ base::string16 WebContents::GetTitle() const {
return web_contents()->GetTitle(); return web_contents()->GetTitle();
} }
gfx::Image WebContents::GetFavicon() const {
auto entry = web_contents()->GetController().GetLastCommittedEntry();
if (!entry)
return gfx::Image();
return entry->GetFavicon().image;
}
bool WebContents::IsLoading() const { bool WebContents::IsLoading() const {
return web_contents()->IsLoading(); return web_contents()->IsLoading();
} }
@ -570,6 +594,7 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
.SetMethod("_loadUrl", &WebContents::LoadURL) .SetMethod("_loadUrl", &WebContents::LoadURL)
.SetMethod("getUrl", &WebContents::GetURL) .SetMethod("getUrl", &WebContents::GetURL)
.SetMethod("getTitle", &WebContents::GetTitle) .SetMethod("getTitle", &WebContents::GetTitle)
.SetMethod("getFavicon", &WebContents::GetFavicon)
.SetMethod("isLoading", &WebContents::IsLoading) .SetMethod("isLoading", &WebContents::IsLoading)
.SetMethod("isWaitingForResponse", &WebContents::IsWaitingForResponse) .SetMethod("isWaitingForResponse", &WebContents::IsWaitingForResponse)
.SetMethod("stop", &WebContents::Stop) .SetMethod("stop", &WebContents::Stop)

View file

@ -6,13 +6,16 @@
#define ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_H_ #define ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_H_
#include <string> #include <string>
#include <vector>
#include "atom/browser/api/event_emitter.h" #include "atom/browser/api/event_emitter.h"
#include "brightray/browser/default_web_contents_delegate.h" #include "brightray/browser/default_web_contents_delegate.h"
#include "content/public/browser/browser_plugin_guest_delegate.h" #include "content/public/browser/browser_plugin_guest_delegate.h"
#include "content/public/common/favicon_url.h"
#include "content/public/browser/web_contents_delegate.h" #include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_observer.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
#include "ui/gfx/image/image.h"
namespace brightray { namespace brightray {
class InspectableWebContents; class InspectableWebContents;
@ -46,6 +49,7 @@ class WebContents : public mate::EventEmitter,
void LoadURL(const GURL& url, const mate::Dictionary& options); void LoadURL(const GURL& url, const mate::Dictionary& options);
GURL GetURL() const; GURL GetURL() const;
base::string16 GetTitle() const; base::string16 GetTitle() const;
gfx::Image GetFavicon() const;
bool IsLoading() const; bool IsLoading() const;
bool IsWaitingForResponse() const; bool IsWaitingForResponse() const;
void Stop(); void Stop();
@ -175,6 +179,8 @@ class WebContents : public mate::EventEmitter,
void NavigationEntryCommitted( void NavigationEntryCommitted(
const content::LoadCommittedDetails& load_details) override; const content::LoadCommittedDetails& load_details) override;
void TitleWasSet(content::NavigationEntry* entry, bool explicit_set) override; void TitleWasSet(content::NavigationEntry* entry, bool explicit_set) override;
void DidUpdateFaviconURL(
const std::vector<content::FaviconURL>& urls) override;
// content::BrowserPluginGuestDelegate: // content::BrowserPluginGuestDelegate:
void DidAttach(int guest_proxy_routing_id) final; void DidAttach(int guest_proxy_routing_id) final;

View file

@ -16,6 +16,7 @@ supportedWebViewEvents = [
'crashed' 'crashed'
'destroyed' 'destroyed'
'page-title-set' 'page-title-set'
'page-favicon-updated'
] ]
nextInstanceId = 0 nextInstanceId = 0

View file

@ -18,6 +18,7 @@ WEB_VIEW_EVENTS =
'crashed': [] 'crashed': []
'destroyed': [] 'destroyed': []
'page-title-set': ['title', 'explicitSet'] 'page-title-set': ['title', 'explicitSet']
'page-favicon-updated': ['favicons']
dispatchEvent = (webView, event, args...) -> dispatchEvent = (webView, event, args...) ->
throw new Error("Unkown event #{event}") unless WEB_VIEW_EVENTS[event]? throw new Error("Unkown event #{event}") unless WEB_VIEW_EVENTS[event]?

View file

@ -276,6 +276,13 @@ registerWebViewElement = ->
remote.getGuestWebContents(internal.guestInstanceId)[m] args... remote.getGuestWebContents(internal.guestInstanceId)[m] args...
proto[m] = createHandler m for m in methods proto[m] = createHandler m for m in methods
# Return dataUrl instead of nativeImage.
proto.getFavicon = (args...) ->
internal = v8Util.getHiddenValue this, 'internal'
return unless internal
favicon = remote.getGuestWebContents(internal.guestInstanceId)['getFavicon'] args...
favicon.toDataUrl()
window.WebView = webFrame.registerEmbedderCustomElement 'webview', window.WebView = webFrame.registerEmbedderCustomElement 'webview',
prototype: proto prototype: proto

View file

@ -662,6 +662,13 @@ Emitted when details regarding a requested resource is available.
Emitted when a redirect was received while requesting a resource. Emitted when a redirect was received while requesting a resource.
### Event: 'page-favicon-updated'
* `event` Event
* `favicons` Array - Array of Urls
Emitted when page receives favicon urls.
### Event: 'new-window' ### Event: 'new-window'
* `event` Event * `event` Event
@ -713,6 +720,10 @@ Returns URL of current web page.
Returns the title of web page. Returns the title of web page.
### WebContents.getFavicon()
Returns the favicon of web page as [NativeImage](native-image.md).
### WebContents.isLoading() ### WebContents.isLoading()
Returns whether web page is still loading resources. Returns whether web page is still loading resources.

View file

@ -130,6 +130,10 @@ Returns URL of guest page.
Returns the title of guest page. Returns the title of guest page.
### `<webview>`.getFavicon()
Returns the favicon of guest page as dataUrl.
### `<webview>`.isLoading() ### `<webview>`.isLoading()
Returns whether guest page is still loading resources. Returns whether guest page is still loading resources.
@ -330,6 +334,12 @@ 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 Fired when page title is set during navigation. `explicitSet` is false when title is synthesised from file
url. url.
### page-favicon-updated
* `favicons` Array - Array of Urls
Fired when page receives favicon urls.
### console-message ### console-message
* `level` Integer * `level` Integer

View file

@ -1,4 +1,6 @@
<html> <html>
<link rel="icon" type="image/png" href="/favicon.png"/>
<link rel="icon" type="image/png" href="http://test.com/favicon.png"/>
<body> <body>
<script type="text/javascript" charset="utf-8"> <script type="text/javascript" charset="utf-8">
console.log('a'); console.log('a');

View file

@ -151,3 +151,12 @@ describe '<webview> tag', ->
done() done()
webview.src = "file://#{fixtures}/pages/a.html" webview.src = "file://#{fixtures}/pages/a.html"
document.body.appendChild webview document.body.appendChild webview
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()
webview.src = "file://#{fixtures}/pages/a.html"
document.body.appendChild webview