Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / renderer / chrome_render_frame_observer.cc
blobdbd03e6657fd7fef52cd9571a63455bdc5073dd1
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/WebDocument.h"
27 #include "third_party/WebKit/public/web/WebElement.h"
28 #include "third_party/WebKit/public/web/WebFrame.h"
29 #include "third_party/WebKit/public/web/WebLocalFrame.h"
30 #include "third_party/WebKit/public/web/WebNode.h"
31 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
32 #include "third_party/skia/include/core/SkBitmap.h"
33 #include "ui/gfx/codec/jpeg_codec.h"
34 #include "url/gurl.h"
36 using blink::WebDataSource;
37 using blink::WebElement;
38 using blink::WebNode;
39 using content::SSLStatus;
41 namespace {
43 // If the source image is null or occupies less area than
44 // |thumbnail_min_area_pixels|, we return the image unmodified. Otherwise, we
45 // scale down the image so that the width and height do not exceed
46 // |thumbnail_max_size_pixels|, preserving the original aspect ratio.
47 SkBitmap Downscale(const blink::WebImage& image,
48 int thumbnail_min_area_pixels,
49 const gfx::Size& thumbnail_max_size_pixels) {
50 if (image.isNull())
51 return SkBitmap();
53 gfx::Size image_size = image.size();
55 if (image_size.GetArea() < thumbnail_min_area_pixels)
56 return image.getSkBitmap();
58 if (image_size.width() <= thumbnail_max_size_pixels.width() &&
59 image_size.height() <= thumbnail_max_size_pixels.height())
60 return image.getSkBitmap();
62 gfx::SizeF scaled_size = image_size;
64 if (scaled_size.width() > thumbnail_max_size_pixels.width()) {
65 scaled_size.Scale(thumbnail_max_size_pixels.width() / scaled_size.width());
68 if (scaled_size.height() > thumbnail_max_size_pixels.height()) {
69 scaled_size.Scale(
70 thumbnail_max_size_pixels.height() / scaled_size.height());
73 return skia::ImageOperations::Resize(image.getSkBitmap(),
74 skia::ImageOperations::RESIZE_GOOD,
75 static_cast<int>(scaled_size.width()),
76 static_cast<int>(scaled_size.height()));
79 } // namespace
81 ChromeRenderFrameObserver::ChromeRenderFrameObserver(
82 content::RenderFrame* render_frame)
83 : content::RenderFrameObserver(render_frame) {
86 ChromeRenderFrameObserver::~ChromeRenderFrameObserver() {
89 bool ChromeRenderFrameObserver::OnMessageReceived(const IPC::Message& message) {
90 // Filter only.
91 bool handled = true;
92 IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver, message)
93 IPC_MESSAGE_HANDLER(PrerenderMsg_SetIsPrerendering, OnSetIsPrerendering)
94 IPC_MESSAGE_UNHANDLED(handled = false)
95 IPC_END_MESSAGE_MAP()
96 if (handled)
97 return false;
99 IPC_BEGIN_MESSAGE_MAP(ChromeRenderFrameObserver, message)
100 IPC_MESSAGE_HANDLER(ChromeViewMsg_RequestReloadImageForContextNode,
101 OnRequestReloadImageForContextNode)
102 IPC_MESSAGE_HANDLER(ChromeViewMsg_RequestThumbnailForContextNode,
103 OnRequestThumbnailForContextNode)
104 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu,
105 OnPrintNodeUnderContextMenu)
106 IPC_MESSAGE_HANDLER(ChromeViewMsg_AppBannerPromptRequest,
107 OnAppBannerPromptRequest)
108 IPC_MESSAGE_HANDLER(ChromeViewMsg_AppBannerDebugMessageRequest,
109 OnAppBannerDebugMessageRequest)
110 IPC_MESSAGE_UNHANDLED(handled = false)
111 IPC_END_MESSAGE_MAP()
113 return handled;
116 void ChromeRenderFrameObserver::OnSetIsPrerendering(bool is_prerendering) {
117 if (is_prerendering) {
118 // If the PrerenderHelper for this frame already exists, don't create it. It
119 // can already be created for subframes during handling of
120 // RenderFrameCreated, if the parent frame was prerendering at time of
121 // subframe creation.
122 if (prerender::PrerenderHelper::Get(render_frame()))
123 return;
125 // The PrerenderHelper will destroy itself either after recording histograms
126 // or on destruction of the RenderView.
127 new prerender::PrerenderHelper(render_frame());
131 void ChromeRenderFrameObserver::OnRequestReloadImageForContextNode() {
132 WebNode context_node = render_frame()->GetContextMenuNode();
133 if (!context_node.isNull() && context_node.isElementNode() &&
134 render_frame()->GetWebFrame()) {
135 render_frame()->GetWebFrame()->reloadImage(context_node);
139 void ChromeRenderFrameObserver::OnRequestThumbnailForContextNode(
140 int thumbnail_min_area_pixels,
141 const gfx::Size& thumbnail_max_size_pixels) {
142 WebNode context_node = render_frame()->GetContextMenuNode();
143 SkBitmap thumbnail;
144 gfx::Size original_size;
145 if (!context_node.isNull() && context_node.isElementNode()) {
146 blink::WebImage image = context_node.to<WebElement>().imageContents();
147 original_size = image.size();
148 thumbnail = Downscale(image,
149 thumbnail_min_area_pixels,
150 thumbnail_max_size_pixels);
153 SkBitmap bitmap;
154 if (thumbnail.colorType() == kN32_SkColorType)
155 bitmap = thumbnail;
156 else
157 thumbnail.copyTo(&bitmap, kN32_SkColorType);
159 std::string thumbnail_data;
160 SkAutoLockPixels lock(bitmap);
161 if (bitmap.getPixels()) {
162 const int kDefaultQuality = 90;
163 std::vector<unsigned char> data;
164 if (gfx::JPEGCodec::Encode(
165 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
166 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(),
167 static_cast<int>(bitmap.rowBytes()), kDefaultQuality, &data))
168 thumbnail_data = std::string(data.begin(), data.end());
171 Send(new ChromeViewHostMsg_RequestThumbnailForContextNode_ACK(
172 routing_id(), thumbnail_data, original_size));
175 void ChromeRenderFrameObserver::OnPrintNodeUnderContextMenu() {
176 printing::PrintWebViewHelper* helper =
177 printing::PrintWebViewHelper::Get(render_frame()->GetRenderView());
178 if (helper)
179 helper->PrintNode(render_frame()->GetContextMenuNode());
182 void ChromeRenderFrameObserver::DidFinishDocumentLoad() {
183 // If the navigation is to a localhost URL (and the flag is set to
184 // allow localhost SSL misconfigurations), print a warning to the
185 // console telling the developer to check their SSL configuration
186 // before going to production.
187 bool allow_localhost = base::CommandLine::ForCurrentProcess()->HasSwitch(
188 switches::kAllowInsecureLocalhost);
189 WebDataSource* ds = render_frame()->GetWebFrame()->dataSource();
191 if (allow_localhost) {
192 SSLStatus ssl_status = render_frame()->GetRenderView()->GetSSLStatusOfFrame(
193 render_frame()->GetWebFrame());
194 bool is_cert_error = net::IsCertStatusError(ssl_status.cert_status) &&
195 !net::IsCertStatusMinorError(ssl_status.cert_status);
196 bool is_localhost = net::IsLocalhost(GURL(ds->request().url()).host());
198 if (is_cert_error && is_localhost) {
199 render_frame()->GetWebFrame()->addMessageToConsole(
200 blink::WebConsoleMessage(
201 blink::WebConsoleMessage::LevelWarning,
202 base::ASCIIToUTF16(
203 "This site does not have a valid SSL "
204 "certificate! Without SSL, your site's and "
205 "visitors' data is vulnerable to theft and "
206 "tampering. Get a valid SSL certificate before"
207 " releasing your website to the public.")));
212 void ChromeRenderFrameObserver::OnAppBannerPromptRequest(
213 int request_id,
214 const std::string& platform) {
215 // App banner prompt requests are handled in the general chrome render frame
216 // observer, not the AppBannerClient, as the AppBannerClient is created lazily
217 // by blink and may not exist when the request is sent.
218 blink::WebAppBannerPromptReply reply = blink::WebAppBannerPromptReply::None;
219 blink::WebString web_platform(base::UTF8ToUTF16(platform));
220 blink::WebVector<blink::WebString> web_platforms(&web_platform, 1);
222 blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
223 frame->willShowInstallBannerPrompt(request_id, web_platforms, &reply);
225 // Extract the referrer header for this site according to its referrer policy.
226 // Pass in an empty URL as the destination so that it is always treated
227 // as a cross-origin request.
228 std::string referrer = blink::WebSecurityPolicy::generateReferrerHeader(
229 frame->document().referrerPolicy(), GURL(),
230 frame->document().outgoingReferrer()).utf8();
232 Send(new ChromeViewHostMsg_AppBannerPromptReply(
233 routing_id(), request_id, reply, referrer));
236 void ChromeRenderFrameObserver::OnAppBannerDebugMessageRequest(
237 const std::string& message) {
238 render_frame()->GetWebFrame()->addMessageToConsole(blink::WebConsoleMessage(
239 blink::WebConsoleMessage::LevelDebug, base::UTF8ToUTF16(message)));