| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 10 #include "components/content_settings/core/common/content_settings_types.h" | 11 #include "components/content_settings/core/common/content_settings_types.h" |
| 11 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/storage_partition.h" | 13 #include "content/public/browser/storage_partition.h" |
| 13 #include "content/public/browser/web_ui.h" | 14 #include "content/public/browser/web_ui.h" |
| 14 #include "ui/base/text/bytes_formatting.h" | 15 #include "ui/base/text/bytes_formatting.h" |
| 15 | 16 |
| 16 namespace settings { | 17 namespace settings { |
| 17 | 18 |
| 18 SiteSettingsHandler::SiteSettingsHandler(Profile* profile) | 19 SiteSettingsHandler::SiteSettingsHandler(Profile* profile) |
| 19 : profile_(profile) { | 20 : profile_(profile) { |
| 21 storage::QuotaManager* quota_manager = |
| 22 content::BrowserContext::GetDefaultStoragePartition( |
| 23 profile_)->GetQuotaManager(); |
| 24 storage_info_fetcher_ = new StorageInfoFetcher(quota_manager); |
| 25 storage_info_fetcher_->AddObserver(this); |
| 20 } | 26 } |
| 21 | 27 |
| 22 SiteSettingsHandler::~SiteSettingsHandler() { | 28 SiteSettingsHandler::~SiteSettingsHandler() { |
| 23 if (storage_info_fetcher_.get()) | 29 storage_info_fetcher_->RemoveObserver(this); |
| 24 storage_info_fetcher_->RemoveObserver(this); | |
| 25 } | 30 } |
| 26 | 31 |
| 27 void SiteSettingsHandler::RegisterMessages() { | 32 void SiteSettingsHandler::RegisterMessages() { |
| 28 web_ui()->RegisterMessageCallback( | 33 web_ui()->RegisterMessageCallback( |
| 29 "fetchUsageTotal", | 34 "fetchUsageTotal", |
| 30 base::Bind(&SiteSettingsHandler::HandleFetchUsageTotal, | 35 base::Bind(&SiteSettingsHandler::HandleFetchUsageTotal, |
| 31 base::Unretained(this))); | 36 base::Unretained(this))); |
| 37 web_ui()->RegisterMessageCallback( |
| 38 "clearUsage", |
| 39 base::Bind(&SiteSettingsHandler::HandleClearUsage, |
| 40 base::Unretained(this))); |
| 32 } | 41 } |
| 33 | 42 |
| 34 void SiteSettingsHandler::OnGetUsageInfo( | 43 void SiteSettingsHandler::OnGetUsageInfo( |
| 35 const storage::UsageInfoEntries& entries) { | 44 const storage::UsageInfoEntries& entries) { |
| 36 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 37 | 46 |
| 38 for (const auto& entry : entries) { | 47 for (const auto& entry : entries) { |
| 39 if (entry.usage <= 0) continue; | 48 if (entry.usage <= 0) continue; |
| 40 if (entry.host == usage_host_) { | 49 if (entry.host == usage_host_) { |
| 41 web_ui()->CallJavascriptFunction( | 50 web_ui()->CallJavascriptFunction( |
| 42 "settings.WebsiteUsagePrivateApi.returnUsageTotal", | 51 "settings.WebsiteUsagePrivateApi.returnUsageTotal", |
| 43 base::StringValue(entry.host), | 52 base::StringValue(entry.host), |
| 44 base::StringValue(ui::FormatBytes(entry.usage))); | 53 base::StringValue(ui::FormatBytes(entry.usage)), |
| 45 return; | 54 base::FundamentalValue(entry.type)); |
| 46 } | 55 } |
| 47 } | 56 } |
| 48 } | 57 } |
| 49 | 58 |
| 59 void SiteSettingsHandler::OnUsageInfoCleared(storage::QuotaStatusCode code) { |
| 60 if (code == storage::kQuotaStatusOk) { |
| 61 web_ui()->CallJavascriptFunction( |
| 62 "settings.WebsiteUsagePrivateApi.onUsageCleared", |
| 63 base::StringValue(clearing_origin_)); |
| 64 } |
| 65 } |
| 66 |
| 50 void SiteSettingsHandler::HandleFetchUsageTotal( | 67 void SiteSettingsHandler::HandleFetchUsageTotal( |
| 51 const base::ListValue* args) { | 68 const base::ListValue* args) { |
| 52 CHECK_EQ(1U, args->GetSize()); | 69 CHECK_EQ(1U, args->GetSize()); |
| 53 std::string host; | 70 std::string host; |
| 54 CHECK(args->GetString(0, &host)); | 71 CHECK(args->GetString(0, &host)); |
| 55 usage_host_ = host; | 72 usage_host_ = host; |
| 56 storage_info_fetcher_ = new StorageInfoFetcher( | 73 storage_info_fetcher_->FetchStorageInfo(); |
| 57 content::BrowserContext::GetDefaultStoragePartition( | 74 } |
| 58 profile_)->GetQuotaManager()); | 75 |
| 59 storage_info_fetcher_->AddObserver(this); | 76 void SiteSettingsHandler::HandleClearUsage( |
| 60 storage_info_fetcher_->Run(); | 77 const base::ListValue* args) { |
| 78 CHECK_EQ(2U, args->GetSize()); |
| 79 std::string origin; |
| 80 CHECK(args->GetString(0, &origin)); |
| 81 int type; |
| 82 CHECK(args->GetInteger(1, &type)); |
| 83 |
| 84 GURL url(origin); |
| 85 if (url.is_valid()) { |
| 86 clearing_origin_ = origin; |
| 87 |
| 88 // Start by clearing the storage data asynchronously. |
| 89 storage_info_fetcher_->ClearStorage( |
| 90 url.host(), static_cast<storage::StorageType>(type)); |
| 91 |
| 92 // Also clear the *local* storage data. |
| 93 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper = |
| 94 new BrowsingDataLocalStorageHelper(profile_); |
| 95 local_storage_helper->DeleteOrigin(url); |
| 96 } |
| 61 } | 97 } |
| 62 | 98 |
| 63 } // namespace settings | 99 } // namespace settings |
| OLD | NEW |