[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 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/test_mount_point_provider.h" |
| 6 | |
| 7 | #include <set> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/file_util.h" |
| 12 | #include "base/sequenced_task_runner.h" |
[email protected] | c4ca3b45 | 2012-05-31 03:15:46 | [diff] [blame] | 13 | #include "webkit/fileapi/file_system_file_stream_reader.h" |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 14 | #include "webkit/fileapi/file_system_quota_util.h" |
[email protected] | 7e836a3d | 2012-05-31 05:14:59 | [diff] [blame] | 15 | #include "webkit/fileapi/file_system_util.h" |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 16 | #include "webkit/fileapi/local_file_system_operation.h" |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 17 | #include "webkit/fileapi/local_file_util.h" |
| 18 | #include "webkit/fileapi/native_file_util.h" |
[email protected] | 7469890 | 2012-06-12 12:16:38 | [diff] [blame] | 19 | #include "webkit/fileapi/sandbox_file_stream_writer.h" |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 20 | #include "webkit/quota/quota_manager.h" |
| 21 | |
| 22 | namespace fileapi { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | // This only supports single origin. |
| 27 | class TestFileSystemQuotaUtil : public FileSystemQuotaUtil { |
| 28 | public: |
| 29 | explicit TestFileSystemQuotaUtil(base::SequencedTaskRunner* task_runner) |
| 30 | : FileSystemQuotaUtil(task_runner), usage_(0) {} |
| 31 | virtual ~TestFileSystemQuotaUtil() {} |
| 32 | |
| 33 | virtual void GetOriginsForTypeOnFileThread( |
| 34 | FileSystemType type, |
| 35 | std::set<GURL>* origins) OVERRIDE { |
| 36 | NOTREACHED(); |
| 37 | } |
| 38 | virtual void GetOriginsForHostOnFileThread( |
| 39 | FileSystemType type, |
| 40 | const std::string& host, |
| 41 | std::set<GURL>* origins) OVERRIDE { |
| 42 | NOTREACHED(); |
| 43 | } |
| 44 | virtual int64 GetOriginUsageOnFileThread( |
[email protected] | 2289452 | 2012-05-23 07:14:58 | [diff] [blame] | 45 | FileSystemContext* context, |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 46 | const GURL& origin_url, |
| 47 | FileSystemType type) OVERRIDE { |
| 48 | return usage_; |
| 49 | } |
| 50 | virtual void NotifyOriginWasAccessedOnIOThread( |
| 51 | quota::QuotaManagerProxy* proxy, |
| 52 | const GURL& origin_url, |
| 53 | FileSystemType type) OVERRIDE { |
| 54 | // Do nothing. |
| 55 | } |
| 56 | virtual void UpdateOriginUsageOnFileThread( |
| 57 | quota::QuotaManagerProxy* proxy, |
| 58 | const GURL& origin_url, |
| 59 | FileSystemType type, |
| 60 | int64 delta) OVERRIDE { |
| 61 | usage_ += delta; |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 62 | } |
| 63 | virtual void StartUpdateOriginOnFileThread( |
| 64 | const GURL& origin_url, |
| 65 | FileSystemType type) OVERRIDE { |
| 66 | // Do nothing. |
| 67 | } |
| 68 | virtual void EndUpdateOriginOnFileThread( |
| 69 | const GURL& origin_url, |
| 70 | FileSystemType type) OVERRIDE {} |
| 71 | virtual void InvalidateUsageCache(const GURL& origin_url, |
| 72 | FileSystemType type) OVERRIDE { |
| 73 | // Do nothing. |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | int64 usage_; |
| 78 | }; |
| 79 | |
| 80 | } // namespace |
| 81 | |
| 82 | TestMountPointProvider::TestMountPointProvider( |
| 83 | base::SequencedTaskRunner* task_runner, |
| 84 | const FilePath& base_path) |
| 85 | : base_path_(base_path), |
[email protected] | 95af737 | 2012-05-28 07:51:20 | [diff] [blame] | 86 | local_file_util_(new LocalFileUtil()), |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 87 | quota_util_(new TestFileSystemQuotaUtil(task_runner)) { |
| 88 | } |
| 89 | |
| 90 | TestMountPointProvider::~TestMountPointProvider() { |
| 91 | } |
| 92 | |
| 93 | void TestMountPointProvider::ValidateFileSystemRoot( |
| 94 | const GURL& origin_url, |
| 95 | FileSystemType type, |
| 96 | bool create, |
| 97 | const ValidateFileSystemCallback& callback) { |
| 98 | // This won't be called unless we add test code that opens a test |
| 99 | // filesystem by OpenFileSystem. |
| 100 | NOTREACHED(); |
| 101 | } |
| 102 | |
| 103 | FilePath TestMountPointProvider::GetFileSystemRootPathOnFileThread( |
| 104 | const GURL& origin_url, |
| 105 | FileSystemType type, |
| 106 | const FilePath& virtual_path, |
| 107 | bool create) { |
| 108 | DCHECK_EQ(kFileSystemTypeTest, type); |
| 109 | bool success = true; |
| 110 | if (create) |
| 111 | success = file_util::CreateDirectory(base_path_); |
| 112 | else |
| 113 | success = file_util::DirectoryExists(base_path_); |
| 114 | return success ? base_path_ : FilePath(); |
| 115 | } |
| 116 | |
| 117 | bool TestMountPointProvider::IsAccessAllowed( |
| 118 | const GURL& origin_url, FileSystemType type, const FilePath& virtual_path) { |
| 119 | return type == fileapi::kFileSystemTypeTest; |
| 120 | } |
| 121 | |
| 122 | bool TestMountPointProvider::IsRestrictedFileName( |
| 123 | const FilePath& filename) const { |
| 124 | return false; |
| 125 | } |
| 126 | |
[email protected] | d6afd11 | 2012-07-25 22:55:04 | [diff] [blame] | 127 | FileSystemFileUtil* TestMountPointProvider::GetFileUtil(FileSystemType type) { |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 128 | return local_file_util_.get(); |
| 129 | } |
| 130 | |
| 131 | FilePath TestMountPointProvider::GetPathForPermissionsCheck( |
| 132 | const FilePath& virtual_path) const { |
| 133 | return base_path_.Append(virtual_path); |
| 134 | } |
| 135 | |
| 136 | FileSystemOperationInterface* |
| 137 | TestMountPointProvider::CreateFileSystemOperation( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 138 | const FileSystemURL& url, |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 139 | FileSystemContext* context) const { |
[email protected] | 75af1a4 | 2012-07-26 04:06:20 | [diff] [blame^] | 140 | scoped_ptr<FileSystemOperationContext> operation_context( |
| 141 | new FileSystemOperationContext(context)); |
| 142 | return new LocalFileSystemOperation(context, operation_context.Pass()); |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | c4ca3b45 | 2012-05-31 03:15:46 | [diff] [blame] | 145 | webkit_blob::FileStreamReader* TestMountPointProvider::CreateFileStreamReader( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 146 | const FileSystemURL& url, |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 147 | int64 offset, |
| 148 | FileSystemContext* context) const { |
[email protected] | c4ca3b45 | 2012-05-31 03:15:46 | [diff] [blame] | 149 | return new FileSystemFileStreamReader(context, url, offset); |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | 7e836a3d | 2012-05-31 05:14:59 | [diff] [blame] | 152 | fileapi::FileStreamWriter* TestMountPointProvider::CreateFileStreamWriter( |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 153 | const FileSystemURL& url, |
[email protected] | 7e84b91 | 2012-05-16 04:42:01 | [diff] [blame] | 154 | int64 offset, |
| 155 | FileSystemContext* context) const { |
[email protected] | 7469890 | 2012-06-12 12:16:38 | [diff] [blame] | 156 | return new SandboxFileStreamWriter(context, url, offset); |
[email protected] | 7e84b91 | 2012-05-16 04:42:01 | [diff] [blame] | 157 | } |
| 158 | |
[email protected] | 6faad82 | 2012-05-11 12:58:29 | [diff] [blame] | 159 | FileSystemQuotaUtil* TestMountPointProvider::GetQuotaUtil() { |
| 160 | return quota_util_.get(); |
| 161 | } |
| 162 | |
| 163 | } // namespace fileapi |