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/local_file_system_test_helper.h" | |
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" | |
11 #include "webkit/browser/fileapi/file_system_file_util.h" | |
12 #include "webkit/browser/fileapi/file_system_usage_cache.h" | |
13 #include "webkit/browser/fileapi/sandbox_mount_point_provider.h" | |
14 #include "webkit/fileapi/file_system_context.h" | |
15 #include "webkit/fileapi/file_system_operation_context.h" | |
16 #include "webkit/fileapi/file_system_task_runners.h" | |
17 #include "webkit/fileapi/file_system_url.h" | |
18 #include "webkit/fileapi/file_system_util.h" | |
19 #include "webkit/fileapi/local_file_system_operation.h" | |
20 #include "webkit/fileapi/mock_file_system_context.h" | |
21 #include "webkit/quota/mock_special_storage_policy.h" | |
22 | |
23 namespace fileapi { | |
24 | |
25 LocalFileSystemTestOriginHelper::LocalFileSystemTestOriginHelper( | |
26 const GURL& origin, FileSystemType type) | |
27 : origin_(origin), type_(type), file_util_(NULL) { | |
28 } | |
29 | |
30 LocalFileSystemTestOriginHelper::LocalFileSystemTestOriginHelper() | |
31 : origin_(GURL("http://foo.com")), | |
32 type_(kFileSystemTypeTemporary), | |
33 file_util_(NULL) { | |
34 } | |
35 | |
36 LocalFileSystemTestOriginHelper::~LocalFileSystemTestOriginHelper() { | |
37 } | |
38 | |
39 void LocalFileSystemTestOriginHelper::SetUp(const base::FilePath& base_dir) { | |
40 SetUp(base_dir, NULL); | |
41 } | |
42 | |
43 void LocalFileSystemTestOriginHelper::SetUp( | |
44 FileSystemContext* file_system_context) { | |
45 file_system_context_ = file_system_context; | |
46 | |
47 SetUpFileUtil(); | |
48 | |
49 // Prepare the origin's root directory. | |
50 file_system_context_->GetMountPointProvider(type_)-> | |
51 GetFileSystemRootPathOnFileThread(CreateURL(base::FilePath()), | |
52 true /* create */); | |
53 | |
54 // Initialize the usage cache file. | |
55 base::FilePath usage_cache_path = GetUsageCachePath(); | |
56 if (!usage_cache_path.empty()) | |
57 usage_cache()->UpdateUsage(usage_cache_path, 0); | |
58 } | |
59 | |
60 void LocalFileSystemTestOriginHelper::SetUp( | |
61 const base::FilePath& base_dir, | |
62 quota::QuotaManagerProxy* quota_manager_proxy) { | |
63 file_system_context_ = CreateFileSystemContextForTesting( | |
64 quota_manager_proxy, base_dir); | |
65 | |
66 SetUpFileUtil(); | |
67 | |
68 // Prepare the origin's root directory. | |
69 FileSystemMountPointProvider* mount_point_provider = | |
70 file_system_context_->GetMountPointProvider(type_); | |
71 mount_point_provider->GetFileSystemRootPathOnFileThread( | |
72 CreateURL(base::FilePath()), true /* create */); | |
73 | |
74 // Initialize the usage cache file. | |
75 base::FilePath usage_cache_path = GetUsageCachePath(); | |
76 if (!usage_cache_path.empty()) | |
77 usage_cache()->UpdateUsage(usage_cache_path, 0); | |
78 } | |
79 | |
80 void LocalFileSystemTestOriginHelper::TearDown() { | |
81 file_system_context_ = NULL; | |
82 base::MessageLoop::current()->RunUntilIdle(); | |
83 } | |
84 | |
85 base::FilePath LocalFileSystemTestOriginHelper::GetOriginRootPath() const { | |
86 return file_system_context_->GetMountPointProvider(type_)-> | |
87 GetFileSystemRootPathOnFileThread(CreateURL(base::FilePath()), false); | |
88 } | |
89 | |
90 base::FilePath LocalFileSystemTestOriginHelper::GetLocalPath( | |
91 const base::FilePath& path) { | |
92 DCHECK(file_util_); | |
93 base::FilePath local_path; | |
94 scoped_ptr<FileSystemOperationContext> context(NewOperationContext()); | |
95 file_util_->GetLocalFilePath(context.get(), CreateURL(path), &local_path); | |
96 return local_path; | |
97 } | |
98 | |
99 base::FilePath LocalFileSystemTestOriginHelper::GetLocalPathFromASCII( | |
100 const std::string& path) { | |
101 return GetLocalPath(base::FilePath().AppendASCII(path)); | |
102 } | |
103 | |
104 base::FilePath LocalFileSystemTestOriginHelper::GetUsageCachePath() const { | |
105 if (type_ != kFileSystemTypeTemporary && | |
106 type_ != kFileSystemTypePersistent && | |
107 type_ != kFileSystemTypeSyncable) | |
108 return base::FilePath(); | |
109 return file_system_context_-> | |
110 sandbox_provider()->GetUsageCachePathForOriginAndType(origin_, type_); | |
111 } | |
112 | |
113 FileSystemURL LocalFileSystemTestOriginHelper::CreateURL( | |
114 const base::FilePath& path) const { | |
115 return file_system_context_->CreateCrackedFileSystemURL(origin_, type_, path); | |
116 } | |
117 | |
118 int64 LocalFileSystemTestOriginHelper::GetCachedOriginUsage() const { | |
119 return file_system_context_->GetQuotaUtil(type_)->GetOriginUsageOnFileThread( | |
120 file_system_context_, origin_, type_); | |
121 } | |
122 | |
123 int64 LocalFileSystemTestOriginHelper::ComputeCurrentOriginUsage() { | |
124 usage_cache()->CloseCacheFiles(); | |
125 int64 size = file_util::ComputeDirectorySize(GetOriginRootPath()); | |
126 if (file_util::PathExists(GetUsageCachePath())) | |
127 size -= FileSystemUsageCache::kUsageFileSize; | |
128 return size; | |
129 } | |
130 | |
131 int64 | |
132 LocalFileSystemTestOriginHelper::ComputeCurrentDirectoryDatabaseUsage() const { | |
133 return file_util::ComputeDirectorySize( | |
134 GetOriginRootPath().AppendASCII("Paths")); | |
135 } | |
136 | |
137 LocalFileSystemOperation* LocalFileSystemTestOriginHelper::NewOperation() { | |
138 DCHECK(file_system_context_.get()); | |
139 DCHECK(file_util_); | |
140 scoped_ptr<FileSystemOperationContext> operation_context( | |
141 NewOperationContext()); | |
142 LocalFileSystemOperation* operation = static_cast<LocalFileSystemOperation*>( | |
143 file_system_context_->CreateFileSystemOperation( | |
144 CreateURL(base::FilePath()), NULL)); | |
145 return operation; | |
146 } | |
147 | |
148 FileSystemOperationContext* | |
149 LocalFileSystemTestOriginHelper::NewOperationContext() { | |
150 DCHECK(file_system_context_.get()); | |
151 FileSystemOperationContext* context = | |
152 new FileSystemOperationContext(file_system_context_.get()); | |
153 context->set_update_observers( | |
154 *file_system_context_->GetUpdateObservers(type_)); | |
155 return context; | |
156 } | |
157 | |
158 FileSystemUsageCache* LocalFileSystemTestOriginHelper::usage_cache() { | |
159 return file_system_context()->sandbox_provider()->usage_cache(); | |
160 } | |
161 | |
162 void LocalFileSystemTestOriginHelper::SetUpFileUtil() { | |
163 DCHECK(file_system_context_); | |
164 file_util_ = file_system_context_->GetFileUtil(type_); | |
165 DCHECK(file_util_); | |
166 } | |
167 | |
168 } // namespace fileapi | |
OLD | NEW |