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
AllowMixedContent(const blink::WebURL & url)40 bool AllowMixedContent(const blink::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
GetAbsoluteUrl(const blink::WebNode & node,const string16 & url_fragment)49 GURL GetAbsoluteUrl(const blink::WebNode& node, const string16& url_fragment) {
50 return GURL(node.document().completeURL(url_fragment));
51 }
52
GetHref(const blink::WebElement & element)53 string16 GetHref(const blink::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
GetAbsoluteSrcUrl(const blink::WebElement & element)59 GURL GetAbsoluteSrcUrl(const blink::WebElement& element) {
60 if (element.isNull())
61 return GURL();
62 return GetAbsoluteUrl(element, element.getAttribute("src"));
63 }
64
GetImgChild(const blink::WebNode & node)65 blink::WebNode GetImgChild(const blink::WebNode& node) {
66 // This implementation is incomplete (for example if is an area tag) but
67 // matches the original WebViewClassic implementation.
68
69 blink::WebNodeList list = node.getElementsByTagName("img");
70 if (list.length() > 0)
71 return list.item(0);
72 return blink::WebNode();
73 }
74
RemovePrefixAndAssignIfMatches(const base::StringPiece & prefix,const GURL & url,std::string * dest)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
DistinguishAndAssignSrcLinkType(const GURL & url,AwHitTestData * data)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 if (!data->extra_data_for_type.empty())
112 data->href = UTF8ToUTF16(data->extra_data_for_type);
113 }
114 }
115
PopulateHitTestData(const GURL & absolute_link_url,const GURL & absolute_image_url,bool is_editable,AwHitTestData * data)116 void PopulateHitTestData(const GURL& absolute_link_url,
117 const GURL& absolute_image_url,
118 bool is_editable,
119 AwHitTestData* data) {
120 // Note: Using GURL::is_empty instead of GURL:is_valid due to the
121 // WebViewClassic allowing any kind of protocol which GURL::is_valid
122 // disallows. Similar reasons for using GURL::possibly_invalid_spec instead of
123 // GURL::spec.
124 if (!absolute_image_url.is_empty())
125 data->img_src = absolute_image_url;
126
127 const bool is_javascript_scheme =
128 absolute_link_url.SchemeIs(content::kJavaScriptScheme);
129 const bool has_link_url = !absolute_link_url.is_empty();
130 const bool has_image_url = !absolute_image_url.is_empty();
131
132 if (has_link_url && !has_image_url && !is_javascript_scheme) {
133 DistinguishAndAssignSrcLinkType(absolute_link_url, data);
134 } else if (has_link_url && has_image_url && !is_javascript_scheme) {
135 data->type = AwHitTestData::SRC_IMAGE_LINK_TYPE;
136 data->extra_data_for_type = data->img_src.possibly_invalid_spec();
137 if (absolute_link_url.is_valid())
138 data->href = UTF8ToUTF16(absolute_link_url.possibly_invalid_spec());
139 } else if (!has_link_url && has_image_url) {
140 data->type = AwHitTestData::IMAGE_TYPE;
141 data->extra_data_for_type = data->img_src.possibly_invalid_spec();
142 } else if (is_editable) {
143 data->type = AwHitTestData::EDIT_TEXT_TYPE;
144 DCHECK(data->extra_data_for_type.length() == 0);
145 }
146 }
147
148 } // namespace
149
AwRenderViewExt(content::RenderView * render_view)150 AwRenderViewExt::AwRenderViewExt(content::RenderView* render_view)
151 : content::RenderViewObserver(render_view), page_scale_factor_(0.0f) {
152 render_view->GetWebView()->setPermissionClient(this);
153 }
154
~AwRenderViewExt()155 AwRenderViewExt::~AwRenderViewExt() {
156 }
157
158 // static
RenderViewCreated(content::RenderView * render_view)159 void AwRenderViewExt::RenderViewCreated(content::RenderView* render_view) {
160 new AwRenderViewExt(render_view); // |render_view| takes ownership.
161 }
162
OnMessageReceived(const IPC::Message & message)163 bool AwRenderViewExt::OnMessageReceived(const IPC::Message& message) {
164 bool handled = true;
165 IPC_BEGIN_MESSAGE_MAP(AwRenderViewExt, message)
166 IPC_MESSAGE_HANDLER(AwViewMsg_DocumentHasImages, OnDocumentHasImagesRequest)
167 IPC_MESSAGE_HANDLER(AwViewMsg_DoHitTest, OnDoHitTest)
168 IPC_MESSAGE_HANDLER(AwViewMsg_SetTextZoomFactor, OnSetTextZoomFactor)
169 IPC_MESSAGE_HANDLER(AwViewMsg_ResetScrollAndScaleState,
170 OnResetScrollAndScaleState)
171 IPC_MESSAGE_HANDLER(AwViewMsg_SetInitialPageScale, OnSetInitialPageScale)
172 IPC_MESSAGE_HANDLER(AwViewMsg_SetFixedLayoutSize, OnSetFixedLayoutSize)
173 IPC_MESSAGE_HANDLER(AwViewMsg_SetBackgroundColor, OnSetBackgroundColor)
174 IPC_MESSAGE_UNHANDLED(handled = false)
175 IPC_END_MESSAGE_MAP()
176 return handled;
177 }
178
OnDocumentHasImagesRequest(int id)179 void AwRenderViewExt::OnDocumentHasImagesRequest(int id) {
180 bool hasImages = false;
181 if (render_view()) {
182 blink::WebView* webview = render_view()->GetWebView();
183 if (webview) {
184 blink::WebVector<blink::WebElement> images;
185 webview->mainFrame()->document().images(images);
186 hasImages = !images.isEmpty();
187 }
188 }
189 Send(new AwViewHostMsg_DocumentHasImagesResponse(routing_id(), id,
190 hasImages));
191 }
192
allowDisplayingInsecureContent(blink::WebFrame * frame,bool enabled_per_settings,const blink::WebSecurityOrigin & origin,const blink::WebURL & url)193 bool AwRenderViewExt::allowDisplayingInsecureContent(
194 blink::WebFrame* frame,
195 bool enabled_per_settings,
196 const blink::WebSecurityOrigin& origin,
197 const blink::WebURL& url) {
198 return enabled_per_settings ? true : AllowMixedContent(url);
199 }
200
allowRunningInsecureContent(blink::WebFrame * frame,bool enabled_per_settings,const blink::WebSecurityOrigin & origin,const blink::WebURL & url)201 bool AwRenderViewExt::allowRunningInsecureContent(
202 blink::WebFrame* frame,
203 bool enabled_per_settings,
204 const blink::WebSecurityOrigin& origin,
205 const blink::WebURL& url) {
206 return enabled_per_settings ? true : AllowMixedContent(url);
207 }
208
DidCommitProvisionalLoad(blink::WebFrame * frame,bool is_new_navigation)209 void AwRenderViewExt::DidCommitProvisionalLoad(blink::WebFrame* frame,
210 bool is_new_navigation) {
211 content::DocumentState* document_state =
212 content::DocumentState::FromDataSource(frame->dataSource());
213 if (document_state->can_load_local_resources()) {
214 blink::WebSecurityOrigin origin = frame->document().securityOrigin();
215 origin.grantLoadLocalResources();
216 }
217 }
218
DidCommitCompositorFrame()219 void AwRenderViewExt::DidCommitCompositorFrame() {
220 UpdatePageScaleFactor();
221 }
222
DidUpdateLayout()223 void AwRenderViewExt::DidUpdateLayout() {
224 if (check_contents_size_timer_.IsRunning())
225 return;
226
227 check_contents_size_timer_.Start(FROM_HERE,
228 base::TimeDelta::FromMilliseconds(0), this,
229 &AwRenderViewExt::CheckContentsSize);
230 }
231
UpdatePageScaleFactor()232 void AwRenderViewExt::UpdatePageScaleFactor() {
233 if (page_scale_factor_ != render_view()->GetWebView()->pageScaleFactor()) {
234 page_scale_factor_ = render_view()->GetWebView()->pageScaleFactor();
235 Send(new AwViewHostMsg_PageScaleFactorChanged(routing_id(),
236 page_scale_factor_));
237 }
238 }
239
CheckContentsSize()240 void AwRenderViewExt::CheckContentsSize() {
241 if (!render_view()->GetWebView())
242 return;
243
244 gfx::Size contents_size;
245
246 blink::WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
247 if (main_frame)
248 contents_size = main_frame->contentsSize();
249
250 // Fall back to contentsPreferredMinimumSize if the mainFrame is reporting a
251 // 0x0 size (this happens during initial load).
252 if (contents_size.IsEmpty()) {
253 contents_size = render_view()->GetWebView()->contentsPreferredMinimumSize();
254 }
255
256 if (contents_size == last_sent_contents_size_)
257 return;
258
259 last_sent_contents_size_ = contents_size;
260 Send(new AwViewHostMsg_OnContentsSizeChanged(routing_id(), contents_size));
261 }
262
Navigate(const GURL & url)263 void AwRenderViewExt::Navigate(const GURL& url) {
264 // Navigate is called only on NEW navigations, so WebImageCache won't be freed
265 // when the user just clicks on links, but only when a navigation is started,
266 // for instance via loadUrl. A better approach would be clearing the cache on
267 // cross-site boundaries, however this would require too many changes both on
268 // the browser side (in RenderViewHostManger), to the IPCmessages and to the
269 // RenderViewObserver. Thus, clearing decoding image cache on Navigate, seems
270 // a more acceptable compromise.
271 blink::WebImageCache::clear();
272 }
273
FocusedNodeChanged(const blink::WebNode & node)274 void AwRenderViewExt::FocusedNodeChanged(const blink::WebNode& node) {
275 if (node.isNull() || !node.isElementNode() || !render_view())
276 return;
277
278 // Note: element is not const due to innerText() is not const.
279 blink::WebElement element = node.toConst<blink::WebElement>();
280 AwHitTestData data;
281
282 data.href = GetHref(element);
283 data.anchor_text = element.innerText();
284
285 GURL absolute_link_url;
286 if (node.isLink())
287 absolute_link_url = GetAbsoluteUrl(node, data.href);
288
289 GURL absolute_image_url;
290 const blink::WebNode child_img = GetImgChild(node);
291 if (!child_img.isNull() && child_img.isElementNode()) {
292 absolute_image_url =
293 GetAbsoluteSrcUrl(child_img.toConst<blink::WebElement>());
294 }
295
296 PopulateHitTestData(absolute_link_url,
297 absolute_image_url,
298 render_view()->IsEditableNode(node),
299 &data);
300 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
301 }
302
OnDoHitTest(int view_x,int view_y)303 void AwRenderViewExt::OnDoHitTest(int view_x, int view_y) {
304 if (!render_view() || !render_view()->GetWebView())
305 return;
306
307 const blink::WebHitTestResult result =
308 render_view()->GetWebView()->hitTestResultAt(
309 blink::WebPoint(view_x, view_y));
310 AwHitTestData data;
311
312 if (!result.urlElement().isNull()) {
313 data.anchor_text = result.urlElement().innerText();
314 data.href = GetHref(result.urlElement());
315 }
316
317 PopulateHitTestData(result.absoluteLinkURL(),
318 result.absoluteImageURL(),
319 result.isContentEditable(),
320 &data);
321 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
322 }
323
OnSetTextZoomFactor(float zoom_factor)324 void AwRenderViewExt::OnSetTextZoomFactor(float zoom_factor) {
325 if (!render_view() || !render_view()->GetWebView())
326 return;
327 // Hide selection and autofill popups.
328 render_view()->GetWebView()->hidePopups();
329 render_view()->GetWebView()->setTextZoomFactor(zoom_factor);
330 }
331
OnResetScrollAndScaleState()332 void AwRenderViewExt::OnResetScrollAndScaleState() {
333 if (!render_view() || !render_view()->GetWebView())
334 return;
335 render_view()->GetWebView()->resetScrollAndScaleState();
336 }
337
OnSetInitialPageScale(double page_scale_factor)338 void AwRenderViewExt::OnSetInitialPageScale(double page_scale_factor) {
339 if (!render_view() || !render_view()->GetWebView())
340 return;
341 render_view()->GetWebView()->setInitialPageScaleOverride(
342 page_scale_factor);
343 }
344
OnSetFixedLayoutSize(const gfx::Size & size)345 void AwRenderViewExt::OnSetFixedLayoutSize(const gfx::Size& size) {
346 if (!render_view() || !render_view()->GetWebView())
347 return;
348 render_view()->GetWebView()->setFixedLayoutSize(size);
349 }
350
OnSetBackgroundColor(SkColor c)351 void AwRenderViewExt::OnSetBackgroundColor(SkColor c) {
352 if (!render_view() || !render_view()->GetWebView())
353 return;
354 render_view()->GetWebView()->setBaseBackgroundColor(c);
355 }
356
357 } // namespace android_webview
358