- add sources.
[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/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"
35
36 namespace android_webview {
37
38 namespace {
39
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.
45   GURL gurl(url);
46   return !gurl.IsStandard();
47 }
48
49 GURL GetAbsoluteUrl(const WebKit::WebNode& node, const string16& url_fragment) {
50   return GURL(node.document().completeURL(url_fragment));
51 }
52
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");
57 }
58
59 GURL GetAbsoluteSrcUrl(const WebKit::WebElement& element) {
60   if (element.isNull())
61     return GURL();
62   return GetAbsoluteUrl(element, element.getAttribute("src"));
63 }
64
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.
68
69   WebKit::WebNodeList list = node.getElementsByTagName("img");
70   if (list.length() > 0)
71     return list.item(0);
72   return WebKit::WebNode();
73 }
74
75 bool RemovePrefixAndAssignIfMatches(const base::StringPiece& prefix,
76                                     const GURL& url,
77                                     std::string* dest) {
78   const base::StringPiece spec(url.possibly_invalid_spec());
79
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());
87     return true;
88   }
89   return false;
90 }
91
92 void DistinguishAndAssignSrcLinkType(const GURL& url, AwHitTestData* data) {
93   if (RemovePrefixAndAssignIfMatches(
94       content::kAddressPrefix,
95       url,
96       &data->extra_data_for_type)) {
97     data->type = AwHitTestData::GEO_TYPE;
98   } else if (RemovePrefixAndAssignIfMatches(
99       content::kPhoneNumberPrefix,
100       url,
101       &data->extra_data_for_type)) {
102     data->type = AwHitTestData::PHONE_TYPE;
103   } else if (RemovePrefixAndAssignIfMatches(
104       content::kEmailPrefix,
105       url,
106       &data->extra_data_for_type)) {
107     data->type = AwHitTestData::EMAIL_TYPE;
108   } else {
109     data->type = AwHitTestData::SRC_LINK_TYPE;
110     data->extra_data_for_type = url.possibly_invalid_spec();
111   }
112 }
113
114 void PopulateHitTestData(const GURL& absolute_link_url,
115                          const GURL& absolute_image_url,
116                          bool is_editable,
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
121   // GURL::spec.
122   if (!absolute_image_url.is_empty())
123     data->img_src = absolute_image_url;
124
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();
129
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);
141   }
142 }
143
144 }  // namespace
145
146 AwRenderViewExt::AwRenderViewExt(content::RenderView* render_view)
147     : content::RenderViewObserver(render_view), page_scale_factor_(0.0f) {
148   render_view->GetWebView()->setPermissionClient(this);
149 }
150
151 AwRenderViewExt::~AwRenderViewExt() {
152 }
153
154 // static
155 void AwRenderViewExt::RenderViewCreated(content::RenderView* render_view) {
156   new AwRenderViewExt(render_view);  // |render_view| takes ownership.
157 }
158
159 bool AwRenderViewExt::OnMessageReceived(const IPC::Message& message) {
160   bool handled = true;
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()
172   return handled;
173 }
174
175 void AwRenderViewExt::OnDocumentHasImagesRequest(int id) {
176   bool hasImages = false;
177   if (render_view()) {
178     WebKit::WebView* webview = render_view()->GetWebView();
179     if (webview) {
180       WebKit::WebVector<WebKit::WebElement> images;
181       webview->mainFrame()->document().images(images);
182       hasImages = !images.isEmpty();
183     }
184   }
185   Send(new AwViewHostMsg_DocumentHasImagesResponse(routing_id(), id,
186                                                    hasImages));
187 }
188
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);
195 }
196
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);
203 }
204
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();
212   }
213 }
214
215 void AwRenderViewExt::DidCommitCompositorFrame() {
216   UpdatePageScaleFactor();
217 }
218
219 void AwRenderViewExt::DidUpdateLayout() {
220   if (check_contents_size_timer_.IsRunning())
221     return;
222
223   check_contents_size_timer_.Start(FROM_HERE,
224                                    base::TimeDelta::FromMilliseconds(0), this,
225                                    &AwRenderViewExt::CheckContentsSize);
226 }
227
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_));
233   }
234 }
235
236 void AwRenderViewExt::CheckContentsSize() {
237   if (!render_view()->GetWebView())
238     return;
239
240   gfx::Size contents_size;
241
242   WebKit::WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
243   if (main_frame)
244     contents_size = main_frame->contentsSize();
245
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();
250   }
251
252   if (contents_size == last_sent_contents_size_)
253     return;
254
255   last_sent_contents_size_ = contents_size;
256   Send(new AwViewHostMsg_OnContentsSizeChanged(routing_id(), contents_size));
257 }
258
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();
268 }
269
270 void AwRenderViewExt::FocusedNodeChanged(const WebKit::WebNode& node) {
271   if (node.isNull() || !node.isElementNode() || !render_view())
272     return;
273
274   // Note: element is not const due to innerText() is not const.
275   WebKit::WebElement element = node.toConst<WebKit::WebElement>();
276   AwHitTestData data;
277
278   data.href = GetHref(element);
279   data.anchor_text = element.innerText();
280
281   GURL absolute_link_url;
282   if (node.isLink())
283     absolute_link_url = GetAbsoluteUrl(node, data.href);
284
285   GURL absolute_image_url;
286   const WebKit::WebNode child_img = GetImgChild(node);
287   if (!child_img.isNull() && child_img.isElementNode()) {
288     absolute_image_url =
289         GetAbsoluteSrcUrl(child_img.toConst<WebKit::WebElement>());
290   }
291
292   PopulateHitTestData(absolute_link_url,
293                       absolute_image_url,
294                       render_view()->IsEditableNode(node),
295                       &data);
296   Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
297 }
298
299 void AwRenderViewExt::OnDoHitTest(int view_x, int view_y) {
300   if (!render_view() || !render_view()->GetWebView())
301     return;
302
303   const WebKit::WebHitTestResult result =
304       render_view()->GetWebView()->hitTestResultAt(
305           WebKit::WebPoint(view_x, view_y));
306   AwHitTestData data;
307
308   if (!result.urlElement().isNull()) {
309     data.anchor_text = result.urlElement().innerText();
310     data.href = GetHref(result.urlElement());
311   }
312
313   PopulateHitTestData(result.absoluteLinkURL(),
314                       result.absoluteImageURL(),
315                       result.isContentEditable(),
316                       &data);
317   Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
318 }
319
320 void AwRenderViewExt::OnSetTextZoomFactor(float zoom_factor) {
321   if (!render_view() || !render_view()->GetWebView())
322     return;
323   // Hide selection and autofill popups.
324   render_view()->GetWebView()->hidePopups();
325   render_view()->GetWebView()->setTextZoomFactor(zoom_factor);
326 }
327
328 void AwRenderViewExt::OnResetScrollAndScaleState() {
329   if (!render_view() || !render_view()->GetWebView())
330     return;
331   render_view()->GetWebView()->resetScrollAndScaleState();
332 }
333
334 void AwRenderViewExt::OnSetInitialPageScale(double page_scale_factor) {
335   if (!render_view() || !render_view()->GetWebView())
336     return;
337   render_view()->GetWebView()->setInitialPageScaleOverride(
338       page_scale_factor);
339 }
340
341 void AwRenderViewExt::OnSetFixedLayoutSize(const gfx::Size& size) {
342   if (!render_view() || !render_view()->GetWebView())
343     return;
344   render_view()->GetWebView()->setFixedLayoutSize(size);
345 }
346
347 void AwRenderViewExt::OnSetBackgroundColor(SkColor c) {
348   if (!render_view() || !render_view()->GetWebView())
349     return;
350   render_view()->GetWebView()->setBaseBackgroundColor(c);
351 }
352
353 }  // namespace android_webview