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 std::string decoded_url
= base::UTF16ToUTF8(
84 base::string16(output
.data(), output
.length()));
85 dest
->assign(decoded_url
.begin(), decoded_url
.end());
91 void DistinguishAndAssignSrcLinkType(const GURL
& url
, AwHitTestData
* data
) {
92 if (RemovePrefixAndAssignIfMatches(
93 content::kAddressPrefix
,
95 &data
->extra_data_for_type
)) {
96 data
->type
= AwHitTestData::GEO_TYPE
;
97 } else if (RemovePrefixAndAssignIfMatches(
98 content::kPhoneNumberPrefix
,
100 &data
->extra_data_for_type
)) {
101 data
->type
= AwHitTestData::PHONE_TYPE
;
102 } else if (RemovePrefixAndAssignIfMatches(
103 content::kEmailPrefix
,
105 &data
->extra_data_for_type
)) {
106 data
->type
= AwHitTestData::EMAIL_TYPE
;
108 data
->type
= AwHitTestData::SRC_LINK_TYPE
;
109 data
->extra_data_for_type
= url
.possibly_invalid_spec();
110 if (!data
->extra_data_for_type
.empty())
111 data
->href
= base::UTF8ToUTF16(data
->extra_data_for_type
);
115 void PopulateHitTestData(const GURL
& absolute_link_url
,
116 const GURL
& absolute_image_url
,
118 AwHitTestData
* data
) {
119 // Note: Using GURL::is_empty instead of GURL:is_valid due to the
120 // WebViewClassic allowing any kind of protocol which GURL::is_valid
121 // disallows. Similar reasons for using GURL::possibly_invalid_spec instead of
123 if (!absolute_image_url
.is_empty())
124 data
->img_src
= absolute_image_url
;
126 const bool is_javascript_scheme
=
127 absolute_link_url
.SchemeIs(url::kJavaScriptScheme
);
128 const bool has_link_url
= !absolute_link_url
.is_empty();
129 const bool has_image_url
= !absolute_image_url
.is_empty();
131 if (has_link_url
&& !has_image_url
&& !is_javascript_scheme
) {
132 DistinguishAndAssignSrcLinkType(absolute_link_url
, data
);
133 } else if (has_link_url
&& has_image_url
&& !is_javascript_scheme
) {
134 data
->type
= AwHitTestData::SRC_IMAGE_LINK_TYPE
;
135 data
->extra_data_for_type
= data
->img_src
.possibly_invalid_spec();
136 if (absolute_link_url
.is_valid())
137 data
->href
= base::UTF8ToUTF16(absolute_link_url
.possibly_invalid_spec());
138 } else if (!has_link_url
&& has_image_url
) {
139 data
->type
= AwHitTestData::IMAGE_TYPE
;
140 data
->extra_data_for_type
= data
->img_src
.possibly_invalid_spec();
141 } else if (is_editable
) {
142 data
->type
= AwHitTestData::EDIT_TEXT_TYPE
;
143 DCHECK_EQ(0u, data
->extra_data_for_type
.length());
149 AwRenderViewExt::AwRenderViewExt(content::RenderView
* render_view
)
150 : content::RenderViewObserver(render_view
), page_scale_factor_(0.0f
) {
153 AwRenderViewExt::~AwRenderViewExt() {
157 void AwRenderViewExt::RenderViewCreated(content::RenderView
* render_view
) {
158 new AwRenderViewExt(render_view
); // |render_view| takes ownership.
161 bool AwRenderViewExt::OnMessageReceived(const IPC::Message
& message
) {
163 IPC_BEGIN_MESSAGE_MAP(AwRenderViewExt
, message
)
164 IPC_MESSAGE_HANDLER(AwViewMsg_DocumentHasImages
, OnDocumentHasImagesRequest
)
165 IPC_MESSAGE_HANDLER(AwViewMsg_DoHitTest
, OnDoHitTest
)
166 IPC_MESSAGE_HANDLER(AwViewMsg_SetTextZoomFactor
, OnSetTextZoomFactor
)
167 IPC_MESSAGE_HANDLER(AwViewMsg_ResetScrollAndScaleState
,
168 OnResetScrollAndScaleState
)
169 IPC_MESSAGE_HANDLER(AwViewMsg_SetInitialPageScale
, OnSetInitialPageScale
)
170 IPC_MESSAGE_HANDLER(AwViewMsg_SetBackgroundColor
, OnSetBackgroundColor
)
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 } // namespace android_webview