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 "webkit/browser/fileapi/file_system_quota_client.h" 6 7 #include <algorithm> 8 #include <set> 9 10 #include "base/bind.h" 11 #include "base/file_util.h" 12 #include "base/files/file_path.h" 13 #include "base/location.h" 14 #include "base/logging.h" 15 #include "base/memory/scoped_ptr.h" 16 #include "base/sequenced_task_runner.h" 17 #include "base/single_thread_task_runner.h" 18 #include "base/task_runner_util.h" 19 #include "net/base/net_util.h" 20 #include "url/gurl.h" 21 #include "webkit/browser/fileapi/file_system_context.h" 22 #include "webkit/browser/fileapi/file_system_quota_util.h" 23 #include "webkit/browser/fileapi/file_system_usage_cache.h" 24 #include "webkit/browser/fileapi/sandbox_file_system_backend.h" 25 #include "webkit/common/fileapi/file_system_util.h" 26 27 using quota::StorageType; 28 29 namespace fileapi { 30 31 namespace { 32 33 void GetOriginsForTypeOnFileThread( 34 FileSystemContext* context, 35 StorageType storage_type, 36 std::set<GURL>* origins_ptr) { 37 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); 38 DCHECK(type != kFileSystemTypeUnknown); 39 40 FileSystemQuotaUtil* quota_util = context->GetQuotaUtil(type); 41 if (!quota_util) 42 return; 43 quota_util->GetOriginsForTypeOnFileThread(type, origins_ptr); 44 } 45 46 void GetOriginsForHostOnFileThread( 47 FileSystemContext* context, 48 StorageType storage_type, 49 const std::string& host, 50 std::set<GURL>* origins_ptr) { 51 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); 52 DCHECK(type != kFileSystemTypeUnknown); 53 54 FileSystemQuotaUtil* quota_util = context->GetQuotaUtil(type); 55 if (!quota_util) 56 return; 57 quota_util->GetOriginsForHostOnFileThread(type, host, origins_ptr); 58 } 59 60 void DidGetOrigins( 61 const quota::QuotaClient::GetOriginsCallback& callback, 62 std::set<GURL>* origins_ptr) { 63 callback.Run(*origins_ptr); 64 } 65 66 quota::QuotaStatusCode DeleteOriginOnFileThread( 67 FileSystemContext* context, 68 const GURL& origin, 69 FileSystemType type) { 70 FileSystemBackend* provider = context->GetFileSystemBackend(type); 71 if (!provider || !provider->GetQuotaUtil()) 72 return quota::kQuotaErrorNotSupported; 73 base::PlatformFileError result = 74 provider->GetQuotaUtil()->DeleteOriginDataOnFileThread( 75 context, context->quota_manager_proxy(), origin, type); 76 if (result == base::PLATFORM_FILE_OK) 77 return quota::kQuotaStatusOk; 78 return quota::kQuotaErrorInvalidModification; 79 } 80 81 } // namespace 82 83 FileSystemQuotaClient::FileSystemQuotaClient( 84 FileSystemContext* file_system_context, 85 bool is_incognito) 86 : file_system_context_(file_system_context), 87 is_incognito_(is_incognito) { 88 } 89 90 FileSystemQuotaClient::~FileSystemQuotaClient() {} 91 92 quota::QuotaClient::ID FileSystemQuotaClient::id() const { 93 return quota::QuotaClient::kFileSystem; 94 } 95 96 void FileSystemQuotaClient::OnQuotaManagerDestroyed() { 97 delete this; 98 } 99 100 void FileSystemQuotaClient::GetOriginUsage( 101 const GURL& origin_url, 102 StorageType storage_type, 103 const GetUsageCallback& callback) { 104 DCHECK(!callback.is_null()); 105 106 if (is_incognito_) { 107 // We don't support FileSystem in incognito mode yet. 108 callback.Run(0); 109 return; 110 } 111 112 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); 113 DCHECK(type != kFileSystemTypeUnknown); 114 115 FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type); 116 if (!quota_util) { 117 callback.Run(0); 118 return; 119 } 120 121 base::PostTaskAndReplyWithResult( 122 file_task_runner(), 123 FROM_HERE, 124 // It is safe to pass Unretained(quota_util) since context owns it. 125 base::Bind(&FileSystemQuotaUtil::GetOriginUsageOnFileThread, 126 base::Unretained(quota_util), 127 file_system_context_, 128 origin_url, 129 type), 130 callback); 131 } 132 133 void FileSystemQuotaClient::GetOriginsForType( 134 StorageType storage_type, 135 const GetOriginsCallback& callback) { 136 DCHECK(!callback.is_null()); 137 138 if (is_incognito_) { 139 // We don't support FileSystem in incognito mode yet. 140 std::set<GURL> origins; 141 callback.Run(origins); 142 return; 143 } 144 145 std::set<GURL>* origins_ptr = new std::set<GURL>(); 146 file_task_runner()->PostTaskAndReply( 147 FROM_HERE, 148 base::Bind(&GetOriginsForTypeOnFileThread, 149 file_system_context_, 150 storage_type, 151 base::Unretained(origins_ptr)), 152 base::Bind(&DidGetOrigins, 153 callback, 154 base::Owned(origins_ptr))); 155 } 156 157 void FileSystemQuotaClient::GetOriginsForHost( 158 StorageType storage_type, 159 const std::string& host, 160 const GetOriginsCallback& callback) { 161 DCHECK(!callback.is_null()); 162 163 if (is_incognito_) { 164 // We don't support FileSystem in incognito mode yet. 165 std::set<GURL> origins; 166 callback.Run(origins); 167 return; 168 } 169 170 std::set<GURL>* origins_ptr = new std::set<GURL>(); 171 file_task_runner()->PostTaskAndReply( 172 FROM_HERE, 173 base::Bind(&GetOriginsForHostOnFileThread, 174 file_system_context_, 175 storage_type, 176 host, 177 base::Unretained(origins_ptr)), 178 base::Bind(&DidGetOrigins, 179 callback, 180 base::Owned(origins_ptr))); 181 } 182 183 void FileSystemQuotaClient::DeleteOriginData( 184 const GURL& origin, 185 StorageType type, 186 const DeletionCallback& callback) { 187 FileSystemType fs_type = QuotaStorageTypeToFileSystemType(type); 188 DCHECK(fs_type != kFileSystemTypeUnknown); 189 190 base::PostTaskAndReplyWithResult( 191 file_task_runner(), 192 FROM_HERE, 193 base::Bind(&DeleteOriginOnFileThread, 194 file_system_context_, 195 origin, 196 fs_type), 197 callback); 198 } 199 200 bool FileSystemQuotaClient::DoesSupport(quota::StorageType storage_type) const { 201 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); 202 DCHECK(type != kFileSystemTypeUnknown); 203 return file_system_context_->IsSandboxFileSystem(type); 204 } 205 206 base::SequencedTaskRunner* FileSystemQuotaClient::file_task_runner() const { 207 return file_system_context_->default_file_task_runner(); 208 } 209 210 } // namespace fileapi 211