Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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. | |
| 4 | |
| 5 #include "base/command_line.h" | |
| 6 #include "base/metrics/histogram_macros.h" | |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "chrome/renderer/plugins/power_saver_info.h" | |
| 9 #include "content/public/common/content_constants.h" | |
| 10 #include "content/public/common/content_switches.h" | |
| 11 #include "content/public/common/webplugininfo.h" | |
| 12 #include "content/public/renderer/render_frame.h" | |
| 13 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" | |
| 14 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
| 15 #include "third_party/WebKit/public/web/WebPluginParams.h" | |
| 16 #include "url/origin.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // Presence of the poster param within plugin object tags. | |
| 21 // These numeric values are used in UMA logs; do not change them. | |
| 22 enum PosterParamPresence { | |
| 23 POSTER_PRESENCE_NO_PARAM_PPS_DISABLED = 0, | |
| 24 POSTER_PRESENCE_NO_PARAM_PPS_ENABLED = 1, | |
| 25 POSTER_PRESENCE_PARAM_EXISTS_PPS_DISABLED = 2, | |
| 26 POSTER_PRESENCE_PARAM_EXISTS_PPS_ENABLED = 3, | |
| 27 POSTER_PRESENCE_NUM_ITEMS | |
| 28 }; | |
| 29 | |
| 30 const char kPluginPowerSaverPosterParamPresenceHistogram[] = | |
| 31 "Plugin.PowerSaver.PosterParamPresence"; | |
| 32 | |
| 33 void RecordPosterParamPresence(PosterParamPresence presence) { | |
| 34 UMA_HISTOGRAM_ENUMERATION(kPluginPowerSaverPosterParamPresenceHistogram, | |
| 35 presence, POSTER_PRESENCE_NUM_ITEMS); | |
| 36 } | |
| 37 | |
| 38 void TrackPosterParamPresence(const blink::WebPluginParams& params, | |
| 39 bool power_saver_enabled) { | |
| 40 DCHECK_EQ(params.attributeNames.size(), params.attributeValues.size()); | |
| 41 | |
| 42 for (size_t i = 0; i < params.attributeNames.size(); ++i) { | |
| 43 if (params.attributeNames[i].utf8() == "poster") { | |
| 44 if (power_saver_enabled) | |
|
Lei Zhang
2015/12/10 01:01:33
RecordPosterParamPresence(power_saver_enabled ? FO
tommycli
2015/12/10 23:11:16
Done.
| |
| 45 RecordPosterParamPresence(POSTER_PRESENCE_PARAM_EXISTS_PPS_ENABLED); | |
| 46 else | |
| 47 RecordPosterParamPresence(POSTER_PRESENCE_PARAM_EXISTS_PPS_DISABLED); | |
| 48 | |
| 49 return; | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 if (power_saver_enabled) | |
| 54 RecordPosterParamPresence(POSTER_PRESENCE_NO_PARAM_PPS_ENABLED); | |
| 55 else | |
| 56 RecordPosterParamPresence(POSTER_PRESENCE_NO_PARAM_PPS_DISABLED); | |
| 57 } | |
| 58 | |
| 59 std::string GetPluginInstancePosterAttribute( | |
| 60 const blink::WebPluginParams& params) { | |
| 61 DCHECK_EQ(params.attributeNames.size(), params.attributeValues.size()); | |
| 62 | |
| 63 for (size_t i = 0; i < params.attributeNames.size(); ++i) { | |
| 64 if (params.attributeNames[i].utf8() == "poster" && | |
| 65 !params.attributeValues[i].isEmpty()) { | |
| 66 return params.attributeValues[i].utf8(); | |
| 67 } | |
| 68 } | |
| 69 return std::string(); | |
| 70 } | |
| 71 | |
| 72 } // namespace | |
| 73 | |
| 74 PowerSaverInfo::PowerSaverInfo() | |
| 75 : power_saver_enabled(false), blocked_for_background_tab(false) {} | |
| 76 | |
| 77 PowerSaverInfo PowerSaverInfo::Get(content::RenderFrame* render_frame, | |
| 78 bool power_saver_setting_on, | |
| 79 const blink::WebPluginParams& params, | |
| 80 const content::WebPluginInfo& plugin_info, | |
| 81 const GURL& document_url) { | |
| 82 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
| 83 std::string override_for_testing = command_line->GetSwitchValueASCII( | |
| 84 switches::kOverridePluginPowerSaverForTesting); | |
| 85 | |
| 86 // This feature has only been tested thoroughly with Flash thus far. | |
| 87 // It is also enabled for the Power Saver test plugin for browser tests. | |
| 88 bool is_flash = | |
| 89 plugin_info.name == base::ASCIIToUTF16(content::kFlashPluginName); | |
| 90 bool can_throttle_plugin_type = | |
| 91 is_flash || override_for_testing == "ignore-list"; | |
| 92 | |
| 93 PowerSaverInfo info; | |
| 94 info.power_saver_enabled = | |
| 95 override_for_testing == "always" || | |
| 96 (power_saver_setting_on && can_throttle_plugin_type); | |
| 97 | |
| 98 if (info.power_saver_enabled) { | |
| 99 // Even if we disable PPS in the next block because content is same-origin, | |
| 100 // it should still be eligible for background tab deferral if PPS is on. | |
| 101 info.blocked_for_background_tab = render_frame->IsHidden(); | |
| 102 | |
| 103 auto status = render_frame->GetPeripheralContentStatus( | |
| 104 render_frame->GetWebFrame()->top()->securityOrigin(), | |
| 105 url::Origin(params.url), gfx::Size()); | |
| 106 | |
| 107 if (status == content::RenderFrame::CONTENT_STATUS_ESSENTIAL_SAME_ORIGIN || | |
| 108 status == content::RenderFrame:: | |
| 109 CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_WHITELISTED) { | |
|
Lei Zhang
2015/12/10 01:01:33
If you add "using content::RenderFrame", can you g
tommycli
2015/12/10 23:11:16
Unfortunately it still doesn't fit.
| |
| 110 info.power_saver_enabled = false; | |
| 111 } else { | |
| 112 info.poster_attribute = GetPluginInstancePosterAttribute(params); | |
| 113 info.base_url = document_url; | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 if (is_flash) | |
| 118 TrackPosterParamPresence(params, info.power_saver_enabled); | |
| 119 | |
| 120 return info; | |
| 121 } | |
| OLD | NEW |