| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/settings/site_settings_handler.h" | 5 #include "chrome/browser/ui/webui/settings/site_settings_handler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h" | 14 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 16 #include "chrome/browser/permissions/chooser_context_base.h" | 17 #include "chrome/browser/permissions/chooser_context_base.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/webui/site_settings_helper.h" | 19 #include "chrome/browser/ui/webui/site_settings_helper.h" |
| 19 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" | 20 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
| 20 #include "components/content_settings/core/browser/host_content_settings_map.h" | 21 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 21 #include "components/content_settings/core/common/content_settings_types.h" | 22 #include "components/content_settings/core/common/content_settings_types.h" |
| 22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/notification_service.h" |
| 23 #include "content/public/browser/web_ui.h" | 25 #include "content/public/browser/web_ui.h" |
| 24 #include "extensions/browser/extension_registry.h" | 26 #include "extensions/browser/extension_registry.h" |
| 25 #include "extensions/common/permissions/api_permission.h" | 27 #include "extensions/common/permissions/api_permission.h" |
| 26 #include "extensions/common/permissions/permissions_data.h" | 28 #include "extensions/common/permissions/permissions_data.h" |
| 27 #include "storage/browser/quota/quota_manager.h" | 29 #include "storage/browser/quota/quota_manager.h" |
| 28 #include "storage/common/quota/quota_status_code.h" | 30 #include "storage/common/quota/quota_status_code.h" |
| 29 #include "ui/base/text/bytes_formatting.h" | 31 #include "ui/base/text/bytes_formatting.h" |
| 30 | 32 |
| 31 | |
| 32 namespace settings { | 33 namespace settings { |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 | 36 |
| 36 const char kAppName[] = "appName"; | 37 const char kAppName[] = "appName"; |
| 37 const char kAppId[] = "appId"; | 38 const char kAppId[] = "appId"; |
| 38 | 39 |
| 39 // Return an appropriate API Permission ID for the given string name. | 40 // Return an appropriate API Permission ID for the given string name. |
| 40 extensions::APIPermission::APIPermission::ID APIPermissionFromGroupName( | 41 extensions::APIPermission::APIPermission::ID APIPermissionFromGroupName( |
| 41 std::string type) { | 42 std::string type) { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 base::Bind(&SiteSettingsHandler::HandleResetCategoryPermissionForOrigin, | 147 base::Bind(&SiteSettingsHandler::HandleResetCategoryPermissionForOrigin, |
| 147 base::Unretained(this))); | 148 base::Unretained(this))); |
| 148 web_ui()->RegisterMessageCallback( | 149 web_ui()->RegisterMessageCallback( |
| 149 "setCategoryPermissionForOrigin", | 150 "setCategoryPermissionForOrigin", |
| 150 base::Bind(&SiteSettingsHandler::HandleSetCategoryPermissionForOrigin, | 151 base::Bind(&SiteSettingsHandler::HandleSetCategoryPermissionForOrigin, |
| 151 base::Unretained(this))); | 152 base::Unretained(this))); |
| 152 web_ui()->RegisterMessageCallback( | 153 web_ui()->RegisterMessageCallback( |
| 153 "isPatternValid", | 154 "isPatternValid", |
| 154 base::Bind(&SiteSettingsHandler::HandleIsPatternValid, | 155 base::Bind(&SiteSettingsHandler::HandleIsPatternValid, |
| 155 base::Unretained(this))); | 156 base::Unretained(this))); |
| 157 web_ui()->RegisterMessageCallback( |
| 158 "updateIncognitoStatus", |
| 159 base::Bind(&SiteSettingsHandler::HandleUpdateIncognitoStatus, |
| 160 base::Unretained(this))); |
| 156 } | 161 } |
| 157 | 162 |
| 158 void SiteSettingsHandler::OnJavascriptAllowed() { | 163 void SiteSettingsHandler::OnJavascriptAllowed() { |
| 159 observer_.Add(HostContentSettingsMapFactory::GetForProfile(profile_)); | 164 observer_.Add(HostContentSettingsMapFactory::GetForProfile(profile_)); |
| 160 if (profile_->HasOffTheRecordProfile()) { | 165 if (profile_->HasOffTheRecordProfile()) { |
| 161 auto* map = HostContentSettingsMapFactory::GetForProfile( | 166 auto* map = HostContentSettingsMapFactory::GetForProfile( |
| 162 profile_->GetOffTheRecordProfile()); | 167 profile_->GetOffTheRecordProfile()); |
| 163 if (!observer_.IsObserving(map)) | 168 if (!observer_.IsObserving(map)) |
| 164 observer_.Add(map); | 169 observer_.Add(map); |
| 165 } | 170 } |
| 171 |
| 172 notification_registrar_.Add( |
| 173 this, chrome::NOTIFICATION_PROFILE_CREATED, |
| 174 content::NotificationService::AllSources()); |
| 175 notification_registrar_.Add( |
| 176 this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 177 content::NotificationService::AllSources()); |
| 166 } | 178 } |
| 167 | 179 |
| 168 void SiteSettingsHandler::OnJavascriptDisallowed() { | 180 void SiteSettingsHandler::OnJavascriptDisallowed() { |
| 169 observer_.RemoveAll(); | 181 observer_.RemoveAll(); |
| 182 notification_registrar_.RemoveAll(); |
| 170 } | 183 } |
| 171 | 184 |
| 172 void SiteSettingsHandler::OnGetUsageInfo( | 185 void SiteSettingsHandler::OnGetUsageInfo( |
| 173 const storage::UsageInfoEntries& entries) { | 186 const storage::UsageInfoEntries& entries) { |
| 174 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 187 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 175 | 188 |
| 176 for (const auto& entry : entries) { | 189 for (const auto& entry : entries) { |
| 177 if (entry.usage <= 0) continue; | 190 if (entry.usage <= 0) continue; |
| 178 if (entry.host == usage_host_) { | 191 if (entry.host == usage_host_) { |
| 179 CallJavascriptFunction("settings.WebsiteUsagePrivateApi.returnUsageTotal", | 192 CallJavascriptFunction("settings.WebsiteUsagePrivateApi.returnUsageTotal", |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 base::StringValue("contentSettingSitePermissionChanged"), | 225 base::StringValue("contentSettingSitePermissionChanged"), |
| 213 base::StringValue(site_settings::ContentSettingsTypeToGroupName( | 226 base::StringValue(site_settings::ContentSettingsTypeToGroupName( |
| 214 content_type)), | 227 content_type)), |
| 215 base::StringValue(primary_pattern.ToString()), | 228 base::StringValue(primary_pattern.ToString()), |
| 216 base::StringValue( | 229 base::StringValue( |
| 217 secondary_pattern == ContentSettingsPattern::Wildcard() ? | 230 secondary_pattern == ContentSettingsPattern::Wildcard() ? |
| 218 "" : secondary_pattern.ToString())); | 231 "" : secondary_pattern.ToString())); |
| 219 } | 232 } |
| 220 } | 233 } |
| 221 | 234 |
| 235 void SiteSettingsHandler::Observe( |
| 236 int type, |
| 237 const content::NotificationSource& source, |
| 238 const content::NotificationDetails& details) { |
| 239 switch (type) { |
| 240 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
| 241 Profile* profile = content::Source<Profile>(source).ptr(); |
| 242 if (!profile_->IsSameProfile(profile)) |
| 243 break; |
| 244 SendIncognitoStatus(profile, /*was_destroyed=*/ true); |
| 245 |
| 246 HostContentSettingsMap* settings_map = |
| 247 HostContentSettingsMapFactory::GetForProfile(profile); |
| 248 if (profile->IsOffTheRecord() && |
| 249 observer_.IsObserving(settings_map)) { |
| 250 observer_.Remove(settings_map); |
| 251 } |
| 252 |
| 253 break; |
| 254 } |
| 255 |
| 256 case chrome::NOTIFICATION_PROFILE_CREATED: { |
| 257 Profile* profile = content::Source<Profile>(source).ptr(); |
| 258 if (!profile_->IsSameProfile(profile)) |
| 259 break; |
| 260 SendIncognitoStatus(profile, /*was_destroyed=*/ false); |
| 261 |
| 262 observer_.Add(HostContentSettingsMapFactory::GetForProfile(profile)); |
| 263 break; |
| 264 } |
| 265 } |
| 266 } |
| 267 |
| 222 void SiteSettingsHandler::HandleFetchUsageTotal( | 268 void SiteSettingsHandler::HandleFetchUsageTotal( |
| 223 const base::ListValue* args) { | 269 const base::ListValue* args) { |
| 224 AllowJavascript(); | 270 AllowJavascript(); |
| 225 | 271 |
| 226 CHECK_EQ(1U, args->GetSize()); | 272 CHECK_EQ(1U, args->GetSize()); |
| 227 std::string host; | 273 std::string host; |
| 228 CHECK(args->GetString(0, &host)); | 274 CHECK(args->GetString(0, &host)); |
| 229 usage_host_ = host; | 275 usage_host_ = host; |
| 230 | 276 |
| 231 scoped_refptr<StorageInfoFetcher> storage_info_fetcher | 277 scoped_refptr<StorageInfoFetcher> storage_info_fetcher |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 | 392 |
| 347 CHECK_EQ(2U, args->GetSize()); | 393 CHECK_EQ(2U, args->GetSize()); |
| 348 const base::Value* callback_id; | 394 const base::Value* callback_id; |
| 349 CHECK(args->Get(0, &callback_id)); | 395 CHECK(args->Get(0, &callback_id)); |
| 350 std::string type; | 396 std::string type; |
| 351 CHECK(args->GetString(1, &type)); | 397 CHECK(args->GetString(1, &type)); |
| 352 ContentSettingsType content_type = | 398 ContentSettingsType content_type = |
| 353 static_cast<ContentSettingsType>(static_cast<int>( | 399 static_cast<ContentSettingsType>(static_cast<int>( |
| 354 site_settings::ContentSettingsTypeFromGroupName(type))); | 400 site_settings::ContentSettingsTypeFromGroupName(type))); |
| 355 | 401 |
| 402 std::unique_ptr<base::ListValue> exceptions(new base::ListValue); |
| 403 |
| 356 HostContentSettingsMap* map = | 404 HostContentSettingsMap* map = |
| 357 HostContentSettingsMapFactory::GetForProfile(profile_); | 405 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 358 std::unique_ptr<base::ListValue> exceptions(new base::ListValue); | |
| 359 | |
| 360 AddExceptionsGrantedByHostedApps(profile_, APIPermissionFromGroupName(type), | 406 AddExceptionsGrantedByHostedApps(profile_, APIPermissionFromGroupName(type), |
| 361 exceptions.get()); | 407 exceptions.get()); |
| 408 site_settings::GetExceptionsFromHostContentSettingsMap( |
| 409 map, content_type, web_ui(), false, exceptions.get()); |
| 362 | 410 |
| 363 site_settings::GetExceptionsFromHostContentSettingsMap( | 411 if (profile_->HasOffTheRecordProfile()) { |
| 364 map, content_type, web_ui(), exceptions.get()); | 412 Profile* incognito = profile_->GetOffTheRecordProfile(); |
| 413 map = HostContentSettingsMapFactory::GetForProfile(incognito); |
| 414 site_settings::GetExceptionsFromHostContentSettingsMap( |
| 415 map, content_type, web_ui(), true, exceptions.get()); |
| 416 } |
| 417 |
| 365 ResolveJavascriptCallback(*callback_id, *exceptions.get()); | 418 ResolveJavascriptCallback(*callback_id, *exceptions.get()); |
| 366 } | 419 } |
| 367 | 420 |
| 368 void SiteSettingsHandler::HandleResetCategoryPermissionForOrigin( | 421 void SiteSettingsHandler::HandleResetCategoryPermissionForOrigin( |
| 369 const base::ListValue* args) { | 422 const base::ListValue* args) { |
| 370 CHECK_EQ(3U, args->GetSize()); | 423 CHECK_EQ(4U, args->GetSize()); |
| 371 std::string primary_pattern; | 424 std::string primary_pattern; |
| 372 CHECK(args->GetString(0, &primary_pattern)); | 425 CHECK(args->GetString(0, &primary_pattern)); |
| 373 std::string secondary_pattern; | 426 std::string secondary_pattern; |
| 374 CHECK(args->GetString(1, &secondary_pattern)); | 427 CHECK(args->GetString(1, &secondary_pattern)); |
| 375 std::string type; | 428 std::string type; |
| 376 CHECK(args->GetString(2, &type)); | 429 CHECK(args->GetString(2, &type)); |
| 430 bool incognito; |
| 431 CHECK(args->GetBoolean(3, &incognito)); |
| 377 | 432 |
| 378 ContentSettingsType content_type = | 433 ContentSettingsType content_type = |
| 379 static_cast<ContentSettingsType>(static_cast<int>( | 434 static_cast<ContentSettingsType>(static_cast<int>( |
| 380 site_settings::ContentSettingsTypeFromGroupName(type))); | 435 site_settings::ContentSettingsTypeFromGroupName(type))); |
| 381 | 436 |
| 437 Profile* profile = nullptr; |
| 438 if (incognito) { |
| 439 if (!profile_->HasOffTheRecordProfile()) |
| 440 return; |
| 441 profile = profile_->GetOffTheRecordProfile(); |
| 442 } else { |
| 443 profile = profile_; |
| 444 } |
| 445 |
| 382 HostContentSettingsMap* map = | 446 HostContentSettingsMap* map = |
| 383 HostContentSettingsMapFactory::GetForProfile(profile_); | 447 HostContentSettingsMapFactory::GetForProfile(profile); |
| 384 map->SetContentSettingCustomScope( | 448 map->SetContentSettingCustomScope( |
| 385 ContentSettingsPattern::FromString(primary_pattern), | 449 ContentSettingsPattern::FromString(primary_pattern), |
| 386 secondary_pattern.empty() ? | 450 secondary_pattern.empty() ? |
| 387 ContentSettingsPattern::Wildcard() : | 451 ContentSettingsPattern::Wildcard() : |
| 388 ContentSettingsPattern::FromString(secondary_pattern), | 452 ContentSettingsPattern::FromString(secondary_pattern), |
| 389 content_type, "", CONTENT_SETTING_DEFAULT); | 453 content_type, "", CONTENT_SETTING_DEFAULT); |
| 390 } | 454 } |
| 391 | 455 |
| 392 void SiteSettingsHandler::HandleSetCategoryPermissionForOrigin( | 456 void SiteSettingsHandler::HandleSetCategoryPermissionForOrigin( |
| 393 const base::ListValue* args) { | 457 const base::ListValue* args) { |
| 394 CHECK_EQ(4U, args->GetSize()); | 458 CHECK_EQ(5U, args->GetSize()); |
| 395 std::string primary_pattern; | 459 std::string primary_pattern; |
| 396 CHECK(args->GetString(0, &primary_pattern)); | 460 CHECK(args->GetString(0, &primary_pattern)); |
| 397 std::string secondary_pattern; | 461 std::string secondary_pattern; |
| 398 CHECK(args->GetString(1, &secondary_pattern)); | 462 CHECK(args->GetString(1, &secondary_pattern)); |
| 399 std::string type; | 463 std::string type; |
| 400 CHECK(args->GetString(2, &type)); | 464 CHECK(args->GetString(2, &type)); |
| 401 std::string value; | 465 std::string value; |
| 402 CHECK(args->GetString(3, &value)); | 466 CHECK(args->GetString(3, &value)); |
| 467 bool incognito; |
| 468 CHECK(args->GetBoolean(4, &incognito)); |
| 403 | 469 |
| 404 ContentSettingsType content_type = | 470 ContentSettingsType content_type = |
| 405 static_cast<ContentSettingsType>(static_cast<int>( | 471 static_cast<ContentSettingsType>(static_cast<int>( |
| 406 site_settings::ContentSettingsTypeFromGroupName(type))); | 472 site_settings::ContentSettingsTypeFromGroupName(type))); |
| 407 ContentSetting setting; | 473 ContentSetting setting; |
| 408 CHECK(content_settings::ContentSettingFromString(value, &setting)); | 474 CHECK(content_settings::ContentSettingFromString(value, &setting)); |
| 409 | 475 |
| 476 Profile* profile = nullptr; |
| 477 if (incognito) { |
| 478 if (!profile_->HasOffTheRecordProfile()) |
| 479 return; |
| 480 profile = profile_->GetOffTheRecordProfile(); |
| 481 } else { |
| 482 profile = profile_; |
| 483 } |
| 484 |
| 410 HostContentSettingsMap* map = | 485 HostContentSettingsMap* map = |
| 411 HostContentSettingsMapFactory::GetForProfile(profile_); | 486 HostContentSettingsMapFactory::GetForProfile(profile); |
| 412 map->SetContentSettingCustomScope( | 487 map->SetContentSettingCustomScope( |
| 413 ContentSettingsPattern::FromString(primary_pattern), | 488 ContentSettingsPattern::FromString(primary_pattern), |
| 414 secondary_pattern.empty() ? | 489 secondary_pattern.empty() ? |
| 415 ContentSettingsPattern::Wildcard() : | 490 ContentSettingsPattern::Wildcard() : |
| 416 ContentSettingsPattern::FromString(secondary_pattern), | 491 ContentSettingsPattern::FromString(secondary_pattern), |
| 417 content_type, "", setting); | 492 content_type, "", setting); |
| 418 } | 493 } |
| 419 | 494 |
| 420 void SiteSettingsHandler::HandleIsPatternValid( | 495 void SiteSettingsHandler::HandleIsPatternValid( |
| 421 const base::ListValue* args) { | 496 const base::ListValue* args) { |
| 422 CHECK_EQ(2U, args->GetSize()); | 497 CHECK_EQ(2U, args->GetSize()); |
| 423 const base::Value* callback_id; | 498 const base::Value* callback_id; |
| 424 CHECK(args->Get(0, &callback_id)); | 499 CHECK(args->Get(0, &callback_id)); |
| 425 std::string pattern_string; | 500 std::string pattern_string; |
| 426 CHECK(args->GetString(1, &pattern_string)); | 501 CHECK(args->GetString(1, &pattern_string)); |
| 427 | 502 |
| 428 ContentSettingsPattern pattern = | 503 ContentSettingsPattern pattern = |
| 429 ContentSettingsPattern::FromString(pattern_string); | 504 ContentSettingsPattern::FromString(pattern_string); |
| 430 ResolveJavascriptCallback( | 505 ResolveJavascriptCallback( |
| 431 *callback_id, base::FundamentalValue(pattern.IsValid())); | 506 *callback_id, base::FundamentalValue(pattern.IsValid())); |
| 432 } | 507 } |
| 433 | 508 |
| 509 void SiteSettingsHandler::HandleUpdateIncognitoStatus( |
| 510 const base::ListValue* args) { |
| 511 AllowJavascript(); |
| 512 SendIncognitoStatus(profile_, /*was_destroyed=*/ false); |
| 513 } |
| 514 |
| 515 void SiteSettingsHandler::SendIncognitoStatus( |
| 516 Profile* profile, bool was_destroyed) { |
| 517 if (!IsJavascriptAllowed()) |
| 518 return; |
| 519 |
| 520 // When an incognito profile is destroyed, it sends out the destruction |
| 521 // message before destroying, so HasOffTheRecordProfile for profile_ won't |
| 522 // return false until after the profile actually been destroyed. |
| 523 bool incognito_enabled = profile_->HasOffTheRecordProfile() && |
| 524 !(was_destroyed && profile == profile_->GetOffTheRecordProfile()); |
| 525 |
| 526 CallJavascriptFunction("cr.webUIListenerCallback", |
| 527 base::StringValue("onIncognitoStatusChanged"), |
| 528 base::FundamentalValue(incognito_enabled)); |
| 529 } |
| 530 |
| 434 } // namespace settings | 531 } // namespace settings |
| OLD | NEW |