Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / android_webview / renderer / aw_render_view_ext.cc
blobac377253e24b3351b7f070fbfae5bff3e26ffbf6
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::WebNode& node) {
58 // This implementation is incomplete (for example if is an area tag) but
59 // matches the original WebViewClassic implementation.
61 blink::WebElementCollection collection = node.getElementsByHTMLTagName("img");
62 DCHECK(!collection.isNull());
63 return collection.firstItem();
66 GURL GetChildImageUrlFromElement(const blink::WebElement& element) {
67 const blink::WebElement child_img = GetImgChild(element);
68 if (child_img.isNull())
69 return GURL();
70 return GetAbsoluteSrcUrl(child_img);
73 bool RemovePrefixAndAssignIfMatches(const base::StringPiece& prefix,
74 const GURL& url,
75 std::string* dest) {
76 const base::StringPiece spec(url.possibly_invalid_spec());
78 if (spec.starts_with(prefix)) {
79 url::RawCanonOutputW<1024> output;
80 url::DecodeURLEscapeSequences(spec.data() + prefix.length(),
81 spec.length() - prefix.length(),
82 &output);
83 *dest = base::UTF16ToUTF8(
84 base::StringPiece16(output.data(), output.length()));
85 return true;
87 return false;
90 void DistinguishAndAssignSrcLinkType(const GURL& url, AwHitTestData* data) {
91 if (RemovePrefixAndAssignIfMatches(
92 content::kAddressPrefix,
93 url,
94 &data->extra_data_for_type)) {
95 data->type = AwHitTestData::GEO_TYPE;
96 } else if (RemovePrefixAndAssignIfMatches(
97 content::kPhoneNumberPrefix,
98 url,
99 &data->extra_data_for_type)) {
100 data->type = AwHitTestData::PHONE_TYPE;
101 } else if (RemovePrefixAndAssignIfMatches(
102 content::kEmailPrefix,
103 url,
104 &data->extra_data_for_type)) {
105 data->type = AwHitTestData::EMAIL_TYPE;
106 } else {
107 data->type = AwHitTestData::SRC_LINK_TYPE;
108 data->extra_data_for_type = url.possibly_invalid_spec();
109 if (!data->extra_data_for_type.empty())
110 data->href = base::UTF8ToUTF16(data->extra_data_for_type);
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;
125 const bool is_javascript_scheme =
126 absolute_link_url.SchemeIs(url::kJavaScriptScheme);
127 const bool has_link_url = !absolute_link_url.is_empty();
128 const bool has_image_url = !absolute_image_url.is_empty();
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 if (absolute_link_url.is_valid())
136 data->href = base::UTF8ToUTF16(absolute_link_url.possibly_invalid_spec());
137 } else if (!has_link_url && has_image_url) {
138 data->type = AwHitTestData::IMAGE_TYPE;
139 data->extra_data_for_type = data->img_src.possibly_invalid_spec();
140 } else if (is_editable) {
141 data->type = AwHitTestData::EDIT_TEXT_TYPE;
142 DCHECK_EQ(0u, data->extra_data_for_type.length());
146 } // namespace
148 AwRenderViewExt::AwRenderViewExt(content::RenderView* render_view)
149 : content::RenderViewObserver(render_view), page_scale_factor_(0.0f) {
152 AwRenderViewExt::~AwRenderViewExt() {
155 // static
156 void AwRenderViewExt::RenderViewCreated(content::RenderView* render_view) {
157 new AwRenderViewExt(render_view); // |render_view| takes ownership.
160 bool AwRenderViewExt::OnMessageReceived(const IPC::Message& message) {
161 bool handled = true;
162 IPC_BEGIN_MESSAGE_MAP(AwRenderViewExt, message)
163 IPC_MESSAGE_HANDLER(AwViewMsg_DocumentHasImages, OnDocumentHasImagesRequest)
164 IPC_MESSAGE_HANDLER(AwViewMsg_DoHitTest, OnDoHitTest)
165 IPC_MESSAGE_HANDLER(AwViewMsg_SetTextZoomFactor, OnSetTextZoomFactor)
166 IPC_MESSAGE_HANDLER(AwViewMsg_ResetScrollAndScaleState,
167 OnResetScrollAndScaleState)
168 IPC_MESSAGE_HANDLER(AwViewMsg_SetInitialPageScale, OnSetInitialPageScale)
169 IPC_MESSAGE_HANDLER(AwViewMsg_SetBackgroundColor, OnSetBackgroundColor)
170 IPC_MESSAGE_UNHANDLED(handled = false)
171 IPC_END_MESSAGE_MAP()
172 return handled;
175 void AwRenderViewExt::OnDocumentHasImagesRequest(int id) {
176 bool hasImages = false;
177 if (render_view()) {
178 blink::WebView* webview = render_view()->GetWebView();
179 if (webview) {
180 blink::WebDocument document = webview->mainFrame()->document();
181 const blink::WebElement child_img = GetImgChild(document);
182 hasImages = !child_img.isNull();
185 Send(new AwViewHostMsg_DocumentHasImagesResponse(routing_id(), id,
186 hasImages));
189 void AwRenderViewExt::DidCommitCompositorFrame() {
190 UpdatePageScaleFactor();
193 void AwRenderViewExt::DidUpdateLayout() {
194 if (check_contents_size_timer_.IsRunning())
195 return;
197 check_contents_size_timer_.Start(FROM_HERE,
198 base::TimeDelta::FromMilliseconds(0), this,
199 &AwRenderViewExt::CheckContentsSize);
202 void AwRenderViewExt::UpdatePageScaleFactor() {
203 if (page_scale_factor_ != render_view()->GetWebView()->pageScaleFactor()) {
204 page_scale_factor_ = render_view()->GetWebView()->pageScaleFactor();
205 Send(new AwViewHostMsg_PageScaleFactorChanged(routing_id(),
206 page_scale_factor_));
210 void AwRenderViewExt::CheckContentsSize() {
211 if (!render_view()->GetWebView())
212 return;
214 gfx::Size contents_size;
216 blink::WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
217 if (main_frame)
218 contents_size = main_frame->contentsSize();
220 // Fall back to contentsPreferredMinimumSize if the mainFrame is reporting a
221 // 0x0 size (this happens during initial load).
222 if (contents_size.IsEmpty()) {
223 contents_size = render_view()->GetWebView()->contentsPreferredMinimumSize();
226 if (contents_size == last_sent_contents_size_)
227 return;
229 last_sent_contents_size_ = contents_size;
230 Send(new AwViewHostMsg_OnContentsSizeChanged(routing_id(), contents_size));
233 void AwRenderViewExt::Navigate(const GURL& url) {
234 // Navigate is called only on NEW navigations, so WebImageCache won't be freed
235 // when the user just clicks on links, but only when a navigation is started,
236 // for instance via loadUrl. A better approach would be clearing the cache on
237 // cross-site boundaries, however this would require too many changes both on
238 // the browser side (in RenderViewHostManger), to the IPCmessages and to the
239 // RenderViewObserver. Thus, clearing decoding image cache on Navigate, seems
240 // a more acceptable compromise.
241 blink::WebImageCache::clear();
244 void AwRenderViewExt::FocusedNodeChanged(const blink::WebNode& node) {
245 if (node.isNull() || !node.isElementNode() || !render_view())
246 return;
248 // Note: element is not const due to textContent() is not const.
249 blink::WebElement element = node.toConst<blink::WebElement>();
250 AwHitTestData data;
252 data.href = GetHref(element);
253 data.anchor_text = element.textContent();
255 GURL absolute_link_url;
256 if (node.isLink())
257 absolute_link_url = GetAbsoluteUrl(node, data.href);
259 GURL absolute_image_url = GetChildImageUrlFromElement(element);
261 PopulateHitTestData(absolute_link_url,
262 absolute_image_url,
263 render_view()->IsEditableNode(node),
264 &data);
265 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
268 void AwRenderViewExt::OnDoHitTest(const gfx::PointF& touch_center,
269 const gfx::SizeF& touch_area) {
270 if (!render_view() || !render_view()->GetWebView())
271 return;
273 const blink::WebHitTestResult result =
274 render_view()->GetWebView()->hitTestResultForTap(
275 blink::WebPoint(touch_center.x(), touch_center.y()),
276 blink::WebSize(touch_area.width(), touch_area.height()));
277 AwHitTestData data;
279 GURL absolute_image_url = result.absoluteImageURL();
280 if (!result.urlElement().isNull()) {
281 data.anchor_text = result.urlElement().textContent();
282 data.href = GetHref(result.urlElement());
283 // If we hit an image that failed to load, Blink won't give us its URL.
284 // Fall back to walking the DOM in this case.
285 if (absolute_image_url.is_empty())
286 absolute_image_url = GetChildImageUrlFromElement(result.urlElement());
289 PopulateHitTestData(result.absoluteLinkURL(),
290 absolute_image_url,
291 result.isContentEditable(),
292 &data);
293 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
296 void AwRenderViewExt::OnSetTextZoomFactor(float zoom_factor) {
297 if (!render_view() || !render_view()->GetWebView())
298 return;
299 // Hide selection and autofill popups.
300 render_view()->GetWebView()->hidePopups();
301 render_view()->GetWebView()->setTextZoomFactor(zoom_factor);
304 void AwRenderViewExt::OnResetScrollAndScaleState() {
305 if (!render_view() || !render_view()->GetWebView())
306 return;
307 render_view()->GetWebView()->resetScrollAndScaleState();
310 void AwRenderViewExt::OnSetInitialPageScale(double page_scale_factor) {
311 if (!render_view() || !render_view()->GetWebView())
312 return;
313 render_view()->GetWebView()->setInitialPageScaleOverride(
314 page_scale_factor);
317 void AwRenderViewExt::OnSetBackgroundColor(SkColor c) {
318 if (!render_view() || !render_view()->GetWebView())
319 return;
320 render_view()->GetWebView()->setBaseBackgroundColor(c);
323 } // namespace android_webview