Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(113)

Side by Side Diff: chrome/browser/ui/webui/options/website_settings_handler.cc

Issue 519883002: Update the Website Settings page when the power consumption is updated. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix a bug where the Origin Map may not exist in tests. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/ui/webui/options/website_settings_handler.h" 5 #include "chrome/browser/ui/webui/options/website_settings_handler.h"
6 6
7 #include "chrome/browser/content_settings/content_settings_utils.h" 7 #include "chrome/browser/content_settings/content_settings_utils.h"
8 #include "chrome/browser/content_settings/host_content_settings_map.h" 8 #include "chrome/browser/content_settings/host_content_settings_map.h"
9 #include "chrome/browser/extensions/extension_service.h" 9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
(...skipping 14 matching lines...) Expand all
25 #include "extensions/common/extension.h" 25 #include "extensions/common/extension.h"
26 #include "ui/base/l10n/l10n_util.h" 26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/l10n/time_format.h" 27 #include "ui/base/l10n/time_format.h"
28 #include "ui/base/text/bytes_formatting.h" 28 #include "ui/base/text/bytes_formatting.h"
29 29
30 using power::OriginPowerMap; 30 using power::OriginPowerMap;
31 using power::OriginPowerMapFactory; 31 using power::OriginPowerMapFactory;
32 32
33 namespace { 33 namespace {
34 34
35 const char kBattery[] = "battery";
35 const int kHttpPort = 80; 36 const int kHttpPort = 80;
36 const int kHttpsPort = 443; 37 const int kHttpsPort = 443;
37 const char kPreferencesSource[] = "preference"; 38 const char kPreferencesSource[] = "preference";
38 const char kStorage[] = "storage"; 39 const char kStorage[] = "storage";
39 const ContentSettingsType kValidTypes[] = { 40 const ContentSettingsType kValidTypes[] = {
40 CONTENT_SETTINGS_TYPE_GEOLOCATION, 41 CONTENT_SETTINGS_TYPE_GEOLOCATION,
41 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 42 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
42 CONTENT_SETTINGS_TYPE_MEDIASTREAM, 43 CONTENT_SETTINGS_TYPE_MEDIASTREAM,
43 CONTENT_SETTINGS_TYPE_COOKIES}; 44 CONTENT_SETTINGS_TYPE_COOKIES};
44 const size_t kValidTypesLength = arraysize(kValidTypes); 45 const size_t kValidTypesLength = arraysize(kValidTypes);
46
45 } // namespace 47 } // namespace
46 48
47 namespace options { 49 namespace options {
48 50
49 WebsiteSettingsHandler::WebsiteSettingsHandler() 51 WebsiteSettingsHandler::WebsiteSettingsHandler()
50 : observer_(this), weak_ptr_factory_(this) { 52 : observer_(this),
53 weak_ptr_factory_(this) {
51 } 54 }
52 55
53 WebsiteSettingsHandler::~WebsiteSettingsHandler() { 56 WebsiteSettingsHandler::~WebsiteSettingsHandler() {
54 } 57 }
55 58
56 void WebsiteSettingsHandler::GetLocalizedValues( 59 void WebsiteSettingsHandler::GetLocalizedValues(
57 base::DictionaryValue* localized_strings) { 60 base::DictionaryValue* localized_strings) {
58 DCHECK(localized_strings); 61 DCHECK(localized_strings);
59 62
60 static OptionsStringResource resources[] = { 63 static OptionsStringResource resources[] = {
(...skipping 19 matching lines...) Expand all
80 83
81 RegisterStrings(localized_strings, resources, arraysize(resources)); 84 RegisterStrings(localized_strings, resources, arraysize(resources));
82 RegisterTitle( 85 RegisterTitle(
83 localized_strings, "websiteSettingsPage", IDS_WEBSITE_SETTINGS_TITLE); 86 localized_strings, "websiteSettingsPage", IDS_WEBSITE_SETTINGS_TITLE);
84 } 87 }
85 88
86 void WebsiteSettingsHandler::InitializeHandler() { 89 void WebsiteSettingsHandler::InitializeHandler() {
87 Profile* profile = Profile::FromWebUI(web_ui()); 90 Profile* profile = Profile::FromWebUI(web_ui());
88 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); 91 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
89 observer_.Add(settings); 92 observer_.Add(settings);
93
94 power::OriginPowerMap* origin_power_map =
95 power::OriginPowerMapFactory::GetForBrowserContext(profile);
96 // OriginPowerMap may not be available in tests.
97 if (origin_power_map) {
98 subscription_ = origin_power_map->AddPowerConsumptionUpdatedCallback(
99 base::Bind(&WebsiteSettingsHandler::Update, base::Unretained(this)));
100 }
90 } 101 }
91 102
92 void WebsiteSettingsHandler::RegisterMessages() { 103 void WebsiteSettingsHandler::RegisterMessages() {
93 web_ui()->RegisterMessageCallback( 104 web_ui()->RegisterMessageCallback(
94 "updateOrigins", 105 "updateOrigins",
95 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins, 106 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins,
96 base::Unretained(this))); 107 base::Unretained(this)));
97 108
98 web_ui()->RegisterMessageCallback( 109 web_ui()->RegisterMessageCallback(
99 "updateOriginsSearchResults", 110 "updateOriginsSearchResults",
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) { 221 BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) {
211 local_storage_list_ = storage; 222 local_storage_list_ = storage;
212 Update(); 223 Update();
213 GetInfoForOrigin(last_site_, false); 224 GetInfoForOrigin(last_site_, false);
214 } 225 }
215 226
216 void WebsiteSettingsHandler::Update() { 227 void WebsiteSettingsHandler::Update() {
217 DCHECK(!last_setting_.empty()); 228 DCHECK(!last_setting_.empty());
218 if (last_setting_ == kStorage) 229 if (last_setting_ == kStorage)
219 UpdateLocalStorage(); 230 UpdateLocalStorage();
231 else if (last_setting_ == kBattery)
232 UpdateBatteryUsage();
220 else 233 else
221 UpdateOrigins(); 234 UpdateOrigins();
222 } 235 }
223 236
224 void WebsiteSettingsHandler::UpdateOrigins() { 237 void WebsiteSettingsHandler::UpdateOrigins() {
225 Profile* profile = Profile::FromWebUI(web_ui()); 238 Profile* profile = Profile::FromWebUI(web_ui());
226 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); 239 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
227 240
228 ContentSettingsForOneType all_settings; 241 ContentSettingsForOneType all_settings;
229 ContentSettingsType last_setting; 242 ContentSettingsType last_setting;
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 map->SetNarrowestWebsiteSetting(primary_pattern, 384 map->SetNarrowestWebsiteSetting(primary_pattern,
372 secondary_pattern, 385 secondary_pattern,
373 settings_type, 386 settings_type,
374 std::string(), 387 std::string(),
375 setting, 388 setting,
376 info); 389 info);
377 } 390 }
378 391
379 void WebsiteSettingsHandler::HandleUpdateBatteryUsage( 392 void WebsiteSettingsHandler::HandleUpdateBatteryUsage(
380 const base::ListValue* args) { 393 const base::ListValue* args) {
381 base::DictionaryValue power_map; 394 last_setting_ = kBattery;
382 OriginPowerMap* origins = 395 UpdateBatteryUsage();
383 OriginPowerMapFactory::GetForBrowserContext(Profile::FromWebUI(web_ui()));
384 OriginPowerMap::PercentOriginMap percent_map = origins->GetPercentOriginMap();
385 for (std::map<GURL, int>::iterator it = percent_map.begin();
386 it != percent_map.end();
387 ++it) {
388 std::string origin = it->first.spec();
389
390 if (origin.find(last_filter_) == base::string16::npos)
391 continue;
392
393 base::DictionaryValue* origin_entry = new base::DictionaryValue();
394 origin_entry->SetInteger("usage", it->second);
395 origin_entry->SetString(
396 "usageString",
397 l10n_util::GetStringFUTF16Int(IDS_WEBSITE_SETTINGS_BATTERY_PERCENT,
398 it->second));
399 origin_entry->SetStringWithoutPathExpansion(
400 "readableName", GetReadableName(it->first));
401 power_map.SetWithoutPathExpansion(origin, origin_entry);
402 }
403 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
404 power_map);
405 } 396 }
406 397
407 void WebsiteSettingsHandler::HandleDeleteLocalStorage( 398 void WebsiteSettingsHandler::HandleDeleteLocalStorage(
408 const base::ListValue* args) { 399 const base::ListValue* args) {
409 DCHECK(!last_site_.is_empty()); 400 DCHECK(!last_site_.is_empty());
410 DeleteLocalStorage(last_site_); 401 DeleteLocalStorage(last_site_);
411 } 402 }
412 403
413 void WebsiteSettingsHandler::HandleStopOrigin(const base::ListValue* args) { 404 void WebsiteSettingsHandler::HandleStopOrigin(const base::ListValue* args) {
414 DCHECK(!last_site_.is_empty()); 405 DCHECK(!last_site_.is_empty());
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 origin_entry->SetWithoutPathExpansion( 513 origin_entry->SetWithoutPathExpansion(
523 "usageString", new base::StringValue(ui::FormatBytes(it->size))); 514 "usageString", new base::StringValue(ui::FormatBytes(it->size)));
524 origin_entry->SetStringWithoutPathExpansion( 515 origin_entry->SetStringWithoutPathExpansion(
525 "readableName", GetReadableName(it->origin_url)); 516 "readableName", GetReadableName(it->origin_url));
526 local_storage_map.SetWithoutPathExpansion(origin, origin_entry); 517 local_storage_map.SetWithoutPathExpansion(origin, origin_entry);
527 } 518 }
528 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", 519 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
529 local_storage_map); 520 local_storage_map);
530 } 521 }
531 522
523 void WebsiteSettingsHandler::UpdateBatteryUsage() {
524 base::DictionaryValue power_map;
525 OriginPowerMap* origins =
526 OriginPowerMapFactory::GetForBrowserContext(Profile::FromWebUI(web_ui()));
527 OriginPowerMap::PercentOriginMap percent_map = origins->GetPercentOriginMap();
528 for (std::map<GURL, int>::iterator it = percent_map.begin();
529 it != percent_map.end();
530 ++it) {
531 std::string origin = it->first.spec();
532
533 if (origin.find(last_filter_) == base::string16::npos)
534 continue;
535
536 base::DictionaryValue* origin_entry = new base::DictionaryValue();
537 origin_entry->SetInteger("usage", it->second);
538 origin_entry->SetString(
539 "usageString",
540 l10n_util::GetStringFUTF16Int(IDS_WEBSITE_SETTINGS_BATTERY_PERCENT,
541 it->second));
542 origin_entry->SetStringWithoutPathExpansion("readableName",
543 GetReadableName(it->first));
544 power_map.SetWithoutPathExpansion(origin, origin_entry);
545 }
546 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
547 power_map);
548 }
549
532 void WebsiteSettingsHandler::StopOrigin(const GURL& site_url) { 550 void WebsiteSettingsHandler::StopOrigin(const GURL& site_url) {
533 Profile* profile = Profile::FromWebUI(web_ui()); 551 Profile* profile = Profile::FromWebUI(web_ui());
534 if (site_url.SchemeIs(extensions::kExtensionScheme)) { 552 if (site_url.SchemeIs(extensions::kExtensionScheme)) {
535 const extensions::Extension* extension = 553 const extensions::Extension* extension =
536 extensions::ExtensionRegistry::Get(profile) 554 extensions::ExtensionRegistry::Get(profile)
537 ->enabled_extensions() 555 ->enabled_extensions()
538 .GetHostedAppByURL(site_url); 556 .GetHostedAppByURL(site_url);
539 if (extension) { 557 if (extension) {
540 extensions::AppWindowRegistry::Get(profile) 558 extensions::AppWindowRegistry::Get(profile)
541 ->CloseAllAppWindowsForApp(extension->id()); 559 ->CloseAllAppWindowsForApp(extension->id());
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 // If extension is NULL, it was removed and we cannot look up its name. 607 // If extension is NULL, it was removed and we cannot look up its name.
590 if (!extension) 608 if (!extension)
591 return site_url.spec(); 609 return site_url.spec();
592 610
593 return extension->name(); 611 return extension->name();
594 } 612 }
595 return site_url.spec(); 613 return site_url.spec();
596 } 614 }
597 615
598 } // namespace options 616 } // namespace options
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/options/website_settings_handler.h ('k') | components/power/origin_power_map.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698