Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / android_webview / renderer / aw_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 "android_webview/renderer/aw_render_view_ext.h"
6
7 #include <string>
8
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/WebElementCollection.h"
27 #include "third_party/WebKit/public/web/WebFrame.h"
28 #include "third_party/WebKit/public/web/WebHitTestResult.h"
29 #include "third_party/WebKit/public/web/WebImageCache.h"
30 #include "third_party/WebKit/public/web/WebNode.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"
35
36 namespace android_webview {
37
38 namespace {
39
40 GURL GetAbsoluteUrl(const blink::WebNode& node,
41                     const base::string16& url_fragment) {
42   return GURL(node.document().completeURL(url_fragment));
43 }
44
45 base::string16 GetHref(const blink::WebElement& element) {
46   // 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
51 GURL GetAbsoluteSrcUrl(const blink::WebElement& element) {
52   if (element.isNull())
53     return GURL();
54   return GetAbsoluteUrl(element, element.getAttribute("src"));
55 }
56
57 blink::WebElement GetImgChild(const blink::WebElement& element) {
58   // This implementation is incomplete (for example if is an area tag) but
59   // matches the original WebViewClassic implementation.
60
61   blink::WebElementCollection collection = element.getElementsByTagName("img");
62   DCHECK(!collection.isNull());
63   return collection.firstItem();
64 }
65
66 bool RemovePrefixAndAssignIfMatches(const base::StringPiece& prefix,
67                                     const GURL& url,
68                                     std::string* dest) {
69   const base::StringPiece spec(url.possibly_invalid_spec());
70
71   if (spec.starts_with(prefix)) {
72     url_canon::RawCanonOutputW<1024> output;
73     url_util::DecodeURLEscapeSequences(spec.data() + prefix.length(),
74         spec.length() - prefix.length(), &output);
75     std::string decoded_url = base::UTF16ToUTF8(
76         base::string16(output.data(), output.length()));
77     dest->assign(decoded_url.begin(), decoded_url.end());
78     return true;
79   }
80   return false;
81 }
82
83 void DistinguishAndAssignSrcLinkType(const GURL& url, AwHitTestData* data) {
84   if (RemovePrefixAndAssignIfMatches(
85       content::kAddressPrefix,
86       url,
87       &data->extra_data_for_type)) {
88     data->type = AwHitTestData::GEO_TYPE;
89   } else if (RemovePrefixAndAssignIfMatches(
90       content::kPhoneNumberPrefix,
91       url,
92       &data->extra_data_for_type)) {
93     data->type = AwHitTestData::PHONE_TYPE;
94   } else if (RemovePrefixAndAssignIfMatches(
95       content::kEmailPrefix,
96       url,
97       &data->extra_data_for_type)) {
98     data->type = AwHitTestData::EMAIL_TYPE;
99   } else {
100     data->type = AwHitTestData::SRC_LINK_TYPE;
101     data->extra_data_for_type = url.possibly_invalid_spec();
102     if (!data->extra_data_for_type.empty())
103       data->href = base::UTF8ToUTF16(data->extra_data_for_type);
104   }
105 }
106
107 void PopulateHitTestData(const GURL& absolute_link_url,
108                          const GURL& absolute_image_url,
109                          bool is_editable,
110                          AwHitTestData* data) {
111   // Note: Using GURL::is_empty instead of GURL:is_valid due to the
112   // WebViewClassic allowing any kind of protocol which GURL::is_valid
113   // disallows. Similar reasons for using GURL::possibly_invalid_spec instead of
114   // GURL::spec.
115   if (!absolute_image_url.is_empty())
116     data->img_src = absolute_image_url;
117
118   const bool is_javascript_scheme =
119       absolute_link_url.SchemeIs(content::kJavaScriptScheme);
120   const bool has_link_url = !absolute_link_url.is_empty();
121   const bool has_image_url = !absolute_image_url.is_empty();
122
123   if (has_link_url && !has_image_url && !is_javascript_scheme) {
124     DistinguishAndAssignSrcLinkType(absolute_link_url, data);
125   } else if (has_link_url && has_image_url && !is_javascript_scheme) {
126     data->type = AwHitTestData::SRC_IMAGE_LINK_TYPE;
127     data->extra_data_for_type = data->img_src.possibly_invalid_spec();
128     if (absolute_link_url.is_valid())
129       data->href = base::UTF8ToUTF16(absolute_link_url.possibly_invalid_spec());
130   } else if (!has_link_url && has_image_url) {
131     data->type = AwHitTestData::IMAGE_TYPE;
132     data->extra_data_for_type = data->img_src.possibly_invalid_spec();
133   } else if (is_editable) {
134     data->type = AwHitTestData::EDIT_TEXT_TYPE;
135     DCHECK(data->extra_data_for_type.length() == 0);
136   }
137 }
138
139 }  // namespace
140
141 AwRenderViewExt::AwRenderViewExt(content::RenderView* render_view)
142     : content::RenderViewObserver(render_view), page_scale_factor_(0.0f) {
143 }
144
145 AwRenderViewExt::~AwRenderViewExt() {
146 }
147
148 // static
149 void AwRenderViewExt::RenderViewCreated(content::RenderView* render_view) {
150   new AwRenderViewExt(render_view);  // |render_view| takes ownership.
151 }
152
153 bool AwRenderViewExt::OnMessageReceived(const IPC::Message& message) {
154   bool handled = true;
155   IPC_BEGIN_MESSAGE_MAP(AwRenderViewExt, message)
156     IPC_MESSAGE_HANDLER(AwViewMsg_DocumentHasImages, OnDocumentHasImagesRequest)
157     IPC_MESSAGE_HANDLER(AwViewMsg_DoHitTest, OnDoHitTest)
158     IPC_MESSAGE_HANDLER(AwViewMsg_SetTextZoomFactor, OnSetTextZoomFactor)
159     IPC_MESSAGE_HANDLER(AwViewMsg_ResetScrollAndScaleState,
160                         OnResetScrollAndScaleState)
161     IPC_MESSAGE_HANDLER(AwViewMsg_SetInitialPageScale, OnSetInitialPageScale)
162     IPC_MESSAGE_HANDLER(AwViewMsg_SetFixedLayoutSize, OnSetFixedLayoutSize)
163     IPC_MESSAGE_HANDLER(AwViewMsg_SetBackgroundColor, OnSetBackgroundColor)
164     IPC_MESSAGE_UNHANDLED(handled = false)
165   IPC_END_MESSAGE_MAP()
166   return handled;
167 }
168
169 void AwRenderViewExt::OnDocumentHasImagesRequest(int id) {
170   bool hasImages = false;
171   if (render_view()) {
172     blink::WebView* webview = render_view()->GetWebView();
173     if (webview) {
174       blink::WebVector<blink::WebElement> images;
175       webview->mainFrame()->document().images(images);
176       hasImages = !images.isEmpty();
177     }
178   }
179   Send(new AwViewHostMsg_DocumentHasImagesResponse(routing_id(), id,
180                                                    hasImages));
181 }
182
183 void AwRenderViewExt::DidCommitProvisionalLoad(blink::WebFrame* frame,
184                                                bool is_new_navigation) {
185   content::DocumentState* document_state =
186       content::DocumentState::FromDataSource(frame->dataSource());
187   if (document_state->can_load_local_resources()) {
188     blink::WebSecurityOrigin origin = frame->document().securityOrigin();
189     origin.grantLoadLocalResources();
190   }
191 }
192
193 void AwRenderViewExt::DidCommitCompositorFrame() {
194   UpdatePageScaleFactor();
195 }
196
197 void AwRenderViewExt::DidUpdateLayout() {
198   if (check_contents_size_timer_.IsRunning())
199     return;
200
201   check_contents_size_timer_.Start(FROM_HERE,
202                                    base::TimeDelta::FromMilliseconds(0), this,
203                                    &AwRenderViewExt::CheckContentsSize);
204 }
205
206 void AwRenderViewExt::UpdatePageScaleFactor() {
207   if (page_scale_factor_ != render_view()->GetWebView()->pageScaleFactor()) {
208     page_scale_factor_ = render_view()->GetWebView()->pageScaleFactor();
209     Send(new AwViewHostMsg_PageScaleFactorChanged(routing_id(),
210                                                   page_scale_factor_));
211   }
212 }
213
214 void AwRenderViewExt::CheckContentsSize() {
215   if (!render_view()->GetWebView())
216     return;
217
218   gfx::Size contents_size;
219
220   blink::WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
221   if (main_frame)
222     contents_size = main_frame->contentsSize();
223
224   // Fall back to contentsPreferredMinimumSize if the mainFrame is reporting a
225   // 0x0 size (this happens during initial load).
226   if (contents_size.IsEmpty()) {
227     contents_size = render_view()->GetWebView()->contentsPreferredMinimumSize();
228   }
229
230   if (contents_size == last_sent_contents_size_)
231     return;
232
233   last_sent_contents_size_ = contents_size;
234   Send(new AwViewHostMsg_OnContentsSizeChanged(routing_id(), contents_size));
235 }
236
237 void AwRenderViewExt::Navigate(const GURL& url) {
238   // Navigate is called only on NEW navigations, so WebImageCache won't be freed
239   // when the user just clicks on links, but only when a navigation is started,
240   // for instance via loadUrl. A better approach would be clearing the cache on
241   // cross-site boundaries, however this would require too many changes both on
242   // the browser side (in RenderViewHostManger), to the IPCmessages and to the
243   // RenderViewObserver. Thus, clearing decoding image cache on Navigate, seems
244   // a more acceptable compromise.
245   blink::WebImageCache::clear();
246 }
247
248 void AwRenderViewExt::FocusedNodeChanged(const blink::WebNode& node) {
249   if (node.isNull() || !node.isElementNode() || !render_view())
250     return;
251
252   // Note: element is not const due to innerText() is not const.
253   blink::WebElement element = node.toConst<blink::WebElement>();
254   AwHitTestData data;
255
256   data.href = GetHref(element);
257   data.anchor_text = element.innerText();
258
259   GURL absolute_link_url;
260   if (node.isLink())
261     absolute_link_url = GetAbsoluteUrl(node, data.href);
262
263   GURL absolute_image_url;
264   const blink::WebElement child_img = GetImgChild(element);
265   if (!child_img.isNull()) {
266     absolute_image_url =
267         GetAbsoluteSrcUrl(child_img);
268   }
269
270   PopulateHitTestData(absolute_link_url,
271                       absolute_image_url,
272                       render_view()->IsEditableNode(node),
273                       &data);
274   Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
275 }
276
277 void AwRenderViewExt::OnDoHitTest(int view_x, int view_y) {
278   if (!render_view() || !render_view()->GetWebView())
279     return;
280
281   const blink::WebHitTestResult result =
282       render_view()->GetWebView()->hitTestResultAt(
283           blink::WebPoint(view_x, view_y));
284   AwHitTestData data;
285
286   if (!result.urlElement().isNull()) {
287     data.anchor_text = result.urlElement().innerText();
288     data.href = GetHref(result.urlElement());
289   }
290
291   PopulateHitTestData(result.absoluteLinkURL(),
292                       result.absoluteImageURL(),
293                       result.isContentEditable(),
294                       &data);
295   Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
296 }
297
298 void AwRenderViewExt::OnSetTextZoomFactor(float zoom_factor) {
299   if (!render_view() || !render_view()->GetWebView())
300     return;
301   // Hide selection and autofill popups.
302   render_view()->GetWebView()->hidePopups();
303   render_view()->GetWebView()->setTextZoomFactor(zoom_factor);
304 }
305
306 void AwRenderViewExt::OnResetScrollAndScaleState() {
307   if (!render_view() || !render_view()->GetWebView())
308     return;
309   render_view()->GetWebView()->resetScrollAndScaleState();
310 }
311
312 void 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
319 void AwRenderViewExt::OnSetFixedLayoutSize(const gfx::Size& size) {
320   if (!render_view() || !render_view()->GetWebView())
321     return;
322   render_view()->GetWebView()->setFixedLayoutSize(size);
323 }
324
325 void AwRenderViewExt::OnSetBackgroundColor(SkColor c) {
326   if (!render_view() || !render_view()->GetWebView())
327     return;
328   render_view()->GetWebView()->setBaseBackgroundColor(c);
329 }
330
331 }  // namespace android_webview