Infobar material design refresh: bg color
[chromium-blink-merge.git] / chrome / renderer / chrome_render_view_observer.cc
blobf221836df0072563c80fbeb8e36bec974ccc2820
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"
7 #include "base/bind.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"
59 #endif
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;
70 using blink::WebNode;
71 using blink::WebNodeList;
72 using blink::WebRect;
73 using blink::WebSecurityOrigin;
74 using blink::WebSize;
75 using blink::WebString;
76 using blink::WebTouchEvent;
77 using blink::WebURL;
78 using blink::WebURLRequest;
79 using blink::WebView;
80 using blink::WebVector;
81 using blink::WebWindowFeatures;
83 // Delay in milliseconds that we'll wait before capturing the page contents
84 // and thumbnail.
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
90 // delay.
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";
104 ChromeRenderViewObserver::ChromeRenderViewObserver(
105 content::RenderView* render_view,
106 web_cache::WebCacheRenderProcessObserver* web_cache_render_process_observer)
107 : content::RenderViewObserver(render_view),
108 web_cache_render_process_observer_(web_cache_render_process_observer),
109 translate_helper_(new translate::TranslateHelper(
110 render_view,
111 chrome::ISOLATED_WORLD_ID_TRANSLATE,
113 extensions::kExtensionScheme)),
114 phishing_classifier_(NULL),
115 webview_visually_deemphasized_(false),
116 capture_timer_(false, false) {
117 const base::CommandLine& command_line =
118 *base::CommandLine::ForCurrentProcess();
119 if (!command_line.HasSwitch(switches::kDisableClientSidePhishingDetection))
120 OnSetClientSidePhishingDetection(true);
123 ChromeRenderViewObserver::~ChromeRenderViewObserver() {
126 bool ChromeRenderViewObserver::OnMessageReceived(const IPC::Message& message) {
127 bool handled = true;
128 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewObserver, message)
129 #if !defined(OS_ANDROID) && !defined(OS_IOS)
130 IPC_MESSAGE_HANDLER(ChromeViewMsg_WebUIJavaScript, OnWebUIJavaScript)
131 #endif
132 #if defined(ENABLE_EXTENSIONS)
133 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetVisuallyDeemphasized,
134 OnSetVisuallyDeemphasized)
135 #endif
136 #if defined(OS_ANDROID)
137 IPC_MESSAGE_HANDLER(ChromeViewMsg_UpdateTopControlsState,
138 OnUpdateTopControlsState)
139 #endif
140 IPC_MESSAGE_HANDLER(ChromeViewMsg_GetWebApplicationInfo,
141 OnGetWebApplicationInfo)
142 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetClientSidePhishingDetection,
143 OnSetClientSidePhishingDetection)
144 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetWindowFeatures, OnSetWindowFeatures)
145 IPC_MESSAGE_UNHANDLED(handled = false)
146 IPC_END_MESSAGE_MAP()
148 return handled;
151 #if !defined(OS_ANDROID) && !defined(OS_IOS)
152 void ChromeRenderViewObserver::OnWebUIJavaScript(
153 const base::string16& javascript) {
154 webui_javascript_.push_back(javascript);
156 #endif
158 #if defined(OS_ANDROID)
159 void ChromeRenderViewObserver::OnUpdateTopControlsState(
160 content::TopControlsState constraints,
161 content::TopControlsState current,
162 bool animate) {
163 render_view()->UpdateTopControlsState(constraints, current, animate);
165 #endif
167 void ChromeRenderViewObserver::OnGetWebApplicationInfo() {
168 WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
169 DCHECK(main_frame);
171 WebApplicationInfo web_app_info;
172 web_apps::ParseWebAppFromWebDocument(main_frame, &web_app_info);
174 // The warning below is specific to mobile but it doesn't hurt to show it even
175 // if the Chromium build is running on a desktop. It will get more exposition.
176 if (web_app_info.mobile_capable ==
177 WebApplicationInfo::MOBILE_CAPABLE_APPLE) {
178 blink::WebConsoleMessage message(
179 blink::WebConsoleMessage::LevelWarning,
180 "<meta name=\"apple-mobile-web-app-capable\" content=\"yes\"> is "
181 "deprecated. Please include <meta name=\"mobile-web-app-capable\" "
182 "content=\"yes\"> - "
183 "http://developers.google.com/chrome/mobile/docs/installtohomescreen");
184 main_frame->addMessageToConsole(message);
187 // Prune out any data URLs in the set of icons. The browser process expects
188 // any icon with a data URL to have originated from a favicon. We don't want
189 // to decode arbitrary data URLs in the browser process. See
190 // http://b/issue?id=1162972
191 for (std::vector<WebApplicationInfo::IconInfo>::iterator it =
192 web_app_info.icons.begin(); it != web_app_info.icons.end();) {
193 if (it->url.SchemeIs(url::kDataScheme))
194 it = web_app_info.icons.erase(it);
195 else
196 ++it;
199 // Truncate the strings we send to the browser process.
200 web_app_info.title =
201 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength);
202 web_app_info.description =
203 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength);
205 Send(new ChromeViewHostMsg_DidGetWebApplicationInfo(
206 routing_id(), web_app_info));
209 void ChromeRenderViewObserver::OnSetWindowFeatures(
210 const WebWindowFeatures& window_features) {
211 render_view()->GetWebView()->setWindowFeatures(window_features);
214 void ChromeRenderViewObserver::Navigate(const GURL& url) {
215 // Execute cache clear operations that were postponed until a navigation
216 // event (including tab reload).
217 if (web_cache_render_process_observer_)
218 web_cache_render_process_observer_->ExecutePendingClearCache();
219 // Let translate_helper do any preparatory work for loading a URL.
220 if (translate_helper_)
221 translate_helper_->PrepareForUrl(url);
224 void ChromeRenderViewObserver::OnSetClientSidePhishingDetection(
225 bool enable_phishing_detection) {
226 #if defined(FULL_SAFE_BROWSING) && !defined(OS_CHROMEOS)
227 phishing_classifier_ = enable_phishing_detection ?
228 safe_browsing::PhishingClassifierDelegate::Create(render_view(), NULL) :
229 NULL;
230 #endif
233 #if defined(ENABLE_EXTENSIONS)
234 void ChromeRenderViewObserver::OnSetVisuallyDeemphasized(bool deemphasized) {
235 if (webview_visually_deemphasized_ == deemphasized)
236 return;
238 webview_visually_deemphasized_ = deemphasized;
240 if (deemphasized) {
241 // 70% opaque grey.
242 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
243 render_view()->GetWebView()->setPageOverlayColor(greyish);
244 } else {
245 render_view()->GetWebView()->setPageOverlayColor(SK_ColorTRANSPARENT);
248 #endif
250 void ChromeRenderViewObserver::DidStartLoading() {
251 if ((render_view()->GetEnabledBindings() & content::BINDINGS_POLICY_WEB_UI) &&
252 !webui_javascript_.empty()) {
253 for (size_t i = 0; i < webui_javascript_.size(); ++i) {
254 render_view()->GetMainRenderFrame()->ExecuteJavaScript(
255 webui_javascript_[i]);
257 webui_javascript_.clear();
261 void ChromeRenderViewObserver::DidStopLoading() {
262 WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
264 // Remote frames don't host a document, so return early if that's the case.
265 if (main_frame->isWebRemoteFrame())
266 return;
268 GURL osdd_url = main_frame->document().openSearchDescriptionURL();
269 if (!osdd_url.is_empty()) {
270 Send(new ChromeViewHostMsg_PageHasOSDD(
271 routing_id(), main_frame->document().url(), osdd_url,
272 search_provider::AUTODETECTED_PROVIDER));
275 // Don't capture pages including refresh meta tag.
276 if (HasRefreshMetaTag(main_frame))
277 return;
279 CapturePageInfoLater(
280 false, // preliminary_capture
281 base::TimeDelta::FromMilliseconds(
282 render_view()->GetContentStateImmediately() ?
283 0 : kDelayForCaptureMs));
286 void ChromeRenderViewObserver::DidCommitProvisionalLoad(
287 WebLocalFrame* frame, bool is_new_navigation) {
288 // Don't capture pages being not new, or including refresh meta tag.
289 if (!is_new_navigation || HasRefreshMetaTag(frame))
290 return;
292 base::debug::SetCrashKeyValue(
293 crash_keys::kViewCount,
294 base::SizeTToString(content::RenderView::GetRenderViewCount()));
296 CapturePageInfoLater(
297 true, // preliminary_capture
298 base::TimeDelta::FromMilliseconds(kDelayForForcedCaptureMs));
301 void ChromeRenderViewObserver::CapturePageInfoLater(bool preliminary_capture,
302 base::TimeDelta delay) {
303 capture_timer_.Start(
304 FROM_HERE,
305 delay,
306 base::Bind(&ChromeRenderViewObserver::CapturePageInfo,
307 base::Unretained(this),
308 preliminary_capture));
311 void ChromeRenderViewObserver::CapturePageInfo(bool preliminary_capture) {
312 if (!render_view()->GetWebView())
313 return;
315 WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
316 if (!main_frame)
317 return;
319 // TODO(creis): Refactor WebFrame::contentAsText to handle RemoteFrames,
320 // likely by moving it to the browser process. For now, only capture page
321 // info from main frames that are LocalFrames, and ignore their RemoteFrame
322 // children.
323 if (main_frame->isWebRemoteFrame())
324 return;
326 // Don't index/capture pages that are in view source mode.
327 if (main_frame->isViewSourceModeEnabled())
328 return;
330 // Don't index/capture pages that failed to load. This only checks the top
331 // level frame so the thumbnail may contain a frame that failed to load.
332 WebDataSource* ds = main_frame->dataSource();
333 if (ds && ds->hasUnreachableURL())
334 return;
336 // Don't index/capture pages that are being prerendered.
337 if (prerender::PrerenderHelper::IsPrerendering(
338 render_view()->GetMainRenderFrame())) {
339 return;
342 // Retrieve the frame's full text (up to kMaxIndexChars), and pass it to the
343 // translate helper for language detection and possible translation.
344 base::string16 contents;
345 base::TimeTicks capture_begin_time = base::TimeTicks::Now();
346 CaptureText(main_frame, &contents);
347 UMA_HISTOGRAM_TIMES(kTranslateCaptureText,
348 base::TimeTicks::Now() - capture_begin_time);
349 if (translate_helper_)
350 translate_helper_->PageCaptured(contents);
352 TRACE_EVENT0("renderer", "ChromeRenderViewObserver::CapturePageInfo");
354 #if defined(FULL_SAFE_BROWSING)
355 // Will swap out the string.
356 if (phishing_classifier_)
357 phishing_classifier_->PageCaptured(&contents, preliminary_capture);
358 #endif
361 void ChromeRenderViewObserver::CaptureText(WebFrame* frame,
362 base::string16* contents) {
363 contents->clear();
364 if (!frame)
365 return;
367 #ifdef TIME_TEXT_RETRIEVAL
368 double begin = time_util::GetHighResolutionTimeNow();
369 #endif
371 // get the contents of the frame
372 *contents = frame->contentAsText(kMaxIndexChars);
374 #ifdef TIME_TEXT_RETRIEVAL
375 double end = time_util::GetHighResolutionTimeNow();
376 char buf[128];
377 sprintf_s(buf, "%d chars retrieved for indexing in %gms\n",
378 contents.size(), (end - begin)*1000);
379 OutputDebugStringA(buf);
380 #endif
382 // When the contents are clipped to the maximum, we don't want to have a
383 // partial word indexed at the end that might have been clipped. Therefore,
384 // terminate the string at the last space to ensure no words are clipped.
385 if (contents->size() == kMaxIndexChars) {
386 size_t last_space_index = contents->find_last_of(base::kWhitespaceUTF16);
387 if (last_space_index != base::string16::npos)
388 contents->resize(last_space_index);
392 bool ChromeRenderViewObserver::HasRefreshMetaTag(WebFrame* frame) {
393 if (!frame)
394 return false;
395 WebElement head = frame->document().head();
396 if (head.isNull() || !head.hasChildNodes())
397 return false;
399 const WebString tag_name(base::ASCIIToUTF16("meta"));
400 const WebString attribute_name(base::ASCIIToUTF16("http-equiv"));
402 WebNodeList children = head.childNodes();
403 for (size_t i = 0; i < children.length(); ++i) {
404 WebNode node = children.item(i);
405 if (!node.isElementNode())
406 continue;
407 WebElement element = node.to<WebElement>();
408 if (!element.hasHTMLTagName(tag_name))
409 continue;
410 WebString value = element.getAttribute(attribute_name);
411 if (value.isNull() ||
412 !base::LowerCaseEqualsASCII(base::StringPiece16(value), "refresh"))
413 continue;
414 return true;
416 return false;