Chromium Code Reviews| 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/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/browser_iterator.h" | 12 #include "chrome/browser/ui/browser_iterator.h" |
| 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 14 #include "components/power/origin_power_map.h" | |
| 15 #include "components/power/origin_power_map_factory.h" | |
| 14 #include "content/public/browser/dom_storage_context.h" | 16 #include "content/public/browser/dom_storage_context.h" |
| 15 #include "content/public/browser/storage_partition.h" | 17 #include "content/public/browser/storage_partition.h" |
| 16 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/browser/web_ui.h" | 19 #include "content/public/browser/web_ui.h" |
| 18 #include "extensions/browser/extension_registry.h" | 20 #include "extensions/browser/extension_registry.h" |
| 19 #include "extensions/common/constants.h" | 21 #include "extensions/common/constants.h" |
| 20 #include "extensions/common/extension.h" | 22 #include "extensions/common/extension.h" |
| 21 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/base/l10n/time_format.h" | 25 #include "ui/base/l10n/time_format.h" |
| 24 #include "ui/base/text/bytes_formatting.h" | 26 #include "ui/base/text/bytes_formatting.h" |
| 25 | 27 |
| 28 using power::OriginPowerMap; | |
| 29 using power::OriginPowerMapFactory; | |
| 30 | |
| 26 namespace { | 31 namespace { |
| 27 | 32 |
| 28 const int kHttpPort = 80; | 33 const int kHttpPort = 80; |
| 29 const int kHttpsPort = 443; | 34 const int kHttpsPort = 443; |
| 30 const char kPreferencesSource[] = "preference"; | 35 const char kPreferencesSource[] = "preference"; |
| 31 const char kStorage[] = "storage"; | 36 const char kStorage[] = "storage"; |
| 32 const ContentSettingsType kValidTypes[] = {CONTENT_SETTINGS_TYPE_GEOLOCATION, | 37 const ContentSettingsType kValidTypes[] = {CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 33 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 38 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 34 CONTENT_SETTINGS_TYPE_MEDIASTREAM}; | 39 CONTENT_SETTINGS_TYPE_MEDIASTREAM}; |
| 35 const size_t kValidTypesLength = arraysize(kValidTypes); | 40 const size_t kValidTypesLength = arraysize(kValidTypes); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 50 | 55 |
| 51 static OptionsStringResource resources[] = { | 56 static OptionsStringResource resources[] = { |
| 52 {"websitesOptionsPageTabTitle", IDS_WEBSITE_SETTINGS_TITLE}, | 57 {"websitesOptionsPageTabTitle", IDS_WEBSITE_SETTINGS_TITLE}, |
| 53 {"websitesSettingsEditPage", IDS_WEBSITE_SETTINGS_EDIT_TITLE}, | 58 {"websitesSettingsEditPage", IDS_WEBSITE_SETTINGS_EDIT_TITLE}, |
| 54 {"websitesManage", IDS_WEBSITE_SETTINGS_MANAGE}, | 59 {"websitesManage", IDS_WEBSITE_SETTINGS_MANAGE}, |
| 55 {"websitesSearch", IDS_WEBSITE_SETTINGS_SEARCH_ORIGINS}, | 60 {"websitesSearch", IDS_WEBSITE_SETTINGS_SEARCH_ORIGINS}, |
| 56 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION}, | 61 {"websitesLabelGeolocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION}, |
| 57 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM}, | 62 {"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM}, |
| 58 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS}, | 63 {"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS}, |
| 59 {"websitesLabelStorage", IDS_WEBSITE_SETTINGS_TYPE_STORAGE}, | 64 {"websitesLabelStorage", IDS_WEBSITE_SETTINGS_TYPE_STORAGE}, |
| 65 {"websitesLabelBattery", IDS_WEBSITE_SETTINGS_TYPE_BATTERY}, | |
| 60 {"websitesLocationDescription", | 66 {"websitesLocationDescription", |
| 61 IDS_WEBSITE_SETTINGS_LOCATION_DESCRIPTION}, | 67 IDS_WEBSITE_SETTINGS_LOCATION_DESCRIPTION}, |
| 62 {"websitesMediastreamDescription", | 68 {"websitesMediastreamDescription", |
| 63 IDS_WEBSITE_SETTINGS_MEDIASTREAM_DESCRIPTION}, | 69 IDS_WEBSITE_SETTINGS_MEDIASTREAM_DESCRIPTION}, |
| 64 {"websitesNotificationsDescription", | 70 {"websitesNotificationsDescription", |
| 65 IDS_WEBSITE_SETTINGS_NOTIFICATIONS_DESCRIPTION}, | 71 IDS_WEBSITE_SETTINGS_NOTIFICATIONS_DESCRIPTION}, |
| 66 {"websitesButtonClear", IDS_WEBSITE_SETTINGS_STORAGE_CLEAR_BUTTON}, | 72 {"websitesButtonClear", IDS_WEBSITE_SETTINGS_STORAGE_CLEAR_BUTTON}, |
| 73 {"websitesButtonStop", IDS_WEBSITE_SETTINGS_BATTERY_STOP_BUTTON}, | |
| 67 }; | 74 }; |
| 68 | 75 |
| 69 RegisterStrings(localized_strings, resources, arraysize(resources)); | 76 RegisterStrings(localized_strings, resources, arraysize(resources)); |
| 70 RegisterTitle( | 77 RegisterTitle( |
| 71 localized_strings, "websiteSettingsPage", IDS_WEBSITE_SETTINGS_TITLE); | 78 localized_strings, "websiteSettingsPage", IDS_WEBSITE_SETTINGS_TITLE); |
| 72 } | 79 } |
| 73 | 80 |
| 74 void WebsiteSettingsHandler::InitializeHandler() { | 81 void WebsiteSettingsHandler::InitializeHandler() { |
| 75 Profile* profile = Profile::FromWebUI(web_ui()); | 82 Profile* profile = Profile::FromWebUI(web_ui()); |
| 76 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); | 83 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 87 "updateOriginsSearchResults", | 94 "updateOriginsSearchResults", |
| 88 base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults, | 95 base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults, |
| 89 base::Unretained(this))); | 96 base::Unretained(this))); |
| 90 | 97 |
| 91 web_ui()->RegisterMessageCallback( | 98 web_ui()->RegisterMessageCallback( |
| 92 "updateLocalStorage", | 99 "updateLocalStorage", |
| 93 base::Bind(&WebsiteSettingsHandler::HandleUpdateLocalStorage, | 100 base::Bind(&WebsiteSettingsHandler::HandleUpdateLocalStorage, |
| 94 base::Unretained(this))); | 101 base::Unretained(this))); |
| 95 | 102 |
| 96 web_ui()->RegisterMessageCallback( | 103 web_ui()->RegisterMessageCallback( |
| 104 "updateBatteryUsage", | |
| 105 base::Bind(&WebsiteSettingsHandler::HandleUpdateBatteryUsage, | |
| 106 base::Unretained(this))); | |
| 107 | |
| 108 web_ui()->RegisterMessageCallback( | |
| 97 "getOriginInfo", | 109 "getOriginInfo", |
| 98 base::Bind(&WebsiteSettingsHandler::HandleGetOriginInfo, | 110 base::Bind(&WebsiteSettingsHandler::HandleGetOriginInfo, |
| 99 base::Unretained(this))); | 111 base::Unretained(this))); |
| 100 | 112 |
| 101 web_ui()->RegisterMessageCallback( | 113 web_ui()->RegisterMessageCallback( |
| 102 "setOriginPermission", | 114 "setOriginPermission", |
| 103 base::Bind(&WebsiteSettingsHandler::HandleSetOriginPermission, | 115 base::Bind(&WebsiteSettingsHandler::HandleSetOriginPermission, |
| 104 base::Unretained(this))); | 116 base::Unretained(this))); |
| 105 | 117 |
| 106 web_ui()->RegisterMessageCallback( | 118 web_ui()->RegisterMessageCallback( |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 339 scoped_ptr<base::Value> v(map->GetWebsiteSetting( | 351 scoped_ptr<base::Value> v(map->GetWebsiteSetting( |
| 340 last_site_, last_site_, settings_type, std::string(), &info)); | 352 last_site_, last_site_, settings_type, std::string(), &info)); |
| 341 map->SetNarrowestWebsiteSetting(primary_pattern, | 353 map->SetNarrowestWebsiteSetting(primary_pattern, |
| 342 secondary_pattern, | 354 secondary_pattern, |
| 343 settings_type, | 355 settings_type, |
| 344 std::string(), | 356 std::string(), |
| 345 setting, | 357 setting, |
| 346 info); | 358 info); |
| 347 } | 359 } |
| 348 | 360 |
| 361 void WebsiteSettingsHandler::HandleUpdateBatteryUsage( | |
| 362 const base::ListValue* args) { | |
| 363 base::DictionaryValue power_map; | |
| 364 OriginPowerMap* origins = | |
| 365 OriginPowerMapFactory::GetForBrowserContext(Profile::FromWebUI(web_ui())); | |
| 366 OriginPowerMap::PercentOriginMap percent_map = origins->GetPercentOriginMap(); | |
| 367 for (std::map<GURL, int>::iterator it = percent_map.begin(); | |
| 368 it != percent_map.end(); | |
| 369 ++it) { | |
| 370 std::string origin = it->first.spec(); | |
| 371 | |
| 372 if (origin.find(last_filter_) == base::string16::npos) | |
| 373 continue; | |
| 374 | |
| 375 base::DictionaryValue* origin_entry = new base::DictionaryValue(); | |
| 376 origin_entry->SetWithoutPathExpansion( | |
|
Bernhard Bauer
2014/08/26 08:16:07
The ...WithoutPathExpansion variant isn't necessar
Daniel Nishi
2014/08/26 17:45:39
Done.
| |
| 377 "usage", new base::FundamentalValue(it->second)); | |
| 378 origin_entry->SetWithoutPathExpansion( | |
|
Bernhard Bauer
2014/08/26 08:16:07
SetString() here.
Daniel Nishi
2014/08/26 17:45:39
Done.
| |
| 379 "usageString", | |
| 380 new base::StringValue(l10n_util::GetStringFUTF16Int( | |
| 381 IDS_WEBSITE_SETTINGS_BATTERY_PERCENT, it->second))); | |
| 382 power_map.SetWithoutPathExpansion(origin, origin_entry); | |
| 383 } | |
| 384 web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", | |
| 385 power_map); | |
| 386 } | |
| 387 | |
| 349 void WebsiteSettingsHandler::HandleDeleteLocalStorage( | 388 void WebsiteSettingsHandler::HandleDeleteLocalStorage( |
| 350 const base::ListValue* args) { | 389 const base::ListValue* args) { |
| 351 DCHECK(!last_site_.is_empty()); | 390 DCHECK(!last_site_.is_empty()); |
| 352 DeleteLocalStorage(last_site_); | 391 DeleteLocalStorage(last_site_); |
| 353 } | 392 } |
| 354 | 393 |
| 355 void WebsiteSettingsHandler::HandleStopOrigin(const base::ListValue* args) { | 394 void WebsiteSettingsHandler::HandleStopOrigin(const base::ListValue* args) { |
| 356 DCHECK(!last_site_.is_empty()); | 395 DCHECK(!last_site_.is_empty()); |
| 357 StopOrigin(last_site_); | 396 StopOrigin(last_site_); |
| 358 } | 397 } |
| 359 | 398 |
| 360 void WebsiteSettingsHandler::GetInfoForOrigin(const GURL& site_url, | 399 void WebsiteSettingsHandler::GetInfoForOrigin(const GURL& site_url, |
| 361 bool show_page) { | 400 bool show_page) { |
| 362 Profile* profile = Profile::FromWebUI(web_ui()); | 401 Profile* profile = Profile::FromWebUI(web_ui()); |
| 363 HostContentSettingsMap* map = profile->GetHostContentSettingsMap(); | 402 HostContentSettingsMap* map = profile->GetHostContentSettingsMap(); |
| 364 | 403 |
| 365 double storage = 0.0; | 404 double storage = 0.0; |
| 366 for (LocalStorageList::const_iterator it = local_storage_list_.begin(); | 405 for (LocalStorageList::const_iterator it = local_storage_list_.begin(); |
| 367 it != local_storage_list_.end(); | 406 it != local_storage_list_.end(); |
| 368 it++) { | 407 it++) { |
| 369 if (it->origin_url == site_url) { | 408 if (it->origin_url == site_url) { |
| 370 storage = static_cast<double>(it->size); | 409 storage = static_cast<double>(it->size); |
| 371 break; | 410 break; |
| 372 } | 411 } |
| 373 } | 412 } |
| 374 | 413 |
| 414 int battery = 0; | |
| 415 battery = OriginPowerMapFactory::GetForBrowserContext( | |
| 416 Profile::FromWebUI(web_ui()))->GetPowerForOrigin(site_url); | |
|
Bernhard Bauer
2014/08/26 08:16:08
Indent just four spaces w/r/t the preceding line.
Daniel Nishi
2014/08/26 17:45:39
Done.
| |
| 417 | |
| 375 base::DictionaryValue* permissions = new base::DictionaryValue; | 418 base::DictionaryValue* permissions = new base::DictionaryValue; |
| 376 for (size_t i = 0; i < arraysize(kValidTypes); ++i) { | 419 for (size_t i = 0; i < arraysize(kValidTypes); ++i) { |
| 377 ContentSettingsType permission_type = kValidTypes[i]; | 420 ContentSettingsType permission_type = kValidTypes[i]; |
| 378 | 421 |
| 379 // Append the possible settings. | 422 // Append the possible settings. |
| 380 base::ListValue* options = new base::ListValue; | 423 base::ListValue* options = new base::ListValue; |
| 381 ContentSetting default_value = | 424 ContentSetting default_value = |
| 382 map->GetDefaultContentSetting(permission_type, NULL); | 425 map->GetDefaultContentSetting(permission_type, NULL); |
| 383 if (default_value != CONTENT_SETTING_ALLOW && | 426 if (default_value != CONTENT_SETTING_ALLOW && |
| 384 default_value != CONTENT_SETTING_BLOCK) { | 427 default_value != CONTENT_SETTING_BLOCK) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 422 "setting", content_settings::ContentSettingToString(permission)); | 465 "setting", content_settings::ContentSettingToString(permission)); |
| 423 permission_info->SetWithoutPathExpansion("options", options); | 466 permission_info->SetWithoutPathExpansion("options", options); |
| 424 permission_info->SetBooleanWithoutPathExpansion( | 467 permission_info->SetBooleanWithoutPathExpansion( |
| 425 "editable", info.source == content_settings::SETTING_SOURCE_USER); | 468 "editable", info.source == content_settings::SETTING_SOURCE_USER); |
| 426 permissions->SetWithoutPathExpansion( | 469 permissions->SetWithoutPathExpansion( |
| 427 content_settings::GetTypeName(permission_type), permission_info); | 470 content_settings::GetTypeName(permission_type), permission_info); |
| 428 } | 471 } |
| 429 | 472 |
| 430 base::Value* storage_used = new base::StringValue(l10n_util::GetStringFUTF16( | 473 base::Value* storage_used = new base::StringValue(l10n_util::GetStringFUTF16( |
| 431 IDS_WEBSITE_SETTINGS_STORAGE_USED, ui::FormatBytes(storage))); | 474 IDS_WEBSITE_SETTINGS_STORAGE_USED, ui::FormatBytes(storage))); |
| 475 base::Value* battery_used = | |
| 476 new base::StringValue(l10n_util::GetStringFUTF16Int( | |
| 477 IDS_WEBSITE_SETTINGS_BATTERY_USED, battery)); | |
| 432 | 478 |
| 433 web_ui()->CallJavascriptFunction("WebsiteSettingsEditor.populateOrigin", | 479 web_ui()->CallJavascriptFunction("WebsiteSettingsEditor.populateOrigin", |
| 434 *storage_used, | 480 *storage_used, |
| 481 *battery_used, | |
| 435 *permissions, | 482 *permissions, |
| 436 base::FundamentalValue(show_page)); | 483 base::FundamentalValue(show_page)); |
| 437 } | 484 } |
| 438 | 485 |
| 439 void WebsiteSettingsHandler::UpdateLocalStorage() { | 486 void WebsiteSettingsHandler::UpdateLocalStorage() { |
| 440 base::DictionaryValue local_storage_map; | 487 base::DictionaryValue local_storage_map; |
| 441 for (LocalStorageList::const_iterator it = local_storage_list_.begin(); | 488 for (LocalStorageList::const_iterator it = local_storage_list_.begin(); |
| 442 it != local_storage_list_.end(); | 489 it != local_storage_list_.end(); |
| 443 it++) { | 490 it++) { |
| 444 std::string origin = it->origin_url.spec(); | 491 std::string origin = it->origin_url.spec(); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 | 546 |
| 500 // Load a new BrowsingDataLocalStorageHelper to update. | 547 // Load a new BrowsingDataLocalStorageHelper to update. |
| 501 local_storage_ = new BrowsingDataLocalStorageHelper(profile); | 548 local_storage_ = new BrowsingDataLocalStorageHelper(profile); |
| 502 | 549 |
| 503 local_storage_->StartFetching( | 550 local_storage_->StartFetching( |
| 504 base::Bind(&WebsiteSettingsHandler::OnLocalStorageFetched, | 551 base::Bind(&WebsiteSettingsHandler::OnLocalStorageFetched, |
| 505 weak_ptr_factory_.GetWeakPtr())); | 552 weak_ptr_factory_.GetWeakPtr())); |
| 506 } | 553 } |
| 507 | 554 |
| 508 } // namespace options | 555 } // namespace options |
| OLD | NEW |