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 "chrome/renderer/chrome_render_view_observer.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/debug/crash_logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/histogram.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/trace_event/trace_event.h"
17 #include "chrome/common/chrome_constants.h"
18 #include "chrome/common/chrome_isolated_world_ids.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/crash_keys.h"
21 #include "chrome/common/render_messages.h"
22 #include "chrome/common/url_constants.h"
23 #include "chrome/renderer/prerender/prerender_helper.h"
24 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h"
25 #include "chrome/renderer/web_apps.h"
26 #include "components/translate/content/renderer/translate_helper.h"
27 #include "components/web_cache/renderer/web_cache_render_process_observer.h"
28 #include "content/public/common/bindings_policy.h"
29 #include "content/public/renderer/content_renderer_client.h"
30 #include "content/public/renderer/render_frame.h"
31 #include "content/public/renderer/render_view.h"
32 #include "extensions/common/constants.h"
33 #include "net/base/data_url.h"
34 #include "skia/ext/platform_canvas.h"
35 #include "third_party/WebKit/public/platform/WebCString.h"
36 #include "third_party/WebKit/public/platform/WebRect.h"
37 #include "third_party/WebKit/public/platform/WebSize.h"
38 #include "third_party/WebKit/public/platform/WebString.h"
39 #include "third_party/WebKit/public/platform/WebURLRequest.h"
40 #include "third_party/WebKit/public/platform/WebVector.h"
41 #include "third_party/WebKit/public/web/WebAXObject.h"
42 #include "third_party/WebKit/public/web/WebDataSource.h"
43 #include "third_party/WebKit/public/web/WebDocument.h"
44 #include "third_party/WebKit/public/web/WebElement.h"
45 #include "third_party/WebKit/public/web/WebInputEvent.h"
46 #include "third_party/WebKit/public/web/WebLocalFrame.h"
47 #include "third_party/WebKit/public/web/WebNode.h"
48 #include "third_party/WebKit/public/web/WebNodeList.h"
49 #include "third_party/WebKit/public/web/WebView.h"
50 #include "ui/base/ui_base_switches_util.h"
51 #include "ui/gfx/favicon_size.h"
52 #include "ui/gfx/geometry/size.h"
53 #include "ui/gfx/geometry/size_f.h"
54 #include "ui/gfx/skbitmap_operations.h"
55 #include "v8/include/v8-testing.h"
57 #if defined(ENABLE_EXTENSIONS)
58 #include "chrome/common/extensions/chrome_extension_messages.h"
61 using blink::WebAXObject
;
62 using blink::WebCString
;
63 using blink::WebDataSource
;
64 using blink::WebDocument
;
65 using blink::WebElement
;
66 using blink::WebFrame
;
67 using blink::WebGestureEvent
;
68 using blink::WebIconURL
;
69 using blink::WebLocalFrame
;
71 using blink::WebNodeList
;
73 using blink::WebSecurityOrigin
;
75 using blink::WebString
;
76 using blink::WebTouchEvent
;
78 using blink::WebURLRequest
;
80 using blink::WebVector
;
81 using blink::WebWindowFeatures
;
83 // Delay in milliseconds that we'll wait before capturing the page contents
85 static const int kDelayForCaptureMs
= 500;
87 // Typically, we capture the page data once the page is loaded.
88 // Sometimes, the page never finishes to load, preventing the page capture
89 // To workaround this problem, we always perform a capture after the following
91 static const int kDelayForForcedCaptureMs
= 6000;
93 // define to write the time necessary for thumbnail/DOM text retrieval,
94 // respectively, into the system debug log
95 // #define TIME_TEXT_RETRIEVAL
97 // maximum number of characters in the document to index, any text beyond this
98 // point will be clipped
99 static const size_t kMaxIndexChars
= 65535;
101 // Constants for UMA statistic collection.
102 static const char kTranslateCaptureText
[] = "Translate.CaptureText";
106 #if defined(OS_ANDROID)
107 // Parses the DOM for a <meta> tag with a particular name.
108 // |meta_tag_content| is set to the contents of the 'content' attribute.
109 // |found_tag| is set to true if the tag was successfully found.
110 // Returns true if the document was parsed without errors.
111 bool RetrieveMetaTagContent(const WebFrame
* main_frame
,
112 const GURL
& expected_url
,
113 const std::string
& meta_tag_name
,
115 std::string
* meta_tag_content
) {
116 WebDocument document
=
117 main_frame
? main_frame
->document() : WebDocument();
118 WebElement head
= document
.isNull() ? WebElement() : document
.head();
119 GURL document_url
= document
.isNull() ? GURL() : GURL(document
.url());
121 // Search the DOM for the <meta> tag with the given name.
123 *meta_tag_content
= "";
124 if (!head
.isNull()) {
125 WebNodeList children
= head
.childNodes();
126 for (unsigned i
= 0; i
< children
.length(); ++i
) {
127 WebNode child
= children
.item(i
);
128 if (!child
.isElementNode())
130 WebElement elem
= child
.to
<WebElement
>();
131 if (elem
.hasHTMLTagName("meta")) {
132 if (elem
.hasAttribute("name") && elem
.hasAttribute("content")) {
133 std::string name
= elem
.getAttribute("name").utf8();
134 if (name
== meta_tag_name
) {
135 *meta_tag_content
= elem
.getAttribute("content").utf8();
144 // Make sure we're checking the right page and that the length of the content
145 // string is reasonable.
146 bool success
= document_url
== expected_url
;
147 if (meta_tag_content
->size() > chrome::kMaxMetaTagAttributeLength
) {
148 *meta_tag_content
= "";
158 ChromeRenderViewObserver::ChromeRenderViewObserver(
159 content::RenderView
* render_view
,
160 web_cache::WebCacheRenderProcessObserver
* web_cache_render_process_observer
)
161 : content::RenderViewObserver(render_view
),
162 web_cache_render_process_observer_(web_cache_render_process_observer
),
163 translate_helper_(new translate::TranslateHelper(
165 chrome::ISOLATED_WORLD_ID_TRANSLATE
,
167 extensions::kExtensionScheme
)),
168 phishing_classifier_(NULL
),
169 webview_visually_deemphasized_(false),
170 capture_timer_(false, false) {
171 const base::CommandLine
& command_line
=
172 *base::CommandLine::ForCurrentProcess();
173 if (!command_line
.HasSwitch(switches::kDisableClientSidePhishingDetection
))
174 OnSetClientSidePhishingDetection(true);
177 ChromeRenderViewObserver::~ChromeRenderViewObserver() {
180 bool ChromeRenderViewObserver::OnMessageReceived(const IPC::Message
& message
) {
182 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewObserver
, message
)
183 #if !defined(OS_ANDROID) && !defined(OS_IOS)
184 IPC_MESSAGE_HANDLER(ChromeViewMsg_WebUIJavaScript
, OnWebUIJavaScript
)
186 #if defined(ENABLE_EXTENSIONS)
187 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetVisuallyDeemphasized
,
188 OnSetVisuallyDeemphasized
)
190 #if defined(OS_ANDROID)
191 IPC_MESSAGE_HANDLER(ChromeViewMsg_UpdateTopControlsState
,
192 OnUpdateTopControlsState
)
193 IPC_MESSAGE_HANDLER(ChromeViewMsg_RetrieveMetaTagContent
,
194 OnRetrieveMetaTagContent
)
196 IPC_MESSAGE_HANDLER(ChromeViewMsg_GetWebApplicationInfo
,
197 OnGetWebApplicationInfo
)
198 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetClientSidePhishingDetection
,
199 OnSetClientSidePhishingDetection
)
200 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetWindowFeatures
, OnSetWindowFeatures
)
201 IPC_MESSAGE_UNHANDLED(handled
= false)
202 IPC_END_MESSAGE_MAP()
207 #if !defined(OS_ANDROID) && !defined(OS_IOS)
208 void ChromeRenderViewObserver::OnWebUIJavaScript(
209 const base::string16
& javascript
) {
210 webui_javascript_
.push_back(javascript
);
214 #if defined(OS_ANDROID)
215 void ChromeRenderViewObserver::OnUpdateTopControlsState(
216 content::TopControlsState constraints
,
217 content::TopControlsState current
,
219 render_view()->UpdateTopControlsState(constraints
, current
, animate
);
222 void ChromeRenderViewObserver::OnRetrieveMetaTagContent(
223 const GURL
& expected_url
,
224 const std::string tag_name
) {
226 std::string content_str
;
227 bool parsed_successfully
= RetrieveMetaTagContent(
228 render_view()->GetWebView()->mainFrame(),
234 Send(new ChromeViewHostMsg_DidRetrieveMetaTagContent(
236 parsed_successfully
&& found_tag
,
243 void ChromeRenderViewObserver::OnGetWebApplicationInfo() {
244 WebFrame
* main_frame
= render_view()->GetWebView()->mainFrame();
247 WebApplicationInfo web_app_info
;
248 web_apps::ParseWebAppFromWebDocument(main_frame
, &web_app_info
);
250 // The warning below is specific to mobile but it doesn't hurt to show it even
251 // if the Chromium build is running on a desktop. It will get more exposition.
252 if (web_app_info
.mobile_capable
==
253 WebApplicationInfo::MOBILE_CAPABLE_APPLE
) {
254 blink::WebConsoleMessage
message(
255 blink::WebConsoleMessage::LevelWarning
,
256 "<meta name=\"apple-mobile-web-app-capable\" content=\"yes\"> is "
257 "deprecated. Please include <meta name=\"mobile-web-app-capable\" "
258 "content=\"yes\"> - "
259 "http://developers.google.com/chrome/mobile/docs/installtohomescreen");
260 main_frame
->addMessageToConsole(message
);
263 // Prune out any data URLs in the set of icons. The browser process expects
264 // any icon with a data URL to have originated from a favicon. We don't want
265 // to decode arbitrary data URLs in the browser process. See
266 // http://b/issue?id=1162972
267 for (std::vector
<WebApplicationInfo::IconInfo
>::iterator it
=
268 web_app_info
.icons
.begin(); it
!= web_app_info
.icons
.end();) {
269 if (it
->url
.SchemeIs(url::kDataScheme
))
270 it
= web_app_info
.icons
.erase(it
);
275 // Truncate the strings we send to the browser process.
277 web_app_info
.title
.substr(0, chrome::kMaxMetaTagAttributeLength
);
278 web_app_info
.description
=
279 web_app_info
.description
.substr(0, chrome::kMaxMetaTagAttributeLength
);
281 Send(new ChromeViewHostMsg_DidGetWebApplicationInfo(
282 routing_id(), web_app_info
));
285 void ChromeRenderViewObserver::OnSetWindowFeatures(
286 const WebWindowFeatures
& window_features
) {
287 render_view()->GetWebView()->setWindowFeatures(window_features
);
290 void ChromeRenderViewObserver::Navigate(const GURL
& url
) {
291 // Execute cache clear operations that were postponed until a navigation
292 // event (including tab reload).
293 if (web_cache_render_process_observer_
)
294 web_cache_render_process_observer_
->ExecutePendingClearCache();
295 // Let translate_helper do any preparatory work for loading a URL.
296 if (translate_helper_
)
297 translate_helper_
->PrepareForUrl(url
);
300 void ChromeRenderViewObserver::OnSetClientSidePhishingDetection(
301 bool enable_phishing_detection
) {
302 #if defined(FULL_SAFE_BROWSING) && !defined(OS_CHROMEOS)
303 phishing_classifier_
= enable_phishing_detection
?
304 safe_browsing::PhishingClassifierDelegate::Create(render_view(), NULL
) :
309 #if defined(ENABLE_EXTENSIONS)
310 void ChromeRenderViewObserver::OnSetVisuallyDeemphasized(bool deemphasized
) {
311 if (webview_visually_deemphasized_
== deemphasized
)
314 webview_visually_deemphasized_
= deemphasized
;
318 SkColor greyish
= SkColorSetARGB(178, 0, 0, 0);
319 render_view()->GetWebView()->setPageOverlayColor(greyish
);
321 render_view()->GetWebView()->setPageOverlayColor(SK_ColorTRANSPARENT
);
326 void ChromeRenderViewObserver::DidStartLoading() {
327 if ((render_view()->GetEnabledBindings() & content::BINDINGS_POLICY_WEB_UI
) &&
328 !webui_javascript_
.empty()) {
329 for (size_t i
= 0; i
< webui_javascript_
.size(); ++i
) {
330 render_view()->GetMainRenderFrame()->ExecuteJavaScript(
331 webui_javascript_
[i
]);
333 webui_javascript_
.clear();
337 void ChromeRenderViewObserver::DidStopLoading() {
338 WebFrame
* main_frame
= render_view()->GetWebView()->mainFrame();
340 // Remote frames don't host a document, so return early if that's the case.
341 if (main_frame
->isWebRemoteFrame())
344 GURL osdd_url
= main_frame
->document().openSearchDescriptionURL();
345 if (!osdd_url
.is_empty()) {
346 Send(new ChromeViewHostMsg_PageHasOSDD(
347 routing_id(), main_frame
->document().url(), osdd_url
,
348 search_provider::AUTODETECTED_PROVIDER
));
351 // Don't capture pages including refresh meta tag.
352 if (HasRefreshMetaTag(main_frame
))
355 CapturePageInfoLater(
356 false, // preliminary_capture
357 base::TimeDelta::FromMilliseconds(
358 render_view()->GetContentStateImmediately() ?
359 0 : kDelayForCaptureMs
));
362 void ChromeRenderViewObserver::DidCommitProvisionalLoad(
363 WebLocalFrame
* frame
, bool is_new_navigation
) {
364 // Don't capture pages being not new, or including refresh meta tag.
365 if (!is_new_navigation
|| HasRefreshMetaTag(frame
))
368 base::debug::SetCrashKeyValue(
369 crash_keys::kViewCount
,
370 base::SizeTToString(content::RenderView::GetRenderViewCount()));
372 CapturePageInfoLater(
373 true, // preliminary_capture
374 base::TimeDelta::FromMilliseconds(kDelayForForcedCaptureMs
));
377 void ChromeRenderViewObserver::CapturePageInfoLater(bool preliminary_capture
,
378 base::TimeDelta delay
) {
379 capture_timer_
.Start(
382 base::Bind(&ChromeRenderViewObserver::CapturePageInfo
,
383 base::Unretained(this),
384 preliminary_capture
));
387 void ChromeRenderViewObserver::CapturePageInfo(bool preliminary_capture
) {
388 if (!render_view()->GetWebView())
391 WebFrame
* main_frame
= render_view()->GetWebView()->mainFrame();
395 // TODO(creis): Refactor WebFrame::contentAsText to handle RemoteFrames,
396 // likely by moving it to the browser process. For now, only capture page
397 // info from main frames that are LocalFrames, and ignore their RemoteFrame
399 if (main_frame
->isWebRemoteFrame())
402 // Don't index/capture pages that are in view source mode.
403 if (main_frame
->isViewSourceModeEnabled())
406 // Don't index/capture pages that failed to load. This only checks the top
407 // level frame so the thumbnail may contain a frame that failed to load.
408 WebDataSource
* ds
= main_frame
->dataSource();
409 if (ds
&& ds
->hasUnreachableURL())
412 // Don't index/capture pages that are being prerendered.
413 if (prerender::PrerenderHelper::IsPrerendering(
414 render_view()->GetMainRenderFrame())) {
418 // Retrieve the frame's full text (up to kMaxIndexChars), and pass it to the
419 // translate helper for language detection and possible translation.
420 base::string16 contents
;
421 base::TimeTicks capture_begin_time
= base::TimeTicks::Now();
422 CaptureText(main_frame
, &contents
);
423 UMA_HISTOGRAM_TIMES(kTranslateCaptureText
,
424 base::TimeTicks::Now() - capture_begin_time
);
425 if (translate_helper_
)
426 translate_helper_
->PageCaptured(contents
);
428 TRACE_EVENT0("renderer", "ChromeRenderViewObserver::CapturePageInfo");
430 #if defined(FULL_SAFE_BROWSING)
431 // Will swap out the string.
432 if (phishing_classifier_
)
433 phishing_classifier_
->PageCaptured(&contents
, preliminary_capture
);
437 void ChromeRenderViewObserver::CaptureText(WebFrame
* frame
,
438 base::string16
* contents
) {
443 #ifdef TIME_TEXT_RETRIEVAL
444 double begin
= time_util::GetHighResolutionTimeNow();
447 // get the contents of the frame
448 *contents
= frame
->contentAsText(kMaxIndexChars
);
450 #ifdef TIME_TEXT_RETRIEVAL
451 double end
= time_util::GetHighResolutionTimeNow();
453 sprintf_s(buf
, "%d chars retrieved for indexing in %gms\n",
454 contents
.size(), (end
- begin
)*1000);
455 OutputDebugStringA(buf
);
458 // When the contents are clipped to the maximum, we don't want to have a
459 // partial word indexed at the end that might have been clipped. Therefore,
460 // terminate the string at the last space to ensure no words are clipped.
461 if (contents
->size() == kMaxIndexChars
) {
462 size_t last_space_index
= contents
->find_last_of(base::kWhitespaceUTF16
);
463 if (last_space_index
!= base::string16::npos
)
464 contents
->resize(last_space_index
);
468 bool ChromeRenderViewObserver::HasRefreshMetaTag(WebFrame
* frame
) {
471 WebElement head
= frame
->document().head();
472 if (head
.isNull() || !head
.hasChildNodes())
475 const WebString
tag_name(base::ASCIIToUTF16("meta"));
476 const WebString
attribute_name(base::ASCIIToUTF16("http-equiv"));
478 WebNodeList children
= head
.childNodes();
479 for (size_t i
= 0; i
< children
.length(); ++i
) {
480 WebNode node
= children
.item(i
);
481 if (!node
.isElementNode())
483 WebElement element
= node
.to
<WebElement
>();
484 if (!element
.hasHTMLTagName(tag_name
))
486 WebString value
= element
.getAttribute(attribute_name
);
487 if (value
.isNull() ||
488 !base::LowerCaseEqualsASCII(base::StringPiece16(value
), "refresh"))