blob: 7aa901d5f65465fca06a6ab306bac728cf0ffdb0 [file] [log] [blame]
[email protected]fada28b2012-09-01 01:33:031// 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.
4
5#include "android_webview/renderer/aw_render_view_ext.h"
6
[email protected]1e08a042012-11-13 23:26:367#include <string>
8
[email protected]a7198a12012-11-08 18:34:059#include "android_webview/common/aw_hit_test_data.h"
[email protected]fada28b2012-09-01 01:33:0310#include "android_webview/common/render_view_messages.h"
[email protected]11b426f22013-01-04 12:20:2411#include "base/bind.h"
[email protected]daf079a2013-04-17 21:42:4012#include "base/strings/string_piece.h"
[email protected]3f1b1272013-08-07 05:18:1013#include "base/strings/utf_string_conversions.h"
[email protected]d01b2a62013-09-18 23:21:3314#include "base/time/time.h"
[email protected]1e08a042012-11-13 23:26:3615#include "content/public/renderer/android_content_detection_prefixes.h"
[email protected]951a64832012-10-11 16:26:3716#include "content/public/renderer/document_state.h"
[email protected]fada28b2012-09-01 01:33:0317#include "content/public/renderer/render_view.h"
[email protected]6fceb912013-02-15 06:24:1518#include "skia/ext/refptr.h"
[email protected]e566d2b2013-05-30 07:56:5019#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"
[email protected]a0962a92013-06-20 18:27:3422#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"
[email protected]e0025a52014-02-11 02:13:5325#include "third_party/WebKit/public/web/WebElementCollection.h"
[email protected]a0962a92013-06-20 18:27:3426#include "third_party/WebKit/public/web/WebHitTestResult.h"
[email protected]bc55afd2013-08-14 18:52:3627#include "third_party/WebKit/public/web/WebImageCache.h"
[email protected]a41dec92014-04-12 02:47:5228#include "third_party/WebKit/public/web/WebLocalFrame.h"
[email protected]a0962a92013-06-20 18:27:3429#include "third_party/WebKit/public/web/WebNode.h"
[email protected]a0962a92013-06-20 18:27:3430#include "third_party/WebKit/public/web/WebSecurityOrigin.h"
31#include "third_party/WebKit/public/web/WebView.h"
[email protected]3f1b1272013-08-07 05:18:1032#include "url/url_canon.h"
[email protected]cca6f392014-05-28 21:32:2633#include "url/url_constants.h"
[email protected]3f1b1272013-08-07 05:18:1034#include "url/url_util.h"
[email protected]fada28b2012-09-01 01:33:0335
36namespace android_webview {
37
[email protected]1e08a042012-11-13 23:26:3638namespace {
39
[email protected]865eb542013-12-19 22:44:4940GURL GetAbsoluteUrl(const blink::WebNode& node,
41 const base::string16& url_fragment) {
[email protected]7f6de3a2013-02-07 00:16:2242 return GURL(node.document().completeURL(url_fragment));
43}
44
[email protected]865eb542013-12-19 22:44:4945base::string16 GetHref(const blink::WebElement& element) {
[email protected]7f6de3a2013-02-07 00:16:2246 // 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");
49}
50
[email protected]c32e27082013-11-07 06:58:2051GURL GetAbsoluteSrcUrl(const blink::WebElement& element) {
[email protected]7f6de3a2013-02-07 00:16:2252 if (element.isNull())
53 return GURL();
54 return GetAbsoluteUrl(element, element.getAttribute("src"));
55}
56
[email protected]e0025a52014-02-11 02:13:5357blink::WebElement GetImgChild(const blink::WebElement& element) {
[email protected]7f6de3a2013-02-07 00:16:2258 // This implementation is incomplete (for example if is an area tag) but
59 // matches the original WebViewClassic implementation.
60
[email protected]871e0952014-07-17 23:46:3661 blink::WebElementCollection collection =
62 element.getElementsByHTMLTagName("img");
[email protected]c0b58a872014-02-03 23:40:2363 DCHECK(!collection.isNull());
64 return collection.firstItem();
[email protected]7f6de3a2013-02-07 00:16:2265}
66
skyostila89c8da2015-01-08 16:51:5967GURL GetChildImageUrlFromElement(const blink::WebElement& element) {
68 const blink::WebElement child_img = GetImgChild(element);
69 if (child_img.isNull())
70 return GURL();
71 return GetAbsoluteSrcUrl(child_img);
72}
73
[email protected]1e08a042012-11-13 23:26:3674bool RemovePrefixAndAssignIfMatches(const base::StringPiece& prefix,
75 const GURL& url,
76 std::string* dest) {
[email protected]7f6de3a2013-02-07 00:16:2277 const base::StringPiece spec(url.possibly_invalid_spec());
[email protected]1e08a042012-11-13 23:26:3678
79 if (spec.starts_with(prefix)) {
[email protected]04307e02014-05-01 18:01:4980 url::RawCanonOutputW<1024> output;
81 url::DecodeURLEscapeSequences(spec.data() + prefix.length(),
82 spec.length() - prefix.length(),
83 &output);
[email protected]b57c9d52013-12-24 16:23:2584 std::string decoded_url = base::UTF16ToUTF8(
[email protected]865eb542013-12-19 22:44:4985 base::string16(output.data(), output.length()));
[email protected]3f1b1272013-08-07 05:18:1086 dest->assign(decoded_url.begin(), decoded_url.end());
[email protected]1e08a042012-11-13 23:26:3687 return true;
88 }
89 return false;
90}
91
[email protected]7f6de3a2013-02-07 00:16:2292void DistinguishAndAssignSrcLinkType(const GURL& url, AwHitTestData* data) {
93 if (RemovePrefixAndAssignIfMatches(
94 content::kAddressPrefix,
95 url,
96 &data->extra_data_for_type)) {
97 data->type = AwHitTestData::GEO_TYPE;
98 } else if (RemovePrefixAndAssignIfMatches(
99 content::kPhoneNumberPrefix,
100 url,
101 &data->extra_data_for_type)) {
102 data->type = AwHitTestData::PHONE_TYPE;
103 } else if (RemovePrefixAndAssignIfMatches(
104 content::kEmailPrefix,
105 url,
106 &data->extra_data_for_type)) {
107 data->type = AwHitTestData::EMAIL_TYPE;
108 } else {
109 data->type = AwHitTestData::SRC_LINK_TYPE;
110 data->extra_data_for_type = url.possibly_invalid_spec();
[email protected]bab5f452013-12-18 18:18:44111 if (!data->extra_data_for_type.empty())
[email protected]b57c9d52013-12-24 16:23:25112 data->href = base::UTF8ToUTF16(data->extra_data_for_type);
[email protected]7f6de3a2013-02-07 00:16:22113 }
[email protected]1e08a042012-11-13 23:26:36114}
115
[email protected]7f6de3a2013-02-07 00:16:22116void PopulateHitTestData(const GURL& absolute_link_url,
117 const GURL& absolute_image_url,
118 bool is_editable,
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
123 // GURL::spec.
124 if (!absolute_image_url.is_empty())
125 data->img_src = absolute_image_url;
126
127 const bool is_javascript_scheme =
[email protected]cca6f392014-05-28 21:32:26128 absolute_link_url.SchemeIs(url::kJavaScriptScheme);
[email protected]7f6de3a2013-02-07 00:16:22129 const bool has_link_url = !absolute_link_url.is_empty();
130 const bool has_image_url = !absolute_image_url.is_empty();
131
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();
[email protected]bab5f452013-12-18 18:18:44137 if (absolute_link_url.is_valid())
[email protected]b57c9d52013-12-24 16:23:25138 data->href = base::UTF8ToUTF16(absolute_link_url.possibly_invalid_spec());
[email protected]7f6de3a2013-02-07 00:16:22139 } 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;
boliu2d46b522014-08-29 22:33:44144 DCHECK_EQ(0u, data->extra_data_for_type.length());
[email protected]7f6de3a2013-02-07 00:16:22145 }
146}
147
148} // namespace
149
[email protected]fada28b2012-09-01 01:33:03150AwRenderViewExt::AwRenderViewExt(content::RenderView* render_view)
[email protected]5614eae2013-04-19 12:46:38151 : content::RenderViewObserver(render_view), page_scale_factor_(0.0f) {
[email protected]fada28b2012-09-01 01:33:03152}
153
[email protected]11b426f22013-01-04 12:20:24154AwRenderViewExt::~AwRenderViewExt() {
[email protected]11b426f22013-01-04 12:20:24155}
[email protected]fada28b2012-09-01 01:33:03156
157// static
158void AwRenderViewExt::RenderViewCreated(content::RenderView* render_view) {
159 new AwRenderViewExt(render_view); // |render_view| takes ownership.
160}
161
162bool AwRenderViewExt::OnMessageReceived(const IPC::Message& message) {
163 bool handled = true;
164 IPC_BEGIN_MESSAGE_MAP(AwRenderViewExt, message)
165 IPC_MESSAGE_HANDLER(AwViewMsg_DocumentHasImages, OnDocumentHasImagesRequest)
[email protected]a7198a12012-11-08 18:34:05166 IPC_MESSAGE_HANDLER(AwViewMsg_DoHitTest, OnDoHitTest)
[email protected]91219702013-09-18 07:33:51167 IPC_MESSAGE_HANDLER(AwViewMsg_SetTextZoomFactor, OnSetTextZoomFactor)
[email protected]f4a3a042013-02-26 12:31:49168 IPC_MESSAGE_HANDLER(AwViewMsg_ResetScrollAndScaleState,
169 OnResetScrollAndScaleState)
[email protected]1d7c8e02013-02-28 13:47:54170 IPC_MESSAGE_HANDLER(AwViewMsg_SetInitialPageScale, OnSetInitialPageScale)
[email protected]25645ef2013-07-23 23:03:35171 IPC_MESSAGE_HANDLER(AwViewMsg_SetBackgroundColor, OnSetBackgroundColor)
[email protected]fada28b2012-09-01 01:33:03172 IPC_MESSAGE_UNHANDLED(handled = false)
173 IPC_END_MESSAGE_MAP()
174 return handled;
175}
176
177void AwRenderViewExt::OnDocumentHasImagesRequest(int id) {
178 bool hasImages = false;
179 if (render_view()) {
[email protected]c32e27082013-11-07 06:58:20180 blink::WebView* webview = render_view()->GetWebView();
[email protected]fada28b2012-09-01 01:33:03181 if (webview) {
[email protected]c32e27082013-11-07 06:58:20182 blink::WebVector<blink::WebElement> images;
[email protected]fada28b2012-09-01 01:33:03183 webview->mainFrame()->document().images(images);
184 hasImages = !images.isEmpty();
185 }
186 }
187 Send(new AwViewHostMsg_DocumentHasImagesResponse(routing_id(), id,
188 hasImages));
189}
190
[email protected]5614eae2013-04-19 12:46:38191void AwRenderViewExt::DidCommitCompositorFrame() {
192 UpdatePageScaleFactor();
193}
194
[email protected]d01b2a62013-09-18 23:21:33195void AwRenderViewExt::DidUpdateLayout() {
196 if (check_contents_size_timer_.IsRunning())
197 return;
198
199 check_contents_size_timer_.Start(FROM_HERE,
200 base::TimeDelta::FromMilliseconds(0), this,
201 &AwRenderViewExt::CheckContentsSize);
202}
203
[email protected]5614eae2013-04-19 12:46:38204void 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_));
209 }
210}
211
[email protected]d01b2a62013-09-18 23:21:33212void AwRenderViewExt::CheckContentsSize() {
213 if (!render_view()->GetWebView())
214 return;
215
216 gfx::Size contents_size;
217
[email protected]c32e27082013-11-07 06:58:20218 blink::WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
[email protected]d01b2a62013-09-18 23:21:33219 if (main_frame)
220 contents_size = main_frame->contentsSize();
221
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();
226 }
227
228 if (contents_size == last_sent_contents_size_)
229 return;
230
231 last_sent_contents_size_ = contents_size;
232 Send(new AwViewHostMsg_OnContentsSizeChanged(routing_id(), contents_size));
233}
234
[email protected]bc55afd2013-08-14 18:52:36235void 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.
[email protected]c32e27082013-11-07 06:58:20243 blink::WebImageCache::clear();
[email protected]bc55afd2013-08-14 18:52:36244}
245
[email protected]c32e27082013-11-07 06:58:20246void AwRenderViewExt::FocusedNodeChanged(const blink::WebNode& node) {
[email protected]7f6de3a2013-02-07 00:16:22247 if (node.isNull() || !node.isElementNode() || !render_view())
248 return;
249
dglazkovf4c7e8042015-05-15 23:01:59250 // Note: element is not const due to textContent() is not const.
[email protected]c32e27082013-11-07 06:58:20251 blink::WebElement element = node.toConst<blink::WebElement>();
[email protected]7f6de3a2013-02-07 00:16:22252 AwHitTestData data;
253
254 data.href = GetHref(element);
dglazkovf4c7e8042015-05-15 23:01:59255 data.anchor_text = element.textContent();
[email protected]7f6de3a2013-02-07 00:16:22256
257 GURL absolute_link_url;
258 if (node.isLink())
259 absolute_link_url = GetAbsoluteUrl(node, data.href);
260
skyostila89c8da2015-01-08 16:51:59261 GURL absolute_image_url = GetChildImageUrlFromElement(element);
[email protected]7f6de3a2013-02-07 00:16:22262
263 PopulateHitTestData(absolute_link_url,
264 absolute_image_url,
265 render_view()->IsEditableNode(node),
266 &data);
267 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
[email protected]a7198a12012-11-08 18:34:05268}
269
hush1d93adc2014-11-13 20:11:16270void AwRenderViewExt::OnDoHitTest(const gfx::PointF& touch_center,
271 const gfx::SizeF& touch_area) {
[email protected]a7198a12012-11-08 18:34:05272 if (!render_view() || !render_view()->GetWebView())
273 return;
274
[email protected]c32e27082013-11-07 06:58:20275 const blink::WebHitTestResult result =
hush1d93adc2014-11-13 20:11:16276 render_view()->GetWebView()->hitTestResultForTap(
277 blink::WebPoint(touch_center.x(), touch_center.y()),
278 blink::WebSize(touch_area.width(), touch_area.height()));
[email protected]a7198a12012-11-08 18:34:05279 AwHitTestData data;
280
skyostila89c8da2015-01-08 16:51:59281 GURL absolute_image_url = result.absoluteImageURL();
[email protected]a7198a12012-11-08 18:34:05282 if (!result.urlElement().isNull()) {
dglazkovf4c7e8042015-05-15 23:01:59283 data.anchor_text = result.urlElement().textContent();
[email protected]7f6de3a2013-02-07 00:16:22284 data.href = GetHref(result.urlElement());
skyostila89c8da2015-01-08 16:51:59285 // 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());
[email protected]a7198a12012-11-08 18:34:05289 }
290
[email protected]7f6de3a2013-02-07 00:16:22291 PopulateHitTestData(result.absoluteLinkURL(),
skyostila89c8da2015-01-08 16:51:59292 absolute_image_url,
[email protected]7f6de3a2013-02-07 00:16:22293 result.isContentEditable(),
294 &data);
[email protected]a7198a12012-11-08 18:34:05295 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
296}
297
[email protected]91219702013-09-18 07:33:51298void AwRenderViewExt::OnSetTextZoomFactor(float zoom_factor) {
[email protected]cbe55d22013-02-14 17:04:33299 if (!render_view() || !render_view()->GetWebView())
300 return;
301 // Hide selection and autofill popups.
302 render_view()->GetWebView()->hidePopups();
[email protected]91219702013-09-18 07:33:51303 render_view()->GetWebView()->setTextZoomFactor(zoom_factor);
[email protected]cbe55d22013-02-14 17:04:33304}
305
[email protected]f4a3a042013-02-26 12:31:49306void AwRenderViewExt::OnResetScrollAndScaleState() {
307 if (!render_view() || !render_view()->GetWebView())
308 return;
309 render_view()->GetWebView()->resetScrollAndScaleState();
310}
311
[email protected]1d7c8e02013-02-28 13:47:54312void AwRenderViewExt::OnSetInitialPageScale(double page_scale_factor) {
313 if (!render_view() || !render_view()->GetWebView())
314 return;
315 render_view()->GetWebView()->setInitialPageScaleOverride(
316 page_scale_factor);
317}
318
[email protected]25645ef2013-07-23 23:03:35319void AwRenderViewExt::OnSetBackgroundColor(SkColor c) {
320 if (!render_view() || !render_view()->GetWebView())
321 return;
[email protected]0ff79a92013-07-28 07:28:04322 render_view()->GetWebView()->setBaseBackgroundColor(c);
[email protected]25645ef2013-07-23 23:03:35323}
324
[email protected]fada28b2012-09-01 01:33:03325} // namespace android_webview