Index: content/renderer/pepper/plugin_power_saver_helper.cc |
diff --git a/content/renderer/pepper/plugin_power_saver_helper.cc b/content/renderer/pepper/plugin_power_saver_helper.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..970cdb6464825c55cd95cdeeb5e08fbed4d82003 |
--- /dev/null |
+++ b/content/renderer/pepper/plugin_power_saver_helper.cc |
@@ -0,0 +1,105 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/common/frame_messages.h" |
+#include "content/public/renderer/render_frame.h" |
+#include "content/renderer/pepper/plugin_power_saver_helper.h" |
+#include "third_party/WebKit/public/web/WebDocument.h" |
+#include "third_party/WebKit/public/web/WebLocalFrame.h" |
+#include "third_party/WebKit/public/web/WebView.h" |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+// Maximum dimensions plug-in content may have while still being considered |
+// peripheral content. These match the sizes used by Safari. |
+const int kPeripheralContentMaxWidth = 400; |
+const int kPeripheralContentMaxHeight = 300; |
+} |
Lei Zhang
2014/10/29 09:26:47
nit: blank line before and // namespace
tommycli
2014/10/29 19:59:23
Done.
|
+ |
+PluginPowerSaverHelper::PeripheralPlugin::PeripheralPlugin( |
+ const GURL& content_origin, |
+ const base::Closure& unthrottle) |
+ : content_origin(content_origin), unthrottle(unthrottle) { |
+} |
+ |
+PluginPowerSaverHelper::PeripheralPlugin::~PeripheralPlugin() { |
+} |
+ |
+PluginPowerSaverHelper::PluginPowerSaverHelper(RenderFrame* render_frame) |
+ : RenderFrameObserver(render_frame) { |
+} |
+ |
+PluginPowerSaverHelper::~PluginPowerSaverHelper() { |
+} |
+ |
+bool PluginPowerSaverHelper::OnMessageReceived(const IPC::Message& message) { |
+ bool handled = true; |
+ IPC_BEGIN_MESSAGE_MAP(PluginPowerSaverHelper, message) |
+ IPC_MESSAGE_HANDLER(FrameMsg_UpdatePluginContentOriginWhitelist, |
+ OnUpdatePluginContentOriginWhitelist) |
+ IPC_MESSAGE_UNHANDLED(handled = false) |
+ IPC_END_MESSAGE_MAP() |
+ return handled; |
+} |
+ |
+void PluginPowerSaverHelper::OnUpdatePluginContentOriginWhitelist( |
+ const std::set<GURL>& origin_whitelist) { |
+ origin_whitelist_ = origin_whitelist; |
+ |
+ // Check throttled plugin instances to see if any can be unthrottled. |
+ auto it = peripheral_plugins_.begin(); |
+ while (it != peripheral_plugins_.end()) { |
+ if (origin_whitelist.count(it->content_origin)) { |
+ it->unthrottle.Run(); |
+ it = peripheral_plugins_.erase(it); |
+ } else { |
+ ++it; |
+ } |
+ } |
+} |
+ |
+bool PluginPowerSaverHelper::ShouldThrottleContent( |
+ const GURL& plugin_url, |
+ int width, |
+ int height, |
+ const base::Closure& unthrottle) { |
+ GURL plugin_origin = plugin_url.GetOrigin(); |
+ |
+ // TODO(alexmos): Update this to use the origin of the RemoteFrame when 426512 |
+ // is fixed. For now, case 3 in the comment above doesn't work in |
+ // --site-per-process mode. |
+ blink::WebFrame* main_frame = |
+ render_frame()->GetWebFrame()->view()->mainFrame(); |
+ if (main_frame->isWebRemoteFrame()) { |
+ peripheral_plugins_.push_back(PeripheralPlugin(plugin_origin, unthrottle)); |
Lei Zhang
2014/10/29 09:26:47
ShouldThrottleContent() should be const. You can c
tommycli
2014/10/29 19:59:23
Done.
|
+ return true; |
+ } |
+ |
+ // All same-origin plugin content is essential. |
+ GURL main_frame_origin = GURL(main_frame->document().url()).GetOrigin(); |
+ if (plugin_origin == main_frame_origin) |
+ return false; |
+ |
+ // Whitelisted plugin origins are also essential. |
+ if (origin_whitelist_.count(plugin_origin)) |
+ return false; |
+ |
+ // Cross-origin plugin content is peripheral if smaller than a maximum size. |
+ bool content_is_small = width < kPeripheralContentMaxWidth || |
+ height < kPeripheralContentMaxHeight; |
+ |
+ if (content_is_small) { |
+ peripheral_plugins_.push_back(PeripheralPlugin(plugin_origin, unthrottle)); |
+ return true; |
+ } else { |
Lei Zhang
2014/10/29 09:26:47
nit: you don't need the else, but if you make this
tommycli
2014/10/29 19:59:23
Done.
|
+ origin_whitelist_.insert(plugin_origin); |
+ Send(new FrameHostMsg_PluginContentOriginAllowed( |
Lei Zhang
2014/10/29 09:26:47
You only need to send the message if you actually
tommycli
2014/10/29 19:59:23
Done.
|
+ render_frame()->GetRoutingID(), plugin_origin)); |
+ return false; |
+ } |
+} |
+ |
+} // namespace content |