Skip to content

Commit af685ba

Browse files
committed
On title and favicon change event on webview:
-titlechange -faviconchange
1 parent 7638a43 commit af685ba

File tree

7 files changed

+74
-15
lines changed

7 files changed

+74
-15
lines changed

extensions/browser/extension_event_histogram_value.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ enum HistogramValue {
424424
CLIPBOARD_ON_CLIPBOARD_DATA_CHANGED,
425425
VIRTUAL_KEYBOARD_PRIVATE_ON_KEYBOARD_CLOSED,
426426
FILE_MANAGER_PRIVATE_ON_APPS_UPDATED,
427+
WEB_VIEW_INTERNAL_ON_TITLE_CHANGE,
428+
WEB_VIEW_INTERNAL_ON_FAVICON_CHANGE,
427429
// Last entry: Add new entries above, then run:
428430
// python tools/metrics/histograms/update_extension_histograms.py
429431
ENUM_BOUNDARY

extensions/browser/guest_view/guest_view_events.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class EventMap {
8181
{webview::kEventErrorOccurred,
8282
events::WEB_VIEW_INTERNAL_ON_ERROR_OCCURRED},
8383
{webview::kEventSendHeaders, events::WEB_VIEW_INTERNAL_ON_SEND_HEADERS},
84+
{webview::kEventFaviconChange, events::WEB_VIEW_INTERNAL_ON_FAVICON_CHANGE},
85+
{webview::kEventTitleChange, events::WEB_VIEW_INTERNAL_ON_TITLE_CHANGE},
8486
};
8587
for (const auto& name_and_value : names_and_values) {
8688
values_[name_and_value.name] = name_and_value.value;

extensions/browser/guest_view/web_view/web_view_constants.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const char kEventDialog[] = "webViewInternal.onDialog";
3030
const char kEventDropLink[] = "webViewInternal.onDropLink";
3131
const char kEventExit[] = "webViewInternal.onExit";
3232
const char kEventExitFullscreen[] = "webViewInternal.onExitFullscreen";
33+
const char kEventFaviconChange[] = "webViewInternal.onFaviconChange";
3334
const char kEventFindReply[] = "webViewInternal.onFindReply";
3435
const char kEventFrameNameChanged[] = "webViewInternal.onFrameNameChanged";
3536
const char kEventHeadersReceived[] = "webViewInternal.onHeadersReceived";
@@ -45,6 +46,7 @@ const char kEventPermissionRequest[] = "webViewInternal.onPermissionRequest";
4546
const char kEventResponseStarted[] = "webViewInternal.onResponseStarted";
4647
const char kEventResponsive[] = "webViewInternal.onResponsive";
4748
const char kEventSizeChanged[] = "webViewInternal.onSizeChanged";
49+
const char kEventTitleChange[] = "webViewInternal.onTitleChange";
4850
const char kEventUnresponsive[] = "webViewInternal.onUnresponsive";
4951
const char kEventZoomChange[] = "webViewInternal.onZoomChange";
5052

@@ -63,6 +65,7 @@ const char kWebViewEventPrefix[] = "webViewInternal.";
6365
// Parameters/properties on events.
6466
const char kContextMenuItems[] = "items";
6567
const char kDefaultPromptText[] = "defaultPromptText";
68+
const char kFaviconUrl[] = "faviconUrl";
6669
const char kFindSearchText[] = "searchText";
6770
const char kFindFinalUpdate[] = "finalUpdate";
6871
const char kInitialHeight[] = "initialHeight";
@@ -98,6 +101,7 @@ const char kRequestId[] = "requestId";
98101
const char kRequestInfo[] = "requestInfo";
99102
const char kSourceId[] = "sourceId";
100103
const char kTargetURL[] = "targetUrl";
104+
const char kTitle[] = "title";
101105
const char kWindowID[] = "windowId";
102106
const char kWindowOpenDisposition[] = "windowOpenDisposition";
103107
const char kOldZoomFactor[] = "oldZoomFactor";

extensions/browser/guest_view/web_view/web_view_constants.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern const char kEventDialog[];
3636
extern const char kEventDropLink[];
3737
extern const char kEventExit[];
3838
extern const char kEventExitFullscreen[];
39+
extern const char kEventFaviconChange[];
3940
extern const char kEventFindReply[];
4041
extern const char kEventFrameNameChanged[];
4142
extern const char kEventHeadersReceived[];
@@ -51,6 +52,7 @@ extern const char kEventPermissionRequest[];
5152
extern const char kEventResponseStarted[];
5253
extern const char kEventResponsive[];
5354
extern const char kEventSizeChanged[];
55+
extern const char kEventTitleChange[];
5456
extern const char kEventUnresponsive[];
5557
extern const char kEventZoomChange[];
5658

@@ -69,6 +71,7 @@ extern const char kWebViewEventPrefix[];
6971
// Parameters/properties on events.
7072
extern const char kContextMenuItems[];
7173
extern const char kDefaultPromptText[];
74+
extern const char kFaviconUrl[];
7275
extern const char kFindSearchText[];
7376
extern const char kFindFinalUpdate[];
7477
extern const char kInitialHeight[];
@@ -104,6 +107,7 @@ extern const char kRequestId[];
104107
extern const char kRequestInfo[];
105108
extern const char kSourceId[];
106109
extern const char kTargetURL[];
110+
extern const char kTitle[];
107111
extern const char kWindowID[];
108112
extern const char kWindowOpenDisposition[];
109113
extern const char kOldZoomFactor[];

extensions/browser/guest_view/web_view/web_view_guest.cc

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "content/public/browser/browser_context.h"
2727
#include "content/public/browser/browser_thread.h"
2828
#include "content/public/browser/child_process_security_policy.h"
29+
#include "content/public/browser/favicon_status.h"
2930
#include "content/public/browser/native_web_keyboard_event.h"
3031
#include "content/public/browser/navigation_entry.h"
3132
#include "content/public/browser/navigation_handle.h"
@@ -44,6 +45,7 @@
4445
#include "content/public/browser/web_contents.h"
4546
#include "content/public/browser/web_contents_delegate.h"
4647
#include "content/public/common/browser_side_navigation_policy.h"
48+
#include "content/public/common/favicon_url.h"
4749
#include "content/public/common/media_stream_request.h"
4850
#include "content/public/common/page_zoom.h"
4951
#include "content/public/common/result_codes.h"
@@ -355,10 +357,10 @@ void WebViewGuest::CreateWebContents(
355357
persist_storage ? "persist" : "",
356358
url_encoded_partition.c_str()));
357359

358-
// If we already have a webview tag in the same app using the same storage
359-
// partition, we should use the same SiteInstance so the existing tag and
360-
// the new tag can script each other.
361-
auto* guest_view_manager = GuestViewManager::FromBrowserContext(
360+
// If we already have a webview tag in the same app using the same storage
361+
// partition, we should use the same SiteInstance so the existing tag and
362+
// the new tag can script each other.
363+
auto* guest_view_manager = GuestViewManager::FromBrowserContext(
362364
owner_render_process_host->GetBrowserContext());
363365
scoped_refptr<content::SiteInstance> guest_site_instance =
364366
guest_view_manager->GetGuestSiteInstance(guest_site);
@@ -534,10 +536,10 @@ void WebViewGuest::WillDestroy() {
534536
}
535537

536538
bool WebViewGuest::DidAddMessageToConsole(WebContents* source,
537-
int32_t level,
538-
const base::string16& message,
539-
int32_t line_no,
540-
const base::string16& source_id) {
539+
int32_t level,
540+
const base::string16& message,
541+
int32_t line_no,
542+
const base::string16& source_id) {
541543
std::unique_ptr<base::DictionaryValue> args(new base::DictionaryValue());
542544
// Log levels are from base/logging.h: LogSeverity.
543545
args->SetInteger(webview::kLevel, level);
@@ -845,6 +847,12 @@ void WebViewGuest::DidFinishNavigation(
845847
SetZoom(pending_zoom_factor_);
846848
pending_zoom_factor_ = 0.0;
847849
}
850+
851+
if ((int)navigation_handle->GetPageTransition() == (int)ui::PageTransition::PAGE_TRANSITION_AUTO_TOPLEVEL ||
852+
(int)navigation_handle->GetPageTransition() & (int)ui::PageTransition::PAGE_TRANSITION_FORWARD_BACK) {
853+
FaviconEvent(web_contents()->GetController().GetEntryAtIndex(web_contents()->GetController().GetCurrentEntryIndex())->GetFavicon().url.spec());
854+
TitleWasSet(web_contents()->GetController().GetEntryAtIndex(web_contents()->GetController().GetCurrentEntryIndex()), true);
855+
}
848856
}
849857
std::unique_ptr<base::DictionaryValue> args(new base::DictionaryValue());
850858
args->SetString(guest_view::kUrl, src_.spec());
@@ -990,8 +998,8 @@ void WebViewGuest::RemoveWebViewStateFromIOThread(
990998
content::BrowserThread::PostTask(
991999
content::BrowserThread::IO, FROM_HERE,
9921000
base::Bind(&WebViewRendererState::RemoveGuest,
993-
base::Unretained(WebViewRendererState::GetInstance()),
994-
web_contents->GetRenderProcessHost()->GetID(),
1001+
base::Unretained(WebViewRendererState::GetInstance()),
1002+
web_contents->GetRenderProcessHost()->GetID(),
9951003
web_contents->GetRenderViewHost()->GetRoutingID()));
9961004
}
9971005

@@ -1183,7 +1191,7 @@ void WebViewGuest::ApplyAttributes(const base::DictionaryValue& params) {
11831191
if (!is_pending_new_window) {
11841192
std::string src;
11851193
if (params.GetString(webview::kAttributeSrc, &src))
1186-
NavigateGuest(src, true /* force_navigation */);
1194+
NavigateGuest(src, true /* force_navigation */);
11871195
}
11881196
}
11891197

@@ -1415,6 +1423,28 @@ void WebViewGuest::RequestToLockMouse(WebContents* web_contents,
14151423
base::Unretained(web_contents)));
14161424
}
14171425

1426+
void WebViewGuest::FaviconEvent(const std::string& faviconUrl)
1427+
{
1428+
std::unique_ptr<base::DictionaryValue> args(new base::DictionaryValue());
1429+
args->SetString(webview::kFaviconUrl, faviconUrl);
1430+
DispatchEventToView(base::MakeUnique<GuestViewEvent>(webview::kEventFaviconChange,
1431+
std::move(args)));
1432+
}
1433+
1434+
void WebViewGuest::DidUpdateFaviconURL(const std::vector<content::FaviconURL>& candidates) {
1435+
if (!candidates.empty())
1436+
FaviconEvent(candidates[0].icon_url.spec());
1437+
else
1438+
FaviconEvent("");
1439+
}
1440+
1441+
void WebViewGuest::TitleWasSet(content::NavigationEntry* entry, bool explicit_set) {
1442+
std::unique_ptr<base::DictionaryValue> args(new base::DictionaryValue());
1443+
args->SetString(webview::kTitle, entry->GetTitleForDisplay());
1444+
DispatchEventToView(base::MakeUnique<GuestViewEvent>(webview::kEventTitleChange,
1445+
std::move(args)));
1446+
}
1447+
14181448
void WebViewGuest::LoadURLWithParams(
14191449
const GURL& url,
14201450
const content::Referrer& referrer,

extensions/browser/guest_view/web_view/web_view_guest.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ namespace blink {
2626
struct WebFindOptions;
2727
} // namespace blink
2828

29+
namespace content {
30+
class NavigationEntry;
31+
struct FaviconURL;
32+
}
33+
2934
namespace extensions {
3035

3136
class WebViewInternalFindFunction;
@@ -203,10 +208,10 @@ class WebViewGuest : public guest_view::GuestView<WebViewGuest>,
203208

204209
// WebContentsDelegate implementation.
205210
bool DidAddMessageToConsole(content::WebContents* source,
206-
int32_t level,
207-
const base::string16& message,
208-
int32_t line_no,
209-
const base::string16& source_id) final;
211+
int32_t level,
212+
const base::string16& message,
213+
int32_t line_no,
214+
const base::string16& source_id) final;
210215
void CloseContents(content::WebContents* source) final;
211216
bool HandleContextMenu(const content::ContextMenuParams& params) final;
212217
void HandleKeyboardEvent(content::WebContents* source,
@@ -259,6 +264,10 @@ class WebViewGuest : public guest_view::GuestView<WebViewGuest>,
259264
bool user_gesture,
260265
bool last_unlocked_by_target) override;
261266

267+
void FaviconEvent(const std::string& faviconUrl);
268+
void DidUpdateFaviconURL(const std::vector<content::FaviconURL>& candidates) final;
269+
void TitleWasSet(content::NavigationEntry* entry, bool explicit_set) final;
270+
262271
// WebContentsObserver implementation.
263272
void DidStartNavigation(content::NavigationHandle* navigation_handle) final;
264273
void DidFinishNavigation(content::NavigationHandle* navigation_handle) final;

extensions/renderer/resources/guest_view/web_view/web_view_events.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ WebViewEvents.EVENTS = {
6262
handler: 'handleFullscreenExitEvent',
6363
internal: true
6464
},
65+
'faviconchange': {
66+
evt: CreateEvent('webViewInternal.onFaviconChange'),
67+
fields: ['faviconUrl']
68+
},
6569
'findupdate': {
6670
evt: CreateEvent('webViewInternal.onFindReply'),
6771
fields: [
@@ -139,6 +143,10 @@ WebViewEvents.EVENTS = {
139143
fields: ['oldHeight', 'oldWidth', 'newHeight', 'newWidth'],
140144
handler: 'handleSizeChangedEvent'
141145
},
146+
'titlechange': {
147+
evt: CreateEvent('webViewInternal.onTitleChange'),
148+
fields: ['title']
149+
},
142150
'unresponsive': {
143151
evt: CreateEvent('webViewInternal.onUnresponsive'),
144152
fields: ['processId']

0 commit comments

Comments
 (0)