Commit eda5899e authored by dhnishi's avatar dhnishi Committed by Commit bot

Surface battery auditing information on the website settings option page.

BUG=372607

Review URL: https://codereview.chromium.org/501063002

Cr-Commit-Position: refs/heads/master@{#292190}
parent a1a039ae
......@@ -344,6 +344,9 @@ are declared in build/common.gypi.
<message name="IDS_WEBSITE_SETTINGS_PERMISSION_ASK" desc="The label used in the permissions dropdowns for the option that makes the browser asks for permission. ">
ask
</message>
<message name="IDS_WEBSITE_SETTINGS_TYPE_BATTERY" desc="The label used for the battery consumption type in the Manage Sites settings.">
Battery
</message>
<message name="IDS_WEBSITE_SETTINGS_TYPE_IMAGES" desc="The label used for images permission controls in the Website Settings popup.">
Images
</message>
......@@ -7980,10 +7983,18 @@ Keep your key file in a safe place. You will need it to create new versions of y
<message name="IDS_WEBSITE_SETTINGS_STORAGE_USED" desc="The status text for how much local storage a website origin is using.">
<ph name="USAGE">$1<ex>2.0 KB</ex></ph> stored data
</message>
<message name="IDS_WEBSITE_SETTINGS_BATTERY_USED" desc="The status text for how much power a website origin is using.">
<ph name="USAGE">$1<ex>16</ex></ph>% of battery
</message>
<message name="IDS_WEBSITE_SETTINGS_STORAGE_CLEAR_BUTTON" desc="The 'Clear' button in the Website Settings single site view dialog.">
Clear
</message>
<message name="IDS_WEBSITE_SETTINGS_BATTERY_STOP_BUTTON" desc="The 'Stop' button in the Website Settings single site view dialog.">
Stop
</message>
<message name="IDS_WEBSITE_SETTINGS_BATTERY_PERCENT" desc="The percent from 0 to 100 of battery consumption from a given origin or app on the Website Settings dialog.">
<ph name="VALUE">$1<ex>100</ex></ph>%
</message>
<!-- Automatic updates -->
<if expr="is_macosx">
......
......@@ -62,10 +62,11 @@
.website-properties {
-webkit-margin-after: 8px;
-webkit-margin-start: 8px;
-webkit-padding-after: 4px;
}
.website-property-area {
-webkit-margin-after: 22px;
-webkit-margin-after: 8px;
align-items: center;
display: flex;
justify-content: space-between;
......@@ -102,6 +103,14 @@
border: 1px solid #d9d9d9;
}
#website-settings-edit-page .website-property-button {
min-width: 70px;
}
#website-settings-edit-page .website-property-controls {
min-width: 165px;
}
/* Styles for the origin list elements in the website settings page. */
#origin-list {
border: 1px solid #d9d9d9;
......
......@@ -14,6 +14,8 @@
i18n-content="websitesLabelNotifications"></option>
<option value="storage"
i18n-content="websitesLabelStorage"></option>
<option value="battery"
i18n-content="websitesLabelBattery"></option>
</select>
<input type="search" id="website-settings-search-box"
i18n-values="placeholder:websitesSearch" incremental>
......
......@@ -44,6 +44,8 @@ cr.define('options', function() {
assert(target.tagName == 'SELECT');
if (target.value == 'storage')
chrome.send('updateLocalStorage');
else if (target.value == 'battery')
chrome.send('updateBatteryUsage');
else
chrome.send('updateOrigins', [target.value]);
};
......
......@@ -34,6 +34,10 @@ cr.define('options.WebsiteSettings', function() {
chrome.send('deleteLocalStorage');
};
$('website-settings-battery-stop-button').onclick = function(event) {
chrome.send('stopOrigin');
};
$('websiteSettingsEditorCancelButton').onclick =
PageManager.closeOverlay.bind(PageManager);
......@@ -61,13 +65,16 @@ cr.define('options.WebsiteSettings', function() {
/**
* Populates and displays the page with given origin information.
* @param {string} localStorage A string describing the local storage use.
* @param {string} batteryUsage A string describing the battery use.
* @param {Object} permissions A dictionary of permissions to their
* available and current settings, and if it is editable.
* @param {boolean} showPage If the page should raised.
* @private
*/
populateOrigin_: function(localStorage, permissions, showPage) {
populateOrigin_: function(localStorage, batteryUsage, permissions,
showPage) {
$('local-storage-title').textContent = localStorage;
$('battery-title').textContent = batteryUsage;
for (var key in permissions) {
var selector = $(key + '-select-option');
......@@ -98,9 +105,10 @@ cr.define('options.WebsiteSettings', function() {
},
};
WebsiteSettingsEditor.populateOrigin = function(localStorage, permissions,
showPage) {
WebsiteSettingsEditor.populateOrigin = function(localStorage, batteryUsage,
permissions, showPage) {
WebsiteSettingsEditor.getInstance().populateOrigin_(localStorage,
batteryUsage,
permissions,
showPage);
};
......
......@@ -6,8 +6,19 @@
<h3 id="website-title" class="title favicon-cell weaktrl"></h3>
<div class="website-property-area">
<div id="local-storage-title" class="website-property"></div>
<button id="website-settings-storage-delete-button"
class="website-permission-button" i18n-content="websitesButtonClear">
<div class="website-property-controls">
<button id="website-settings-storage-delete-button"
class="website-property-button"
i18n-content="websitesButtonClear">
</div>
</div>
<div class="website-property-area">
<div id="battery-title" class="website-property"></div>
<div class="website-property-controls">
<button id="website-settings-battery-stop-button"
class="website-property-button"
i18n-content="websitesButtonStop">
</div>
</div>
</div>
<div class="website-column-headers">
......
......@@ -13,6 +13,8 @@
#include "chrome/browser/ui/browser_iterator.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/grit/generated_resources.h"
#include "components/power/origin_power_map.h"
#include "components/power/origin_power_map_factory.h"
#include "content/public/browser/dom_storage_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
......@@ -25,6 +27,9 @@
#include "ui/base/l10n/time_format.h"
#include "ui/base/text/bytes_formatting.h"
using power::OriginPowerMap;
using power::OriginPowerMapFactory;
namespace {
const int kHttpPort = 80;
......@@ -61,6 +66,7 @@ void WebsiteSettingsHandler::GetLocalizedValues(
{"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM},
{"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS},
{"websitesLabelStorage", IDS_WEBSITE_SETTINGS_TYPE_STORAGE},
{"websitesLabelBattery", IDS_WEBSITE_SETTINGS_TYPE_BATTERY},
{"websitesCookiesDescription", IDS_WEBSITE_SETTINGS_COOKIES_DESCRIPTION},
{"websitesLocationDescription",
IDS_WEBSITE_SETTINGS_LOCATION_DESCRIPTION},
......@@ -69,6 +75,7 @@ void WebsiteSettingsHandler::GetLocalizedValues(
{"websitesNotificationsDescription",
IDS_WEBSITE_SETTINGS_NOTIFICATIONS_DESCRIPTION},
{"websitesButtonClear", IDS_WEBSITE_SETTINGS_STORAGE_CLEAR_BUTTON},
{"websitesButtonStop", IDS_WEBSITE_SETTINGS_BATTERY_STOP_BUTTON},
};
RegisterStrings(localized_strings, resources, arraysize(resources));
......@@ -98,6 +105,11 @@ void WebsiteSettingsHandler::RegisterMessages() {
base::Bind(&WebsiteSettingsHandler::HandleUpdateLocalStorage,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"updateBatteryUsage",
base::Bind(&WebsiteSettingsHandler::HandleUpdateBatteryUsage,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getOriginInfo",
base::Bind(&WebsiteSettingsHandler::HandleGetOriginInfo,
......@@ -364,6 +376,34 @@ void WebsiteSettingsHandler::HandleSetOriginPermission(
info);
}
void WebsiteSettingsHandler::HandleUpdateBatteryUsage(
const base::ListValue* args) {
base::DictionaryValue power_map;
OriginPowerMap* origins =
OriginPowerMapFactory::GetForBrowserContext(Profile::FromWebUI(web_ui()));
OriginPowerMap::PercentOriginMap percent_map = origins->GetPercentOriginMap();
for (std::map<GURL, int>::iterator it = percent_map.begin();
it != percent_map.end();
++it) {
std::string origin = it->first.spec();
if (origin.find(last_filter_) == base::string16::npos)
continue;
base::DictionaryValue* origin_entry = new base::DictionaryValue();
origin_entry->SetInteger("usage", it->second);
origin_entry->SetString(
"usageString",
l10n_util::GetStringFUTF16Int(IDS_WEBSITE_SETTINGS_BATTERY_PERCENT,
it->second));
origin_entry->SetStringWithoutPathExpansion(
"readableName", GetReadableName(it->first));
power_map.SetWithoutPathExpansion(origin, origin_entry);
}
web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
power_map);
}
void WebsiteSettingsHandler::HandleDeleteLocalStorage(
const base::ListValue* args) {
DCHECK(!last_site_.is_empty());
......@@ -390,6 +430,10 @@ void WebsiteSettingsHandler::GetInfoForOrigin(const GURL& site_url,
}
}
int battery = 0;
battery = OriginPowerMapFactory::GetForBrowserContext(
Profile::FromWebUI(web_ui()))->GetPowerForOrigin(site_url);
base::DictionaryValue* permissions = new base::DictionaryValue;
for (size_t i = 0; i < arraysize(kValidTypes); ++i) {
ContentSettingsType permission_type = kValidTypes[i];
......@@ -451,9 +495,13 @@ void WebsiteSettingsHandler::GetInfoForOrigin(const GURL& site_url,
base::Value* storage_used = new base::StringValue(l10n_util::GetStringFUTF16(
IDS_WEBSITE_SETTINGS_STORAGE_USED, ui::FormatBytes(storage)));
base::Value* battery_used =
new base::StringValue(l10n_util::GetStringFUTF16Int(
IDS_WEBSITE_SETTINGS_BATTERY_USED, battery));
web_ui()->CallJavascriptFunction("WebsiteSettingsEditor.populateOrigin",
*storage_used,
*battery_used,
*permissions,
base::FundamentalValue(show_page));
}
......
......@@ -70,6 +70,10 @@ class WebsiteSettingsHandler : public content_settings::Observer,
// |args| is the URL.
void HandleMaybeShowEditPage(const base::ListValue* args);
// Get all origins that have used power, filter them by |last_filter_|, and
// update the page.
void HandleUpdateBatteryUsage(const base::ListValue* args);
// Deletes the local storage and repopulates the page.
void HandleDeleteLocalStorage(const base::ListValue* args);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment