OLD | NEW |
---|---|
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 13 matching lines...) Expand all Loading... | |
24 #include "extensions/common/constants.h" | 24 #include "extensions/common/constants.h" |
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 const char kBattery[] = "battery"; |
Bernhard Bauer
2014/08/29 19:16:31
Instead of removing this blank line, I would rathe
Daniel Erat
2014/08/29 21:14:02
(i concur)
Daniel Nishi
2014/09/02 16:30:30
Done.
| |
35 const int kHttpPort = 80; | 35 const int kHttpPort = 80; |
36 const int kHttpsPort = 443; | 36 const int kHttpsPort = 443; |
37 const char kPreferencesSource[] = "preference"; | 37 const char kPreferencesSource[] = "preference"; |
38 const char kStorage[] = "storage"; | 38 const char kStorage[] = "storage"; |
39 const ContentSettingsType kValidTypes[] = { | 39 const ContentSettingsType kValidTypes[] = { |
40 CONTENT_SETTINGS_TYPE_GEOLOCATION, | 40 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
41 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 41 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
42 CONTENT_SETTINGS_TYPE_MEDIASTREAM, | 42 CONTENT_SETTINGS_TYPE_MEDIASTREAM, |
43 CONTENT_SETTINGS_TYPE_COOKIES}; | 43 CONTENT_SETTINGS_TYPE_COOKIES}; |
44 const size_t kValidTypesLength = arraysize(kValidTypes); | 44 const size_t kValidTypesLength = arraysize(kValidTypes); |
45 } // namespace | 45 } // namespace |
46 | 46 |
47 namespace options { | 47 namespace options { |
48 | 48 |
49 WebsiteSettingsHandler::WebsiteSettingsHandler() | 49 WebsiteSettingsHandler::WebsiteSettingsHandler() |
50 : observer_(this), weak_ptr_factory_(this) { | 50 : observer_(this), power_observer_(this), weak_ptr_factory_(this) { |
Daniel Erat
2014/08/29 21:14:02
nit: chromium style is usually one member per line
Daniel Nishi
2014/09/02 16:30:30
Done.
| |
51 } | 51 } |
52 | 52 |
53 WebsiteSettingsHandler::~WebsiteSettingsHandler() { | 53 WebsiteSettingsHandler::~WebsiteSettingsHandler() { |
54 } | 54 } |
55 | 55 |
56 void WebsiteSettingsHandler::GetLocalizedValues( | 56 void WebsiteSettingsHandler::GetLocalizedValues( |
57 base::DictionaryValue* localized_strings) { | 57 base::DictionaryValue* localized_strings) { |
58 DCHECK(localized_strings); | 58 DCHECK(localized_strings); |
59 | 59 |
60 static OptionsStringResource resources[] = { | 60 static OptionsStringResource resources[] = { |
(...skipping 19 matching lines...) Expand all Loading... | |
80 | 80 |
81 RegisterStrings(localized_strings, resources, arraysize(resources)); | 81 RegisterStrings(localized_strings, resources, arraysize(resources)); |
82 RegisterTitle( | 82 RegisterTitle( |
83 localized_strings, "websiteSettingsPage", IDS_WEBSITE_SETTINGS_TITLE); | 83 localized_strings, "websiteSettingsPage", IDS_WEBSITE_SETTINGS_TITLE); |
84 } | 84 } |
85 | 85 |
86 void WebsiteSettingsHandler::InitializeHandler() { | 86 void WebsiteSettingsHandler::InitializeHandler() { |
87 Profile* profile = Profile::FromWebUI(web_ui()); | 87 Profile* profile = Profile::FromWebUI(web_ui()); |
88 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); | 88 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); |
89 observer_.Add(settings); | 89 observer_.Add(settings); |
90 | |
91 power::OriginPowerMap* origin_power_map = | |
92 power::OriginPowerMapFactory::GetForBrowserContext(profile); | |
93 power_observer_.Add(origin_power_map); | |
90 } | 94 } |
91 | 95 |
92 void WebsiteSettingsHandler::RegisterMessages() { | 96 void WebsiteSettingsHandler::RegisterMessages() { |
93 web_ui()->RegisterMessageCallback( | 97 web_ui()->RegisterMessageCallback( |
94 "updateOrigins", | 98 "updateOrigins", |
95 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins, | 99 base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins, |
96 base::Unretained(this))); | 100 base::Unretained(this))); |
97 | 101 |
98 web_ui()->RegisterMessageCallback( | 102 web_ui()->RegisterMessageCallback( |
99 "updateOriginsSearchResults", | 103 "updateOriginsSearchResults", |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 Update(); | 149 Update(); |
146 } | 150 } |
147 | 151 |
148 void WebsiteSettingsHandler::OnContentSettingUsed( | 152 void WebsiteSettingsHandler::OnContentSettingUsed( |
149 const ContentSettingsPattern& primary_pattern, | 153 const ContentSettingsPattern& primary_pattern, |
150 const ContentSettingsPattern& secondary_pattern, | 154 const ContentSettingsPattern& secondary_pattern, |
151 ContentSettingsType content_type) { | 155 ContentSettingsType content_type) { |
152 Update(); | 156 Update(); |
153 } | 157 } |
154 | 158 |
159 void WebsiteSettingsHandler::OnPowerConsumptionUpdated() { | |
160 Update(); | |
161 } | |
162 | |
155 void WebsiteSettingsHandler::HandleUpdateOrigins(const base::ListValue* args) { | 163 void WebsiteSettingsHandler::HandleUpdateOrigins(const base::ListValue* args) { |
156 std::string content_setting_name; | 164 std::string content_setting_name; |
157 bool rv = args->GetString(0, &content_setting_name); | 165 bool rv = args->GetString(0, &content_setting_name); |
158 DCHECK(rv); | 166 DCHECK(rv); |
159 | 167 |
160 ContentSettingsType content_type; | 168 ContentSettingsType content_type; |
161 rv = content_settings::GetTypeFromName(content_setting_name, &content_type); | 169 rv = content_settings::GetTypeFromName(content_setting_name, &content_type); |
162 DCHECK(rv); | 170 DCHECK(rv); |
163 DCHECK_NE( | 171 DCHECK_NE( |
164 kValidTypes + kValidTypesLength, | 172 kValidTypes + kValidTypesLength, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
210 BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) { | 218 BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) { |
211 local_storage_list_ = storage; | 219 local_storage_list_ = storage; |
212 Update(); | 220 Update(); |
213 GetInfoForOrigin(last_site_, false); | 221 GetInfoForOrigin(last_site_, false); |
214 } | 222 } |
215 | 223 |
216 void WebsiteSettingsHandler::Update() { | 224 void WebsiteSettingsHandler::Update() { |
217 DCHECK(!last_setting_.empty()); | 225 DCHECK(!last_setting_.empty()); |
218 if (last_setting_ == kStorage) | 226 if (last_setting_ == kStorage) |
219 UpdateLocalStorage(); | 227 UpdateLocalStorage(); |
228 else if (last_setting_ == kBattery) | |
229 UpdateBatteryUsage(); | |
220 else | 230 else |
221 UpdateOrigins(); | 231 UpdateOrigins(); |
222 } | 232 } |
223 | 233 |
224 void WebsiteSettingsHandler::UpdateOrigins() { | 234 void WebsiteSettingsHandler::UpdateOrigins() { |
225 Profile* profile = Profile::FromWebUI(web_ui()); | 235 Profile* profile = Profile::FromWebUI(web_ui()); |
226 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); | 236 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); |
227 | 237 |
228 ContentSettingsForOneType all_settings; | 238 ContentSettingsForOneType all_settings; |
229 ContentSettingsType last_setting; | 239 ContentSettingsType last_setting; |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 map->SetNarrowestWebsiteSetting(primary_pattern, | 381 map->SetNarrowestWebsiteSetting(primary_pattern, |
372 secondary_pattern, | 382 secondary_pattern, |
373 settings_type, | 383 settings_type, |
374 std::string(), | 384 std::string(), |
375 setting, | 385 setting, |
376 info); | 386 info); |
377 } | 387 } |
378 | 388 |
379 void WebsiteSettingsHandler::HandleUpdateBatteryUsage( | 389 void WebsiteSettingsHandler::HandleUpdateBatteryUsage( |
380 const base::ListValue* args) { | 390 const base::ListValue* args) { |
381 base::DictionaryValue power_map; | 391 last_setting_ = kBattery; |
382 OriginPowerMap* origins = | 392 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 } | 393 } |
406 | 394 |
407 void WebsiteSettingsHandler::HandleDeleteLocalStorage( | 395 void WebsiteSettingsHandler::HandleDeleteLocalStorage( |
408 const base::ListValue* args) { | 396 const base::ListValue* args) { |
409 DCHECK(!last_site_.is_empty()); | 397 DCHECK(!last_site_.is_empty()); |
410 DeleteLocalStorage(last_site_); | 398 DeleteLocalStorage(last_site_); |
411 } | 399 } |
412 | 400 |
413 void WebsiteSettingsHandler::HandleStopOrigin(const base::ListValue* args) { | 401 void WebsiteSettingsHandler::HandleStopOrigin(const base::ListValue* args) { |
414 DCHECK(!last_site_.is_empty()); | 402 DCHECK(!last_site_.is_empty()); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
522 origin_entry->SetWithoutPathExpansion( | 510 origin_entry->SetWithoutPathExpansion( |
523 "usageString", new base::StringValue(ui::FormatBytes(it->size))); | 511 "usageString", new base::StringValue(ui::FormatBytes(it->size))); |
524 origin_entry->SetStringWithoutPathExpansion( | 512 origin_entry->SetStringWithoutPathExpansion( |
525 "readableName", GetReadableName(it->origin_url)); | 513 "readableName", GetReadableName(it->origin_url)); |
526 local_storage_map.SetWithoutPathExpansion(origin, origin_entry); | 514 local_storage_map.SetWithoutPathExpansion(origin, origin_entry); |
527 } | 515 } |
528 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", | 516 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", |
529 local_storage_map); | 517 local_storage_map); |
530 } | 518 } |
531 | 519 |
520 void WebsiteSettingsHandler::UpdateBatteryUsage() { | |
521 base::DictionaryValue power_map; | |
522 OriginPowerMap* origins = | |
523 OriginPowerMapFactory::GetForBrowserContext(Profile::FromWebUI(web_ui())); | |
524 OriginPowerMap::PercentOriginMap percent_map = origins->GetPercentOriginMap(); | |
525 for (std::map<GURL, int>::iterator it = percent_map.begin(); | |
526 it != percent_map.end(); | |
527 ++it) { | |
528 std::string origin = it->first.spec(); | |
529 | |
530 if (origin.find(last_filter_) == base::string16::npos) | |
531 continue; | |
532 | |
533 base::DictionaryValue* origin_entry = new base::DictionaryValue(); | |
534 origin_entry->SetInteger("usage", it->second); | |
535 origin_entry->SetString( | |
536 "usageString", | |
537 l10n_util::GetStringFUTF16Int(IDS_WEBSITE_SETTINGS_BATTERY_PERCENT, | |
538 it->second)); | |
539 origin_entry->SetStringWithoutPathExpansion("readableName", | |
540 GetReadableName(it->first)); | |
541 power_map.SetWithoutPathExpansion(origin, origin_entry); | |
542 } | |
543 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", | |
544 power_map); | |
545 } | |
546 | |
532 void WebsiteSettingsHandler::StopOrigin(const GURL& site_url) { | 547 void WebsiteSettingsHandler::StopOrigin(const GURL& site_url) { |
533 Profile* profile = Profile::FromWebUI(web_ui()); | 548 Profile* profile = Profile::FromWebUI(web_ui()); |
534 if (site_url.SchemeIs(extensions::kExtensionScheme)) { | 549 if (site_url.SchemeIs(extensions::kExtensionScheme)) { |
535 const extensions::Extension* extension = | 550 const extensions::Extension* extension = |
536 extensions::ExtensionRegistry::Get(profile) | 551 extensions::ExtensionRegistry::Get(profile) |
537 ->enabled_extensions() | 552 ->enabled_extensions() |
538 .GetHostedAppByURL(site_url); | 553 .GetHostedAppByURL(site_url); |
539 if (extension) { | 554 if (extension) { |
540 extensions::AppWindowRegistry::Get(profile) | 555 extensions::AppWindowRegistry::Get(profile) |
541 ->CloseAllAppWindowsForApp(extension->id()); | 556 ->CloseAllAppWindowsForApp(extension->id()); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
589 // If extension is NULL, it was removed and we cannot look up its name. | 604 // If extension is NULL, it was removed and we cannot look up its name. |
590 if (!extension) | 605 if (!extension) |
591 return site_url.spec(); | 606 return site_url.spec(); |
592 | 607 |
593 return extension->name(); | 608 return extension->name(); |
594 } | 609 } |
595 return site_url.spec(); | 610 return site_url.spec(); |
596 } | 611 } |
597 | 612 |
598 } // namespace options | 613 } // namespace options |
OLD | NEW |