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

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: Remove the Observer, replace with Subscription. 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), weak_ptr_factory_(this) {
51 } 53 }
52 54
53 WebsiteSettingsHandler::~WebsiteSettingsHandler() { 55 WebsiteSettingsHandler::~WebsiteSettingsHandler() {
54 } 56 }
(...skipping 25 matching lines...) Expand all
80 82
81 RegisterStrings(localized_strings, resources, arraysize(resources)); 83 RegisterStrings(localized_strings, resources, arraysize(resources));
82 RegisterTitle( 84 RegisterTitle(
83 localized_strings, "websiteSettingsPage", IDS_WEBSITE_SETTINGS_TITLE); 85 localized_strings, "websiteSettingsPage", IDS_WEBSITE_SETTINGS_TITLE);
84 } 86 }
85 87
86 void WebsiteSettingsHandler::InitializeHandler() { 88 void WebsiteSettingsHandler::InitializeHandler() {
87 Profile* profile = Profile::FromWebUI(web_ui()); 89 Profile* profile = Profile::FromWebUI(web_ui());
88 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); 90 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
89 observer_.Add(settings); 91 observer_.Add(settings);
92
93 power::OriginPowerMap* origin_power_map =
94 power::OriginPowerMapFactory::GetForBrowserContext(profile);
95 subscription_ = origin_power_map->AddPowerConsumptionUpdatedCallback(
96 base::Bind(&WebsiteSettingsHandler::Update, base::Unretained(this)));
90 } 97 }
91 98
92 void WebsiteSettingsHandler::RegisterMessages() { 99 void WebsiteSettingsHandler::RegisterMessages() {
93 web_ui()->RegisterMessageCallback( 100 web_ui()->RegisterMessageCallback(
94 "updateOrigins", 101 "updateOrigins",
95 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins, 102 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins,
96 base::Unretained(this))); 103 base::Unretained(this)));
97 104
98 web_ui()->RegisterMessageCallback( 105 web_ui()->RegisterMessageCallback(
99 "updateOriginsSearchResults", 106 "updateOriginsSearchResults",
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) { 217 BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) {
211 local_storage_list_ = storage; 218 local_storage_list_ = storage;
212 Update(); 219 Update();
213 GetInfoForOrigin(last_site_, false); 220 GetInfoForOrigin(last_site_, false);
214 } 221 }
215 222
216 void WebsiteSettingsHandler::Update() { 223 void WebsiteSettingsHandler::Update() {
217 DCHECK(!last_setting_.empty()); 224 DCHECK(!last_setting_.empty());
218 if (last_setting_ == kStorage) 225 if (last_setting_ == kStorage)
219 UpdateLocalStorage(); 226 UpdateLocalStorage();
227 else if (last_setting_ == kBattery)
228 UpdateBatteryUsage();
220 else 229 else
221 UpdateOrigins(); 230 UpdateOrigins();
222 } 231 }
223 232
224 void WebsiteSettingsHandler::UpdateOrigins() { 233 void WebsiteSettingsHandler::UpdateOrigins() {
225 Profile* profile = Profile::FromWebUI(web_ui()); 234 Profile* profile = Profile::FromWebUI(web_ui());
226 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); 235 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
227 236
228 ContentSettingsForOneType all_settings; 237 ContentSettingsForOneType all_settings;
229 ContentSettingsType last_setting; 238 ContentSettingsType last_setting;
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 map->SetNarrowestWebsiteSetting(primary_pattern, 380 map->SetNarrowestWebsiteSetting(primary_pattern,
372 secondary_pattern, 381 secondary_pattern,
373 settings_type, 382 settings_type,
374 std::string(), 383 std::string(),
375 setting, 384 setting,
376 info); 385 info);
377 } 386 }
378 387
379 void WebsiteSettingsHandler::HandleUpdateBatteryUsage( 388 void WebsiteSettingsHandler::HandleUpdateBatteryUsage(
380 const base::ListValue* args) { 389 const base::ListValue* args) {
381 base::DictionaryValue power_map; 390 last_setting_ = kBattery;
382 OriginPowerMap* origins = 391 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 } 392 }
406 393
407 void WebsiteSettingsHandler::HandleDeleteLocalStorage( 394 void WebsiteSettingsHandler::HandleDeleteLocalStorage(
408 const base::ListValue* args) { 395 const base::ListValue* args) {
409 DCHECK(!last_site_.is_empty()); 396 DCHECK(!last_site_.is_empty());
410 DeleteLocalStorage(last_site_); 397 DeleteLocalStorage(last_site_);
411 } 398 }
412 399
413 void WebsiteSettingsHandler::HandleStopOrigin(const base::ListValue* args) { 400 void WebsiteSettingsHandler::HandleStopOrigin(const base::ListValue* args) {
414 DCHECK(!last_site_.is_empty()); 401 DCHECK(!last_site_.is_empty());
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 origin_entry->SetWithoutPathExpansion( 509 origin_entry->SetWithoutPathExpansion(
523 "usageString", new base::StringValue(ui::FormatBytes(it->size))); 510 "usageString", new base::StringValue(ui::FormatBytes(it->size)));
524 origin_entry->SetStringWithoutPathExpansion( 511 origin_entry->SetStringWithoutPathExpansion(
525 "readableName", GetReadableName(it->origin_url)); 512 "readableName", GetReadableName(it->origin_url));
526 local_storage_map.SetWithoutPathExpansion(origin, origin_entry); 513 local_storage_map.SetWithoutPathExpansion(origin, origin_entry);
527 } 514 }
528 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", 515 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
529 local_storage_map); 516 local_storage_map);
530 } 517 }
531 518
519 void WebsiteSettingsHandler::UpdateBatteryUsage() {
520 base::DictionaryValue power_map;
521 OriginPowerMap* origins =
522 OriginPowerMapFactory::GetForBrowserContext(Profile::FromWebUI(web_ui()));
523 OriginPowerMap::PercentOriginMap percent_map = origins->GetPercentOriginMap();
524 for (std::map<GURL, int>::iterator it = percent_map.begin();
525 it != percent_map.end();
526 ++it) {
527 std::string origin = it->first.spec();
528
529 if (origin.find(last_filter_) == base::string16::npos)
530 continue;
531
532 base::DictionaryValue* origin_entry = new base::DictionaryValue();
533 origin_entry->SetInteger("usage", it->second);
534 origin_entry->SetString(
535 "usageString",
536 l10n_util::GetStringFUTF16Int(IDS_WEBSITE_SETTINGS_BATTERY_PERCENT,
537 it->second));
538 origin_entry->SetStringWithoutPathExpansion("readableName",
539 GetReadableName(it->first));
540 power_map.SetWithoutPathExpansion(origin, origin_entry);
541 }
542 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
543 power_map);
544 }
545
532 void WebsiteSettingsHandler::StopOrigin(const GURL& site_url) { 546 void WebsiteSettingsHandler::StopOrigin(const GURL& site_url) {
533 Profile* profile = Profile::FromWebUI(web_ui()); 547 Profile* profile = Profile::FromWebUI(web_ui());
534 if (site_url.SchemeIs(extensions::kExtensionScheme)) { 548 if (site_url.SchemeIs(extensions::kExtensionScheme)) {
535 const extensions::Extension* extension = 549 const extensions::Extension* extension =
536 extensions::ExtensionRegistry::Get(profile) 550 extensions::ExtensionRegistry::Get(profile)
537 ->enabled_extensions() 551 ->enabled_extensions()
538 .GetHostedAppByURL(site_url); 552 .GetHostedAppByURL(site_url);
539 if (extension) { 553 if (extension) {
540 extensions::AppWindowRegistry::Get(profile) 554 extensions::AppWindowRegistry::Get(profile)
541 ->CloseAllAppWindowsForApp(extension->id()); 555 ->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. 603 // If extension is NULL, it was removed and we cannot look up its name.
590 if (!extension) 604 if (!extension)
591 return site_url.spec(); 605 return site_url.spec();
592 606
593 return extension->name(); 607 return extension->name();
594 } 608 }
595 return site_url.spec(); 609 return site_url.spec();
596 } 610 }
597 611
598 } // namespace options 612 } // namespace options
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698