Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / android_webview / renderer / aw_render_view_ext.cc
blobc2877c59f0f2ac81af1215515e3a4cfae5817c25
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.
5 #include "android_webview/renderer/aw_render_view_ext.h"
7 #include <string>
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/renderer/android_content_detection_prefixes.h"
16 #include "content/public/renderer/document_state.h"
17 #include "content/public/renderer/render_view.h"
18 #include "skia/ext/refptr.h"
19 #include "third_party/WebKit/public/platform/WebSize.h"
20 #include "third_party/WebKit/public/platform/WebURL.h"
21 #include "third_party/WebKit/public/platform/WebVector.h"
22 #include "third_party/WebKit/public/web/WebDataSource.h"
23 #include "third_party/WebKit/public/web/WebDocument.h"
24 #include "third_party/WebKit/public/web/WebElement.h"
25 #include "third_party/WebKit/public/web/WebElementCollection.h"
26 #include "third_party/WebKit/public/web/WebHitTestResult.h"
27 #include "third_party/WebKit/public/web/WebImageCache.h"
28 #include "third_party/WebKit/public/web/WebLocalFrame.h"
29 #include "third_party/WebKit/public/web/WebNode.h"
30 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
31 #include "third_party/WebKit/public/web/WebView.h"
32 #include "url/url_canon.h"
33 #include "url/url_constants.h"
34 #include "url/url_util.h"
36 namespace android_webview {
38 namespace {
40 GURL GetAbsoluteUrl(const blink::WebNode& node,
41 const base::string16& url_fragment) {
42 return GURL(node.document().completeURL(url_fragment));
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");
51 GURL GetAbsoluteSrcUrl(const blink::WebElement& element) {
52 if (element.isNull())
53 return GURL();
54 return GetAbsoluteUrl(element, element.getAttribute("src"));
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.
61 blink::WebElementCollection collection =
62 element.getElementsByHTMLTagName("img");
63 DCHECK(!collection.isNull());
64 return collection.firstItem();
67 GURL GetChildImageUrlFromElement(const blink::WebElement& element) {
68 const blink::WebElement child_img = GetImgChild(element);
69 if (child_img.isNull())
70 return GURL();
71 return GetAbsoluteSrcUrl(child_img);
74 bool RemovePrefixAndAssignIfMatches(const base::StringPiece& prefix,
75 const GURL& url,
76 std::string* dest) {
77 const base::StringPiece spec(url.possibly_invalid_spec());
79 if (spec.starts_with(prefix)) {
80 url::RawCanonOutputW<1024> output;
81 url::DecodeURLEscapeSequences(spec.data() + prefix.length(),
82 spec.length() - prefix.length(),
83 &output);
84 std::string decoded_url = base::UTF16ToUTF8(
85 base::string16(output.data(), output.length()));
86 dest->assign(decoded_url.begin(), decoded_url.end());
87 return true;
89 return false;
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 = base::UTF8ToUTF16(data->extra_data_for_type);
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;
127 const bool is_javascript_scheme =
128 absolute_link_url.SchemeIs(url::kJavaScriptScheme);
129 const bool has_link_url = !absolute_link_url.is_empty();
130 const bool has_image_url = !absolute_image_url.is_empty();
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 = base::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_EQ(0u, data->extra_data_for_type.length());
148 } // namespace
150 AwRenderViewExt::AwRenderViewExt(content::RenderView* render_view)
151 : content::RenderViewObserver(render_view), page_scale_factor_(0.0f) {
154 AwRenderViewExt::~AwRenderViewExt() {
157 // static
158 void AwRenderViewExt::RenderViewCreated(content::RenderView* render_view) {
159 new AwRenderViewExt(render_view); // |render_view| takes ownership.
162 bool AwRenderViewExt::OnMessageReceived(const IPC::Message& message) {
163 bool handled = true;
164 IPC_BEGIN_MESSAGE_MAP(AwRenderViewExt, message)
165 IPC_MESSAGE_HANDLER(AwViewMsg_DocumentHasImages, OnDocumentHasImagesRequest)
166 IPC_MESSAGE_HANDLER(AwViewMsg_DoHitTest, OnDoHitTest)
167 IPC_MESSAGE_HANDLER(AwViewMsg_SetTextZoomFactor, OnSetTextZoomFactor)
168 IPC_MESSAGE_HANDLER(AwViewMsg_ResetScrollAndScaleState,
169 OnResetScrollAndScaleState)
170 IPC_MESSAGE_HANDLER(AwViewMsg_SetInitialPageScale, OnSetInitialPageScale)
171 IPC_MESSAGE_HANDLER(AwViewMsg_SetBackgroundColor, OnSetBackgroundColor)
172 IPC_MESSAGE_UNHANDLED(handled = false)
173 IPC_END_MESSAGE_MAP()
174 return handled;
177 void AwRenderViewExt::OnDocumentHasImagesRequest(int id) {
178 bool hasImages = false;
179 if (render_view()) {
180 blink::WebView* webview = render_view()->GetWebView();
181 if (webview) {
182 blink::WebVector<blink::WebElement> images;
183 webview->mainFrame()->document().images(images);
184 hasImages = !images.isEmpty();
187 Send(new AwViewHostMsg_DocumentHasImagesResponse(routing_id(), id,
188 hasImages));
191 void AwRenderViewExt::DidCommitCompositorFrame() {
192 UpdatePageScaleFactor();
195 void AwRenderViewExt::DidUpdateLayout() {
196 if (check_contents_size_timer_.IsRunning())
197 return;
199 check_contents_size_timer_.Start(FROM_HERE,
200 base::TimeDelta::FromMilliseconds(0), this,
201 &AwRenderViewExt::CheckContentsSize);
204 void AwRenderViewExt::UpdatePageScaleFactor() {
205 if (page_scale_factor_ != render_view()->GetWebView()->pageScaleFactor()) {
206 page_scale_factor_ = render_view()->GetWebView()->pageScaleFactor();
207 Send(new AwViewHostMsg_PageScaleFactorChanged(routing_id(),
208 page_scale_factor_));
212 void AwRenderViewExt::CheckContentsSize() {
213 if (!render_view()->GetWebView())
214 return;
216 gfx::Size contents_size;
218 blink::WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
219 if (main_frame)
220 contents_size = main_frame->contentsSize();
222 // Fall back to contentsPreferredMinimumSize if the mainFrame is reporting a
223 // 0x0 size (this happens during initial load).
224 if (contents_size.IsEmpty()) {
225 contents_size = render_view()->GetWebView()->contentsPreferredMinimumSize();
228 if (contents_size == last_sent_contents_size_)
229 return;
231 last_sent_contents_size_ = contents_size;
232 Send(new AwViewHostMsg_OnContentsSizeChanged(routing_id(), contents_size));
235 void AwRenderViewExt::Navigate(const GURL& url) {
236 // Navigate is called only on NEW navigations, so WebImageCache won't be freed
237 // when the user just clicks on links, but only when a navigation is started,
238 // for instance via loadUrl. A better approach would be clearing the cache on
239 // cross-site boundaries, however this would require too many changes both on
240 // the browser side (in RenderViewHostManger), to the IPCmessages and to the
241 // RenderViewObserver. Thus, clearing decoding image cache on Navigate, seems
242 // a more acceptable compromise.
243 blink::WebImageCache::clear();
246 void AwRenderViewExt::FocusedNodeChanged(const blink::WebNode& node) {
247 if (node.isNull() || !node.isElementNode() || !render_view())
248 return;
250 // Note: element is not const due to innerText() is not const.
251 blink::WebElement element = node.toConst<blink::WebElement>();
252 AwHitTestData data;
254 data.href = GetHref(element);
255 data.anchor_text = element.innerText();
257 GURL absolute_link_url;
258 if (node.isLink())
259 absolute_link_url = GetAbsoluteUrl(node, data.href);
261 GURL absolute_image_url = GetChildImageUrlFromElement(element);
263 PopulateHitTestData(absolute_link_url,
264 absolute_image_url,
265 render_view()->IsEditableNode(node),
266 &data);
267 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
270 void AwRenderViewExt::OnDoHitTest(const gfx::PointF& touch_center,
271 const gfx::SizeF& touch_area) {
272 if (!render_view() || !render_view()->GetWebView())
273 return;
275 const blink::WebHitTestResult result =
276 render_view()->GetWebView()->hitTestResultForTap(
277 blink::WebPoint(touch_center.x(), touch_center.y()),
278 blink::WebSize(touch_area.width(), touch_area.height()));
279 AwHitTestData data;
281 GURL absolute_image_url = result.absoluteImageURL();
282 if (!result.urlElement().isNull()) {
283 data.anchor_text = result.urlElement().innerText();
284 data.href = GetHref(result.urlElement());
285 // If we hit an image that failed to load, Blink won't give us its URL.
286 // Fall back to walking the DOM in this case.
287 if (absolute_image_url.is_empty())
288 absolute_image_url = GetChildImageUrlFromElement(result.urlElement());
291 PopulateHitTestData(result.absoluteLinkURL(),
292 absolute_image_url,
293 result.isContentEditable(),
294 &data);
295 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
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);
306 void AwRenderViewExt::OnResetScrollAndScaleState() {
307 if (!render_view() || !render_view()->GetWebView())
308 return;
309 render_view()->GetWebView()->resetScrollAndScaleState();
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);
319 void AwRenderViewExt::OnSetBackgroundColor(SkColor c) {
320 if (!render_view() || !render_view()->GetWebView())
321 return;
322 render_view()->GetWebView()->setBaseBackgroundColor(c);
325 } // namespace android_webview