[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 5 | #include "webkit/fileapi/local_file_system_test_helper.h" |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 6 | |
| 7 | #include "base/file_util.h" |
| 8 | #include "base/message_loop.h" |
| 9 | #include "base/message_loop_proxy.h" |
| 10 | #include "googleurl/src/gurl.h" |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 11 | #include "webkit/fileapi/external_mount_points.h" |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 12 | #include "webkit/fileapi/file_system_context.h" |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 13 | #include "webkit/fileapi/file_system_operation_context.h" |
[email protected] | dc57ec8 | 2012-08-07 03:50:10 | [diff] [blame] | 14 | #include "webkit/fileapi/file_system_task_runners.h" |
[email protected] | 199263e | 2012-12-14 07:14:20 | [diff] [blame] | 15 | #include "webkit/fileapi/file_system_url.h" |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 16 | #include "webkit/fileapi/file_system_usage_cache.h" |
| 17 | #include "webkit/fileapi/file_system_util.h" |
[email protected] | c3d5d25 | 2012-03-08 04:56:21 | [diff] [blame] | 18 | #include "webkit/fileapi/file_util_helper.h" |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 19 | #include "webkit/fileapi/local_file_system_operation.h" |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 20 | #include "webkit/fileapi/mock_file_system_options.h" |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 21 | #include "webkit/fileapi/sandbox_mount_point_provider.h" |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 22 | #include "webkit/fileapi/test_mount_point_provider.h" |
[email protected] | d024044 | 2011-08-17 09:23:00 | [diff] [blame] | 23 | #include "webkit/quota/mock_special_storage_policy.h" |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 24 | |
| 25 | namespace fileapi { |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 26 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 27 | LocalFileSystemTestOriginHelper::LocalFileSystemTestOriginHelper( |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 28 | const GURL& origin, FileSystemType type) |
| 29 | : origin_(origin), type_(type), file_util_(NULL) { |
| 30 | } |
| 31 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 32 | LocalFileSystemTestOriginHelper::LocalFileSystemTestOriginHelper() |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 33 | : origin_(GURL("http://foo.com")), |
| 34 | type_(kFileSystemTypeTemporary), |
| 35 | file_util_(NULL) { |
| 36 | } |
| 37 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 38 | LocalFileSystemTestOriginHelper::~LocalFileSystemTestOriginHelper() { |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 39 | } |
| 40 | |
[email protected] | 4f056a87 | 2013-01-23 04:24:36 | [diff] [blame] | 41 | void LocalFileSystemTestOriginHelper::SetUp(const FilePath& base_dir) { |
| 42 | SetUp(base_dir, false, NULL); |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 43 | } |
| 44 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 45 | void LocalFileSystemTestOriginHelper::SetUp( |
[email protected] | 4f056a87 | 2013-01-23 04:24:36 | [diff] [blame] | 46 | FileSystemContext* file_system_context) { |
[email protected] | 054702d | 2011-05-28 01:12:18 | [diff] [blame] | 47 | file_system_context_ = file_system_context; |
[email protected] | 4f056a87 | 2013-01-23 04:24:36 | [diff] [blame] | 48 | |
| 49 | SetUpFileUtil(); |
[email protected] | 054702d | 2011-05-28 01:12:18 | [diff] [blame] | 50 | |
| 51 | // Prepare the origin's root directory. |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 52 | file_system_context_->GetMountPointProvider(type_)-> |
[email protected] | 5b7e42e6 | 2013-01-24 22:01:51 | [diff] [blame] | 53 | GetFileSystemRootPathOnFileThread(CreateURL(FilePath()), |
| 54 | true /* create */); |
[email protected] | 054702d | 2011-05-28 01:12:18 | [diff] [blame] | 55 | |
| 56 | // Initialize the usage cache file. |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 57 | FilePath usage_cache_path = GetUsageCachePath(); |
| 58 | if (!usage_cache_path.empty()) |
| 59 | FileSystemUsageCache::UpdateUsage(usage_cache_path, 0); |
[email protected] | 054702d | 2011-05-28 01:12:18 | [diff] [blame] | 60 | } |
| 61 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 62 | void LocalFileSystemTestOriginHelper::SetUp( |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 63 | const FilePath& base_dir, |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 64 | bool unlimited_quota, |
[email protected] | 4f056a87 | 2013-01-23 04:24:36 | [diff] [blame] | 65 | quota::QuotaManagerProxy* quota_manager_proxy) { |
[email protected] | d024044 | 2011-08-17 09:23:00 | [diff] [blame] | 66 | scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
| 67 | new quota::MockSpecialStoragePolicy; |
| 68 | special_storage_policy->SetAllUnlimited(unlimited_quota); |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 69 | file_system_context_ = new FileSystemContext( |
[email protected] | dc57ec8 | 2012-08-07 03:50:10 | [diff] [blame] | 70 | FileSystemTaskRunners::CreateMockTaskRunners(), |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 71 | ExternalMountPoints::CreateRefCounted().get(), |
[email protected] | d024044 | 2011-08-17 09:23:00 | [diff] [blame] | 72 | special_storage_policy, |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 73 | quota_manager_proxy, |
| 74 | base_dir, |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 75 | CreateAllowFileAccessOptions()); |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 76 | |
[email protected] | 4f056a87 | 2013-01-23 04:24:36 | [diff] [blame] | 77 | SetUpFileUtil(); |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 78 | |
| 79 | // Prepare the origin's root directory. |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 80 | FileSystemMountPointProvider* mount_point_provider = |
| 81 | file_system_context_->GetMountPointProvider(type_); |
[email protected] | 5b7e42e6 | 2013-01-24 22:01:51 | [diff] [blame] | 82 | mount_point_provider->GetFileSystemRootPathOnFileThread(CreateURL(FilePath()), |
| 83 | true /* create */); |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 84 | |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 85 | // Initialize the usage cache file. |
| 86 | FilePath usage_cache_path = GetUsageCachePath(); |
| 87 | if (!usage_cache_path.empty()) |
| 88 | FileSystemUsageCache::UpdateUsage(usage_cache_path, 0); |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 91 | void LocalFileSystemTestOriginHelper::TearDown() { |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 92 | file_system_context_ = NULL; |
[email protected] | 4e1c7e5 | 2012-12-16 01:06:36 | [diff] [blame] | 93 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 96 | FilePath LocalFileSystemTestOriginHelper::GetOriginRootPath() const { |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 97 | return file_system_context_->GetMountPointProvider(type_)-> |
[email protected] | 5b7e42e6 | 2013-01-24 22:01:51 | [diff] [blame] | 98 | GetFileSystemRootPathOnFileThread(CreateURL(FilePath()), false); |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 99 | } |
| 100 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 101 | FilePath LocalFileSystemTestOriginHelper::GetLocalPath(const FilePath& path) { |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 102 | DCHECK(file_util_); |
| 103 | FilePath local_path; |
| 104 | scoped_ptr<FileSystemOperationContext> context(NewOperationContext()); |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 105 | file_util_->GetLocalFilePath(context.get(), CreateURL(path), &local_path); |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 106 | return local_path; |
| 107 | } |
| 108 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 109 | FilePath LocalFileSystemTestOriginHelper::GetLocalPathFromASCII( |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 110 | const std::string& path) { |
| 111 | return GetLocalPath(FilePath().AppendASCII(path)); |
| 112 | } |
| 113 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 114 | FilePath LocalFileSystemTestOriginHelper::GetUsageCachePath() const { |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 115 | if (type_ != kFileSystemTypeTemporary && |
| 116 | type_ != kFileSystemTypePersistent) |
| 117 | return FilePath(); |
[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 118 | return file_system_context_-> |
| 119 | sandbox_provider()->GetUsageCachePathForOriginAndType(origin_, type_); |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 122 | FileSystemURL LocalFileSystemTestOriginHelper::CreateURL(const FilePath& path) |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 123 | const { |
[email protected] | 5b7e42e6 | 2013-01-24 22:01:51 | [diff] [blame] | 124 | return file_system_context_->CreateCrackedFileSystemURL(origin_, type_, path); |
[email protected] | 45ea0fbbb | 2012-02-27 22:28:49 | [diff] [blame] | 125 | } |
| 126 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 127 | base::PlatformFileError LocalFileSystemTestOriginHelper::SameFileUtilCopy( |
[email protected] | 45ea0fbbb | 2012-02-27 22:28:49 | [diff] [blame] | 128 | FileSystemOperationContext* context, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 129 | const FileSystemURL& src, |
| 130 | const FileSystemURL& dest) const { |
[email protected] | c3d5d25 | 2012-03-08 04:56:21 | [diff] [blame] | 131 | return FileUtilHelper::Copy(context, file_util(), file_util(), src, dest); |
[email protected] | 45ea0fbbb | 2012-02-27 22:28:49 | [diff] [blame] | 132 | } |
| 133 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 134 | base::PlatformFileError LocalFileSystemTestOriginHelper::SameFileUtilMove( |
[email protected] | 45ea0fbbb | 2012-02-27 22:28:49 | [diff] [blame] | 135 | FileSystemOperationContext* context, |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 136 | const FileSystemURL& src, |
| 137 | const FileSystemURL& dest) const { |
[email protected] | c3d5d25 | 2012-03-08 04:56:21 | [diff] [blame] | 138 | return FileUtilHelper::Move(context, file_util(), file_util(), src, dest); |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 141 | int64 LocalFileSystemTestOriginHelper::GetCachedOriginUsage() const { |
[email protected] | 022d270 | 2012-05-14 16:04:26 | [diff] [blame] | 142 | return file_system_context_->GetQuotaUtil(type_)->GetOriginUsageOnFileThread( |
[email protected] | 2289452 | 2012-05-23 07:14:58 | [diff] [blame] | 143 | file_system_context_, origin_, type_); |
[email protected] | ffac1ac4 | 2011-07-25 15:03:15 | [diff] [blame] | 144 | } |
| 145 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 146 | int64 LocalFileSystemTestOriginHelper::ComputeCurrentOriginUsage() const { |
[email protected] | 0c5ebe3 | 2011-08-19 22:37:16 | [diff] [blame] | 147 | int64 size = file_util::ComputeDirectorySize(GetOriginRootPath()); |
| 148 | if (file_util::PathExists(GetUsageCachePath())) |
| 149 | size -= FileSystemUsageCache::kUsageFileSize; |
| 150 | return size; |
[email protected] | 8bf65df | 2011-05-27 07:26:15 | [diff] [blame] | 151 | } |
| 152 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 153 | int64 |
| 154 | LocalFileSystemTestOriginHelper::ComputeCurrentDirectoryDatabaseUsage() const { |
[email protected] | ecdfd6c5 | 2012-04-11 13:35:44 | [diff] [blame] | 155 | return file_util::ComputeDirectorySize( |
| 156 | GetOriginRootPath().AppendASCII("Paths")); |
| 157 | } |
| 158 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 159 | LocalFileSystemOperation* LocalFileSystemTestOriginHelper::NewOperation() { |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 160 | DCHECK(file_system_context_.get()); |
| 161 | DCHECK(file_util_); |
[email protected] | fc2e14fa | 2012-10-18 14:06:11 | [diff] [blame] | 162 | scoped_ptr<FileSystemOperationContext> operation_context( |
| 163 | NewOperationContext()); |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 164 | LocalFileSystemOperation* operation = static_cast<LocalFileSystemOperation*>( |
[email protected] | baa6e11 | 2012-09-10 06:24:46 | [diff] [blame] | 165 | file_system_context_->CreateFileSystemOperation( |
| 166 | CreateURL(FilePath()), NULL)); |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 167 | return operation; |
| 168 | } |
| 169 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 170 | FileSystemOperationContext* |
| 171 | LocalFileSystemTestOriginHelper::NewOperationContext() { |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 172 | DCHECK(file_system_context_.get()); |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 173 | FileSystemOperationContext* context = |
[email protected] | 08f8feb | 2012-02-26 11:53:50 | [diff] [blame] | 174 | new FileSystemOperationContext(file_system_context_.get()); |
[email protected] | caf6670 | 2012-09-07 07:02:20 | [diff] [blame] | 175 | context->set_update_observers( |
| 176 | *file_system_context_->GetUpdateObservers(type_)); |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 177 | return context; |
| 178 | } |
| 179 | |
[email protected] | 4f056a87 | 2013-01-23 04:24:36 | [diff] [blame] | 180 | void LocalFileSystemTestOriginHelper::SetUpFileUtil() { |
| 181 | DCHECK(file_system_context_); |
| 182 | if (type_ == kFileSystemTypeTest) { |
| 183 | file_system_context_->RegisterMountPointProvider( |
| 184 | type_, |
| 185 | new TestMountPointProvider( |
| 186 | file_system_context_->task_runners()->file_task_runner(), |
| 187 | file_system_context_->partition_path())); |
| 188 | } |
[email protected] | 25b69799 | 2013-01-29 07:10:05 | [diff] [blame] | 189 | file_util_ = file_system_context_->GetFileUtil(type_); |
[email protected] | 4f056a87 | 2013-01-23 04:24:36 | [diff] [blame] | 190 | DCHECK(file_util_); |
| 191 | } |
| 192 | |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 193 | } // namespace fileapi |