359316ed2a8e7916672bd5d66eaf33f844ac02a1
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / renderer / android / xwalk_render_view_ext.cc
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.
4
5 #include "xwalk/runtime/renderer/android/xwalk_render_view_ext.h"
6
7 #include <string>
8
9 #include "base/bind.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"
32
33 namespace xwalk {
34
35 namespace {
36
37 GURL GetAbsoluteUrl(const blink::WebNode& node,
38                     const base::string16& url_fragment) {
39   return GURL(node.document().completeURL(url_fragment));
40 }
41
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");
46 }
47
48 GURL GetAbsoluteSrcUrl(const blink::WebElement& element) {
49   if (element.isNull())
50     return GURL();
51   return GetAbsoluteUrl(element, element.getAttribute("src"));
52 }
53
54 blink::WebElement GetImgChild(const blink::WebElement& element) {
55   // This implementation is incomplete (for example if is an area tag) but
56   // matches the original WebViewClassic implementation.
57
58   blink::WebElementCollection collection =
59       element.getElementsByHTMLTagName("img");
60   DCHECK(!collection.isNull());
61   return collection.firstItem();
62 }
63
64 bool RemovePrefixAndAssignIfMatches(const base::StringPiece& prefix,
65                                     const GURL& url,
66                                     std::string* dest) {
67   const base::StringPiece spec(url.possibly_invalid_spec());
68
69   if (spec.starts_with(prefix)) {
70     dest->assign(spec.begin() + prefix.length(), spec.end());
71     return true;
72   }
73   return false;
74 }
75
76 void DistinguishAndAssignSrcLinkType(const GURL& url, XWalkHitTestData* data) {
77   if (RemovePrefixAndAssignIfMatches(
78       content::kAddressPrefix,
79       url,
80       &data->extra_data_for_type)) {
81     data->type = XWalkHitTestData::GEO_TYPE;
82   } else if (RemovePrefixAndAssignIfMatches(
83       content::kPhoneNumberPrefix,
84       url,
85       &data->extra_data_for_type)) {
86     data->type = XWalkHitTestData::PHONE_TYPE;
87   } else if (RemovePrefixAndAssignIfMatches(
88       content::kEmailPrefix,
89       url,
90       &data->extra_data_for_type)) {
91     data->type = XWalkHitTestData::EMAIL_TYPE;
92   } else {
93     data->type = XWalkHitTestData::SRC_LINK_TYPE;
94     data->extra_data_for_type = url.possibly_invalid_spec();
95   }
96 }
97
98 void PopulateHitTestData(const GURL& absolute_link_url,
99                          const GURL& absolute_image_url,
100                          bool is_editable,
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
105   // GURL::spec.
106   if (!absolute_image_url.is_empty())
107     data->img_src = absolute_image_url;
108
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();
113
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);
125   }
126 }
127
128 }  // namespace
129
130 XWalkRenderViewExt::XWalkRenderViewExt(content::RenderView* render_view)
131     : content::RenderViewObserver(render_view), page_scale_factor_(0.0f) {
132 }
133
134 XWalkRenderViewExt::~XWalkRenderViewExt() {
135 }
136
137 // static
138 void XWalkRenderViewExt::RenderViewCreated(content::RenderView* render_view) {
139   new XWalkRenderViewExt(render_view);  // |render_view| takes ownership.
140 }
141
142 bool XWalkRenderViewExt::OnMessageReceived(const IPC::Message& message) {
143   bool handled = true;
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()
154   return handled;
155 }
156
157 void XWalkRenderViewExt::OnDocumentHasImagesRequest(int id) {
158   bool hasImages = false;
159   if (render_view()) {
160     blink::WebView* webview = render_view()->GetWebView();
161     if (webview) {
162       blink::WebVector<blink::WebElement> images;
163       webview->mainFrame()->document().images(images);
164       hasImages = !images.isEmpty();
165     }
166   }
167   Send(new XWalkViewHostMsg_DocumentHasImagesResponse(routing_id(), id,
168                                                    hasImages));
169 }
170
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();
178   }
179 }
180
181 void XWalkRenderViewExt::DidCommitCompositorFrame() {
182   UpdatePageScaleFactor();
183 }
184
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_));
190   }
191 }
192
193 void XWalkRenderViewExt::FocusedNodeChanged(const blink::WebNode& node) {
194   if (node.isNull() || !node.isElementNode() || !render_view())
195     return;
196
197   // Note: element is not const due to innerText() is not const.
198   blink::WebElement element = node.toConst<blink::WebElement>();
199   XWalkHitTestData data;
200
201   data.href = GetHref(element);
202   data.anchor_text = element.innerText();
203
204   GURL absolute_link_url;
205   if (node.isLink())
206     absolute_link_url = GetAbsoluteUrl(node, data.href);
207
208   GURL absolute_image_url;
209   const blink::WebElement child_img = GetImgChild(element);
210   if (!child_img.isNull()) {
211     absolute_image_url =
212         GetAbsoluteSrcUrl(child_img);
213   }
214
215   PopulateHitTestData(absolute_link_url,
216                       absolute_image_url,
217                       render_view()->IsEditableNode(node),
218                       &data);
219   Send(new XWalkViewHostMsg_UpdateHitTestData(routing_id(), data));
220 }
221
222 void XWalkRenderViewExt::OnDoHitTest(int view_x, int view_y) {
223   if (!render_view() || !render_view()->GetWebView())
224     return;
225
226   const blink::WebHitTestResult result =
227       render_view()->GetWebView()->hitTestResultAt(
228           blink::WebPoint(view_x, view_y));
229   XWalkHitTestData data;
230
231   if (!result.urlElement().isNull()) {
232     data.anchor_text = result.urlElement().innerText();
233     data.href = GetHref(result.urlElement());
234   }
235
236   PopulateHitTestData(result.absoluteLinkURL(),
237                       result.absoluteImageURL(),
238                       result.isContentEditable(),
239                       &data);
240   Send(new XWalkViewHostMsg_UpdateHitTestData(routing_id(), data));
241 }
242
243 void XWalkRenderViewExt::OnSetTextZoomLevel(double zoom_level) {
244   if (!render_view() || !render_view()->GetWebView())
245     return;
246   // Hide selection and autofill popups.
247   render_view()->GetWebView()->hidePopups();
248   render_view()->GetWebView()->setZoomLevel(zoom_level);
249 }
250
251 void XWalkRenderViewExt::OnResetScrollAndScaleState() {
252   if (!render_view() || !render_view()->GetWebView())
253     return;
254   render_view()->GetWebView()->resetScrollAndScaleState();
255 }
256
257 void XWalkRenderViewExt::OnSetInitialPageScale(double page_scale_factor) {
258   if (!render_view() || !render_view()->GetWebView())
259     return;
260   render_view()->GetWebView()->setInitialPageScaleOverride(
261       page_scale_factor);
262 }
263
264 }  // namespace xwalk