Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / renderer / chrome_render_frame_observer.cc
blobbcfc00d8a0ebcc9dae35a56eb1b339328acd7552
1 // Copyright 2013 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_frame_observer.h"
7 #include <limits>
8 #include <string>
9 #include <vector>
11 #include "base/command_line.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/prerender_messages.h"
15 #include "chrome/common/render_messages.h"
16 #include "chrome/renderer/prerender/prerender_helper.h"
17 #include "components/printing/common/print_messages.h"
18 #include "components/printing/renderer/print_web_view_helper.h"
19 #include "content/public/renderer/render_frame.h"
20 #include "content/public/renderer/render_view.h"
21 #include "net/base/net_util.h"
22 #include "skia/ext/image_operations.h"
23 #include "third_party/WebKit/public/platform/WebImage.h"
24 #include "third_party/WebKit/public/platform/modules/app_banner/WebAppBannerPromptReply.h"
25 #include "third_party/WebKit/public/web/WebDataSource.h"
26 #include "third_party/WebKit/public/web/WebElement.h"
27 #include "third_party/WebKit/public/web/WebFrame.h"
28 #include "third_party/WebKit/public/web/WebLocalFrame.h"
29 #include "third_party/WebKit/public/web/WebNode.h"
30 #include "third_party/skia/include/core/SkBitmap.h"
31 #include "ui/gfx/codec/jpeg_codec.h"
33 using blink::WebDataSource;
34 using blink::WebElement;
35 using blink::WebNode;
36 using content::SSLStatus;
38 namespace {
40 // If the source image is null or occupies less area than
41 // |thumbnail_min_area_pixels|, we return the image unmodified. Otherwise, we
42 // scale down the image so that the width and height do not exceed
43 // |thumbnail_max_size_pixels|, preserving the original aspect ratio.
44 SkBitmap Downscale(const blink::WebImage& image,
45 int thumbnail_min_area_pixels,
46 const gfx::Size& thumbnail_max_size_pixels) {
47 if (image.isNull())
48 return SkBitmap();
50 gfx::Size image_size = image.size();
52 if (image_size.GetArea() < thumbnail_min_area_pixels)
53 return image.getSkBitmap();
55 if (image_size.width() <= thumbnail_max_size_pixels.width() &&
56 image_size.height() <= thumbnail_max_size_pixels.height())
57 return image.getSkBitmap();
59 gfx::SizeF scaled_size = image_size;
61 if (scaled_size.width() > thumbnail_max_size_pixels.width()) {
62 scaled_size.Scale(thumbnail_max_size_pixels.width() / scaled_size.width());
65 if (scaled_size.height() > thumbnail_max_size_pixels.height()) {
66 scaled_size.Scale(
67 thumbnail_max_size_pixels.height() / scaled_size.height());
70 return skia::ImageOperations::Resize(image.getSkBitmap(),
71 skia::ImageOperations::RESIZE_GOOD,
72 static_cast<int>(scaled_size.width()),
73 static_cast<int>(scaled_size.height()));
76 } // namespace
78 ChromeRenderFrameObserver::ChromeRenderFrameObserver(
79 content::RenderFrame* render_frame)
80 : content::RenderFrameObserver(render_frame) {
83 ChromeRenderFrameObserver::~ChromeRenderFrameObserver() {
86 bool ChromeRenderFrameObserver::OnMessageReceived(const IPC::Message& message) {
87 // Filter only.
88 bool handled = true;
89 IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver, message)
90 IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering, OnSetIsPrerendering)
91 IPC_MESSAGE_UNHANDLED(handled = false)
92 IPC_END_MESSAGE_MAP()
93 if (handled)
94 return false;
96 IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver, message)
97 IPC_MESSAGE_HANDLER(ChromeViewMsg_RequestThumbnailForContextNode,
98 OnRequestThumbnailForContextNode)
99 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu,
100 OnPrintNodeUnderContextMenu)
101 IPC_MESSAGE_HANDLER(ChromeViewMsg_AppBannerPromptRequest,
102 OnAppBannerPromptRequest)
103 IPC_MESSAGE_UNHANDLED(handled = false)
104 IPC_END_MESSAGE_MAP()
106 return handled;
109 void ChromeRenderFrameObserver::OnSetIsPrerendering(bool is_prerendering) {
110 if (is_prerendering) {
111 // If the PrerenderHelper for this frame already exists, don't create it. It
112 // can already be created for subframes during handling of
113 // RenderFrameCreated, if the parent frame was prerendering at time of
114 // subframe creation.
115 if (prerender::PrerenderHelper::Get(render_frame()))
116 return;
118 // The PrerenderHelper will destroy itself either after recording histograms
119 // or on destruction of the RenderView.
120 new prerender::PrerenderHelper(render_frame());
124 void ChromeRenderFrameObserver::OnRequestThumbnailForContextNode(
125 int thumbnail_min_area_pixels,
126 const gfx::Size& thumbnail_max_size_pixels) {
127 WebNode context_node = render_frame()->GetContextMenuNode();
128 SkBitmap thumbnail;
129 gfx::Size original_size;
130 if (!context_node.isNull() && context_node.isElementNode()) {
131 blink::WebImage image = context_node.to<WebElement>().imageContents();
132 original_size = image.size();
133 thumbnail = Downscale(image,
134 thumbnail_min_area_pixels,
135 thumbnail_max_size_pixels);
138 SkBitmap bitmap;
139 if (thumbnail.colorType() == kN32_SkColorType)
140 bitmap = thumbnail;
141 else
142 thumbnail.copyTo(&bitmap, kN32_SkColorType);
144 std::string thumbnail_data;
145 SkAutoLockPixels lock(bitmap);
146 if (bitmap.getPixels()) {
147 const int kDefaultQuality = 90;
148 std::vector<unsigned char> data;
149 if (gfx::JPEGCodec::Encode(
150 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
151 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(),
152 static_cast<int>(bitmap.rowBytes()), kDefaultQuality, &data))
153 thumbnail_data = std::string(data.begin(), data.end());
156 Send(new ChromeViewHostMsg_RequestThumbnailForContextNode_ACK(
157 routing_id(), thumbnail_data, original_size));
160 void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() {
161 printing::PrintWebViewHelper* helper =
162 printing::PrintWebViewHelper::Get(render_frame()->GetRenderView());
163 if (helper)
164 helper->PrintNode(render_frame()->GetContextMenuNode());
167 void ChromeRenderFrameObserver::DidFinishDocumentLoad() {
168 // If the navigation is to a localhost URL (and the flag is set to
169 // allow localhost SSL misconfigurations), print a warning to the
170 // console telling the developer to check their SSL configuration
171 // before going to production.
172 bool allow_localhost = base::CommandLine::ForCurrentProcess()->HasSwitch(
173 switches::kAllowInsecureLocalhost);
174 WebDataSource* ds = render_frame()->GetWebFrame()->dataSource();
176 if (allow_localhost) {
177 SSLStatus ssl_status = render_frame()->GetRenderView()->GetSSLStatusOfFrame(
178 render_frame()->GetWebFrame());
179 bool is_cert_error = net::IsCertStatusError(ssl_status.cert_status) &&
180 !net::IsCertStatusMinorError(ssl_status.cert_status);
181 bool is_localhost = net::IsLocalhost(GURL(ds->request().url()).host());
183 if (is_cert_error && is_localhost) {
184 render_frame()->GetWebFrame()->addMessageToConsole(
185 blink::WebConsoleMessage(
186 blink::WebConsoleMessage::LevelWarning,
187 base::ASCIIToUTF16(
188 "This site does not have a valid SSL "
189 "certificate! Without SSL, your site's and "
190 "visitors' data is vulnerable to theft and "
191 "tampering. Get a valid SSL certificate before"
192 " releasing your website to the public.")));
197 void ChromeRenderFrameObserver::OnAppBannerPromptRequest(
198 int request_id, const std::string& platform) {
199 blink::WebAppBannerPromptReply reply = blink::WebAppBannerPromptReply::None;
200 render_frame()->GetWebFrame()->willShowInstallBannerPrompt(
201 base::UTF8ToUTF16(platform), &reply);
203 Send(new ChromeViewHostMsg_AppBannerPromptReply(
204 routing_id(), request_id, reply));