Skip to content

Commit

Permalink
On title and favicon change event on webview:
Browse files Browse the repository at this point in the history
-titlechange
-faviconchange
  • Loading branch information
janRucka committed Feb 17, 2017
1 parent c90786f commit 9bd3e48
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 1 deletion.
2 changes: 2 additions & 0 deletions extensions/browser/extension_event_histogram_value.h
Expand Up @@ -426,6 +426,8 @@ enum HistogramValue {
WEB_VIEW_INTERNAL_ON_SSL_CHANGE,
WEB_VIEW_INTERNAL_ON_CERTIFICATE_ERROR,
WEB_VIEW_INTERNAL_ON_SUBFRAME_CERTIFICATE_ERROR,
WEB_VIEW_INTERNAL_ON_TITLE_CHANGE,
WEB_VIEW_INTERNAL_ON_FAVICON_CHANGE,
// Last entry: Add new entries above, then run:
// python tools/metrics/histograms/update_extension_histograms.py
ENUM_BOUNDARY
Expand Down
2 changes: 2 additions & 0 deletions extensions/browser/guest_view/guest_view_events.cc
Expand Up @@ -86,6 +86,8 @@ class EventMap {
events::WEB_VIEW_INTERNAL_ON_ERROR_OCCURRED},
{webview::kEventSendHeaders, events::WEB_VIEW_INTERNAL_ON_SEND_HEADERS},
{webview::kEventSSLChange, events::WEB_VIEW_INTERNAL_ON_SSL_CHANGE},
{webview::kEventFaviconChange, events::WEB_VIEW_INTERNAL_ON_FAVICON_CHANGE},
{webview::kEventTitleChange, events::WEB_VIEW_INTERNAL_ON_TITLE_CHANGE},
};
for (const auto& name_and_value : names_and_values) {
values_[name_and_value.name] = name_and_value.value;
Expand Down
4 changes: 4 additions & 0 deletions extensions/browser/guest_view/web_view/web_view_constants.cc
Expand Up @@ -33,6 +33,7 @@ const char kEventDialog[] = "webViewInternal.onDialog";
const char kEventDropLink[] = "webViewInternal.onDropLink";
const char kEventExit[] = "webViewInternal.onExit";
const char kEventExitFullscreen[] = "webViewInternal.onExitFullscreen";
const char kEventFaviconChange[] = "webViewInternal.onFaviconChange";
const char kEventFindReply[] = "webViewInternal.onFindReply";
const char kEventFrameNameChanged[] = "webViewInternal.onFrameNameChanged";
const char kEventHeadersReceived[] = "webViewInternal.onHeadersReceived";
Expand All @@ -50,6 +51,7 @@ const char kEventResponsive[] = "webViewInternal.onResponsive";
const char kEventSizeChanged[] = "webViewInternal.onSizeChanged";
const char kEventSSLChange[] = "webViewInternal.onSSLChange";
const char kEventSubFrameCertificateError[] = "webViewInternal.onSubFrameCertificateError";
const char kEventTitleChange[] = "webViewInternal.onTitleChange";
const char kEventUnresponsive[] = "webViewInternal.onUnresponsive";
const char kEventZoomChange[] = "webViewInternal.onZoomChange";

Expand All @@ -69,6 +71,7 @@ const char kWebViewEventPrefix[] = "webViewInternal.";
const char kCertificate[] = "certificate";
const char kContextMenuItems[] = "items";
const char kDefaultPromptText[] = "defaultPromptText";
const char kFaviconUrl[] = "faviconUrl";
const char kFindSearchText[] = "searchText";
const char kFindFinalUpdate[] = "finalUpdate";
const char kInitialHeight[] = "initialHeight";
Expand Down Expand Up @@ -104,6 +107,7 @@ const char kRequestId[] = "requestId";
const char kRequestInfo[] = "requestInfo";
const char kSourceId[] = "sourceId";
const char kTargetURL[] = "targetUrl";
const char kTitle[] = "title";
const char kWindowID[] = "windowId";
const char kWindowOpenDisposition[] = "windowOpenDisposition";
const char kOldZoomFactor[] = "oldZoomFactor";
Expand Down
4 changes: 4 additions & 0 deletions extensions/browser/guest_view/web_view/web_view_constants.h
Expand Up @@ -39,6 +39,7 @@ extern const char kEventDialog[];
extern const char kEventDropLink[];
extern const char kEventExit[];
extern const char kEventExitFullscreen[];
extern const char kEventFaviconChange[];
extern const char kEventFindReply[];
extern const char kEventFrameNameChanged[];
extern const char kEventHeadersReceived[];
Expand All @@ -56,6 +57,7 @@ extern const char kEventResponsive[];
extern const char kEventSizeChanged[];
extern const char kEventSSLChange[];
extern const char kEventSubFrameCertificateError[];
extern const char kEventTitleChange[];
extern const char kEventUnresponsive[];
extern const char kEventZoomChange[];

Expand All @@ -75,6 +77,7 @@ extern const char kWebViewEventPrefix[];
extern const char kCertificate[];
extern const char kContextMenuItems[];
extern const char kDefaultPromptText[];
extern const char kFaviconUrl[];
extern const char kFindSearchText[];
extern const char kFindFinalUpdate[];
extern const char kInitialHeight[];
Expand Down Expand Up @@ -110,6 +113,7 @@ extern const char kRequestId[];
extern const char kRequestInfo[];
extern const char kSourceId[];
extern const char kTargetURL[];
extern const char kTitle[];
extern const char kWindowID[];
extern const char kWindowOpenDisposition[];
extern const char kOldZoomFactor[];
Expand Down
29 changes: 28 additions & 1 deletion extensions/browser/guest_view/web_view/web_view_guest.cc
Expand Up @@ -43,6 +43,7 @@
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/common/favicon_url.h"
#include "content/public/common/media_stream_request.h"
#include "content/public/common/page_zoom.h"
#include "content/public/common/result_codes.h"
Expand Down Expand Up @@ -874,6 +875,10 @@ void WebViewGuest::DidCommitProvisionalLoadForFrame(
dict->SetString("favicon", web_contents()->GetController().GetEntryAtIndex(i)->GetFavicon().url.spec());
history->Append(dict);
}

FaviconEvent(web_contents()->GetController().GetEntryAtIndex(web_contents()->GetController().GetCurrentEntryIndex())->GetFavicon().url.spec());
TitleWasSet(web_contents()->GetController().GetEntryAtIndex(web_contents()->GetController().GetCurrentEntryIndex()), true);

args->Set("pagesHistory", history);
args->SetInteger(webview::kInternalProcessId,
web_contents()->GetRenderProcessHost()->GetID());
Expand Down Expand Up @@ -1483,6 +1488,28 @@ void WebViewGuest::VisibleSSLStateChanged(const content::WebContents* source) {
webview::kEventSSLChange, std::move(args)));
}

void WebViewGuest::FaviconEvent(const std::string& faviconUrl)
{
std::unique_ptr<base::DictionaryValue> args(new base::DictionaryValue());
args->SetString(webview::kFaviconUrl, faviconUrl);
DispatchEventToView(base::MakeUnique<GuestViewEvent>(webview::kEventFaviconChange,
std::move(args)));
}

void WebViewGuest::DidUpdateFaviconURL(const std::vector<content::FaviconURL>& candidates) {
if (!candidates.empty())
FaviconEvent(candidates[0].icon_url.spec());
else
FaviconEvent("");
}

void WebViewGuest::TitleWasSet(content::NavigationEntry* entry, bool explicit_set) {
std::unique_ptr<base::DictionaryValue> args(new base::DictionaryValue());
args->SetString(webview::kTitle, entry->GetTitleForDisplay());
DispatchEventToView(base::MakeUnique<GuestViewEvent>(webview::kEventTitleChange,
std::move(args)));
}

void WebViewGuest::LoadURLWithParams(
const GURL& url,
const content::Referrer& referrer,
Expand Down Expand Up @@ -1510,7 +1537,7 @@ void WebViewGuest::LoadURLWithParams(
scheme_is_blocked = false;
}
}

// Do not allow navigating a guest to schemes other than known safe schemes.
// This will block the embedder trying to load unwanted schemes, e.g.
// chrome://.
Expand Down
9 changes: 9 additions & 0 deletions extensions/browser/guest_view/web_view/web_view_guest.h
Expand Up @@ -30,6 +30,11 @@ namespace content {
struct GlobalRequestID;
} // namespace content

namespace content {
class NavigationEntry;
struct FaviconURL;
}

namespace extensions {

class WebViewInternalFindFunction;
Expand Down Expand Up @@ -260,6 +265,10 @@ class WebViewGuest : public guest_view::GuestView<WebViewGuest>,
void OnCertificateError(base::ListValue* certificate) final;
void OnSubFrameCertificateError(base::ListValue* certificate) final;

void FaviconEvent(const std::string& faviconUrl);
void DidUpdateFaviconURL(const std::vector<content::FaviconURL>& candidates) final;
void TitleWasSet(content::NavigationEntry* entry, bool explicit_set) final;

// WebContentsObserver implementation.
void DidCommitProvisionalLoadForFrame(
content::RenderFrameHost* render_frame_host,
Expand Down
Expand Up @@ -66,6 +66,10 @@ WebViewEvents.EVENTS = {
handler: 'handleFullscreenExitEvent',
internal: true
},
'faviconchange': {
evt: CreateEvent('webViewInternal.onFaviconChange'),
fields: ['faviconUrl']
},
'findupdate': {
evt: CreateEvent('webViewInternal.onFindReply'),
fields: [
Expand Down Expand Up @@ -151,6 +155,10 @@ WebViewEvents.EVENTS = {
evt: CreateEvent('webViewInternal.onSubFrameCertificateError'),
fields: ['certificate']
},
'titlechange': {
evt: CreateEvent('webViewInternal.onTitleChange'),
fields: ['title']
},
'unresponsive': {
evt: CreateEvent('webViewInternal.onUnresponsive'),
fields: ['processId']
Expand Down

0 comments on commit 9bd3e48

Please sign in to comment.