| 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 "apps/app_window_registry.h" | 7 #include "apps/app_window_registry.h" |
| 8 #include "chrome/browser/content_settings/content_settings_utils.h" | 8 #include "chrome/browser/content_settings/content_settings_utils.h" |
| 9 #include "chrome/browser/content_settings/host_content_settings_map.h" | 9 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_iterator.h" | 13 #include "chrome/browser/ui/browser_iterator.h" |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "chrome/grit/generated_resources.h" | 15 #include "chrome/grit/generated_resources.h" |
| 16 #include "components/power/origin_power_map.h" |
| 17 #include "components/power/origin_power_map_factory.h" |
| 16 #include "content/public/browser/dom_storage_context.h" | 18 #include "content/public/browser/dom_storage_context.h" |
| 17 #include "content/public/browser/storage_partition.h" | 19 #include "content/public/browser/storage_partition.h" |
| 18 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/browser/web_ui.h" | 21 #include "content/public/browser/web_ui.h" |
| 20 #include "extensions/browser/extension_registry.h" | 22 #include "extensions/browser/extension_registry.h" |
| 21 #include "extensions/browser/extension_system.h" | 23 #include "extensions/browser/extension_system.h" |
| 22 #include "extensions/common/constants.h" | 24 #include "extensions/common/constants.h" |
| 23 #include "extensions/common/extension.h" | 25 #include "extensions/common/extension.h" |
| 24 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "ui/base/l10n/time_format.h" | 27 #include "ui/base/l10n/time_format.h" |
| 26 #include "ui/base/text/bytes_formatting.h" | 28 #include "ui/base/text/bytes_formatting.h" |
| 27 | 29 |
| 30 using power::OriginPowerMap; |
| 31 using power::OriginPowerMapFactory; |
| 32 |
| 28 namespace { | 33 namespace { |
| 29 | 34 |
| 30 const int kHttpPort = 80; | 35 const int kHttpPort = 80; |
| 31 const int kHttpsPort = 443; | 36 const int kHttpsPort = 443; |
| 32 const char kPreferencesSource[] = "preference"; | 37 const char kPreferencesSource[] = "preference"; |
| 33 const char kStorage[] = "storage"; | 38 const char kStorage[] = "storage"; |
| 34 const ContentSettingsType kValidTypes[] = { | 39 const ContentSettingsType kValidTypes[] = { |
| 35 CONTENT_SETTINGS_TYPE_GEOLOCATION, | 40 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 36 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 41 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 37 CONTENT_SETTINGS_TYPE_MEDIASTREAM, | 42 CONTENT_SETTINGS_TYPE_MEDIASTREAM, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 54 | 59 |
| 55 static OptionsStringResource resources[] = { | 60 static OptionsStringResource resources[] = { |
| 56 {"websitesOptionsPageTabTitle", IDS_WEBSITE_SETTINGS_TITLE}, | 61 {"websitesOptionsPageTabTitle", IDS_WEBSITE_SETTINGS_TITLE}, |
| 57 {"websitesSettingsEditPage", IDS_WEBSITE_SETTINGS_EDIT_TITLE}, | 62 {"websitesSettingsEditPage", IDS_WEBSITE_SETTINGS_EDIT_TITLE}, |
| 58 {"websitesManage", IDS_WEBSITE_SETTINGS_MANAGE}, | 63 {"websitesManage", IDS_WEBSITE_SETTINGS_MANAGE}, |
| 59 {"websitesSearch", IDS_WEBSITE_SETTINGS_SEARCH_ORIGINS}, | 64 {"websitesSearch", IDS_WEBSITE_SETTINGS_SEARCH_ORIGINS}, |
| 60 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION}, | 65 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION}, |
| 61 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM}, | 66 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM}, |
| 62 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS}, | 67 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS}, |
| 63 {"websitesLabelStorage", IDS_WEBSITE_SETTINGS_TYPE_STORAGE}, | 68 {"websitesLabelStorage", IDS_WEBSITE_SETTINGS_TYPE_STORAGE}, |
| 69 {"websitesLabelBattery", IDS_WEBSITE_SETTINGS_TYPE_BATTERY}, |
| 64 {"websitesCookiesDescription", IDS_WEBSITE_SETTINGS_COOKIES_DESCRIPTION}, | 70 {"websitesCookiesDescription", IDS_WEBSITE_SETTINGS_COOKIES_DESCRIPTION}, |
| 65 {"websitesLocationDescription", | 71 {"websitesLocationDescription", |
| 66 IDS_WEBSITE_SETTINGS_LOCATION_DESCRIPTION}, | 72 IDS_WEBSITE_SETTINGS_LOCATION_DESCRIPTION}, |
| 67 {"websitesMediastreamDescription", | 73 {"websitesMediastreamDescription", |
| 68 IDS_WEBSITE_SETTINGS_MEDIASTREAM_DESCRIPTION}, | 74 IDS_WEBSITE_SETTINGS_MEDIASTREAM_DESCRIPTION}, |
| 69 {"websitesNotificationsDescription", | 75 {"websitesNotificationsDescription", |
| 70 IDS_WEBSITE_SETTINGS_NOTIFICATIONS_DESCRIPTION}, | 76 IDS_WEBSITE_SETTINGS_NOTIFICATIONS_DESCRIPTION}, |
| 71 {"websitesButtonClear", IDS_WEBSITE_SETTINGS_STORAGE_CLEAR_BUTTON}, | 77 {"websitesButtonClear", IDS_WEBSITE_SETTINGS_STORAGE_CLEAR_BUTTON}, |
| 78 {"websitesButtonStop", IDS_WEBSITE_SETTINGS_BATTERY_STOP_BUTTON}, |
| 72 }; | 79 }; |
| 73 | 80 |
| 74 RegisterStrings(localized_strings, resources, arraysize(resources)); | 81 RegisterStrings(localized_strings, resources, arraysize(resources)); |
| 75 RegisterTitle( | 82 RegisterTitle( |
| 76 localized_strings, "websiteSettingsPage", IDS_WEBSITE_SETTINGS_TITLE); | 83 localized_strings, "websiteSettingsPage", IDS_WEBSITE_SETTINGS_TITLE); |
| 77 } | 84 } |
| 78 | 85 |
| 79 void WebsiteSettingsHandler::InitializeHandler() { | 86 void WebsiteSettingsHandler::InitializeHandler() { |
| 80 Profile* profile = Profile::FromWebUI(web_ui()); | 87 Profile* profile = Profile::FromWebUI(web_ui()); |
| 81 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); | 88 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 92 "updateOriginsSearchResults", | 99 "updateOriginsSearchResults", |
| 93 base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults, | 100 base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults, |
| 94 base::Unretained(this))); | 101 base::Unretained(this))); |
| 95 | 102 |
| 96 web_ui()->RegisterMessageCallback( | 103 web_ui()->RegisterMessageCallback( |
| 97 "updateLocalStorage", | 104 "updateLocalStorage", |
| 98 base::Bind(&WebsiteSettingsHandler::HandleUpdateLocalStorage, | 105 base::Bind(&WebsiteSettingsHandler::HandleUpdateLocalStorage, |
| 99 base::Unretained(this))); | 106 base::Unretained(this))); |
| 100 | 107 |
| 101 web_ui()->RegisterMessageCallback( | 108 web_ui()->RegisterMessageCallback( |
| 109 "updateBatteryUsage", |
| 110 base::Bind(&WebsiteSettingsHandler::HandleUpdateBatteryUsage, |
| 111 base::Unretained(this))); |
| 112 |
| 113 web_ui()->RegisterMessageCallback( |
| 102 "getOriginInfo", | 114 "getOriginInfo", |
| 103 base::Bind(&WebsiteSettingsHandler::HandleGetOriginInfo, | 115 base::Bind(&WebsiteSettingsHandler::HandleGetOriginInfo, |
| 104 base::Unretained(this))); | 116 base::Unretained(this))); |
| 105 | 117 |
| 106 web_ui()->RegisterMessageCallback( | 118 web_ui()->RegisterMessageCallback( |
| 107 "setOriginPermission", | 119 "setOriginPermission", |
| 108 base::Bind(&WebsiteSettingsHandler::HandleSetOriginPermission, | 120 base::Bind(&WebsiteSettingsHandler::HandleSetOriginPermission, |
| 109 base::Unretained(this))); | 121 base::Unretained(this))); |
| 110 | 122 |
| 111 web_ui()->RegisterMessageCallback( | 123 web_ui()->RegisterMessageCallback( |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 scoped_ptr<base::Value> v(map->GetWebsiteSetting( | 369 scoped_ptr<base::Value> v(map->GetWebsiteSetting( |
| 358 last_site_, last_site_, settings_type, std::string(), &info)); | 370 last_site_, last_site_, settings_type, std::string(), &info)); |
| 359 map->SetNarrowestWebsiteSetting(primary_pattern, | 371 map->SetNarrowestWebsiteSetting(primary_pattern, |
| 360 secondary_pattern, | 372 secondary_pattern, |
| 361 settings_type, | 373 settings_type, |
| 362 std::string(), | 374 std::string(), |
| 363 setting, | 375 setting, |
| 364 info); | 376 info); |
| 365 } | 377 } |
| 366 | 378 |
| 379 void WebsiteSettingsHandler::HandleUpdateBatteryUsage( |
| 380 const base::ListValue* args) { |
| 381 base::DictionaryValue power_map; |
| 382 OriginPowerMap* origins = |
| 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 } |
| 406 |
| 367 void WebsiteSettingsHandler::HandleDeleteLocalStorage( | 407 void WebsiteSettingsHandler::HandleDeleteLocalStorage( |
| 368 const base::ListValue* args) { | 408 const base::ListValue* args) { |
| 369 DCHECK(!last_site_.is_empty()); | 409 DCHECK(!last_site_.is_empty()); |
| 370 DeleteLocalStorage(last_site_); | 410 DeleteLocalStorage(last_site_); |
| 371 } | 411 } |
| 372 | 412 |
| 373 void WebsiteSettingsHandler::HandleStopOrigin(const base::ListValue* args) { | 413 void WebsiteSettingsHandler::HandleStopOrigin(const base::ListValue* args) { |
| 374 DCHECK(!last_site_.is_empty()); | 414 DCHECK(!last_site_.is_empty()); |
| 375 StopOrigin(last_site_); | 415 StopOrigin(last_site_); |
| 376 } | 416 } |
| 377 | 417 |
| 378 void WebsiteSettingsHandler::GetInfoForOrigin(const GURL& site_url, | 418 void WebsiteSettingsHandler::GetInfoForOrigin(const GURL& site_url, |
| 379 bool show_page) { | 419 bool show_page) { |
| 380 Profile* profile = Profile::FromWebUI(web_ui()); | 420 Profile* profile = Profile::FromWebUI(web_ui()); |
| 381 HostContentSettingsMap* map = profile->GetHostContentSettingsMap(); | 421 HostContentSettingsMap* map = profile->GetHostContentSettingsMap(); |
| 382 | 422 |
| 383 double storage = 0.0; | 423 double storage = 0.0; |
| 384 for (LocalStorageList::const_iterator it = local_storage_list_.begin(); | 424 for (LocalStorageList::const_iterator it = local_storage_list_.begin(); |
| 385 it != local_storage_list_.end(); | 425 it != local_storage_list_.end(); |
| 386 it++) { | 426 it++) { |
| 387 if (it->origin_url == site_url) { | 427 if (it->origin_url == site_url) { |
| 388 storage = static_cast<double>(it->size); | 428 storage = static_cast<double>(it->size); |
| 389 break; | 429 break; |
| 390 } | 430 } |
| 391 } | 431 } |
| 392 | 432 |
| 433 int battery = 0; |
| 434 battery = OriginPowerMapFactory::GetForBrowserContext( |
| 435 Profile::FromWebUI(web_ui()))->GetPowerForOrigin(site_url); |
| 436 |
| 393 base::DictionaryValue* permissions = new base::DictionaryValue; | 437 base::DictionaryValue* permissions = new base::DictionaryValue; |
| 394 for (size_t i = 0; i < arraysize(kValidTypes); ++i) { | 438 for (size_t i = 0; i < arraysize(kValidTypes); ++i) { |
| 395 ContentSettingsType permission_type = kValidTypes[i]; | 439 ContentSettingsType permission_type = kValidTypes[i]; |
| 396 | 440 |
| 397 // Append the possible settings. | 441 // Append the possible settings. |
| 398 base::ListValue* options = new base::ListValue; | 442 base::ListValue* options = new base::ListValue; |
| 399 ContentSetting default_value = | 443 ContentSetting default_value = |
| 400 map->GetDefaultContentSetting(permission_type, NULL); | 444 map->GetDefaultContentSetting(permission_type, NULL); |
| 401 if (default_value != CONTENT_SETTING_ALLOW && | 445 if (default_value != CONTENT_SETTING_ALLOW && |
| 402 default_value != CONTENT_SETTING_BLOCK) { | 446 default_value != CONTENT_SETTING_BLOCK) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 "setting", content_settings::ContentSettingToString(permission)); | 488 "setting", content_settings::ContentSettingToString(permission)); |
| 445 permission_info->SetWithoutPathExpansion("options", options); | 489 permission_info->SetWithoutPathExpansion("options", options); |
| 446 permission_info->SetBooleanWithoutPathExpansion( | 490 permission_info->SetBooleanWithoutPathExpansion( |
| 447 "editable", info.source == content_settings::SETTING_SOURCE_USER); | 491 "editable", info.source == content_settings::SETTING_SOURCE_USER); |
| 448 permissions->SetWithoutPathExpansion( | 492 permissions->SetWithoutPathExpansion( |
| 449 content_settings::GetTypeName(permission_type), permission_info); | 493 content_settings::GetTypeName(permission_type), permission_info); |
| 450 } | 494 } |
| 451 | 495 |
| 452 base::Value* storage_used = new base::StringValue(l10n_util::GetStringFUTF16( | 496 base::Value* storage_used = new base::StringValue(l10n_util::GetStringFUTF16( |
| 453 IDS_WEBSITE_SETTINGS_STORAGE_USED, ui::FormatBytes(storage))); | 497 IDS_WEBSITE_SETTINGS_STORAGE_USED, ui::FormatBytes(storage))); |
| 498 base::Value* battery_used = |
| 499 new base::StringValue(l10n_util::GetStringFUTF16Int( |
| 500 IDS_WEBSITE_SETTINGS_BATTERY_USED, battery)); |
| 454 | 501 |
| 455 web_ui()->CallJavascriptFunction("WebsiteSettingsEditor.populateOrigin", | 502 web_ui()->CallJavascriptFunction("WebsiteSettingsEditor.populateOrigin", |
| 456 *storage_used, | 503 *storage_used, |
| 504 *battery_used, |
| 457 *permissions, | 505 *permissions, |
| 458 base::FundamentalValue(show_page)); | 506 base::FundamentalValue(show_page)); |
| 459 } | 507 } |
| 460 | 508 |
| 461 void WebsiteSettingsHandler::UpdateLocalStorage() { | 509 void WebsiteSettingsHandler::UpdateLocalStorage() { |
| 462 base::DictionaryValue local_storage_map; | 510 base::DictionaryValue local_storage_map; |
| 463 for (LocalStorageList::const_iterator it = local_storage_list_.begin(); | 511 for (LocalStorageList::const_iterator it = local_storage_list_.begin(); |
| 464 it != local_storage_list_.end(); | 512 it != local_storage_list_.end(); |
| 465 it++) { | 513 it++) { |
| 466 std::string origin = it->origin_url.spec(); | 514 std::string origin = it->origin_url.spec(); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 // If extension is NULL, it was removed and we cannot look up its name. | 589 // If extension is NULL, it was removed and we cannot look up its name. |
| 542 if (!extension) | 590 if (!extension) |
| 543 return site_url.spec(); | 591 return site_url.spec(); |
| 544 | 592 |
| 545 return extension->name(); | 593 return extension->name(); |
| 546 } | 594 } |
| 547 return site_url.spec(); | 595 return site_url.spec(); |
| 548 } | 596 } |
| 549 | 597 |
| 550 } // namespace options | 598 } // namespace options |
| OLD | NEW |