Commit 828da409 authored by dhnishi's avatar dhnishi Committed by Commit bot

Update the Website Settings page when the origin power map for the

profile is updated.

BUG=409093

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

Cr-Commit-Position: refs/heads/master@{#293177}
parent 71992c77
......@@ -174,6 +174,17 @@ void ProcessPowerCollector::RecordCpuUsageByOrigin(double total_cpu_percent) {
DCHECK(origin_power_map);
origin_power_map->AddPowerForOrigin(origin, last_process_power_usage);
}
// Iterate over all profiles to let them know we've finished updating.
ProfileManager* pm = g_browser_process->profile_manager();
std::vector<Profile*> open_profiles = pm->GetLoadedProfiles();
for (std::vector<Profile*>::const_iterator it = open_profiles.begin();
it != open_profiles.end();
++it) {
power::OriginPowerMap* origin_power_map =
power::OriginPowerMapFactory::GetForBrowserContext(*it);
origin_power_map->OnAllOriginsUpdated();
}
}
void ProcessPowerCollector::UpdateProcessInMap(
......
......@@ -32,6 +32,7 @@ using power::OriginPowerMapFactory;
namespace {
const char kBattery[] = "battery";
const int kHttpPort = 80;
const int kHttpsPort = 443;
const char kPreferencesSource[] = "preference";
......@@ -42,12 +43,14 @@ const ContentSettingsType kValidTypes[] = {
CONTENT_SETTINGS_TYPE_MEDIASTREAM,
CONTENT_SETTINGS_TYPE_COOKIES};
const size_t kValidTypesLength = arraysize(kValidTypes);
} // namespace
namespace options {
WebsiteSettingsHandler::WebsiteSettingsHandler()
: observer_(this), weak_ptr_factory_(this) {
: observer_(this),
weak_ptr_factory_(this) {
}
WebsiteSettingsHandler::~WebsiteSettingsHandler() {
......@@ -87,6 +90,14 @@ void WebsiteSettingsHandler::InitializeHandler() {
Profile* profile = Profile::FromWebUI(web_ui());
HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
observer_.Add(settings);
power::OriginPowerMap* origin_power_map =
power::OriginPowerMapFactory::GetForBrowserContext(profile);
// OriginPowerMap may not be available in tests.
if (origin_power_map) {
subscription_ = origin_power_map->AddPowerConsumptionUpdatedCallback(
base::Bind(&WebsiteSettingsHandler::Update, base::Unretained(this)));
}
}
void WebsiteSettingsHandler::RegisterMessages() {
......@@ -217,6 +228,8 @@ void WebsiteSettingsHandler::Update() {
DCHECK(!last_setting_.empty());
if (last_setting_ == kStorage)
UpdateLocalStorage();
else if (last_setting_ == kBattery)
UpdateBatteryUsage();
else
UpdateOrigins();
}
......@@ -378,30 +391,8 @@ void WebsiteSettingsHandler::HandleSetOriginPermission(
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);
last_setting_ = kBattery;
UpdateBatteryUsage();
}
void WebsiteSettingsHandler::HandleDeleteLocalStorage(
......@@ -529,6 +520,33 @@ void WebsiteSettingsHandler::UpdateLocalStorage() {
local_storage_map);
}
void WebsiteSettingsHandler::UpdateBatteryUsage() {
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::StopOrigin(const GURL& site_url) {
Profile* profile = Profile::FromWebUI(web_ui());
if (site_url.SchemeIs(extensions::kExtensionScheme)) {
......
......@@ -15,6 +15,7 @@
#include "chrome/browser/content_settings/host_content_settings_map.h"
#include "chrome/browser/content_settings/local_shared_objects_container.h"
#include "chrome/browser/ui/webui/options/options_ui.h"
#include "components/power/origin_power_map.h"
namespace options {
......@@ -92,6 +93,10 @@ class WebsiteSettingsHandler : public content_settings::Observer,
// and update the page.
void UpdateLocalStorage();
// Get all origins with power consumption, filter them by |last_filter_|,
// and update the page.
void UpdateBatteryUsage();
// Kill all tabs and app windows which have the same origin as |site_url|.
void StopOrigin(const GURL& site_url);
......@@ -118,6 +123,9 @@ class WebsiteSettingsHandler : public content_settings::Observer,
// Observer to watch for content settings changes.
ScopedObserver<HostContentSettingsMap, content_settings::Observer> observer_;
// Subscription to watch for power consumption updates.
scoped_ptr<power::OriginPowerMap::Subscription> subscription_;
base::WeakPtrFactory<WebsiteSettingsHandler> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(WebsiteSettingsHandler);
......
......@@ -49,4 +49,14 @@ OriginPowerMap::PercentOriginMap OriginPowerMap::GetPercentOriginMap() {
return percent_map;
}
scoped_ptr<OriginPowerMap::Subscription>
OriginPowerMap::AddPowerConsumptionUpdatedCallback(
const base::Closure& callback) {
return callback_list_.Add(callback);
}
void OriginPowerMap::OnAllOriginsUpdated() {
callback_list_.Notify();
}
} // namespace power
......@@ -7,6 +7,7 @@
#include <map>
#include "base/callback_list.h"
#include "components/keyed_service/core/keyed_service.h"
#include "url/gurl.h"
......@@ -17,6 +18,7 @@ namespace power {
class OriginPowerMap : public KeyedService {
public:
typedef std::map<GURL, int> PercentOriginMap;
typedef base::CallbackList<void(void)>::Subscription Subscription;
OriginPowerMap();
virtual ~OriginPowerMap();
......@@ -33,6 +35,14 @@ class OriginPowerMap : public KeyedService {
// consumed.
PercentOriginMap GetPercentOriginMap();
// Adds a callback for the completion of a round of updates to |origin_map_|.
scoped_ptr<Subscription> AddPowerConsumptionUpdatedCallback(
const base::Closure& callback);
// Notifies observers to let them know that the origin power map has finished
// updating for all origins this cycle.
void OnAllOriginsUpdated();
private:
// OriginMap maps a URL to the amount of power consumed by the URL using the
// same units as |total_consumed_|.
......@@ -43,6 +53,8 @@ class OriginPowerMap : public KeyedService {
// the power heuristics available to the platform.
double total_consumed_;
base::CallbackList<void(void)> callback_list_;
DISALLOW_COPY_AND_ASSIGN(OriginPowerMap);
};
......
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