Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / renderer / chrome_render_view_observer.cc
blobdb99bdeee24ce9f98c15fcf37fe035f158cbcb38
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/message_loop/message_loop.h"
11 #include "base/metrics/histogram.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/trace_event/trace_event.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/prerender_messages.h"
18 #include "chrome/common/render_messages.h"
19 #include "chrome/common/url_constants.h"
20 #include "chrome/renderer/isolated_world_ids.h"
21 #include "chrome/renderer/prerender/prerender_helper.h"
22 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h"
23 #include "chrome/renderer/web_apps.h"
24 #include "chrome/renderer/webview_color_overlay.h"
25 #include "components/translate/content/renderer/translate_helper.h"
26 #include "components/web_cache/renderer/web_cache_render_process_observer.h"
27 #include "content/public/common/bindings_policy.h"
28 #include "content/public/renderer/content_renderer_client.h"
29 #include "content/public/renderer/render_frame.h"
30 #include "content/public/renderer/render_view.h"
31 #include "extensions/common/constants.h"
32 #include "extensions/renderer/extension_groups.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 namespace {
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,
114 bool* found_tag,
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.
122 *found_tag = false;
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())
129 continue;
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();
136 *found_tag = true;
137 break;
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 = "";
149 success = false;
152 return success;
154 #endif
156 } // namespace
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(
164 render_view,
165 chrome::ISOLATED_WORLD_ID_TRANSLATE,
166 extensions::EXTENSION_GROUP_INTERNAL_TRANSLATE_SCRIPTS,
167 extensions::kExtensionScheme)),
168 phishing_classifier_(NULL),
169 capture_timer_(false, false) {
170 const base::CommandLine& command_line =
171 *base::CommandLine::ForCurrentProcess();
172 if (!command_line.HasSwitch(switches::kDisableClientSidePhishingDetection))
173 OnSetClientSidePhishingDetection(true);
176 ChromeRenderViewObserver::~ChromeRenderViewObserver() {
179 bool ChromeRenderViewObserver::OnMessageReceived(const IPC::Message& message) {
180 bool handled = true;
181 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewObserver, message)
182 #if !defined(OS_ANDROID) && !defined(OS_IOS)
183 IPC_MESSAGE_HANDLER(ChromeViewMsg_WebUIJavaScript, OnWebUIJavaScript)
184 #endif
185 #if defined(ENABLE_EXTENSIONS)
186 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetVisuallyDeemphasized,
187 OnSetVisuallyDeemphasized)
188 #endif
189 #if defined(OS_ANDROID)
190 IPC_MESSAGE_HANDLER(ChromeViewMsg_UpdateTopControlsState,
191 OnUpdateTopControlsState)
192 IPC_MESSAGE_HANDLER(ChromeViewMsg_RetrieveMetaTagContent,
193 OnRetrieveMetaTagContent)
194 #endif
195 IPC_MESSAGE_HANDLER(ChromeViewMsg_GetWebApplicationInfo,
196 OnGetWebApplicationInfo)
197 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetClientSidePhishingDetection,
198 OnSetClientSidePhishingDetection)
199 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetWindowFeatures, OnSetWindowFeatures)
200 IPC_MESSAGE_UNHANDLED(handled = false)
201 IPC_END_MESSAGE_MAP()
203 return handled;
206 #if !defined(OS_ANDROID) && !defined(OS_IOS)
207 void ChromeRenderViewObserver::OnWebUIJavaScript(
208 const base::string16& javascript) {
209 webui_javascript_.push_back(javascript);
211 #endif
213 #if defined(OS_ANDROID)
214 void ChromeRenderViewObserver::OnUpdateTopControlsState(
215 content::TopControlsState constraints,
216 content::TopControlsState current,
217 bool animate) {
218 render_view()->UpdateTopControlsState(constraints, current, animate);
221 void ChromeRenderViewObserver::OnRetrieveMetaTagContent(
222 const GURL& expected_url,
223 const std::string tag_name) {
224 bool found_tag;
225 std::string content_str;
226 bool parsed_successfully = RetrieveMetaTagContent(
227 render_view()->GetWebView()->mainFrame(),
228 expected_url,
229 tag_name,
230 &found_tag,
231 &content_str);
233 Send(new ChromeViewHostMsg_DidRetrieveMetaTagContent(
234 routing_id(),
235 parsed_successfully && found_tag,
236 tag_name,
237 content_str,
238 expected_url));
240 #endif
242 void ChromeRenderViewObserver::OnGetWebApplicationInfo() {
243 WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
244 DCHECK(main_frame);
246 WebApplicationInfo web_app_info;
247 web_apps::ParseWebAppFromWebDocument(main_frame, &web_app_info);
249 // The warning below is specific to mobile but it doesn't hurt to show it even
250 // if the Chromium build is running on a desktop. It will get more exposition.
251 if (web_app_info.mobile_capable ==
252 WebApplicationInfo::MOBILE_CAPABLE_APPLE) {
253 blink::WebConsoleMessage message(
254 blink::WebConsoleMessage::LevelWarning,
255 "<meta name=\"apple-mobile-web-app-capable\" content=\"yes\"> is "
256 "deprecated. Please include <meta name=\"mobile-web-app-capable\" "
257 "content=\"yes\"> - "
258 "http://developers.google.com/chrome/mobile/docs/installtohomescreen");
259 main_frame->addMessageToConsole(message);
262 // Prune out any data URLs in the set of icons. The browser process expects
263 // any icon with a data URL to have originated from a favicon. We don't want
264 // to decode arbitrary data URLs in the browser process. See
265 // http://b/issue?id=1162972
266 for (std::vector<WebApplicationInfo::IconInfo>::iterator it =
267 web_app_info.icons.begin(); it != web_app_info.icons.end();) {
268 if (it->url.SchemeIs(url::kDataScheme))
269 it = web_app_info.icons.erase(it);
270 else
271 ++it;
274 // Truncate the strings we send to the browser process.
275 web_app_info.title =
276 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength);
277 web_app_info.description =
278 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength);
280 Send(new ChromeViewHostMsg_DidGetWebApplicationInfo(
281 routing_id(), web_app_info));
284 void ChromeRenderViewObserver::OnSetWindowFeatures(
285 const WebWindowFeatures& window_features) {
286 render_view()->GetWebView()->setWindowFeatures(window_features);
289 void ChromeRenderViewObserver::Navigate(const GURL& url) {
290 // Execute cache clear operations that were postponed until a navigation
291 // event (including tab reload).
292 if (web_cache_render_process_observer_)
293 web_cache_render_process_observer_->ExecutePendingClearCache();
294 // Let translate_helper do any preparatory work for loading a URL.
295 if (translate_helper_)
296 translate_helper_->PrepareForUrl(url);
299 void ChromeRenderViewObserver::OnSetClientSidePhishingDetection(
300 bool enable_phishing_detection) {
301 #if defined(FULL_SAFE_BROWSING) && !defined(OS_CHROMEOS)
302 phishing_classifier_ = enable_phishing_detection ?
303 safe_browsing::PhishingClassifierDelegate::Create(render_view(), NULL) :
304 NULL;
305 #endif
308 #if defined(ENABLE_EXTENSIONS)
309 void ChromeRenderViewObserver::OnSetVisuallyDeemphasized(bool deemphasized) {
310 bool already_deemphasized = !!dimmed_color_overlay_.get();
311 if (already_deemphasized == deemphasized)
312 return;
314 if (deemphasized) {
315 // 70% opaque grey.
316 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
317 dimmed_color_overlay_.reset(
318 new WebViewColorOverlay(render_view(), greyish));
319 } else {
320 dimmed_color_overlay_.reset();
323 #endif
325 void ChromeRenderViewObserver::DidStartLoading() {
326 if ((render_view()->GetEnabledBindings() & content::BINDINGS_POLICY_WEB_UI) &&
327 !webui_javascript_.empty()) {
328 for (size_t i = 0; i < webui_javascript_.size(); ++i) {
329 render_view()->GetMainRenderFrame()->ExecuteJavaScript(
330 webui_javascript_[i]);
332 webui_javascript_.clear();
336 void ChromeRenderViewObserver::DidStopLoading() {
337 WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
339 // Remote frames don't host a document, so return early if that's the case.
340 if (main_frame->isWebRemoteFrame())
341 return;
343 GURL osdd_url = main_frame->document().openSearchDescriptionURL();
344 if (!osdd_url.is_empty()) {
345 Send(new ChromeViewHostMsg_PageHasOSDD(
346 routing_id(), main_frame->document().url(), osdd_url,
347 search_provider::AUTODETECTED_PROVIDER));
350 // Don't capture pages including refresh meta tag.
351 if (HasRefreshMetaTag(main_frame))
352 return;
354 CapturePageInfoLater(
355 false, // preliminary_capture
356 base::TimeDelta::FromMilliseconds(
357 render_view()->GetContentStateImmediately() ?
358 0 : kDelayForCaptureMs));
361 void ChromeRenderViewObserver::DidCommitProvisionalLoad(
362 WebLocalFrame* frame, bool is_new_navigation) {
363 // Don't capture pages being not new, or including refresh meta tag.
364 if (!is_new_navigation || HasRefreshMetaTag(frame))
365 return;
367 CapturePageInfoLater(
368 true, // preliminary_capture
369 base::TimeDelta::FromMilliseconds(kDelayForForcedCaptureMs));
372 void ChromeRenderViewObserver::CapturePageInfoLater(bool preliminary_capture,
373 base::TimeDelta delay) {
374 capture_timer_.Start(
375 FROM_HERE,
376 delay,
377 base::Bind(&ChromeRenderViewObserver::CapturePageInfo,
378 base::Unretained(this),
379 preliminary_capture));
382 void ChromeRenderViewObserver::CapturePageInfo(bool preliminary_capture) {
383 if (!render_view()->GetWebView())
384 return;
386 WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
387 if (!main_frame)
388 return;
390 // TODO(creis): Refactor WebFrame::contentAsText to handle RemoteFrames,
391 // likely by moving it to the browser process. For now, only capture page
392 // info from main frames that are LocalFrames, and ignore their RemoteFrame
393 // children.
394 if (main_frame->isWebRemoteFrame())
395 return;
397 // Don't index/capture pages that are in view source mode.
398 if (main_frame->isViewSourceModeEnabled())
399 return;
401 // Don't index/capture pages that failed to load. This only checks the top
402 // level frame so the thumbnail may contain a frame that failed to load.
403 WebDataSource* ds = main_frame->dataSource();
404 if (ds && ds->hasUnreachableURL())
405 return;
407 // Don't index/capture pages that are being prerendered.
408 if (prerender::PrerenderHelper::IsPrerendering(
409 render_view()->GetMainRenderFrame())) {
410 return;
413 // Retrieve the frame's full text (up to kMaxIndexChars), and pass it to the
414 // translate helper for language detection and possible translation.
415 base::string16 contents;
416 base::TimeTicks capture_begin_time = base::TimeTicks::Now();
417 CaptureText(main_frame, &contents);
418 UMA_HISTOGRAM_TIMES(kTranslateCaptureText,
419 base::TimeTicks::Now() - capture_begin_time);
420 if (translate_helper_)
421 translate_helper_->PageCaptured(contents);
423 TRACE_EVENT0("renderer", "ChromeRenderViewObserver::CapturePageInfo");
425 #if defined(FULL_SAFE_BROWSING)
426 // Will swap out the string.
427 if (phishing_classifier_)
428 phishing_classifier_->PageCaptured(&contents, preliminary_capture);
429 #endif
432 void ChromeRenderViewObserver::CaptureText(WebFrame* frame,
433 base::string16* contents) {
434 contents->clear();
435 if (!frame)
436 return;
438 #ifdef TIME_TEXT_RETRIEVAL
439 double begin = time_util::GetHighResolutionTimeNow();
440 #endif
442 // get the contents of the frame
443 *contents = frame->contentAsText(kMaxIndexChars);
445 #ifdef TIME_TEXT_RETRIEVAL
446 double end = time_util::GetHighResolutionTimeNow();
447 char buf[128];
448 sprintf_s(buf, "%d chars retrieved for indexing in %gms\n",
449 contents.size(), (end - begin)*1000);
450 OutputDebugStringA(buf);
451 #endif
453 // When the contents are clipped to the maximum, we don't want to have a
454 // partial word indexed at the end that might have been clipped. Therefore,
455 // terminate the string at the last space to ensure no words are clipped.
456 if (contents->size() == kMaxIndexChars) {
457 size_t last_space_index = contents->find_last_of(base::kWhitespaceUTF16);
458 if (last_space_index != base::string16::npos)
459 contents->resize(last_space_index);
463 bool ChromeRenderViewObserver::HasRefreshMetaTag(WebFrame* frame) {
464 if (!frame)
465 return false;
466 WebElement head = frame->document().head();
467 if (head.isNull() || !head.hasChildNodes())
468 return false;
470 const WebString tag_name(base::ASCIIToUTF16("meta"));
471 const WebString attribute_name(base::ASCIIToUTF16("http-equiv"));
473 WebNodeList children = head.childNodes();
474 for (size_t i = 0; i < children.length(); ++i) {
475 WebNode node = children.item(i);
476 if (!node.isElementNode())
477 continue;
478 WebElement element = node.to<WebElement>();
479 if (!element.hasHTMLTagName(tag_name))
480 continue;
481 WebString value = element.getAttribute(attribute_name);
482 if (value.isNull() || !LowerCaseEqualsASCII(value, "refresh"))
483 continue;
484 return true;
486 return false;