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 "xwalk/runtime/renderer/android/xwalk_render_view_ext.h"
10 #include "base/strings/string_piece.h"
11 #include "content/public/common/url_constants.h"
12 #include "content/public/renderer/android_content_detection_prefixes.h"
13 #include "content/public/renderer/document_state.h"
14 #include "content/public/renderer/render_view.h"
15 #include "skia/ext/refptr.h"
16 #include "third_party/WebKit/public/platform/WebSize.h"
17 #include "third_party/WebKit/public/platform/WebURL.h"
18 #include "third_party/WebKit/public/platform/WebVector.h"
19 #include "third_party/WebKit/public/web/WebDataSource.h"
20 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebElement.h"
22 #include "third_party/WebKit/public/web/WebElementCollection.h"
23 #include "third_party/WebKit/public/web/WebHitTestResult.h"
24 #include "third_party/WebKit/public/web/WebLocalFrame.h"
25 #include "third_party/WebKit/public/web/WebNode.h"
26 #include "third_party/WebKit/public/web/WebNodeList.h"
27 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
28 #include "third_party/WebKit/public/web/WebView.h"
29 #include "third_party/skia/include/core/SkPicture.h"
30 #include "xwalk/runtime/common/android/xwalk_hit_test_data.h"
31 #include "xwalk/runtime/common/android/xwalk_render_view_messages.h"
37 GURL GetAbsoluteUrl(const blink::WebNode& node,
38 const base::string16& url_fragment) {
39 return GURL(node.document().completeURL(url_fragment));
42 base::string16 GetHref(const blink::WebElement& element) {
43 // Get the actual 'href' attribute, which might relative if valid or can
44 // possibly contain garbage otherwise, so not using absoluteLinkURL here.
45 return element.getAttribute("href");
48 GURL GetAbsoluteSrcUrl(const blink::WebElement& element) {
51 return GetAbsoluteUrl(element, element.getAttribute("src"));
54 blink::WebNode GetImgChild(const blink::WebNode& node) {
55 // This implementation is incomplete (for example if is an area tag) but
56 // matches the original WebViewClassic implementation.
58 blink::WebElementCollection list = node.getElementsByTagName("img");
59 if (list.length() > 0)
60 return list.firstItem();
61 return blink::WebNode();
64 bool RemovePrefixAndAssignIfMatches(const base::StringPiece& prefix,
67 const base::StringPiece spec(url.possibly_invalid_spec());
69 if (spec.starts_with(prefix)) {
70 dest->assign(spec.begin() + prefix.length(), spec.end());
76 void DistinguishAndAssignSrcLinkType(const GURL& url, XWalkHitTestData* data) {
77 if (RemovePrefixAndAssignIfMatches(
78 content::kAddressPrefix,
80 &data->extra_data_for_type)) {
81 data->type = XWalkHitTestData::GEO_TYPE;
82 } else if (RemovePrefixAndAssignIfMatches(
83 content::kPhoneNumberPrefix,
85 &data->extra_data_for_type)) {
86 data->type = XWalkHitTestData::PHONE_TYPE;
87 } else if (RemovePrefixAndAssignIfMatches(
88 content::kEmailPrefix,
90 &data->extra_data_for_type)) {
91 data->type = XWalkHitTestData::EMAIL_TYPE;
93 data->type = XWalkHitTestData::SRC_LINK_TYPE;
94 data->extra_data_for_type = url.possibly_invalid_spec();
98 void PopulateHitTestData(const GURL& absolute_link_url,
99 const GURL& absolute_image_url,
101 XWalkHitTestData* data) {
102 // Note: Using GURL::is_empty instead of GURL:is_valid due to the
103 // WebViewClassic allowing any kind of protocol which GURL::is_valid
104 // disallows. Similar reasons for using GURL::possibly_invalid_spec instead of
106 if (!absolute_image_url.is_empty())
107 data->img_src = absolute_image_url;
109 const bool is_javascript_scheme =
110 absolute_link_url.SchemeIs(url::kJavaScriptScheme);
111 const bool has_link_url = !absolute_link_url.is_empty();
112 const bool has_image_url = !absolute_image_url.is_empty();
114 if (has_link_url && !has_image_url && !is_javascript_scheme) {
115 DistinguishAndAssignSrcLinkType(absolute_link_url, data);
116 } else if (has_link_url && has_image_url && !is_javascript_scheme) {
117 data->type = XWalkHitTestData::SRC_IMAGE_LINK_TYPE;
118 data->extra_data_for_type = data->img_src.possibly_invalid_spec();
119 } else if (!has_link_url && has_image_url) {
120 data->type = XWalkHitTestData::IMAGE_TYPE;
121 data->extra_data_for_type = data->img_src.possibly_invalid_spec();
122 } else if (is_editable) {
123 data->type = XWalkHitTestData::EDIT_TEXT_TYPE;
124 DCHECK_EQ(data->extra_data_for_type.length(), 0u);
130 XWalkRenderViewExt::XWalkRenderViewExt(content::RenderView* render_view)
131 : content::RenderViewObserver(render_view), page_scale_factor_(0.0f) {
134 XWalkRenderViewExt::~XWalkRenderViewExt() {
138 void XWalkRenderViewExt::RenderViewCreated(content::RenderView* render_view) {
139 new XWalkRenderViewExt(render_view); // |render_view| takes ownership.
142 bool XWalkRenderViewExt::OnMessageReceived(const IPC::Message& message) {
144 IPC_BEGIN_MESSAGE_MAP(XWalkRenderViewExt, message)
145 IPC_MESSAGE_HANDLER(XWalkViewMsg_DocumentHasImages,
146 OnDocumentHasImagesRequest)
147 IPC_MESSAGE_HANDLER(XWalkViewMsg_DoHitTest, OnDoHitTest)
148 IPC_MESSAGE_HANDLER(XWalkViewMsg_SetTextZoomLevel, OnSetTextZoomLevel)
149 IPC_MESSAGE_HANDLER(XWalkViewMsg_ResetScrollAndScaleState,
150 OnResetScrollAndScaleState)
151 IPC_MESSAGE_HANDLER(XWalkViewMsg_SetInitialPageScale, OnSetInitialPageScale)
152 IPC_MESSAGE_UNHANDLED(handled = false)
153 IPC_END_MESSAGE_MAP()
157 void XWalkRenderViewExt::OnDocumentHasImagesRequest(int id) {
158 bool hasImages = false;
160 blink::WebView* webview = render_view()->GetWebView();
162 blink::WebVector<blink::WebElement> images;
163 webview->mainFrame()->document().images(images);
164 hasImages = !images.isEmpty();
167 Send(new XWalkViewHostMsg_DocumentHasImagesResponse(routing_id(), id,
171 void XWalkRenderViewExt::DidCommitProvisionalLoad(blink::WebLocalFrame* frame,
172 bool is_new_navigation) {
173 content::DocumentState* document_state =
174 content::DocumentState::FromDataSource(frame->dataSource());
175 if (document_state->can_load_local_resources()) {
176 blink::WebSecurityOrigin origin = frame->document().securityOrigin();
177 origin.grantLoadLocalResources();
181 void XWalkRenderViewExt::DidCommitCompositorFrame() {
182 UpdatePageScaleFactor();
185 void XWalkRenderViewExt::UpdatePageScaleFactor() {
186 if (page_scale_factor_ != render_view()->GetWebView()->pageScaleFactor()) {
187 page_scale_factor_ = render_view()->GetWebView()->pageScaleFactor();
188 Send(new XWalkViewHostMsg_PageScaleFactorChanged(routing_id(),
189 page_scale_factor_));
193 void XWalkRenderViewExt::FocusedNodeChanged(const blink::WebNode& node) {
194 if (node.isNull() || !node.isElementNode() || !render_view())
197 // Note: element is not const due to innerText() is not const.
198 blink::WebElement element = node.toConst<blink::WebElement>();
199 XWalkHitTestData data;
201 data.href = GetHref(element);
202 data.anchor_text = element.innerText();
204 GURL absolute_link_url;
206 absolute_link_url = GetAbsoluteUrl(node, data.href);
208 GURL absolute_image_url;
209 const blink::WebNode child_img = GetImgChild(node);
210 if (!child_img.isNull() && child_img.isElementNode()) {
212 GetAbsoluteSrcUrl(child_img.toConst<blink::WebElement>());
215 PopulateHitTestData(absolute_link_url,
217 render_view()->IsEditableNode(node),
219 Send(new XWalkViewHostMsg_UpdateHitTestData(routing_id(), data));
222 void XWalkRenderViewExt::OnDoHitTest(int view_x, int view_y) {
223 if (!render_view() || !render_view()->GetWebView())
226 const blink::WebHitTestResult result =
227 render_view()->GetWebView()->hitTestResultAt(
228 blink::WebPoint(view_x, view_y));
229 XWalkHitTestData data;
231 if (!result.urlElement().isNull()) {
232 data.anchor_text = result.urlElement().innerText();
233 data.href = GetHref(result.urlElement());
236 PopulateHitTestData(result.absoluteLinkURL(),
237 result.absoluteImageURL(),
238 result.isContentEditable(),
240 Send(new XWalkViewHostMsg_UpdateHitTestData(routing_id(), data));
243 void XWalkRenderViewExt::OnSetTextZoomLevel(double zoom_level) {
244 if (!render_view() || !render_view()->GetWebView())
246 // Hide selection and autofill popups.
247 render_view()->GetWebView()->hidePopups();
248 render_view()->GetWebView()->setZoomLevel(zoom_level);
251 void XWalkRenderViewExt::OnResetScrollAndScaleState() {
252 if (!render_view() || !render_view()->GetWebView())
254 render_view()->GetWebView()->resetScrollAndScaleState();
257 void XWalkRenderViewExt::OnSetInitialPageScale(double page_scale_factor) {
258 if (!render_view() || !render_view()->GetWebView())
260 render_view()->GetWebView()->setInitialPageScaleOverride(