diff options
author | dhnishi <dhnishi@chromium.org> | 2014-09-03 11:40:49 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-09-03 18:53:29 +0000 |
commit | 828da409d7ffa5a837187eedf77af3f867f5ee31 (patch) | |
tree | b44e4d60d14ac8162aa59aec9f10ebbbb4587438 | |
parent | 71992c77b8cdb27ea11920536ac3dd19ff5fedf8 (diff) | |
download | chromium_src-828da409d7ffa5a837187eedf77af3f867f5ee31.zip chromium_src-828da409d7ffa5a837187eedf77af3f867f5ee31.tar.gz chromium_src-828da409d7ffa5a837187eedf77af3f867f5ee31.tar.bz2 |
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}
-rw-r--r-- | chrome/browser/power/process_power_collector.cc | 11 | ||||
-rw-r--r-- | chrome/browser/ui/webui/options/website_settings_handler.cc | 68 | ||||
-rw-r--r-- | chrome/browser/ui/webui/options/website_settings_handler.h | 8 | ||||
-rw-r--r-- | components/power/origin_power_map.cc | 10 | ||||
-rw-r--r-- | components/power/origin_power_map.h | 12 |
5 files changed, 84 insertions, 25 deletions
diff --git a/chrome/browser/power/process_power_collector.cc b/chrome/browser/power/process_power_collector.cc index da85573..5d44e61 100644 --- a/chrome/browser/power/process_power_collector.cc +++ b/chrome/browser/power/process_power_collector.cc @@ -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( diff --git a/chrome/browser/ui/webui/options/website_settings_handler.cc b/chrome/browser/ui/webui/options/website_settings_handler.cc index a31bbb1..38405e1 100644 --- a/chrome/browser/ui/webui/options/website_settings_handler.cc +++ b/chrome/browser/ui/webui/options/website_settings_handler.cc @@ -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)) { diff --git a/chrome/browser/ui/webui/options/website_settings_handler.h b/chrome/browser/ui/webui/options/website_settings_handler.h index 2ebf495..fd83829 100644 --- a/chrome/browser/ui/webui/options/website_settings_handler.h +++ b/chrome/browser/ui/webui/options/website_settings_handler.h @@ -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); diff --git a/components/power/origin_power_map.cc b/components/power/origin_power_map.cc index dfb0f17..4ce32e2 100644 --- a/components/power/origin_power_map.cc +++ b/components/power/origin_power_map.cc @@ -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 diff --git a/components/power/origin_power_map.h b/components/power/origin_power_map.h index 0936be4..36caa02 100644 --- a/components/power/origin_power_map.h +++ b/components/power/origin_power_map.h @@ -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); }; |