Upstream version 11.39.244.0
[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_HANDLER(XWalkViewMsg_SetBackgroundColor, OnSetBackgroundColor)
153     IPC_MESSAGE_UNHANDLED(handled = false)
154   IPC_END_MESSAGE_MAP()
155   return handled;
156 }
157
158 void XWalkRenderViewExt::OnDocumentHasImagesRequest(int id) {
159   bool hasImages = false;
160   if (render_view()) {
161     blink::WebView* webview = render_view()->GetWebView();
162     if (webview) {
163       blink::WebVector<blink::WebElement> images;
164       webview->mainFrame()->document().images(images);
165       hasImages = !images.isEmpty();
166     }
167   }
168   Send(new XWalkViewHostMsg_DocumentHasImagesResponse(routing_id(), id,
169                                                    hasImages));
170 }
171
172 void XWalkRenderViewExt::DidCommitProvisionalLoad(blink::WebLocalFrame* frame,
173                                                   bool is_new_navigation) {
174   content::DocumentState* document_state =
175       content::DocumentState::FromDataSource(frame->dataSource());
176   if (document_state->can_load_local_resources()) {
177     blink::WebSecurityOrigin origin = frame->document().securityOrigin();
178     origin.grantLoadLocalResources();
179   }
180 }
181
182 void XWalkRenderViewExt::DidCommitCompositorFrame() {
183   UpdatePageScaleFactor();
184 }
185
186 void XWalkRenderViewExt::UpdatePageScaleFactor() {
187   if (page_scale_factor_ != render_view()->GetWebView()->pageScaleFactor()) {
188     page_scale_factor_ = render_view()->GetWebView()->pageScaleFactor();
189     Send(new XWalkViewHostMsg_PageScaleFactorChanged(routing_id(),
190                                                   page_scale_factor_));
191   }
192 }
193
194 void XWalkRenderViewExt::FocusedNodeChanged(const blink::WebNode& node) {
195   if (node.isNull() || !node.isElementNode() || !render_view())
196     return;
197
198   // Note: element is not const due to innerText() is not const.
199   blink::WebElement element = node.toConst<blink::WebElement>();
200   XWalkHitTestData data;
201
202   data.href = GetHref(element);
203   data.anchor_text = element.innerText();
204
205   GURL absolute_link_url;
206   if (node.isLink())
207     absolute_link_url = GetAbsoluteUrl(node, data.href);
208
209   GURL absolute_image_url;
210   const blink::WebElement child_img = GetImgChild(element);
211   if (!child_img.isNull()) {
212     absolute_image_url =
213         GetAbsoluteSrcUrl(child_img);
214   }
215
216   PopulateHitTestData(absolute_link_url,
217                       absolute_image_url,
218                       render_view()->IsEditableNode(node),
219                       &data);
220   Send(new XWalkViewHostMsg_UpdateHitTestData(routing_id(), data));
221 }
222
223 void XWalkRenderViewExt::OnDoHitTest(int view_x, int view_y) {
224   if (!render_view() || !render_view()->GetWebView())
225     return;
226
227   const blink::WebHitTestResult result =
228       render_view()->GetWebView()->hitTestResultAt(
229           blink::WebPoint(view_x, view_y));
230   XWalkHitTestData data;
231
232   if (!result.urlElement().isNull()) {
233     data.anchor_text = result.urlElement().innerText();
234     data.href = GetHref(result.urlElement());
235   }
236
237   PopulateHitTestData(result.absoluteLinkURL(),
238                       result.absoluteImageURL(),
239                       result.isContentEditable(),
240                       &data);
241   Send(new XWalkViewHostMsg_UpdateHitTestData(routing_id(), data));
242 }
243
244 void XWalkRenderViewExt::OnSetTextZoomLevel(double zoom_level) {
245   if (!render_view() || !render_view()->GetWebView())
246     return;
247   // Hide selection and autofill popups.
248   render_view()->GetWebView()->hidePopups();
249   render_view()->GetWebView()->setZoomLevel(zoom_level);
250 }
251
252 void XWalkRenderViewExt::OnResetScrollAndScaleState() {
253   if (!render_view() || !render_view()->GetWebView())
254     return;
255   render_view()->GetWebView()->resetScrollAndScaleState();
256 }
257
258 void XWalkRenderViewExt::OnSetInitialPageScale(double page_scale_factor) {
259   if (!render_view() || !render_view()->GetWebView())
260     return;
261   render_view()->GetWebView()->setInitialPageScaleOverride(
262       page_scale_factor);
263 }
264
265 void XWalkRenderViewExt::OnSetBackgroundColor(SkColor c) {
266   if (!render_view() || !render_view()->GetWebView())
267     return;
268   render_view()->GetWebView()->setBaseBackgroundColor(c);
269 }
270
271 }  // namespace xwalk