1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "android_webview/renderer/aw_render_view_ext.h"
9 #include "android_webview/common/aw_hit_test_data.h"
10 #include "android_webview/common/render_view_messages.h"
11 #include "base/bind.h"
12 #include "base/strings/string_piece.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h"
15 #include "content/public/renderer/android_content_detection_prefixes.h"
16 #include "content/public/renderer/document_state.h"
17 #include "content/public/renderer/render_view.h"
18 #include "skia/ext/refptr.h"
19 #include "third_party/WebKit/public/platform/WebSize.h"
20 #include "third_party/WebKit/public/platform/WebURL.h"
21 #include "third_party/WebKit/public/platform/WebVector.h"
22 #include "third_party/WebKit/public/web/WebDataSource.h"
23 #include "third_party/WebKit/public/web/WebDocument.h"
24 #include "third_party/WebKit/public/web/WebElement.h"
25 #include "third_party/WebKit/public/web/WebElementCollection.h"
26 #include "third_party/WebKit/public/web/WebHitTestResult.h"
27 #include "third_party/WebKit/public/web/WebImageCache.h"
28 #include "third_party/WebKit/public/web/WebLocalFrame.h"
29 #include "third_party/WebKit/public/web/WebNode.h"
30 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
31 #include "third_party/WebKit/public/web/WebView.h"
32 #include "url/url_canon.h"
33 #include "url/url_constants.h"
34 #include "url/url_util.h"
36 namespace android_webview
{
40 GURL
GetAbsoluteUrl(const blink::WebNode
& node
,
41 const base::string16
& url_fragment
) {
42 return GURL(node
.document().completeURL(url_fragment
));
45 base::string16
GetHref(const blink::WebElement
& element
) {
46 // Get the actual 'href' attribute, which might relative if valid or can
47 // possibly contain garbage otherwise, so not using absoluteLinkURL here.
48 return element
.getAttribute("href");
51 GURL
GetAbsoluteSrcUrl(const blink::WebElement
& element
) {
54 return GetAbsoluteUrl(element
, element
.getAttribute("src"));
57 blink::WebElement
GetImgChild(const blink::WebElement
& element
) {
58 // This implementation is incomplete (for example if is an area tag) but
59 // matches the original WebViewClassic implementation.
61 blink::WebElementCollection collection
=
62 element
.getElementsByHTMLTagName("img");
63 DCHECK(!collection
.isNull());
64 return collection
.firstItem();
67 GURL
GetChildImageUrlFromElement(const blink::WebElement
& element
) {
68 const blink::WebElement child_img
= GetImgChild(element
);
69 if (child_img
.isNull())
71 return GetAbsoluteSrcUrl(child_img
);
74 bool RemovePrefixAndAssignIfMatches(const base::StringPiece
& prefix
,
77 const base::StringPiece
spec(url
.possibly_invalid_spec());
79 if (spec
.starts_with(prefix
)) {
80 url::RawCanonOutputW
<1024> output
;
81 url::DecodeURLEscapeSequences(spec
.data() + prefix
.length(),
82 spec
.length() - prefix
.length(),
84 std::string decoded_url
= base::UTF16ToUTF8(
85 base::string16(output
.data(), output
.length()));
86 dest
->assign(decoded_url
.begin(), decoded_url
.end());
92 void DistinguishAndAssignSrcLinkType(const GURL
& url
, AwHitTestData
* data
) {
93 if (RemovePrefixAndAssignIfMatches(
94 content::kAddressPrefix
,
96 &data
->extra_data_for_type
)) {
97 data
->type
= AwHitTestData::GEO_TYPE
;
98 } else if (RemovePrefixAndAssignIfMatches(
99 content::kPhoneNumberPrefix
,
101 &data
->extra_data_for_type
)) {
102 data
->type
= AwHitTestData::PHONE_TYPE
;
103 } else if (RemovePrefixAndAssignIfMatches(
104 content::kEmailPrefix
,
106 &data
->extra_data_for_type
)) {
107 data
->type
= AwHitTestData::EMAIL_TYPE
;
109 data
->type
= AwHitTestData::SRC_LINK_TYPE
;
110 data
->extra_data_for_type
= url
.possibly_invalid_spec();
111 if (!data
->extra_data_for_type
.empty())
112 data
->href
= base::UTF8ToUTF16(data
->extra_data_for_type
);
116 void PopulateHitTestData(const GURL
& absolute_link_url
,
117 const GURL
& absolute_image_url
,
119 AwHitTestData
* data
) {
120 // Note: Using GURL::is_empty instead of GURL:is_valid due to the
121 // WebViewClassic allowing any kind of protocol which GURL::is_valid
122 // disallows. Similar reasons for using GURL::possibly_invalid_spec instead of
124 if (!absolute_image_url
.is_empty())
125 data
->img_src
= absolute_image_url
;
127 const bool is_javascript_scheme
=
128 absolute_link_url
.SchemeIs(url::kJavaScriptScheme
);
129 const bool has_link_url
= !absolute_link_url
.is_empty();
130 const bool has_image_url
= !absolute_image_url
.is_empty();
132 if (has_link_url
&& !has_image_url
&& !is_javascript_scheme
) {
133 DistinguishAndAssignSrcLinkType(absolute_link_url
, data
);
134 } else if (has_link_url
&& has_image_url
&& !is_javascript_scheme
) {
135 data
->type
= AwHitTestData::SRC_IMAGE_LINK_TYPE
;
136 data
->extra_data_for_type
= data
->img_src
.possibly_invalid_spec();
137 if (absolute_link_url
.is_valid())
138 data
->href
= base::UTF8ToUTF16(absolute_link_url
.possibly_invalid_spec());
139 } else if (!has_link_url
&& has_image_url
) {
140 data
->type
= AwHitTestData::IMAGE_TYPE
;
141 data
->extra_data_for_type
= data
->img_src
.possibly_invalid_spec();
142 } else if (is_editable
) {
143 data
->type
= AwHitTestData::EDIT_TEXT_TYPE
;
144 DCHECK_EQ(0u, data
->extra_data_for_type
.length());
150 AwRenderViewExt::AwRenderViewExt(content::RenderView
* render_view
)
151 : content::RenderViewObserver(render_view
), page_scale_factor_(0.0f
) {
154 AwRenderViewExt::~AwRenderViewExt() {
158 void AwRenderViewExt::RenderViewCreated(content::RenderView
* render_view
) {
159 new AwRenderViewExt(render_view
); // |render_view| takes ownership.
162 bool AwRenderViewExt::OnMessageReceived(const IPC::Message
& message
) {
164 IPC_BEGIN_MESSAGE_MAP(AwRenderViewExt
, message
)
165 IPC_MESSAGE_HANDLER(AwViewMsg_DocumentHasImages
, OnDocumentHasImagesRequest
)
166 IPC_MESSAGE_HANDLER(AwViewMsg_DoHitTest
, OnDoHitTest
)
167 IPC_MESSAGE_HANDLER(AwViewMsg_SetTextZoomFactor
, OnSetTextZoomFactor
)
168 IPC_MESSAGE_HANDLER(AwViewMsg_ResetScrollAndScaleState
,
169 OnResetScrollAndScaleState
)
170 IPC_MESSAGE_HANDLER(AwViewMsg_SetInitialPageScale
, OnSetInitialPageScale
)
171 IPC_MESSAGE_HANDLER(AwViewMsg_SetBackgroundColor
, OnSetBackgroundColor
)
172 IPC_MESSAGE_UNHANDLED(handled
= false)
173 IPC_END_MESSAGE_MAP()
177 void AwRenderViewExt::OnDocumentHasImagesRequest(int id
) {
178 bool hasImages
= false;
180 blink::WebView
* webview
= render_view()->GetWebView();
182 blink::WebVector
<blink::WebElement
> images
;
183 webview
->mainFrame()->document().images(images
);
184 hasImages
= !images
.isEmpty();
187 Send(new AwViewHostMsg_DocumentHasImagesResponse(routing_id(), id
,
191 void AwRenderViewExt::DidCommitCompositorFrame() {
192 UpdatePageScaleFactor();
195 void AwRenderViewExt::DidUpdateLayout() {
196 if (check_contents_size_timer_
.IsRunning())
199 check_contents_size_timer_
.Start(FROM_HERE
,
200 base::TimeDelta::FromMilliseconds(0), this,
201 &AwRenderViewExt::CheckContentsSize
);
204 void AwRenderViewExt::UpdatePageScaleFactor() {
205 if (page_scale_factor_
!= render_view()->GetWebView()->pageScaleFactor()) {
206 page_scale_factor_
= render_view()->GetWebView()->pageScaleFactor();
207 Send(new AwViewHostMsg_PageScaleFactorChanged(routing_id(),
208 page_scale_factor_
));
212 void AwRenderViewExt::CheckContentsSize() {
213 if (!render_view()->GetWebView())
216 gfx::Size contents_size
;
218 blink::WebFrame
* main_frame
= render_view()->GetWebView()->mainFrame();
220 contents_size
= main_frame
->contentsSize();
222 // Fall back to contentsPreferredMinimumSize if the mainFrame is reporting a
223 // 0x0 size (this happens during initial load).
224 if (contents_size
.IsEmpty()) {
225 contents_size
= render_view()->GetWebView()->contentsPreferredMinimumSize();
228 if (contents_size
== last_sent_contents_size_
)
231 last_sent_contents_size_
= contents_size
;
232 Send(new AwViewHostMsg_OnContentsSizeChanged(routing_id(), contents_size
));
235 void AwRenderViewExt::Navigate(const GURL
& url
) {
236 // Navigate is called only on NEW navigations, so WebImageCache won't be freed
237 // when the user just clicks on links, but only when a navigation is started,
238 // for instance via loadUrl. A better approach would be clearing the cache on
239 // cross-site boundaries, however this would require too many changes both on
240 // the browser side (in RenderViewHostManger), to the IPCmessages and to the
241 // RenderViewObserver. Thus, clearing decoding image cache on Navigate, seems
242 // a more acceptable compromise.
243 blink::WebImageCache::clear();
246 void AwRenderViewExt::FocusedNodeChanged(const blink::WebNode
& node
) {
247 if (node
.isNull() || !node
.isElementNode() || !render_view())
250 // Note: element is not const due to textContent() is not const.
251 blink::WebElement element
= node
.toConst
<blink::WebElement
>();
254 data
.href
= GetHref(element
);
255 data
.anchor_text
= element
.textContent();
257 GURL absolute_link_url
;
259 absolute_link_url
= GetAbsoluteUrl(node
, data
.href
);
261 GURL absolute_image_url
= GetChildImageUrlFromElement(element
);
263 PopulateHitTestData(absolute_link_url
,
265 render_view()->IsEditableNode(node
),
267 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data
));
270 void AwRenderViewExt::OnDoHitTest(const gfx::PointF
& touch_center
,
271 const gfx::SizeF
& touch_area
) {
272 if (!render_view() || !render_view()->GetWebView())
275 const blink::WebHitTestResult result
=
276 render_view()->GetWebView()->hitTestResultForTap(
277 blink::WebPoint(touch_center
.x(), touch_center
.y()),
278 blink::WebSize(touch_area
.width(), touch_area
.height()));
281 GURL absolute_image_url
= result
.absoluteImageURL();
282 if (!result
.urlElement().isNull()) {
283 data
.anchor_text
= result
.urlElement().textContent();
284 data
.href
= GetHref(result
.urlElement());
285 // If we hit an image that failed to load, Blink won't give us its URL.
286 // Fall back to walking the DOM in this case.
287 if (absolute_image_url
.is_empty())
288 absolute_image_url
= GetChildImageUrlFromElement(result
.urlElement());
291 PopulateHitTestData(result
.absoluteLinkURL(),
293 result
.isContentEditable(),
295 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data
));
298 void AwRenderViewExt::OnSetTextZoomFactor(float zoom_factor
) {
299 if (!render_view() || !render_view()->GetWebView())
301 // Hide selection and autofill popups.
302 render_view()->GetWebView()->hidePopups();
303 render_view()->GetWebView()->setTextZoomFactor(zoom_factor
);
306 void AwRenderViewExt::OnResetScrollAndScaleState() {
307 if (!render_view() || !render_view()->GetWebView())
309 render_view()->GetWebView()->resetScrollAndScaleState();
312 void AwRenderViewExt::OnSetInitialPageScale(double page_scale_factor
) {
313 if (!render_view() || !render_view()->GetWebView())
315 render_view()->GetWebView()->setInitialPageScaleOverride(
319 void AwRenderViewExt::OnSetBackgroundColor(SkColor c
) {
320 if (!render_view() || !render_view()->GetWebView())
322 render_view()->GetWebView()->setBaseBackgroundColor(c
);
325 } // namespace android_webview