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::WebNode
& node
) {
58 // This implementation is incomplete (for example if is an area tag) but
59 // matches the original WebViewClassic implementation.
61 blink::WebElementCollection collection
= node
.getElementsByHTMLTagName("img");
62 DCHECK(!collection
.isNull());
63 return collection
.firstItem();
66 GURL
GetChildImageUrlFromElement(const blink::WebElement
& element
) {
67 const blink::WebElement child_img
= GetImgChild(element
);
68 if (child_img
.isNull())
70 return GetAbsoluteSrcUrl(child_img
);
73 bool RemovePrefixAndAssignIfMatches(const base::StringPiece
& prefix
,
76 const base::StringPiece
spec(url
.possibly_invalid_spec());
78 if (spec
.starts_with(prefix
)) {
79 url::RawCanonOutputW
<1024> output
;
80 url::DecodeURLEscapeSequences(spec
.data() + prefix
.length(),
81 spec
.length() - prefix
.length(),
83 *dest
= base::UTF16ToUTF8(
84 base::StringPiece16(output
.data(), output
.length()));
90 void DistinguishAndAssignSrcLinkType(const GURL
& url
, AwHitTestData
* data
) {
91 if (RemovePrefixAndAssignIfMatches(
92 content::kAddressPrefix
,
94 &data
->extra_data_for_type
)) {
95 data
->type
= AwHitTestData::GEO_TYPE
;
96 } else if (RemovePrefixAndAssignIfMatches(
97 content::kPhoneNumberPrefix
,
99 &data
->extra_data_for_type
)) {
100 data
->type
= AwHitTestData::PHONE_TYPE
;
101 } else if (RemovePrefixAndAssignIfMatches(
102 content::kEmailPrefix
,
104 &data
->extra_data_for_type
)) {
105 data
->type
= AwHitTestData::EMAIL_TYPE
;
107 data
->type
= AwHitTestData::SRC_LINK_TYPE
;
108 data
->extra_data_for_type
= url
.possibly_invalid_spec();
109 if (!data
->extra_data_for_type
.empty())
110 data
->href
= base::UTF8ToUTF16(data
->extra_data_for_type
);
114 void PopulateHitTestData(const GURL
& absolute_link_url
,
115 const GURL
& absolute_image_url
,
117 AwHitTestData
* data
) {
118 // Note: Using GURL::is_empty instead of GURL:is_valid due to the
119 // WebViewClassic allowing any kind of protocol which GURL::is_valid
120 // disallows. Similar reasons for using GURL::possibly_invalid_spec instead of
122 if (!absolute_image_url
.is_empty())
123 data
->img_src
= absolute_image_url
;
125 const bool is_javascript_scheme
=
126 absolute_link_url
.SchemeIs(url::kJavaScriptScheme
);
127 const bool has_link_url
= !absolute_link_url
.is_empty();
128 const bool has_image_url
= !absolute_image_url
.is_empty();
130 if (has_link_url
&& !has_image_url
&& !is_javascript_scheme
) {
131 DistinguishAndAssignSrcLinkType(absolute_link_url
, data
);
132 } else if (has_link_url
&& has_image_url
&& !is_javascript_scheme
) {
133 data
->type
= AwHitTestData::SRC_IMAGE_LINK_TYPE
;
134 data
->extra_data_for_type
= data
->img_src
.possibly_invalid_spec();
135 if (absolute_link_url
.is_valid())
136 data
->href
= base::UTF8ToUTF16(absolute_link_url
.possibly_invalid_spec());
137 } else if (!has_link_url
&& has_image_url
) {
138 data
->type
= AwHitTestData::IMAGE_TYPE
;
139 data
->extra_data_for_type
= data
->img_src
.possibly_invalid_spec();
140 } else if (is_editable
) {
141 data
->type
= AwHitTestData::EDIT_TEXT_TYPE
;
142 DCHECK_EQ(0u, data
->extra_data_for_type
.length());
148 AwRenderViewExt::AwRenderViewExt(content::RenderView
* render_view
)
149 : content::RenderViewObserver(render_view
), page_scale_factor_(0.0f
) {
152 AwRenderViewExt::~AwRenderViewExt() {
156 void AwRenderViewExt::RenderViewCreated(content::RenderView
* render_view
) {
157 new AwRenderViewExt(render_view
); // |render_view| takes ownership.
160 bool AwRenderViewExt::OnMessageReceived(const IPC::Message
& message
) {
162 IPC_BEGIN_MESSAGE_MAP(AwRenderViewExt
, message
)
163 IPC_MESSAGE_HANDLER(AwViewMsg_DocumentHasImages
, OnDocumentHasImagesRequest
)
164 IPC_MESSAGE_HANDLER(AwViewMsg_DoHitTest
, OnDoHitTest
)
165 IPC_MESSAGE_HANDLER(AwViewMsg_SetTextZoomFactor
, OnSetTextZoomFactor
)
166 IPC_MESSAGE_HANDLER(AwViewMsg_ResetScrollAndScaleState
,
167 OnResetScrollAndScaleState
)
168 IPC_MESSAGE_HANDLER(AwViewMsg_SetInitialPageScale
, OnSetInitialPageScale
)
169 IPC_MESSAGE_HANDLER(AwViewMsg_SetBackgroundColor
, OnSetBackgroundColor
)
170 IPC_MESSAGE_HANDLER(AwViewMsg_SmoothScroll
, OnSmoothScroll
)
171 IPC_MESSAGE_UNHANDLED(handled
= false)
172 IPC_END_MESSAGE_MAP()
176 void AwRenderViewExt::OnDocumentHasImagesRequest(int id
) {
177 bool hasImages
= false;
179 blink::WebView
* webview
= render_view()->GetWebView();
181 blink::WebDocument document
= webview
->mainFrame()->document();
182 const blink::WebElement child_img
= GetImgChild(document
);
183 hasImages
= !child_img
.isNull();
186 Send(new AwViewHostMsg_DocumentHasImagesResponse(routing_id(), id
,
190 void AwRenderViewExt::DidCommitCompositorFrame() {
191 UpdatePageScaleFactor();
194 void AwRenderViewExt::DidUpdateLayout() {
195 if (check_contents_size_timer_
.IsRunning())
198 check_contents_size_timer_
.Start(FROM_HERE
,
199 base::TimeDelta::FromMilliseconds(0), this,
200 &AwRenderViewExt::CheckContentsSize
);
203 void AwRenderViewExt::UpdatePageScaleFactor() {
204 if (page_scale_factor_
!= render_view()->GetWebView()->pageScaleFactor()) {
205 page_scale_factor_
= render_view()->GetWebView()->pageScaleFactor();
206 Send(new AwViewHostMsg_PageScaleFactorChanged(routing_id(),
207 page_scale_factor_
));
211 void AwRenderViewExt::CheckContentsSize() {
212 if (!render_view()->GetWebView())
215 gfx::Size contents_size
;
217 blink::WebFrame
* main_frame
= render_view()->GetWebView()->mainFrame();
219 contents_size
= main_frame
->contentsSize();
221 // Fall back to contentsPreferredMinimumSize if the mainFrame is reporting a
222 // 0x0 size (this happens during initial load).
223 if (contents_size
.IsEmpty()) {
224 contents_size
= render_view()->GetWebView()->contentsPreferredMinimumSize();
227 if (contents_size
== last_sent_contents_size_
)
230 last_sent_contents_size_
= contents_size
;
231 Send(new AwViewHostMsg_OnContentsSizeChanged(routing_id(), contents_size
));
234 void AwRenderViewExt::Navigate(const GURL
& url
) {
235 // Navigate is called only on NEW navigations, so WebImageCache won't be freed
236 // when the user just clicks on links, but only when a navigation is started,
237 // for instance via loadUrl. A better approach would be clearing the cache on
238 // cross-site boundaries, however this would require too many changes both on
239 // the browser side (in RenderViewHostManger), to the IPCmessages and to the
240 // RenderViewObserver. Thus, clearing decoding image cache on Navigate, seems
241 // a more acceptable compromise.
242 blink::WebImageCache::clear();
245 void AwRenderViewExt::FocusedNodeChanged(const blink::WebNode
& node
) {
246 if (node
.isNull() || !node
.isElementNode() || !render_view())
249 // Note: element is not const due to textContent() is not const.
250 blink::WebElement element
= node
.toConst
<blink::WebElement
>();
253 data
.href
= GetHref(element
);
254 data
.anchor_text
= element
.textContent();
256 GURL absolute_link_url
;
258 absolute_link_url
= GetAbsoluteUrl(node
, data
.href
);
260 GURL absolute_image_url
= GetChildImageUrlFromElement(element
);
262 PopulateHitTestData(absolute_link_url
,
264 render_view()->IsEditableNode(node
),
266 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data
));
269 void AwRenderViewExt::OnDoHitTest(const gfx::PointF
& touch_center
,
270 const gfx::SizeF
& touch_area
) {
271 if (!render_view() || !render_view()->GetWebView())
274 const blink::WebHitTestResult result
=
275 render_view()->GetWebView()->hitTestResultForTap(
276 blink::WebPoint(touch_center
.x(), touch_center
.y()),
277 blink::WebSize(touch_area
.width(), touch_area
.height()));
280 GURL absolute_image_url
= result
.absoluteImageURL();
281 if (!result
.urlElement().isNull()) {
282 data
.anchor_text
= result
.urlElement().textContent();
283 data
.href
= GetHref(result
.urlElement());
284 // If we hit an image that failed to load, Blink won't give us its URL.
285 // Fall back to walking the DOM in this case.
286 if (absolute_image_url
.is_empty())
287 absolute_image_url
= GetChildImageUrlFromElement(result
.urlElement());
290 PopulateHitTestData(result
.absoluteLinkURL(),
292 result
.isContentEditable(),
294 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data
));
297 void AwRenderViewExt::OnSetTextZoomFactor(float zoom_factor
) {
298 if (!render_view() || !render_view()->GetWebView())
300 // Hide selection and autofill popups.
301 render_view()->GetWebView()->hidePopups();
302 render_view()->GetWebView()->setTextZoomFactor(zoom_factor
);
305 void AwRenderViewExt::OnResetScrollAndScaleState() {
306 if (!render_view() || !render_view()->GetWebView())
308 render_view()->GetWebView()->resetScrollAndScaleState();
311 void AwRenderViewExt::OnSetInitialPageScale(double page_scale_factor
) {
312 if (!render_view() || !render_view()->GetWebView())
314 render_view()->GetWebView()->setInitialPageScaleOverride(
318 void AwRenderViewExt::OnSetBackgroundColor(SkColor c
) {
319 if (!render_view() || !render_view()->GetWebView())
321 render_view()->GetWebView()->setBaseBackgroundColor(c
);
324 void AwRenderViewExt::OnSmoothScroll(int target_x
,
327 if (!render_view() || !render_view()->GetWebView())
330 render_view()->GetWebView()->smoothScroll(target_x
, target_y
, duration_ms
);
333 } // namespace android_webview