blob: e3dbe612c4fe9882ef62ffbf5233e51c6eec2e27 [file] [log] [blame]
[email protected]e7e46732012-01-05 11:45:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]77316bf2011-05-19 08:16:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]02a60542012-07-24 20:05:335#include "webkit/fileapi/local_file_system_test_helper.h"
[email protected]77316bf2011-05-19 08:16:446
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]6ef0c3912013-01-25 22:46:3411#include "webkit/fileapi/external_mount_points.h"
[email protected]77316bf2011-05-19 08:16:4412#include "webkit/fileapi/file_system_context.h"
[email protected]77316bf2011-05-19 08:16:4413#include "webkit/fileapi/file_system_operation_context.h"
[email protected]dc57ec82012-08-07 03:50:1014#include "webkit/fileapi/file_system_task_runners.h"
[email protected]199263e2012-12-14 07:14:2015#include "webkit/fileapi/file_system_url.h"
[email protected]77316bf2011-05-19 08:16:4416#include "webkit/fileapi/file_system_usage_cache.h"
17#include "webkit/fileapi/file_system_util.h"
[email protected]c3d5d252012-03-08 04:56:2118#include "webkit/fileapi/file_util_helper.h"
[email protected]02a60542012-07-24 20:05:3319#include "webkit/fileapi/local_file_system_operation.h"
[email protected]e7e46732012-01-05 11:45:5520#include "webkit/fileapi/mock_file_system_options.h"
[email protected]77316bf2011-05-19 08:16:4421#include "webkit/fileapi/sandbox_mount_point_provider.h"
[email protected]022d2702012-05-14 16:04:2622#include "webkit/fileapi/test_mount_point_provider.h"
[email protected]d0240442011-08-17 09:23:0023#include "webkit/quota/mock_special_storage_policy.h"
[email protected]77316bf2011-05-19 08:16:4424
25namespace fileapi {
[email protected]77316bf2011-05-19 08:16:4426
[email protected]02a60542012-07-24 20:05:3327LocalFileSystemTestOriginHelper::LocalFileSystemTestOriginHelper(
[email protected]77316bf2011-05-19 08:16:4428 const GURL& origin, FileSystemType type)
29 : origin_(origin), type_(type), file_util_(NULL) {
30}
31
[email protected]02a60542012-07-24 20:05:3332LocalFileSystemTestOriginHelper::LocalFileSystemTestOriginHelper()
[email protected]77316bf2011-05-19 08:16:4433 : origin_(GURL("http://foo.com")),
34 type_(kFileSystemTypeTemporary),
35 file_util_(NULL) {
36}
37
[email protected]02a60542012-07-24 20:05:3338LocalFileSystemTestOriginHelper::~LocalFileSystemTestOriginHelper() {
[email protected]77316bf2011-05-19 08:16:4439}
40
[email protected]4f056a872013-01-23 04:24:3641void LocalFileSystemTestOriginHelper::SetUp(const FilePath& base_dir) {
42 SetUp(base_dir, false, NULL);
[email protected]77316bf2011-05-19 08:16:4443}
44
[email protected]02a60542012-07-24 20:05:3345void LocalFileSystemTestOriginHelper::SetUp(
[email protected]4f056a872013-01-23 04:24:3646 FileSystemContext* file_system_context) {
[email protected]054702d2011-05-28 01:12:1847 file_system_context_ = file_system_context;
[email protected]4f056a872013-01-23 04:24:3648
49 SetUpFileUtil();
[email protected]054702d2011-05-28 01:12:1850
51 // Prepare the origin's root directory.
[email protected]e7e46732012-01-05 11:45:5552 file_system_context_->GetMountPointProvider(type_)->
[email protected]5b7e42e62013-01-24 22:01:5153 GetFileSystemRootPathOnFileThread(CreateURL(FilePath()),
54 true /* create */);
[email protected]054702d2011-05-28 01:12:1855
56 // Initialize the usage cache file.
[email protected]022d2702012-05-14 16:04:2657 FilePath usage_cache_path = GetUsageCachePath();
58 if (!usage_cache_path.empty())
59 FileSystemUsageCache::UpdateUsage(usage_cache_path, 0);
[email protected]054702d2011-05-28 01:12:1860}
61
[email protected]02a60542012-07-24 20:05:3362void LocalFileSystemTestOriginHelper::SetUp(
[email protected]77316bf2011-05-19 08:16:4463 const FilePath& base_dir,
[email protected]77316bf2011-05-19 08:16:4464 bool unlimited_quota,
[email protected]4f056a872013-01-23 04:24:3665 quota::QuotaManagerProxy* quota_manager_proxy) {
[email protected]d0240442011-08-17 09:23:0066 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy =
67 new quota::MockSpecialStoragePolicy;
68 special_storage_policy->SetAllUnlimited(unlimited_quota);
[email protected]77316bf2011-05-19 08:16:4469 file_system_context_ = new FileSystemContext(
[email protected]dc57ec82012-08-07 03:50:1070 FileSystemTaskRunners::CreateMockTaskRunners(),
[email protected]6ef0c3912013-01-25 22:46:3471 ExternalMountPoints::CreateRefCounted().get(),
[email protected]d0240442011-08-17 09:23:0072 special_storage_policy,
[email protected]77316bf2011-05-19 08:16:4473 quota_manager_proxy,
74 base_dir,
[email protected]e7e46732012-01-05 11:45:5575 CreateAllowFileAccessOptions());
[email protected]77316bf2011-05-19 08:16:4476
[email protected]4f056a872013-01-23 04:24:3677 SetUpFileUtil();
[email protected]77316bf2011-05-19 08:16:4478
79 // Prepare the origin's root directory.
[email protected]ecdfd6c52012-04-11 13:35:4480 FileSystemMountPointProvider* mount_point_provider =
81 file_system_context_->GetMountPointProvider(type_);
[email protected]5b7e42e62013-01-24 22:01:5182 mount_point_provider->GetFileSystemRootPathOnFileThread(CreateURL(FilePath()),
83 true /* create */);
[email protected]ecdfd6c52012-04-11 13:35:4484
[email protected]022d2702012-05-14 16:04:2685 // 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]77316bf2011-05-19 08:16:4489}
90
[email protected]02a60542012-07-24 20:05:3391void LocalFileSystemTestOriginHelper::TearDown() {
[email protected]77316bf2011-05-19 08:16:4492 file_system_context_ = NULL;
[email protected]4e1c7e52012-12-16 01:06:3693 MessageLoop::current()->RunUntilIdle();
[email protected]77316bf2011-05-19 08:16:4494}
95
[email protected]02a60542012-07-24 20:05:3396FilePath LocalFileSystemTestOriginHelper::GetOriginRootPath() const {
[email protected]e7e46732012-01-05 11:45:5597 return file_system_context_->GetMountPointProvider(type_)->
[email protected]5b7e42e62013-01-24 22:01:5198 GetFileSystemRootPathOnFileThread(CreateURL(FilePath()), false);
[email protected]77316bf2011-05-19 08:16:4499}
100
[email protected]02a60542012-07-24 20:05:33101FilePath LocalFileSystemTestOriginHelper::GetLocalPath(const FilePath& path) {
[email protected]77316bf2011-05-19 08:16:44102 DCHECK(file_util_);
103 FilePath local_path;
104 scoped_ptr<FileSystemOperationContext> context(NewOperationContext());
[email protected]949f25a2012-06-27 01:53:09105 file_util_->GetLocalFilePath(context.get(), CreateURL(path), &local_path);
[email protected]77316bf2011-05-19 08:16:44106 return local_path;
107}
108
[email protected]02a60542012-07-24 20:05:33109FilePath LocalFileSystemTestOriginHelper::GetLocalPathFromASCII(
[email protected]77316bf2011-05-19 08:16:44110 const std::string& path) {
111 return GetLocalPath(FilePath().AppendASCII(path));
112}
113
[email protected]02a60542012-07-24 20:05:33114FilePath LocalFileSystemTestOriginHelper::GetUsageCachePath() const {
[email protected]022d2702012-05-14 16:04:26115 if (type_ != kFileSystemTypeTemporary &&
116 type_ != kFileSystemTypePersistent)
117 return FilePath();
[email protected]e7e46732012-01-05 11:45:55118 return file_system_context_->
119 sandbox_provider()->GetUsageCachePathForOriginAndType(origin_, type_);
[email protected]77316bf2011-05-19 08:16:44120}
121
[email protected]02a60542012-07-24 20:05:33122FileSystemURL LocalFileSystemTestOriginHelper::CreateURL(const FilePath& path)
[email protected]949f25a2012-06-27 01:53:09123 const {
[email protected]5b7e42e62013-01-24 22:01:51124 return file_system_context_->CreateCrackedFileSystemURL(origin_, type_, path);
[email protected]45ea0fbbb2012-02-27 22:28:49125}
126
[email protected]02a60542012-07-24 20:05:33127base::PlatformFileError LocalFileSystemTestOriginHelper::SameFileUtilCopy(
[email protected]45ea0fbbb2012-02-27 22:28:49128 FileSystemOperationContext* context,
[email protected]949f25a2012-06-27 01:53:09129 const FileSystemURL& src,
130 const FileSystemURL& dest) const {
[email protected]c3d5d252012-03-08 04:56:21131 return FileUtilHelper::Copy(context, file_util(), file_util(), src, dest);
[email protected]45ea0fbbb2012-02-27 22:28:49132}
133
[email protected]02a60542012-07-24 20:05:33134base::PlatformFileError LocalFileSystemTestOriginHelper::SameFileUtilMove(
[email protected]45ea0fbbb2012-02-27 22:28:49135 FileSystemOperationContext* context,
[email protected]949f25a2012-06-27 01:53:09136 const FileSystemURL& src,
137 const FileSystemURL& dest) const {
[email protected]c3d5d252012-03-08 04:56:21138 return FileUtilHelper::Move(context, file_util(), file_util(), src, dest);
[email protected]08f8feb2012-02-26 11:53:50139}
140
[email protected]02a60542012-07-24 20:05:33141int64 LocalFileSystemTestOriginHelper::GetCachedOriginUsage() const {
[email protected]022d2702012-05-14 16:04:26142 return file_system_context_->GetQuotaUtil(type_)->GetOriginUsageOnFileThread(
[email protected]22894522012-05-23 07:14:58143 file_system_context_, origin_, type_);
[email protected]ffac1ac42011-07-25 15:03:15144}
145
[email protected]02a60542012-07-24 20:05:33146int64 LocalFileSystemTestOriginHelper::ComputeCurrentOriginUsage() const {
[email protected]0c5ebe32011-08-19 22:37:16147 int64 size = file_util::ComputeDirectorySize(GetOriginRootPath());
148 if (file_util::PathExists(GetUsageCachePath()))
149 size -= FileSystemUsageCache::kUsageFileSize;
150 return size;
[email protected]8bf65df2011-05-27 07:26:15151}
152
[email protected]02a60542012-07-24 20:05:33153int64
154LocalFileSystemTestOriginHelper::ComputeCurrentDirectoryDatabaseUsage() const {
[email protected]ecdfd6c52012-04-11 13:35:44155 return file_util::ComputeDirectorySize(
156 GetOriginRootPath().AppendASCII("Paths"));
157}
158
[email protected]02a60542012-07-24 20:05:33159LocalFileSystemOperation* LocalFileSystemTestOriginHelper::NewOperation() {
[email protected]77316bf2011-05-19 08:16:44160 DCHECK(file_system_context_.get());
161 DCHECK(file_util_);
[email protected]fc2e14fa2012-10-18 14:06:11162 scoped_ptr<FileSystemOperationContext> operation_context(
163 NewOperationContext());
[email protected]caf66702012-09-07 07:02:20164 LocalFileSystemOperation* operation = static_cast<LocalFileSystemOperation*>(
[email protected]baa6e112012-09-10 06:24:46165 file_system_context_->CreateFileSystemOperation(
166 CreateURL(FilePath()), NULL));
[email protected]77316bf2011-05-19 08:16:44167 return operation;
168}
169
[email protected]02a60542012-07-24 20:05:33170FileSystemOperationContext*
171LocalFileSystemTestOriginHelper::NewOperationContext() {
[email protected]77316bf2011-05-19 08:16:44172 DCHECK(file_system_context_.get());
[email protected]77316bf2011-05-19 08:16:44173 FileSystemOperationContext* context =
[email protected]08f8feb2012-02-26 11:53:50174 new FileSystemOperationContext(file_system_context_.get());
[email protected]caf66702012-09-07 07:02:20175 context->set_update_observers(
176 *file_system_context_->GetUpdateObservers(type_));
[email protected]77316bf2011-05-19 08:16:44177 return context;
178}
179
[email protected]4f056a872013-01-23 04:24:36180void 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]25b697992013-01-29 07:10:05189 file_util_ = file_system_context_->GetFileUtil(type_);
[email protected]4f056a872013-01-23 04:24:36190 DCHECK(file_util_);
191}
192
[email protected]77316bf2011-05-19 08:16:44193} // namespace fileapi