1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h" 6 7 #include "base/bind.h" 8 #include "chrome/browser/browsing_data/browsing_data_helper.h" 9 #include "chrome/browser/profiles/profile.h" 10 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/local_storage_usage_info.h" 12 #include "content/public/browser/storage_partition.h" 13 14 using content::BrowserContext; 15 using content::BrowserThread; 16 using content::DOMStorageContext; 17 18 BrowsingDataLocalStorageHelper::LocalStorageInfo::LocalStorageInfo( 19 const GURL& origin_url, int64 size, base::Time last_modified) 20 : origin_url(origin_url), size(size), last_modified(last_modified) {} 21 22 BrowsingDataLocalStorageHelper::LocalStorageInfo::~LocalStorageInfo() {} 23 24 BrowsingDataLocalStorageHelper::BrowsingDataLocalStorageHelper( 25 Profile* profile) 26 : dom_storage_context_( 27 BrowserContext::GetDefaultStoragePartition(profile)-> 28 GetDOMStorageContext()), 29 is_fetching_(false) { 30 DCHECK(dom_storage_context_); 31 } 32 33 BrowsingDataLocalStorageHelper::~BrowsingDataLocalStorageHelper() { 34 } 35 36 void BrowsingDataLocalStorageHelper::StartFetching( 37 const base::Callback<void(const std::list<LocalStorageInfo>&)>& callback) { 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 39 DCHECK(!is_fetching_); 40 DCHECK_EQ(false, callback.is_null()); 41 42 is_fetching_ = true; 43 completion_callback_ = callback; 44 dom_storage_context_->GetLocalStorageUsage( 45 base::Bind( 46 &BrowsingDataLocalStorageHelper::GetUsageInfoCallback, this)); 47 } 48 49 void BrowsingDataLocalStorageHelper::DeleteOrigin(const GURL& origin) { 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 51 dom_storage_context_->DeleteLocalStorage(origin); 52 } 53 54 void BrowsingDataLocalStorageHelper::GetUsageInfoCallback( 55 const std::vector<content::LocalStorageUsageInfo>& infos) { 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 57 58 for (size_t i = 0; i < infos.size(); ++i) { 59 // Non-websafe state is not considered browsing data. 60 const content::LocalStorageUsageInfo& info = infos[i]; 61 if (BrowsingDataHelper::HasWebScheme(info.origin)) { 62 local_storage_info_.push_back( 63 LocalStorageInfo(info.origin, info.data_size, info.last_modified)); 64 } 65 } 66 67 BrowserThread::PostTask( 68 BrowserThread::UI, FROM_HERE, 69 base::Bind(&BrowsingDataLocalStorageHelper::CallCompletionCallback, 70 this)); 71 } 72 73 void BrowsingDataLocalStorageHelper::CallCompletionCallback() { 74 DCHECK(is_fetching_); 75 completion_callback_.Run(local_storage_info_); 76 completion_callback_.Reset(); 77 is_fetching_ = false; 78 } 79 80 //--------------------------------------------------------- 81 82 CannedBrowsingDataLocalStorageHelper::CannedBrowsingDataLocalStorageHelper( 83 Profile* profile) 84 : BrowsingDataLocalStorageHelper(profile), 85 profile_(profile) { 86 } 87 88 CannedBrowsingDataLocalStorageHelper* 89 CannedBrowsingDataLocalStorageHelper::Clone() { 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 91 CannedBrowsingDataLocalStorageHelper* clone = 92 new CannedBrowsingDataLocalStorageHelper(profile_); 93 94 clone->pending_local_storage_info_ = pending_local_storage_info_; 95 return clone; 96 } 97 98 void CannedBrowsingDataLocalStorageHelper::AddLocalStorage( 99 const GURL& origin) { 100 if (BrowsingDataHelper::HasWebScheme(origin)) 101 pending_local_storage_info_.insert(origin); 102 } 103 104 void CannedBrowsingDataLocalStorageHelper::Reset() { 105 pending_local_storage_info_.clear(); 106 } 107 108 bool CannedBrowsingDataLocalStorageHelper::empty() const { 109 return pending_local_storage_info_.empty(); 110 } 111 112 size_t CannedBrowsingDataLocalStorageHelper::GetLocalStorageCount() const { 113 return pending_local_storage_info_.size(); 114 } 115 116 const std::set<GURL>& 117 CannedBrowsingDataLocalStorageHelper::GetLocalStorageInfo() const { 118 return pending_local_storage_info_; 119 } 120 121 void CannedBrowsingDataLocalStorageHelper::StartFetching( 122 const base::Callback<void(const std::list<LocalStorageInfo>&)>& callback) { 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 124 DCHECK(!callback.is_null()); 125 126 std::list<LocalStorageInfo> result; 127 for (std::set<GURL>::iterator iter = pending_local_storage_info_.begin(); 128 iter != pending_local_storage_info_.end(); ++iter) { 129 result.push_back( 130 LocalStorageInfo(*iter, 0, base::Time())); 131 } 132 133 BrowserThread::PostTask( 134 BrowserThread::UI, FROM_HERE, base::Bind(callback, result)); 135 } 136 137 void CannedBrowsingDataLocalStorageHelper::DeleteOrigin(const GURL& origin) { 138 pending_local_storage_info_.erase(origin); 139 BrowsingDataLocalStorageHelper::DeleteOrigin(origin); 140 } 141 142 CannedBrowsingDataLocalStorageHelper::~CannedBrowsingDataLocalStorageHelper() {} 143