15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/browser/fileapi/file_system_quota_client.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm> 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/file_util.h" 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/location.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/sequenced_task_runner.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/single_thread_task_runner.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/task_runner_util.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_util.h" 20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h" 2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/browser/fileapi/file_system_context.h" 2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/browser/fileapi/file_system_quota_util.h" 2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/browser/fileapi/file_system_usage_cache.h" 247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "webkit/browser/fileapi/sandbox_file_system_backend.h" 2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/common/fileapi/file_system_util.h" 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using quota::StorageType; 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace fileapi { 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GetOriginsForTypeOnFileThread( 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FileSystemContext* context, 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) StorageType storage_type, 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::set<GURL>* origins_ptr) { 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(type != kFileSystemTypeUnknown); 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FileSystemQuotaUtil* quota_util = context->GetQuotaUtil(type); 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!quota_util) 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) quota_util->GetOriginsForTypeOnFileThread(type, origins_ptr); 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GetOriginsForHostOnFileThread( 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FileSystemContext* context, 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) StorageType storage_type, 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& host, 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::set<GURL>* origins_ptr) { 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(type != kFileSystemTypeUnknown); 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FileSystemQuotaUtil* quota_util = context->GetQuotaUtil(type); 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!quota_util) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) quota_util->GetOriginsForHostOnFileThread(type, host, origins_ptr); 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DidGetOrigins( 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const quota::QuotaClient::GetOriginsCallback& callback, 6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::set<GURL>* origins_ptr) { 6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) callback.Run(*origins_ptr); 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)quota::QuotaStatusCode DeleteOriginOnFileThread( 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FileSystemContext* context, 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const GURL& origin, 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FileSystemType type) { 707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch FileSystemBackend* provider = context->GetFileSystemBackend(type); 71eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (!provider || !provider->GetQuotaUtil()) 72eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return quota::kQuotaErrorNotSupported; 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::PlatformFileError result = 74eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch provider->GetQuotaUtil()->DeleteOriginDataOnFileThread( 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) context, context->quota_manager_proxy(), origin, type); 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (result == base::PLATFORM_FILE_OK) 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return quota::kQuotaStatusOk; 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return quota::kQuotaErrorInvalidModification; 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)FileSystemQuotaClient::FileSystemQuotaClient( 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FileSystemContext* file_system_context, 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool is_incognito) 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : file_system_context_(file_system_context), 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) is_incognito_(is_incognito) { 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)FileSystemQuotaClient::~FileSystemQuotaClient() {} 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)quota::QuotaClient::ID FileSystemQuotaClient::id() const { 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return quota::QuotaClient::kFileSystem; 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FileSystemQuotaClient::OnQuotaManagerDestroyed() { 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) delete this; 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FileSystemQuotaClient::GetOriginUsage( 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const GURL& origin_url, 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) StorageType storage_type, 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const GetUsageCallback& callback) { 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(!callback.is_null()); 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (is_incognito_) { 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We don't support FileSystem in incognito mode yet. 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) callback.Run(0); 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(type != kFileSystemTypeUnknown); 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type); 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!quota_util) { 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) callback.Run(0); 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::PostTaskAndReplyWithResult( 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) file_task_runner(), 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FROM_HERE, 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // It is safe to pass Unretained(quota_util) since context owns it. 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Bind(&FileSystemQuotaUtil::GetOriginUsageOnFileThread, 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Unretained(quota_util), 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) file_system_context_, 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) origin_url, 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) type), 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) callback); 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FileSystemQuotaClient::GetOriginsForType( 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) StorageType storage_type, 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const GetOriginsCallback& callback) { 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(!callback.is_null()); 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (is_incognito_) { 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We don't support FileSystem in incognito mode yet. 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::set<GURL> origins; 14190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) callback.Run(origins); 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::set<GURL>* origins_ptr = new std::set<GURL>(); 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) file_task_runner()->PostTaskAndReply( 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FROM_HERE, 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Bind(&GetOriginsForTypeOnFileThread, 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) file_system_context_, 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) storage_type, 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Unretained(origins_ptr)), 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Bind(&DidGetOrigins, 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) callback, 15490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::Owned(origins_ptr))); 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FileSystemQuotaClient::GetOriginsForHost( 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) StorageType storage_type, 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& host, 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const GetOriginsCallback& callback) { 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(!callback.is_null()); 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (is_incognito_) { 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We don't support FileSystem in incognito mode yet. 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::set<GURL> origins; 16690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) callback.Run(origins); 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return; 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::set<GURL>* origins_ptr = new std::set<GURL>(); 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) file_task_runner()->PostTaskAndReply( 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FROM_HERE, 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Bind(&GetOriginsForHostOnFileThread, 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) file_system_context_, 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) storage_type, 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) host, 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Unretained(origins_ptr)), 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Bind(&DidGetOrigins, 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) callback, 18090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::Owned(origins_ptr))); 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void FileSystemQuotaClient::DeleteOriginData( 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const GURL& origin, 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) StorageType type, 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const DeletionCallback& callback) { 1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FileSystemType fs_type = QuotaStorageTypeToFileSystemType(type); 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(fs_type != kFileSystemTypeUnknown); 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::PostTaskAndReplyWithResult( 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) file_task_runner(), 1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FROM_HERE, 1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Bind(&DeleteOriginOnFileThread, 1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) file_system_context_, 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) origin, 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) fs_type), 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) callback); 1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::SequencedTaskRunner* FileSystemQuotaClient::file_task_runner() const { 201a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) return file_system_context_->default_file_task_runner(); 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace fileapi 205