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/common/url_constants.h"
16 #include "content/public/renderer/android_content_detection_prefixes.h"
17 #include "content/public/renderer/document_state.h"
18 #include "content/public/renderer/render_view.h"
19 #include "skia/ext/refptr.h"
20 #include "third_party/WebKit/public/platform/WebSize.h"
21 #include "third_party/WebKit/public/platform/WebURL.h"
22 #include "third_party/WebKit/public/platform/WebVector.h"
23 #include "third_party/WebKit/public/web/WebDataSource.h"
24 #include "third_party/WebKit/public/web/WebDocument.h"
25 #include "third_party/WebKit/public/web/WebElement.h"
26 #include "third_party/WebKit/public/web/WebFrame.h"
27 #include "third_party/WebKit/public/web/WebHitTestResult.h"
28 #include "third_party/WebKit/public/web/WebImageCache.h"
29 #include "third_party/WebKit/public/web/WebNode.h"
30 #include "third_party/WebKit/public/web/WebNodeList.h"
31 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
32 #include "third_party/WebKit/public/web/WebView.h"
33 #include "url/url_canon.h"
34 #include "url/url_util.h"
36 namespace android_webview {
40 bool AllowMixedContent(const WebKit::WebURL& url) {
41 // We treat non-standard schemes as "secure" in the WebView to allow them to
42 // be used for request interception.
43 // TODO(benm): Tighten this restriction by requiring embedders to register
44 // their custom schemes? See b/9420953.
46 return !gurl.IsStandard();
49 GURL GetAbsoluteUrl(const WebKit::WebNode& node, const string16& url_fragment) {
50 return GURL(node.document().completeURL(url_fragment));
53 string16 GetHref(const WebKit::WebElement& element) {
54 // Get the actual 'href' attribute, which might relative if valid or can
55 // possibly contain garbage otherwise, so not using absoluteLinkURL here.
56 return element.getAttribute("href");
59 GURL GetAbsoluteSrcUrl(const WebKit::WebElement& element) {
62 return GetAbsoluteUrl(element, element.getAttribute("src"));
65 WebKit::WebNode GetImgChild(const WebKit::WebNode& node) {
66 // This implementation is incomplete (for example if is an area tag) but
67 // matches the original WebViewClassic implementation.
69 WebKit::WebNodeList list = node.getElementsByTagName("img");
70 if (list.length() > 0)
72 return WebKit::WebNode();
75 bool RemovePrefixAndAssignIfMatches(const base::StringPiece& prefix,
78 const base::StringPiece spec(url.possibly_invalid_spec());
80 if (spec.starts_with(prefix)) {
81 url_canon::RawCanonOutputW<1024> output;
82 url_util::DecodeURLEscapeSequences(spec.data() + prefix.length(),
83 spec.length() - prefix.length(), &output);
84 std::string decoded_url = UTF16ToUTF8(
85 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();
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(content::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 } else if (!has_link_url && has_image_url) {
136 data->type = AwHitTestData::IMAGE_TYPE;
137 data->extra_data_for_type = data->img_src.possibly_invalid_spec();
138 } else if (is_editable) {
139 data->type = AwHitTestData::EDIT_TEXT_TYPE;
140 DCHECK(data->extra_data_for_type.length() == 0);
146 AwRenderViewExt::AwRenderViewExt(content::RenderView* render_view)
147 : content::RenderViewObserver(render_view), page_scale_factor_(0.0f) {
148 render_view->GetWebView()->setPermissionClient(this);
151 AwRenderViewExt::~AwRenderViewExt() {
155 void AwRenderViewExt::RenderViewCreated(content::RenderView* render_view) {
156 new AwRenderViewExt(render_view); // |render_view| takes ownership.
159 bool AwRenderViewExt::OnMessageReceived(const IPC::Message& message) {
161 IPC_BEGIN_MESSAGE_MAP(AwRenderViewExt, message)
162 IPC_MESSAGE_HANDLER(AwViewMsg_DocumentHasImages, OnDocumentHasImagesRequest)
163 IPC_MESSAGE_HANDLER(AwViewMsg_DoHitTest, OnDoHitTest)
164 IPC_MESSAGE_HANDLER(AwViewMsg_SetTextZoomFactor, OnSetTextZoomFactor)
165 IPC_MESSAGE_HANDLER(AwViewMsg_ResetScrollAndScaleState,
166 OnResetScrollAndScaleState)
167 IPC_MESSAGE_HANDLER(AwViewMsg_SetInitialPageScale, OnSetInitialPageScale)
168 IPC_MESSAGE_HANDLER(AwViewMsg_SetFixedLayoutSize, OnSetFixedLayoutSize)
169 IPC_MESSAGE_HANDLER(AwViewMsg_SetBackgroundColor, OnSetBackgroundColor)
170 IPC_MESSAGE_UNHANDLED(handled = false)
171 IPC_END_MESSAGE_MAP()
175 void AwRenderViewExt::OnDocumentHasImagesRequest(int id) {
176 bool hasImages = false;
178 WebKit::WebView* webview = render_view()->GetWebView();
180 WebKit::WebVector<WebKit::WebElement> images;
181 webview->mainFrame()->document().images(images);
182 hasImages = !images.isEmpty();
185 Send(new AwViewHostMsg_DocumentHasImagesResponse(routing_id(), id,
189 bool AwRenderViewExt::allowDisplayingInsecureContent(
190 WebKit::WebFrame* frame,
191 bool enabled_per_settings,
192 const WebKit::WebSecurityOrigin& origin,
193 const WebKit::WebURL& url) {
194 return enabled_per_settings ? true : AllowMixedContent(url);
197 bool AwRenderViewExt::allowRunningInsecureContent(
198 WebKit::WebFrame* frame,
199 bool enabled_per_settings,
200 const WebKit::WebSecurityOrigin& origin,
201 const WebKit::WebURL& url) {
202 return enabled_per_settings ? true : AllowMixedContent(url);
205 void AwRenderViewExt::DidCommitProvisionalLoad(WebKit::WebFrame* frame,
206 bool is_new_navigation) {
207 content::DocumentState* document_state =
208 content::DocumentState::FromDataSource(frame->dataSource());
209 if (document_state->can_load_local_resources()) {
210 WebKit::WebSecurityOrigin origin = frame->document().securityOrigin();
211 origin.grantLoadLocalResources();
215 void AwRenderViewExt::DidCommitCompositorFrame() {
216 UpdatePageScaleFactor();
219 void AwRenderViewExt::DidUpdateLayout() {
220 if (check_contents_size_timer_.IsRunning())
223 check_contents_size_timer_.Start(FROM_HERE,
224 base::TimeDelta::FromMilliseconds(0), this,
225 &AwRenderViewExt::CheckContentsSize);
228 void AwRenderViewExt::UpdatePageScaleFactor() {
229 if (page_scale_factor_ != render_view()->GetWebView()->pageScaleFactor()) {
230 page_scale_factor_ = render_view()->GetWebView()->pageScaleFactor();
231 Send(new AwViewHostMsg_PageScaleFactorChanged(routing_id(),
232 page_scale_factor_));
236 void AwRenderViewExt::CheckContentsSize() {
237 if (!render_view()->GetWebView())
240 gfx::Size contents_size;
242 WebKit::WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
244 contents_size = main_frame->contentsSize();
246 // Fall back to contentsPreferredMinimumSize if the mainFrame is reporting a
247 // 0x0 size (this happens during initial load).
248 if (contents_size.IsEmpty()) {
249 contents_size = render_view()->GetWebView()->contentsPreferredMinimumSize();
252 if (contents_size == last_sent_contents_size_)
255 last_sent_contents_size_ = contents_size;
256 Send(new AwViewHostMsg_OnContentsSizeChanged(routing_id(), contents_size));
259 void AwRenderViewExt::Navigate(const GURL& url) {
260 // Navigate is called only on NEW navigations, so WebImageCache won't be freed
261 // when the user just clicks on links, but only when a navigation is started,
262 // for instance via loadUrl. A better approach would be clearing the cache on
263 // cross-site boundaries, however this would require too many changes both on
264 // the browser side (in RenderViewHostManger), to the IPCmessages and to the
265 // RenderViewObserver. Thus, clearing decoding image cache on Navigate, seems
266 // a more acceptable compromise.
267 WebKit::WebImageCache::clear();
270 void AwRenderViewExt::FocusedNodeChanged(const WebKit::WebNode& node) {
271 if (node.isNull() || !node.isElementNode() || !render_view())
274 // Note: element is not const due to innerText() is not const.
275 WebKit::WebElement element = node.toConst<WebKit::WebElement>();
278 data.href = GetHref(element);
279 data.anchor_text = element.innerText();
281 GURL absolute_link_url;
283 absolute_link_url = GetAbsoluteUrl(node, data.href);
285 GURL absolute_image_url;
286 const WebKit::WebNode child_img = GetImgChild(node);
287 if (!child_img.isNull() && child_img.isElementNode()) {
289 GetAbsoluteSrcUrl(child_img.toConst<WebKit::WebElement>());
292 PopulateHitTestData(absolute_link_url,
294 render_view()->IsEditableNode(node),
296 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
299 void AwRenderViewExt::OnDoHitTest(int view_x, int view_y) {
300 if (!render_view() || !render_view()->GetWebView())
303 const WebKit::WebHitTestResult result =
304 render_view()->GetWebView()->hitTestResultAt(
305 WebKit::WebPoint(view_x, view_y));
308 if (!result.urlElement().isNull()) {
309 data.anchor_text = result.urlElement().innerText();
310 data.href = GetHref(result.urlElement());
313 PopulateHitTestData(result.absoluteLinkURL(),
314 result.absoluteImageURL(),
315 result.isContentEditable(),
317 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
320 void AwRenderViewExt::OnSetTextZoomFactor(float zoom_factor) {
321 if (!render_view() || !render_view()->GetWebView())
323 // Hide selection and autofill popups.
324 render_view()->GetWebView()->hidePopups();
325 render_view()->GetWebView()->setTextZoomFactor(zoom_factor);
328 void AwRenderViewExt::OnResetScrollAndScaleState() {
329 if (!render_view() || !render_view()->GetWebView())
331 render_view()->GetWebView()->resetScrollAndScaleState();
334 void AwRenderViewExt::OnSetInitialPageScale(double page_scale_factor) {
335 if (!render_view() || !render_view()->GetWebView())
337 render_view()->GetWebView()->setInitialPageScaleOverride(
341 void AwRenderViewExt::OnSetFixedLayoutSize(const gfx::Size& size) {
342 if (!render_view() || !render_view()->GetWebView())
344 render_view()->GetWebView()->setFixedLayoutSize(size);
347 void AwRenderViewExt::OnSetBackgroundColor(SkColor c) {
348 if (!render_view() || !render_view()->GetWebView())
350 render_view()->GetWebView()->setBaseBackgroundColor(c);
353 } // namespace android_webview