1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/options/website_settings_handler.h"
#include "base/metrics/histogram.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_iterator.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/grit/generated_resources.h"
#include "components/content_settings/core/browser/content_settings_utils.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/power/origin_power_map.h"
#include "components/power/origin_power_map_factory.h"
#include "content/public/browser/dom_storage_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/time_format.h"
#include "ui/base/text/bytes_formatting.h"
#if defined(OS_CHROMEOS)
#include "components/user_manager/user_manager.h"
#endif
using base::UserMetricsAction;
using power::OriginPowerMap;
using power::OriginPowerMapFactory;
namespace {
const char kBattery[] = "battery";
const int kHttpPort = 80;
const int kHttpsPort = 443;
const char kPreferencesSource[] = "preference";
const char kStorage[] = "storage";
const ContentSettingsType kValidTypes[] = {
CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS,
CONTENT_SETTINGS_TYPE_COOKIES,
CONTENT_SETTINGS_TYPE_GEOLOCATION,
CONTENT_SETTINGS_TYPE_IMAGES,
CONTENT_SETTINGS_TYPE_JAVASCRIPT,
CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
CONTENT_SETTINGS_TYPE_MEDIASTREAM,
CONTENT_SETTINGS_TYPE_PLUGINS,
CONTENT_SETTINGS_TYPE_POPUPS};
const size_t kValidTypesLength = arraysize(kValidTypes);
// Enumeration used for UMA Histograms of parsing last_setting_.
// Do not insert, remove, or reorder. Add only to end.
// TODO: Remove histogram by 2015. crbug.com/433475
enum LastSettingParsed {
UPDATE_EMPTY,
UPDATE_PARSED,
UPDATE_INVALID,
UPDATE_ORIGINS_EMPTY,
UPDATE_ORIGINS_PARSED,
UPDATE_ORIGINS_INVALID,
HANDLE_UPDATE_ORIGINS_EMPTY,
HANDLE_UPDATE_ORIGINS_PARSED,
HANDLE_UPDATE_ORIGINS_INVALID,
LAST_SETTING_MAX_ENUMERATION_VALUE
};
void RecordUmaHistogramOfLastSetting(LastSettingParsed parsed) {
UMA_HISTOGRAM_ENUMERATION("ContentSettings.LastSettingParsed", parsed,
LAST_SETTING_MAX_ENUMERATION_VALUE);
}
} // namespace
namespace options {
WebsiteSettingsHandler::WebsiteSettingsHandler()
: observer_(this),
weak_ptr_factory_(this) {
}
WebsiteSettingsHandler::~WebsiteSettingsHandler() {
}
void WebsiteSettingsHandler::GetLocalizedValues(
base::DictionaryValue* localized_strings) {
DCHECK(localized_strings);
static OptionsStringResource resources[] = {
{"websitesOptionsPageTabTitle", IDS_WEBSITE_SETTINGS_TITLE},
{"websitesSettingsEditPage", IDS_WEBSITE_SETTINGS_EDIT_TITLE},
{"websitesManage", IDS_WEBSITE_SETTINGS_MANAGE},
{"websitesSearch", IDS_WEBSITE_SETTINGS_SEARCH_ORIGINS},
{"websitesLabelLocation", IDS_WEBSITE_SETTINGS_TYPE_LOCATION},
{"websitesLabelMediaStream", IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM},
{"websitesLabelNotifications", IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS},
{"websitesLabelOn", IDS_WEBSITE_SETTINGS_CONTENT_SETTING_ENABLED},
{"websitesLabelStorage", IDS_WEBSITE_SETTINGS_TYPE_STORAGE},
{"websitesLabelBattery", IDS_WEBSITE_SETTINGS_TYPE_BATTERY},
{"websitesCookiesDescription", IDS_WEBSITE_SETTINGS_COOKIES_DESCRIPTION},
{"websitesLocationDescription",
IDS_WEBSITE_SETTINGS_LOCATION_DESCRIPTION},
{"websitesMediaStreamDescription",
IDS_WEBSITE_SETTINGS_MEDIASTREAM_DESCRIPTION},
{"websitesNotificationsDescription",
IDS_WEBSITE_SETTINGS_NOTIFICATIONS_DESCRIPTION},
{"websitesDownloadsDescription",
IDS_WEBSITE_SETTINGS_DOWNLOAD_DESCRIPTION},
{"websitesPluginsDescription", IDS_WEBSITE_SETTINGS_PLUGINS_DESCRIPTION},
{"websitesPopupsDescription", IDS_WEBSITE_SETTINGS_POPUPS_DESCRIPTION},
{"websitesJavascriptDescription",
IDS_WEBSITE_SETTINGS_JAVASCRIPT_DESCRIPTION},
{"websitesImagesDescription", IDS_WEBSITE_SETTINGS_IMAGES_DESCRIPTION},
{"websitesButtonClear", IDS_WEBSITE_SETTINGS_STORAGE_CLEAR_BUTTON},
{"websitesButtonStop", IDS_WEBSITE_SETTINGS_BATTERY_STOP_BUTTON},
{"websitesAllowedListTitle", IDS_WEBSITE_SETTINGS_ALLOWED_LIST_TITLE},
{"websitesBlockedListTitle", IDS_WEBSITE_SETTINGS_BLOCKED_LIST_TITLE},
{"storageTabLabel", IDS_WEBSITE_SETTINGS_TYPE_STORAGE},
{"batteryTabLabel", IDS_WEBSITE_SETTINGS_TYPE_BATTERY},
};
RegisterStrings(localized_strings, resources, arraysize(resources));
RegisterTitle(
localized_strings, "websiteSettingsPage", IDS_WEBSITE_SETTINGS_TITLE);
}
void WebsiteSettingsHandler::InitializeHandler() {
Profile* profile = GetProfile();
HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
observer_.Add(settings);
power::OriginPowerMap* origin_power_map =
power::OriginPowerMapFactory::GetForBrowserContext(profile);
// OriginPowerMap may not be available in tests.
if (origin_power_map) {
subscription_ = origin_power_map->AddPowerConsumptionUpdatedCallback(
base::Bind(&WebsiteSettingsHandler::Update, base::Unretained(this)));
}
}
void WebsiteSettingsHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"updateOrigins",
base::Bind(&WebsiteSettingsHandler::HandleUpdateOrigins,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"updateOriginsSearchResults",
base::Bind(&WebsiteSettingsHandler::HandleUpdateSearchResults,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"updateLocalStorage",
base::Bind(&WebsiteSettingsHandler::HandleUpdateLocalStorage,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"updateBatteryUsage",
base::Bind(&WebsiteSettingsHandler::HandleUpdateBatteryUsage,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getOriginInfo",
base::Bind(&WebsiteSettingsHandler::HandleGetOriginInfo,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"setOriginPermission",
base::Bind(&WebsiteSettingsHandler::HandleSetOriginPermission,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"maybeShowEditPage",
base::Bind(&WebsiteSettingsHandler::HandleMaybeShowEditPage,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"deleteLocalStorage",
base::Bind(&WebsiteSettingsHandler::HandleDeleteLocalStorage,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"stopOrigin",
base::Bind(&WebsiteSettingsHandler::HandleStopOrigin,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"updateDefaultSetting",
base::Bind(&WebsiteSettingsHandler::HandleUpdateDefaultSetting,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"setDefaultContentSetting",
base::Bind(&WebsiteSettingsHandler::HandleSetDefaultSetting,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"setGlobalEnabled",
base::Bind(&WebsiteSettingsHandler::HandleSetGlobalToggle,
base::Unretained(this)));
}
// content_settings::Observer implementation.
void WebsiteSettingsHandler::OnContentSettingChanged(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
std::string resource_identifier) {
Update();
}
void WebsiteSettingsHandler::OnContentSettingUsed(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type) {
Update();
}
void WebsiteSettingsHandler::HandleUpdateOrigins(const base::ListValue* args) {
std::string content_setting_name;
bool rv = args->GetString(0, &content_setting_name);
DCHECK(rv);
ContentSettingsType content_type;
if (!content_settings::GetTypeFromName(content_setting_name, &content_type))
return;
DCHECK_NE(
kValidTypes + kValidTypesLength,
std::find(kValidTypes, kValidTypes + kValidTypesLength, content_type));
last_setting_ = content_setting_name;
// Histogram to understand the source of parsing errors. crbug.com/432600.
// TODO: Remove histogram by 2015. crbug.com/433475
ContentSettingsType last_setting;
if (!content_settings::GetTypeFromName(last_setting_, &last_setting)) {
if (last_setting_.empty())
RecordUmaHistogramOfLastSetting(HANDLE_UPDATE_ORIGINS_EMPTY);
else
RecordUmaHistogramOfLastSetting(HANDLE_UPDATE_ORIGINS_INVALID);
} else {
RecordUmaHistogramOfLastSetting(HANDLE_UPDATE_ORIGINS_PARSED);
}
UpdateOrigins();
}
void WebsiteSettingsHandler::HandleUpdateSearchResults(
const base::ListValue* args) {
bool rv = args->GetString(0, &last_filter_);
DCHECK(rv);
Update();
}
void WebsiteSettingsHandler::HandleUpdateLocalStorage(
const base::ListValue* args) {
if (!local_storage_.get()) {
Profile* profile = GetProfile();
local_storage_ = new BrowsingDataLocalStorageHelper(profile);
}
last_setting_ = kStorage;
local_storage_->StartFetching(
base::Bind(&WebsiteSettingsHandler::OnLocalStorageFetched,
weak_ptr_factory_.GetWeakPtr()));
}
void WebsiteSettingsHandler::HandleMaybeShowEditPage(
const base::ListValue* args) {
std::string site;
bool rv = args->GetString(0, &site);
DCHECK(rv);
GURL last_site(site);
if (!last_site.is_valid())
return;
last_site_ = last_site;
base::StringValue site_value(site);
web_ui()->CallJavascriptFunction("WebsiteSettingsEditor.showEditPage",
site_value);
}
void WebsiteSettingsHandler::OnLocalStorageFetched(const std::list<
BrowsingDataLocalStorageHelper::LocalStorageInfo>& storage) {
local_storage_list_ = storage;
Update();
GetInfoForOrigin(last_site_, false);
}
void WebsiteSettingsHandler::Update() {
DCHECK(!last_setting_.empty());
// Histogram to understand the source of parsing errors. crbug.com/432600.
// TODO: Remove histogram by 2015. crbug.com/433475
ContentSettingsType last_setting;
if (!content_settings::GetTypeFromName(last_setting_, &last_setting)) {
if (last_setting_.empty())
RecordUmaHistogramOfLastSetting(UPDATE_EMPTY);
else
RecordUmaHistogramOfLastSetting(UPDATE_INVALID);
} else {
RecordUmaHistogramOfLastSetting(UPDATE_PARSED);
}
if (last_setting_ == kStorage)
UpdateLocalStorage();
else if (last_setting_ == kBattery)
UpdateBatteryUsage();
else
UpdateOrigins();
}
void WebsiteSettingsHandler::UpdateOrigins() {
Profile* profile = GetProfile();
HostContentSettingsMap* settings = profile->GetHostContentSettingsMap();
ContentSettingsForOneType all_settings;
ContentSettingsType last_setting;
if (!content_settings::GetTypeFromName(last_setting_, &last_setting)) {
// TODO: Remove histogram by 2015. crbug.com/433475
if (last_setting_.empty())
RecordUmaHistogramOfLastSetting(UPDATE_ORIGINS_EMPTY);
else
RecordUmaHistogramOfLastSetting(UPDATE_ORIGINS_INVALID);
return;
}
RecordUmaHistogramOfLastSetting(UPDATE_ORIGINS_PARSED);
if (last_setting == CONTENT_SETTINGS_TYPE_MEDIASTREAM)
last_setting = CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC;
settings->GetSettingsForOneType(last_setting, std::string(), &all_settings);
base::DictionaryValue allowed_origins;
base::DictionaryValue blocked_origins;
for (ContentSettingsForOneType::const_iterator it = all_settings.begin();
it != all_settings.end();
++it) {
// Don't add default settings.
if (it->primary_pattern == ContentSettingsPattern::Wildcard() &&
it->secondary_pattern == ContentSettingsPattern::Wildcard() &&
it->source != kPreferencesSource) {
continue;
}
GURL origin_url(it->primary_pattern.ToString());
std::string origin = origin_url.spec();
// Hide the port if it is using a standard URL scheme.
if ((origin_url.SchemeIs(url::kHttpScheme) &&
origin_url.IntPort() == kHttpPort) ||
(origin_url.SchemeIs(url::kHttpsScheme) &&
origin_url.IntPort() == kHttpsPort)) {
url::Replacements<char> replacements;
replacements.ClearPort();
origin = origin_url.ReplaceComponents(replacements).spec();
}
// Mediastream isn't set unless both mic and camera are set to the same.
if (last_setting == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC) {
ContentSetting cam_setting = settings->GetContentSettingWithoutOverride(
origin_url,
origin_url,
CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA,
std::string());
if (it->setting != cam_setting)
continue;
}
if (origin.find(last_filter_) == base::string16::npos)
continue;
base::Time last_usage = settings->GetLastUsageByPattern(
it->primary_pattern, it->secondary_pattern, last_setting);
scoped_ptr<base::DictionaryValue> origin_entry(new base::DictionaryValue());
origin_entry->SetDoubleWithoutPathExpansion("usage",
last_usage.ToDoubleT());
base::string16 usage_string;
if (last_usage.ToDoubleT()) {
usage_string = ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_ELAPSED,
ui::TimeFormat::LENGTH_SHORT,
base::Time::Now() - last_usage);
}
origin_entry->SetStringWithoutPathExpansion("usageString", usage_string);
origin_entry->SetStringWithoutPathExpansion("readableName",
GetReadableName(origin_url));
if (it->setting == CONTENT_SETTING_BLOCK)
blocked_origins.SetWithoutPathExpansion(origin, origin_entry.Pass());
else
allowed_origins.SetWithoutPathExpansion(origin, origin_entry.Pass());
}
bool is_globally_allowed = settings->GetContentSettingOverride(last_setting);
web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
allowed_origins,
blocked_origins,
base::FundamentalValue(is_globally_allowed));
}
void WebsiteSettingsHandler::HandleGetOriginInfo(const base::ListValue* args) {
std::string url;
bool rv = args->GetString(0, &url);
DCHECK(rv);
GURL origin(url);
if (!origin.is_valid())
return;
GetInfoForOrigin(origin, true);
}
void WebsiteSettingsHandler::HandleSetOriginPermission(
const base::ListValue* args) {
std::string setting_name;
bool rv = args->GetString(0, &setting_name);
DCHECK(rv);
ContentSettingsType settings_type;
if (!content_settings::GetTypeFromName(setting_name, &settings_type))
return;
std::string value;
rv = args->GetString(1, &value);
DCHECK(rv);
ContentSetting setting = content_settings::ContentSettingFromString(value);
Profile* profile = GetProfile();
HostContentSettingsMap* map = profile->GetHostContentSettingsMap();
ContentSetting default_value =
map->GetDefaultContentSetting(settings_type, NULL);
// Users are not allowed to be the source of the "ask" setting. It is an
// ephemeral setting which is removed once the question is asked.
if (setting == CONTENT_SETTING_ASK && setting == default_value)
setting = CONTENT_SETTING_DEFAULT;
ContentSettingsPattern primary_pattern;
ContentSettingsPattern secondary_pattern;
switch (settings_type) {
case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
primary_pattern = ContentSettingsPattern::FromURLNoWildcard(last_site_);
secondary_pattern = ContentSettingsPattern::Wildcard();
break;
case CONTENT_SETTINGS_TYPE_MEDIASTREAM:
primary_pattern = ContentSettingsPattern::FromURLNoWildcard(last_site_);
secondary_pattern = ContentSettingsPattern::Wildcard();
map->SetContentSetting(primary_pattern,
secondary_pattern,
CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC,
std::string(),
setting);
map->SetContentSetting(primary_pattern,
secondary_pattern,
CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA,
std::string(),
setting);
return;
case CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS:
case CONTENT_SETTINGS_TYPE_COOKIES:
case CONTENT_SETTINGS_TYPE_GEOLOCATION:
case CONTENT_SETTINGS_TYPE_IMAGES:
case CONTENT_SETTINGS_TYPE_JAVASCRIPT:
case CONTENT_SETTINGS_TYPE_PLUGINS:
case CONTENT_SETTINGS_TYPE_POPUPS:
primary_pattern = ContentSettingsPattern::FromURLNoWildcard(last_site_);
secondary_pattern = ContentSettingsPattern::FromURLNoWildcard(last_site_);
break;
default:
NOTREACHED() << "Content settings type not yet supported.";
return;
}
content_settings::SettingInfo info;
scoped_ptr<base::Value> v(map->GetWebsiteSettingWithoutOverride(
last_site_, last_site_, settings_type, std::string(), &info));
map->SetNarrowestWebsiteSetting(primary_pattern,
secondary_pattern,
settings_type,
std::string(),
setting,
info);
}
void WebsiteSettingsHandler::HandleUpdateBatteryUsage(
const base::ListValue* args) {
last_setting_ = kBattery;
UpdateBatteryUsage();
}
void WebsiteSettingsHandler::HandleDeleteLocalStorage(
const base::ListValue* args) {
DCHECK(!last_site_.is_empty());
DeleteLocalStorage(last_site_);
}
void WebsiteSettingsHandler::HandleStopOrigin(const base::ListValue* args) {
DCHECK(!last_site_.is_empty());
StopOrigin(last_site_);
}
void WebsiteSettingsHandler::HandleUpdateDefaultSetting(
const base::ListValue* args) {
ContentSettingsType type;
if (!content_settings::GetTypeFromName(last_setting_, &type))
return;
Profile* profile = GetProfile();
std::string provider_id;
ContentSetting default_setting =
profile->GetHostContentSettingsMap()->GetDefaultContentSetting(
type, &provider_id);
base::DictionaryValue filter_settings;
filter_settings.SetString(
"value", content_settings::ContentSettingToString(default_setting));
filter_settings.SetString("managedBy", provider_id);
web_ui()->CallJavascriptFunction("WebsiteSettingsManager.updateDefault",
filter_settings);
}
void WebsiteSettingsHandler::HandleSetDefaultSetting(
const base::ListValue* args) {
DCHECK_EQ(1U, args->GetSize());
std::string setting;
if (!args->GetString(0, &setting)) {
NOTREACHED();
return;
}
ContentSetting new_default =
content_settings::ContentSettingFromString(setting);
ContentSettingsType last_setting;
if (!content_settings::GetTypeFromName(last_setting_, &last_setting))
return;
Profile* profile = GetProfile();
HostContentSettingsMap* map = profile->GetHostContentSettingsMap();
map->SetDefaultContentSetting(last_setting, new_default);
switch (last_setting) {
case CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS:
content::RecordAction(
UserMetricsAction("Options_DefaultMultipleAutomaticDLSettingChange"));
break;
case CONTENT_SETTINGS_TYPE_COOKIES:
content::RecordAction(
UserMetricsAction("Options_DefaultCookieSettingChanged"));
break;
case CONTENT_SETTINGS_TYPE_GEOLOCATION:
content::RecordAction(
UserMetricsAction("Options_DefaultGeolocationSettingChanged"));
break;
case CONTENT_SETTINGS_TYPE_IMAGES:
content::RecordAction(
UserMetricsAction("Options_DefaultImagesSettingChanged"));
break;
case CONTENT_SETTINGS_TYPE_JAVASCRIPT:
content::RecordAction(
UserMetricsAction("Options_DefaultJavaScriptSettingChanged"));
break;
case CONTENT_SETTINGS_TYPE_MEDIASTREAM:
content::RecordAction(
UserMetricsAction("Options_DefaultMediaStreamMicSettingChanged"));
break;
case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
content::RecordAction(
UserMetricsAction("Options_DefaultNotificationsSettingChanged"));
break;
case CONTENT_SETTINGS_TYPE_PLUGINS:
content::RecordAction(
UserMetricsAction("Options_DefaultPluginsSettingChanged"));
break;
case CONTENT_SETTINGS_TYPE_POPUPS:
content::RecordAction(
UserMetricsAction("Options_DefaultPopupsSettingChanged"));
break;
default:
NOTREACHED();
return;
}
}
void WebsiteSettingsHandler::HandleSetGlobalToggle(
const base::ListValue* args) {
DCHECK_EQ(1U, args->GetSize());
bool is_enabled;
bool rv = args->GetBoolean(0, &is_enabled);
DCHECK(rv);
ContentSettingsType last_setting;
if (!content_settings::GetTypeFromName(last_setting_, &last_setting))
return;
Profile* profile = GetProfile();
HostContentSettingsMap* map = profile->GetHostContentSettingsMap();
map->SetContentSettingOverride(last_setting, is_enabled);
}
void WebsiteSettingsHandler::GetInfoForOrigin(const GURL& site_url,
bool show_page) {
Profile* profile = GetProfile();
HostContentSettingsMap* map = profile->GetHostContentSettingsMap();
double storage = 0.0;
for (LocalStorageList::const_iterator it = local_storage_list_.begin();
it != local_storage_list_.end();
it++) {
if (it->origin_url == site_url) {
storage = static_cast<double>(it->size);
break;
}
}
int battery = 0;
battery = OriginPowerMapFactory::GetForBrowserContext(
GetProfile())->GetPowerForOrigin(site_url);
base::DictionaryValue* permissions = new base::DictionaryValue;
for (size_t i = 0; i < arraysize(kValidTypes); ++i) {
ContentSettingsType permission_type = kValidTypes[i];
// Append the possible settings.
scoped_ptr<base::ListValue> options(new base::ListValue());
ContentSetting default_value =
map->GetDefaultContentSetting(permission_type, NULL);
if (default_value != CONTENT_SETTING_ALLOW &&
default_value != CONTENT_SETTING_BLOCK) {
options->AppendString(
content_settings::ContentSettingToString(default_value));
}
options->AppendString(
content_settings::ContentSettingToString(CONTENT_SETTING_ALLOW));
options->AppendString(
content_settings::ContentSettingToString(CONTENT_SETTING_BLOCK));
if (permission_type == CONTENT_SETTINGS_TYPE_COOKIES) {
options->AppendString(content_settings::ContentSettingToString(
CONTENT_SETTING_SESSION_ONLY));
}
ContentSetting permission;
content_settings::SettingInfo info;
if (permission_type == CONTENT_SETTINGS_TYPE_MEDIASTREAM) {
scoped_ptr<base::Value> mic_value(map->GetWebsiteSettingWithoutOverride(
site_url,
site_url,
CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC,
std::string(),
&info));
ContentSetting mic_setting =
content_settings::ValueToContentSetting(mic_value.get());
ContentSetting cam_setting = map->GetContentSettingWithoutOverride(
site_url,
site_url,
CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA,
std::string());
if (mic_setting != cam_setting)
permission = CONTENT_SETTING_ASK;
else
permission = mic_setting;
} else {
scoped_ptr<base::Value> v(map->GetWebsiteSettingWithoutOverride(
site_url, site_url, permission_type, std::string(), &info));
permission = content_settings::ValueToContentSetting(v.get());
}
scoped_ptr<base::DictionaryValue>
permission_info(new base::DictionaryValue());
permission_info->SetStringWithoutPathExpansion(
"setting", content_settings::ContentSettingToString(permission));
permission_info->SetWithoutPathExpansion("options", options.Pass());
permission_info->SetBooleanWithoutPathExpansion(
"editable", info.source == content_settings::SETTING_SOURCE_USER);
permissions->SetWithoutPathExpansion(
content_settings::GetTypeName(permission_type), permission_info.Pass());
}
base::Value* storage_used = new base::StringValue(l10n_util::GetStringFUTF16(
IDS_WEBSITE_SETTINGS_STORAGE_USED, ui::FormatBytes(storage)));
base::Value* battery_used =
new base::StringValue(l10n_util::GetStringFUTF16Int(
IDS_WEBSITE_SETTINGS_BATTERY_USED, battery));
web_ui()->CallJavascriptFunction("WebsiteSettingsEditor.populateOrigin",
*storage_used,
*battery_used,
*permissions,
base::FundamentalValue(show_page));
}
void WebsiteSettingsHandler::UpdateLocalStorage() {
base::DictionaryValue local_storage_map;
for (LocalStorageList::const_iterator it = local_storage_list_.begin();
it != local_storage_list_.end();
it++) {
std::string origin = it->origin_url.spec();
if (origin.find(last_filter_) == base::string16::npos)
continue;
scoped_ptr<base::DictionaryValue> origin_entry(new base::DictionaryValue());
origin_entry->SetDoubleWithoutPathExpansion(
"usage", static_cast<double>(it->size));
origin_entry->SetStringWithoutPathExpansion(
"usageString", ui::FormatBytes(it->size));
origin_entry->SetStringWithoutPathExpansion(
"readableName", GetReadableName(it->origin_url));
local_storage_map.SetWithoutPathExpansion(origin, origin_entry.Pass());
}
web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
local_storage_map);
}
void WebsiteSettingsHandler::UpdateBatteryUsage() {
base::DictionaryValue power_map;
OriginPowerMap* origins =
OriginPowerMapFactory::GetForBrowserContext(GetProfile());
OriginPowerMap::PercentOriginMap percent_map = origins->GetPercentOriginMap();
for (std::map<GURL, int>::iterator it = percent_map.begin();
it != percent_map.end();
++it) {
std::string origin = it->first.spec();
if (origin.find(last_filter_) == base::string16::npos)
continue;
scoped_ptr<base::DictionaryValue> origin_entry(new base::DictionaryValue());
origin_entry->SetInteger("usage", it->second);
if (it->second == 0) {
origin_entry->SetString(
"usageString",
l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_BATTERY_ZERO_PERCENT));
} else {
origin_entry->SetString(
"usageString",
l10n_util::GetStringFUTF16Int(IDS_WEBSITE_SETTINGS_BATTERY_PERCENT,
it->second));
}
origin_entry->SetStringWithoutPathExpansion("readableName",
GetReadableName(it->first));
power_map.SetWithoutPathExpansion(origin, origin_entry.Pass());
}
web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins",
power_map);
}
void WebsiteSettingsHandler::StopOrigin(const GURL& site_url) {
Profile* profile = GetProfile();
if (site_url.SchemeIs(extensions::kExtensionScheme)) {
const extensions::Extension* extension =
extensions::ExtensionRegistry::Get(profile)
->enabled_extensions()
.GetHostedAppByURL(site_url);
if (extension) {
extensions::AppWindowRegistry::Get(profile)
->CloseAllAppWindowsForApp(extension->id());
}
}
for (chrome::BrowserIterator it; !it.done(); it.Next()) {
Browser* browser = *it;
TabStripModel* model = browser->tab_strip_model();
for (int idx = 0; idx < model->count(); idx++) {
content::WebContents* web_contents = model->GetWebContentsAt(idx);
// Can't discard tabs that are already discarded or active.
if (model->IsTabDiscarded(idx) || (model->active_index() == idx))
continue;
// Don't discard tabs that belong to other profiles or other origins.
if (web_contents->GetLastCommittedURL().GetOrigin() != site_url ||
profile !=
Profile::FromBrowserContext(web_contents->GetBrowserContext())) {
continue;
}
model->DiscardWebContentsAt(idx);
}
}
}
void WebsiteSettingsHandler::DeleteLocalStorage(const GURL& site_url) {
Profile* profile = GetProfile();
content::DOMStorageContext* dom_storage_context_ =
content::BrowserContext::GetDefaultStoragePartition(profile)
->GetDOMStorageContext();
dom_storage_context_->DeleteLocalStorage(site_url);
// Load a new BrowsingDataLocalStorageHelper to update.
local_storage_ = new BrowsingDataLocalStorageHelper(profile);
local_storage_->StartFetching(
base::Bind(&WebsiteSettingsHandler::OnLocalStorageFetched,
weak_ptr_factory_.GetWeakPtr()));
}
const std::string& WebsiteSettingsHandler::GetReadableName(
const GURL& site_url) {
if (site_url.SchemeIs(extensions::kExtensionScheme)) {
const extensions::ExtensionRegistry* registry =
extensions::ExtensionRegistry::Get(GetProfile());
const extensions::Extension* extension =
registry->enabled_extensions().GetByID(site_url.host());
// If extension is NULL, it was removed and we cannot look up its name.
if (!extension)
return site_url.spec();
return extension->name();
}
return site_url.spec();
}
Profile* WebsiteSettingsHandler::GetProfile() {
Profile* profile = Profile::FromWebUI(web_ui());
#if defined(OS_CHROMEOS)
// Chrome OS special case: in Guest mode settings are opened in Incognito
// mode, so we need original profile to actually modify settings.
if (user_manager::UserManager::Get()->IsLoggedInAsGuest())
profile = profile->GetOriginalProfile();
#endif
return profile;
}
} // namespace options
|