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"
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
;
36 using content::SSLStatus
;
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
) {
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()) {
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()));
78 ChromeRenderFrameObserver::ChromeRenderFrameObserver(
79 content::RenderFrame
* render_frame
)
80 : content::RenderFrameObserver(render_frame
) {
83 ChromeRenderFrameObserver::~ChromeRenderFrameObserver() {
86 bool ChromeRenderFrameObserver::OnMessageReceived(const IPC::Message
& message
) {
89 IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver
, message
)
90 IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering
, OnSetIsPrerendering
)
91 IPC_MESSAGE_UNHANDLED(handled
= false)
96 IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver
, message
)
97 IPC_MESSAGE_HANDLER(ChromeViewMsg_RequestReloadImageForContextNode
,
98 OnRequestReloadImageForContextNode
)
99 IPC_MESSAGE_HANDLER(ChromeViewMsg_RequestThumbnailForContextNode
,
100 OnRequestThumbnailForContextNode
)
101 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu
,
102 OnPrintNodeUnderContextMenu
)
103 IPC_MESSAGE_HANDLER(ChromeViewMsg_AppBannerPromptRequest
,
104 OnAppBannerPromptRequest
)
105 IPC_MESSAGE_HANDLER(ChromeViewMsg_AppBannerDebugMessageRequest
,
106 OnAppBannerDebugMessageRequest
)
107 IPC_MESSAGE_UNHANDLED(handled
= false)
108 IPC_END_MESSAGE_MAP()
113 void ChromeRenderFrameObserver::OnSetIsPrerendering(bool is_prerendering
) {
114 if (is_prerendering
) {
115 // If the PrerenderHelper for this frame already exists, don't create it. It
116 // can already be created for subframes during handling of
117 // RenderFrameCreated, if the parent frame was prerendering at time of
118 // subframe creation.
119 if (prerender::PrerenderHelper::Get(render_frame()))
122 // The PrerenderHelper will destroy itself either after recording histograms
123 // or on destruction of the RenderView.
124 new prerender::PrerenderHelper(render_frame());
128 void ChromeRenderFrameObserver::OnRequestReloadImageForContextNode() {
129 WebNode context_node
= render_frame()->GetContextMenuNode();
130 if (!context_node
.isNull() && context_node
.isElementNode() &&
131 render_frame()->GetWebFrame()) {
132 render_frame()->GetWebFrame()->reloadImage(context_node
);
136 void ChromeRenderFrameObserver::OnRequestThumbnailForContextNode(
137 int thumbnail_min_area_pixels
,
138 const gfx::Size
& thumbnail_max_size_pixels
) {
139 WebNode context_node
= render_frame()->GetContextMenuNode();
141 gfx::Size original_size
;
142 if (!context_node
.isNull() && context_node
.isElementNode()) {
143 blink::WebImage image
= context_node
.to
<WebElement
>().imageContents();
144 original_size
= image
.size();
145 thumbnail
= Downscale(image
,
146 thumbnail_min_area_pixels
,
147 thumbnail_max_size_pixels
);
151 if (thumbnail
.colorType() == kN32_SkColorType
)
154 thumbnail
.copyTo(&bitmap
, kN32_SkColorType
);
156 std::string thumbnail_data
;
157 SkAutoLockPixels
lock(bitmap
);
158 if (bitmap
.getPixels()) {
159 const int kDefaultQuality
= 90;
160 std::vector
<unsigned char> data
;
161 if (gfx::JPEGCodec::Encode(
162 reinterpret_cast<unsigned char*>(bitmap
.getAddr32(0, 0)),
163 gfx::JPEGCodec::FORMAT_SkBitmap
, bitmap
.width(), bitmap
.height(),
164 static_cast<int>(bitmap
.rowBytes()), kDefaultQuality
, &data
))
165 thumbnail_data
= std::string(data
.begin(), data
.end());
168 Send(new ChromeViewHostMsg_RequestThumbnailForContextNode_ACK(
169 routing_id(), thumbnail_data
, original_size
));
172 void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() {
173 printing::PrintWebViewHelper
* helper
=
174 printing::PrintWebViewHelper::Get(render_frame()->GetRenderView());
176 helper
->PrintNode(render_frame()->GetContextMenuNode());
179 void ChromeRenderFrameObserver::DidFinishDocumentLoad() {
180 // If the navigation is to a localhost URL (and the flag is set to
181 // allow localhost SSL misconfigurations), print a warning to the
182 // console telling the developer to check their SSL configuration
183 // before going to production.
184 bool allow_localhost
= base::CommandLine::ForCurrentProcess()->HasSwitch(
185 switches::kAllowInsecureLocalhost
);
186 WebDataSource
* ds
= render_frame()->GetWebFrame()->dataSource();
188 if (allow_localhost
) {
189 SSLStatus ssl_status
= render_frame()->GetRenderView()->GetSSLStatusOfFrame(
190 render_frame()->GetWebFrame());
191 bool is_cert_error
= net::IsCertStatusError(ssl_status
.cert_status
) &&
192 !net::IsCertStatusMinorError(ssl_status
.cert_status
);
193 bool is_localhost
= net::IsLocalhost(GURL(ds
->request().url()).host());
195 if (is_cert_error
&& is_localhost
) {
196 render_frame()->GetWebFrame()->addMessageToConsole(
197 blink::WebConsoleMessage(
198 blink::WebConsoleMessage::LevelWarning
,
200 "This site does not have a valid SSL "
201 "certificate! Without SSL, your site's and "
202 "visitors' data is vulnerable to theft and "
203 "tampering. Get a valid SSL certificate before"
204 " releasing your website to the public.")));
209 void ChromeRenderFrameObserver::OnAppBannerPromptRequest(
210 int request_id
, const std::string
& platform
) {
211 // App banner prompt requests are handled in the general chrome render frame
212 // observer, not the AppBannerClient, as the AppBannerClient is created lazily
213 // by blink and may not exist when the request is sent.
214 blink::WebAppBannerPromptReply reply
= blink::WebAppBannerPromptReply::None
;
215 blink::WebString
web_platform(base::UTF8ToUTF16(platform
));
216 blink::WebVector
<blink::WebString
> web_platforms(&web_platform
, 1);
217 render_frame()->GetWebFrame()->willShowInstallBannerPrompt(
218 request_id
, web_platforms
, &reply
);
220 Send(new ChromeViewHostMsg_AppBannerPromptReply(
221 routing_id(), request_id
, reply
));
224 void ChromeRenderFrameObserver::OnAppBannerDebugMessageRequest(
225 const std::string
& message
) {
226 render_frame()->GetWebFrame()->addMessageToConsole(blink::WebConsoleMessage(
227 blink::WebConsoleMessage::LevelDebug
, base::UTF8ToUTF16(message
)));