| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/pepper/plugin_power_saver_helper.h" | 5 #include "content/renderer/pepper/plugin_power_saver_helper.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "content/common/frame_messages.h" | 12 #include "content/common/frame_messages.h" |
| 13 #include "content/public/common/content_constants.h" | 13 #include "content/public/common/content_constants.h" |
| 14 #include "content/public/common/content_switches.h" | 14 #include "content/public/common/content_switches.h" |
| 15 #include "content/public/renderer/plugin_instance_throttler.h" | 15 #include "content/public/renderer/plugin_instance_throttler.h" |
| 16 #include "content/public/renderer/render_frame.h" | 16 #include "content/public/renderer/render_frame.h" |
| 17 #include "ppapi/shared_impl/ppapi_constants.h" | 17 #include "ppapi/shared_impl/ppapi_constants.h" |
| 18 #include "third_party/WebKit/public/web/WebDocument.h" | |
| 19 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 18 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 20 #include "third_party/WebKit/public/web/WebPluginParams.h" | |
| 21 #include "third_party/WebKit/public/web/WebView.h" | |
| 22 | 19 |
| 23 namespace content { | 20 namespace content { |
| 24 | 21 |
| 25 namespace { | 22 namespace { |
| 26 | 23 |
| 27 // Initial decision of the peripheral content decision. | 24 // Initial decision of the peripheral content decision. |
| 28 // These numeric values are used in UMA logs; do not change them. | 25 // These numeric values are used in UMA logs; do not change them. |
| 29 enum PeripheralHeuristicDecision { | 26 enum PeripheralHeuristicDecision { |
| 30 HEURISTIC_DECISION_PERIPHERAL = 0, | 27 HEURISTIC_DECISION_PERIPHERAL = 0, |
| 31 HEURISTIC_DECISION_ESSENTIAL_SAME_ORIGIN = 1, | 28 HEURISTIC_DECISION_ESSENTIAL_SAME_ORIGIN = 1, |
| 32 HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_BIG = 2, | 29 HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_BIG = 2, |
| 33 HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_WHITELISTED = 3, | 30 HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_WHITELISTED = 3, |
| 34 HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_TINY = 4, | 31 HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_TINY = 4, |
| 32 HEURISTIC_DECISION_ESSENTIAL_UNKNOWN_SIZE = 5, |
| 35 HEURISTIC_DECISION_NUM_ITEMS | 33 HEURISTIC_DECISION_NUM_ITEMS |
| 36 }; | 34 }; |
| 37 | 35 |
| 38 const char kPeripheralHeuristicHistogram[] = | 36 const char kPeripheralHeuristicHistogram[] = |
| 39 "Plugin.PowerSaver.PeripheralHeuristic"; | 37 "Plugin.PowerSaver.PeripheralHeuristic"; |
| 40 | 38 |
| 41 // Plugin content below this size in height and width is considered "tiny". | 39 // Plugin content below this size in height and width is considered "tiny". |
| 42 // Tiny content is never peripheral, as tiny plugins often serve a critical | 40 // Tiny content is never peripheral, as tiny plugins often serve a critical |
| 43 // purpose, and the user often cannot find and click to unthrottle it. | 41 // purpose, and the user often cannot find and click to unthrottle it. |
| 44 const int kPeripheralContentTinySize = 5; | 42 const int kPeripheralContentTinySize = 5; |
| 45 | 43 |
| 46 void RecordDecisionMetric(PeripheralHeuristicDecision decision) { | 44 void RecordDecisionMetric(PeripheralHeuristicDecision decision) { |
| 47 UMA_HISTOGRAM_ENUMERATION(kPeripheralHeuristicHistogram, decision, | 45 UMA_HISTOGRAM_ENUMERATION(kPeripheralHeuristicHistogram, decision, |
| 48 HEURISTIC_DECISION_NUM_ITEMS); | 46 HEURISTIC_DECISION_NUM_ITEMS); |
| 49 } | 47 } |
| 50 | 48 |
| 51 } // namespace | 49 } // namespace |
| 52 | 50 |
| 53 PluginPowerSaverHelper::PeripheralPlugin::PeripheralPlugin( | 51 PluginPowerSaverHelper::PeripheralPlugin::PeripheralPlugin( |
| 54 const GURL& content_origin, | 52 const url::Origin& content_origin, |
| 55 const base::Closure& unthrottle_callback) | 53 const base::Closure& unthrottle_callback) |
| 56 : content_origin(content_origin), unthrottle_callback(unthrottle_callback) { | 54 : content_origin(content_origin), |
| 57 } | 55 unthrottle_callback(unthrottle_callback) {} |
| 58 | 56 |
| 59 PluginPowerSaverHelper::PeripheralPlugin::~PeripheralPlugin() { | 57 PluginPowerSaverHelper::PeripheralPlugin::~PeripheralPlugin() { |
| 60 } | 58 } |
| 61 | 59 |
| 62 PluginPowerSaverHelper::PluginPowerSaverHelper(RenderFrame* render_frame) | 60 PluginPowerSaverHelper::PluginPowerSaverHelper(RenderFrame* render_frame) |
| 63 : RenderFrameObserver(render_frame) | 61 : RenderFrameObserver(render_frame) |
| 64 , override_for_testing_(Normal) { | 62 , override_for_testing_(Normal) { |
| 65 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); | 63 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); |
| 66 std::string override_for_testing = command_line.GetSwitchValueASCII( | 64 std::string override_for_testing = command_line.GetSwitchValueASCII( |
| 67 switches::kOverridePluginPowerSaverForTesting); | 65 switches::kOverridePluginPowerSaverForTesting); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 91 bool handled = true; | 89 bool handled = true; |
| 92 IPC_BEGIN_MESSAGE_MAP(PluginPowerSaverHelper, message) | 90 IPC_BEGIN_MESSAGE_MAP(PluginPowerSaverHelper, message) |
| 93 IPC_MESSAGE_HANDLER(FrameMsg_UpdatePluginContentOriginWhitelist, | 91 IPC_MESSAGE_HANDLER(FrameMsg_UpdatePluginContentOriginWhitelist, |
| 94 OnUpdatePluginContentOriginWhitelist) | 92 OnUpdatePluginContentOriginWhitelist) |
| 95 IPC_MESSAGE_UNHANDLED(handled = false) | 93 IPC_MESSAGE_UNHANDLED(handled = false) |
| 96 IPC_END_MESSAGE_MAP() | 94 IPC_END_MESSAGE_MAP() |
| 97 return handled; | 95 return handled; |
| 98 } | 96 } |
| 99 | 97 |
| 100 void PluginPowerSaverHelper::OnUpdatePluginContentOriginWhitelist( | 98 void PluginPowerSaverHelper::OnUpdatePluginContentOriginWhitelist( |
| 101 const std::set<GURL>& origin_whitelist) { | 99 const std::set<url::Origin>& origin_whitelist) { |
| 102 origin_whitelist_ = origin_whitelist; | 100 origin_whitelist_ = origin_whitelist; |
| 103 | 101 |
| 104 // Check throttled plugin instances to see if any can be unthrottled. | 102 // Check throttled plugin instances to see if any can be unthrottled. |
| 105 auto it = peripheral_plugins_.begin(); | 103 auto it = peripheral_plugins_.begin(); |
| 106 while (it != peripheral_plugins_.end()) { | 104 while (it != peripheral_plugins_.end()) { |
| 107 if (origin_whitelist.count(it->content_origin)) { | 105 if (origin_whitelist.count(it->content_origin)) { |
| 108 it->unthrottle_callback.Run(); | 106 it->unthrottle_callback.Run(); |
| 109 it = peripheral_plugins_.erase(it); | 107 it = peripheral_plugins_.erase(it); |
| 110 } else { | 108 } else { |
| 111 ++it; | 109 ++it; |
| 112 } | 110 } |
| 113 } | 111 } |
| 114 } | 112 } |
| 115 | 113 |
| 116 void PluginPowerSaverHelper::RegisterPeripheralPlugin( | 114 void PluginPowerSaverHelper::RegisterPeripheralPlugin( |
| 117 const GURL& content_origin, | 115 const url::Origin& content_origin, |
| 118 const base::Closure& unthrottle_callback) { | 116 const base::Closure& unthrottle_callback) { |
| 119 DCHECK_EQ(content_origin.GetOrigin(), content_origin); | |
| 120 peripheral_plugins_.push_back( | 117 peripheral_plugins_.push_back( |
| 121 PeripheralPlugin(content_origin, unthrottle_callback)); | 118 PeripheralPlugin(content_origin, unthrottle_callback)); |
| 122 } | 119 } |
| 123 | 120 |
| 124 bool PluginPowerSaverHelper::ShouldThrottleContent( | 121 bool PluginPowerSaverHelper::ShouldThrottleContent( |
| 125 const GURL& content_origin, | 122 const url::Origin& main_frame_origin, |
| 123 const url::Origin& content_origin, |
| 126 const std::string& plugin_module_name, | 124 const std::string& plugin_module_name, |
| 127 int width, | 125 int width, |
| 128 int height, | 126 int height, |
| 129 bool* cross_origin_main_content) const { | 127 bool* cross_origin_main_content) const { |
| 130 DCHECK_EQ(content_origin.GetOrigin(), content_origin); | |
| 131 if (cross_origin_main_content) | 128 if (cross_origin_main_content) |
| 132 *cross_origin_main_content = false; | 129 *cross_origin_main_content = false; |
| 133 | 130 |
| 134 // This feature has only been tested throughly with Flash thus far. | 131 // This feature has only been tested throughly with Flash thus far. |
| 135 // It is also enabled for the Power Saver test plugin for browser tests. | 132 // It is also enabled for the Power Saver test plugin for browser tests. |
| 136 if (override_for_testing_ == Always) { | 133 if (override_for_testing_ == Always) { |
| 137 return true; | 134 return true; |
| 138 } else if (override_for_testing_ == Never) { | 135 } else if (override_for_testing_ == Never) { |
| 139 return false; | 136 return false; |
| 140 } else if (override_for_testing_ == Normal && | 137 } else if (override_for_testing_ == Normal && |
| 141 plugin_module_name != content::kFlashPluginName) { | 138 plugin_module_name != content::kFlashPluginName) { |
| 142 return false; | 139 return false; |
| 143 } | 140 } |
| 144 | 141 |
| 145 if (width <= 0 || height <= 0) | 142 if (main_frame_origin.IsSameOriginWith(content_origin)) { |
| 146 return false; | |
| 147 | |
| 148 // TODO(alexmos): Update this to use the origin of the RemoteFrame when 426512 | |
| 149 // is fixed. For now, case 3 in the class level comment doesn't work in | |
| 150 // --site-per-process mode. | |
| 151 blink::WebFrame* main_frame = | |
| 152 render_frame()->GetWebFrame()->view()->mainFrame(); | |
| 153 if (main_frame->isWebRemoteFrame()) { | |
| 154 RecordDecisionMetric(HEURISTIC_DECISION_PERIPHERAL); | |
| 155 return true; | |
| 156 } | |
| 157 | |
| 158 // All same-origin plugin content is essential. | |
| 159 GURL main_frame_origin = GURL(main_frame->document().url()).GetOrigin(); | |
| 160 if (content_origin == main_frame_origin) { | |
| 161 RecordDecisionMetric(HEURISTIC_DECISION_ESSENTIAL_SAME_ORIGIN); | 143 RecordDecisionMetric(HEURISTIC_DECISION_ESSENTIAL_SAME_ORIGIN); |
| 162 return false; | 144 return false; |
| 163 } | 145 } |
| 164 | 146 |
| 147 if (width <= 0 || height <= 0) { |
| 148 RecordDecisionMetric(HEURISTIC_DECISION_ESSENTIAL_UNKNOWN_SIZE); |
| 149 return false; |
| 150 } |
| 151 |
| 165 // Whitelisted plugin origins are also essential. | 152 // Whitelisted plugin origins are also essential. |
| 166 if (origin_whitelist_.count(content_origin)) { | 153 if (origin_whitelist_.count(content_origin)) { |
| 167 RecordDecisionMetric(HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_WHITELISTED); | 154 RecordDecisionMetric(HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_WHITELISTED); |
| 168 return false; | 155 return false; |
| 169 } | 156 } |
| 170 | 157 |
| 171 // Never mark tiny content as peripheral. | 158 // Never mark tiny content as peripheral. |
| 172 if (width <= kPeripheralContentTinySize && | 159 if (width <= kPeripheralContentTinySize && |
| 173 height <= kPeripheralContentTinySize) { | 160 height <= kPeripheralContentTinySize) { |
| 174 RecordDecisionMetric(HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_TINY); | 161 RecordDecisionMetric(HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_TINY); |
| 175 return false; | 162 return false; |
| 176 } | 163 } |
| 177 | 164 |
| 178 // Plugin content large in both dimensions are the "main attraction". | 165 // Plugin content large in both dimensions are the "main attraction". |
| 179 if (PluginInstanceThrottler::IsLargeContent(width, height)) { | 166 if (PluginInstanceThrottler::IsLargeContent(width, height)) { |
| 180 RecordDecisionMetric(HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_BIG); | 167 RecordDecisionMetric(HEURISTIC_DECISION_ESSENTIAL_CROSS_ORIGIN_BIG); |
| 181 if (cross_origin_main_content) | 168 if (cross_origin_main_content) |
| 182 *cross_origin_main_content = true; | 169 *cross_origin_main_content = true; |
| 183 return false; | 170 return false; |
| 184 } | 171 } |
| 185 | 172 |
| 186 RecordDecisionMetric(HEURISTIC_DECISION_PERIPHERAL); | 173 RecordDecisionMetric(HEURISTIC_DECISION_PERIPHERAL); |
| 187 return true; | 174 return true; |
| 188 } | 175 } |
| 189 | 176 |
| 190 void PluginPowerSaverHelper::WhitelistContentOrigin( | 177 void PluginPowerSaverHelper::WhitelistContentOrigin( |
| 191 const GURL& content_origin) { | 178 const url::Origin& content_origin) { |
| 192 DCHECK_EQ(content_origin.GetOrigin(), content_origin); | |
| 193 if (origin_whitelist_.insert(content_origin).second) { | 179 if (origin_whitelist_.insert(content_origin).second) { |
| 194 Send(new FrameHostMsg_PluginContentOriginAllowed( | 180 Send(new FrameHostMsg_PluginContentOriginAllowed( |
| 195 render_frame()->GetRoutingID(), content_origin)); | 181 render_frame()->GetRoutingID(), content_origin)); |
| 196 } | 182 } |
| 197 } | 183 } |
| 198 | 184 |
| 199 } // namespace content | 185 } // namespace content |
| OLD | NEW |