| OLD | NEW |
| (Empty) |
| 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/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 "googleurl/src/gurl.h" | |
| 20 #include "net/base/net_util.h" | |
| 21 #include "webkit/fileapi/file_system_context.h" | |
| 22 #include "webkit/fileapi/file_system_quota_util.h" | |
| 23 #include "webkit/fileapi/file_system_task_runners.h" | |
| 24 #include "webkit/fileapi/file_system_usage_cache.h" | |
| 25 #include "webkit/fileapi/file_system_util.h" | |
| 26 #include "webkit/fileapi/sandbox_mount_point_provider.h" | |
| 27 | |
| 28 using quota::StorageType; | |
| 29 | |
| 30 namespace fileapi { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 void GetOriginsForTypeOnFileThread( | |
| 35 FileSystemContext* context, | |
| 36 StorageType storage_type, | |
| 37 std::set<GURL>* origins_ptr) { | |
| 38 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); | |
| 39 DCHECK(type != kFileSystemTypeUnknown); | |
| 40 | |
| 41 FileSystemQuotaUtil* quota_util = context->GetQuotaUtil(type); | |
| 42 if (!quota_util) | |
| 43 return; | |
| 44 quota_util->GetOriginsForTypeOnFileThread(type, origins_ptr); | |
| 45 } | |
| 46 | |
| 47 void GetOriginsForHostOnFileThread( | |
| 48 FileSystemContext* context, | |
| 49 StorageType storage_type, | |
| 50 const std::string& host, | |
| 51 std::set<GURL>* origins_ptr) { | |
| 52 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); | |
| 53 DCHECK(type != kFileSystemTypeUnknown); | |
| 54 | |
| 55 FileSystemQuotaUtil* quota_util = context->GetQuotaUtil(type); | |
| 56 if (!quota_util) | |
| 57 return; | |
| 58 quota_util->GetOriginsForHostOnFileThread(type, host, origins_ptr); | |
| 59 } | |
| 60 | |
| 61 void DidGetOrigins( | |
| 62 const quota::QuotaClient::GetOriginsCallback& callback, | |
| 63 std::set<GURL>* origins_ptr, | |
| 64 StorageType storage_type) { | |
| 65 callback.Run(*origins_ptr, storage_type); | |
| 66 } | |
| 67 | |
| 68 quota::QuotaStatusCode DeleteOriginOnFileThread( | |
| 69 FileSystemContext* context, | |
| 70 const GURL& origin, | |
| 71 FileSystemType type) { | |
| 72 base::PlatformFileError result = | |
| 73 context->sandbox_provider()->DeleteOriginDataOnFileThread( | |
| 74 context, context->quota_manager_proxy(), origin, type); | |
| 75 if (result == base::PLATFORM_FILE_OK) | |
| 76 return quota::kQuotaStatusOk; | |
| 77 return quota::kQuotaErrorInvalidModification; | |
| 78 } | |
| 79 | |
| 80 } // namespace | |
| 81 | |
| 82 FileSystemQuotaClient::FileSystemQuotaClient( | |
| 83 FileSystemContext* file_system_context, | |
| 84 bool is_incognito) | |
| 85 : file_system_context_(file_system_context), | |
| 86 is_incognito_(is_incognito) { | |
| 87 } | |
| 88 | |
| 89 FileSystemQuotaClient::~FileSystemQuotaClient() {} | |
| 90 | |
| 91 quota::QuotaClient::ID FileSystemQuotaClient::id() const { | |
| 92 return quota::QuotaClient::kFileSystem; | |
| 93 } | |
| 94 | |
| 95 void FileSystemQuotaClient::OnQuotaManagerDestroyed() { | |
| 96 delete this; | |
| 97 } | |
| 98 | |
| 99 void FileSystemQuotaClient::GetOriginUsage( | |
| 100 const GURL& origin_url, | |
| 101 StorageType storage_type, | |
| 102 const GetUsageCallback& callback) { | |
| 103 DCHECK(!callback.is_null()); | |
| 104 | |
| 105 if (is_incognito_) { | |
| 106 // We don't support FileSystem in incognito mode yet. | |
| 107 callback.Run(0); | |
| 108 return; | |
| 109 } | |
| 110 | |
| 111 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); | |
| 112 DCHECK(type != kFileSystemTypeUnknown); | |
| 113 | |
| 114 FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type); | |
| 115 if (!quota_util) { | |
| 116 callback.Run(0); | |
| 117 return; | |
| 118 } | |
| 119 | |
| 120 base::PostTaskAndReplyWithResult( | |
| 121 file_task_runner(), | |
| 122 FROM_HERE, | |
| 123 // It is safe to pass Unretained(quota_util) since context owns it. | |
| 124 base::Bind(&FileSystemQuotaUtil::GetOriginUsageOnFileThread, | |
| 125 base::Unretained(quota_util), | |
| 126 file_system_context_, | |
| 127 origin_url, | |
| 128 type), | |
| 129 callback); | |
| 130 } | |
| 131 | |
| 132 void FileSystemQuotaClient::GetOriginsForType( | |
| 133 StorageType storage_type, | |
| 134 const GetOriginsCallback& callback) { | |
| 135 DCHECK(!callback.is_null()); | |
| 136 | |
| 137 if (is_incognito_) { | |
| 138 // We don't support FileSystem in incognito mode yet. | |
| 139 std::set<GURL> origins; | |
| 140 callback.Run(origins, storage_type); | |
| 141 return; | |
| 142 } | |
| 143 | |
| 144 std::set<GURL>* origins_ptr = new std::set<GURL>(); | |
| 145 file_task_runner()->PostTaskAndReply( | |
| 146 FROM_HERE, | |
| 147 base::Bind(&GetOriginsForTypeOnFileThread, | |
| 148 file_system_context_, | |
| 149 storage_type, | |
| 150 base::Unretained(origins_ptr)), | |
| 151 base::Bind(&DidGetOrigins, | |
| 152 callback, | |
| 153 base::Owned(origins_ptr), | |
| 154 storage_type)); | |
| 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, storage_type); | |
| 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 storage_type)); | |
| 182 } | |
| 183 | |
| 184 void FileSystemQuotaClient::DeleteOriginData( | |
| 185 const GURL& origin, | |
| 186 StorageType type, | |
| 187 const DeletionCallback& callback) { | |
| 188 FileSystemType fs_type = QuotaStorageTypeToFileSystemType(type); | |
| 189 DCHECK(fs_type != kFileSystemTypeUnknown); | |
| 190 | |
| 191 base::PostTaskAndReplyWithResult( | |
| 192 file_task_runner(), | |
| 193 FROM_HERE, | |
| 194 base::Bind(&DeleteOriginOnFileThread, | |
| 195 file_system_context_, | |
| 196 origin, | |
| 197 fs_type), | |
| 198 callback); | |
| 199 } | |
| 200 | |
| 201 base::SequencedTaskRunner* FileSystemQuotaClient::file_task_runner() const { | |
| 202 return file_system_context_->task_runners()->file_task_runner(); | |
| 203 } | |
| 204 | |
| 205 } // namespace fileapi | |
| OLD | NEW |