OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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/browser/fileapi/syncable/sync_file_system_backend.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "webkit/browser/fileapi/async_file_util_adapter.h" |
| 9 #include "webkit/browser/fileapi/file_system_context.h" |
| 10 #include "webkit/browser/fileapi/file_system_file_stream_reader.h" |
| 11 #include "webkit/browser/fileapi/file_system_operation_impl.h" |
| 12 #include "webkit/browser/fileapi/obfuscated_file_util.h" |
| 13 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h" |
| 14 #include "webkit/browser/fileapi/sandbox_quota_observer.h" |
| 15 #include "webkit/browser/fileapi/syncable/syncable_file_system_operation.h" |
| 16 #include "webkit/browser/fileapi/syncable/syncable_file_system_util.h" |
| 17 #include "webkit/common/fileapi/file_system_util.h" |
| 18 |
| 19 namespace sync_file_system { |
| 20 |
| 21 SyncFileSystemBackend::SyncFileSystemBackend() |
| 22 : sandbox_context_(NULL) { |
| 23 } |
| 24 |
| 25 SyncFileSystemBackend::~SyncFileSystemBackend() { |
| 26 } |
| 27 |
| 28 bool SyncFileSystemBackend::CanHandleType( |
| 29 fileapi::FileSystemType type) const { |
| 30 return type == fileapi::kFileSystemTypeSyncable || |
| 31 type == fileapi::kFileSystemTypeSyncableForInternalSync; |
| 32 } |
| 33 |
| 34 void SyncFileSystemBackend::Initialize(fileapi::FileSystemContext* context) { |
| 35 DCHECK(context); |
| 36 DCHECK(!sandbox_context_); |
| 37 sandbox_context_ = context->sandbox_context(); |
| 38 update_observers_ = update_observers_.AddObserver( |
| 39 sandbox_context_->quota_observer(), |
| 40 sandbox_context_->file_task_runner()); |
| 41 syncable_update_observers_ = syncable_update_observers_.AddObserver( |
| 42 sandbox_context_->quota_observer(), |
| 43 sandbox_context_->file_task_runner()); |
| 44 } |
| 45 |
| 46 void SyncFileSystemBackend::OpenFileSystem( |
| 47 const GURL& origin_url, |
| 48 fileapi::FileSystemType type, |
| 49 fileapi::OpenFileSystemMode mode, |
| 50 const OpenFileSystemCallback& callback) { |
| 51 DCHECK(CanHandleType(type)); |
| 52 DCHECK(sandbox_context_); |
| 53 sandbox_context_->OpenFileSystem( |
| 54 origin_url, type, mode, callback, |
| 55 GetSyncableFileSystemRootURI(origin_url)); |
| 56 } |
| 57 |
| 58 fileapi::FileSystemFileUtil* SyncFileSystemBackend::GetFileUtil( |
| 59 fileapi::FileSystemType type) { |
| 60 DCHECK(sandbox_context_); |
| 61 return sandbox_context_->sync_file_util(); |
| 62 } |
| 63 |
| 64 fileapi::AsyncFileUtil* SyncFileSystemBackend::GetAsyncFileUtil( |
| 65 fileapi::FileSystemType type) { |
| 66 DCHECK(sandbox_context_); |
| 67 return sandbox_context_->file_util(); |
| 68 } |
| 69 |
| 70 fileapi::CopyOrMoveFileValidatorFactory* |
| 71 SyncFileSystemBackend::GetCopyOrMoveFileValidatorFactory( |
| 72 fileapi::FileSystemType type, |
| 73 base::PlatformFileError* error_code) { |
| 74 DCHECK(error_code); |
| 75 *error_code = base::PLATFORM_FILE_OK; |
| 76 return NULL; |
| 77 } |
| 78 |
| 79 fileapi::FileSystemOperation* |
| 80 SyncFileSystemBackend::CreateFileSystemOperation( |
| 81 const fileapi::FileSystemURL& url, |
| 82 fileapi::FileSystemContext* context, |
| 83 base::PlatformFileError* error_code) const { |
| 84 DCHECK(CanHandleType(url.type())); |
| 85 DCHECK(context); |
| 86 DCHECK(error_code); |
| 87 if (!sandbox_context_->IsAccessValid(url)) { |
| 88 *error_code = base::PLATFORM_FILE_ERROR_SECURITY; |
| 89 return NULL; |
| 90 } |
| 91 |
| 92 scoped_ptr<fileapi::FileSystemOperationContext> operation_context( |
| 93 new fileapi::FileSystemOperationContext(context)); |
| 94 |
| 95 if (url.type() == fileapi::kFileSystemTypeSyncableForInternalSync) { |
| 96 operation_context->set_update_observers(update_observers_); |
| 97 operation_context->set_change_observers(change_observers_); |
| 98 return new fileapi::FileSystemOperationImpl( |
| 99 url, context, operation_context.Pass()); |
| 100 } |
| 101 |
| 102 operation_context->set_update_observers(syncable_update_observers_); |
| 103 operation_context->set_change_observers(syncable_change_observers_); |
| 104 return new SyncableFileSystemOperation( |
| 105 url, context, operation_context.Pass()); |
| 106 } |
| 107 |
| 108 scoped_ptr<webkit_blob::FileStreamReader> |
| 109 SyncFileSystemBackend::CreateFileStreamReader( |
| 110 const fileapi::FileSystemURL& url, |
| 111 int64 offset, |
| 112 const base::Time& expected_modification_time, |
| 113 fileapi::FileSystemContext* context) const { |
| 114 DCHECK(CanHandleType(url.type())); |
| 115 if (!sandbox_context_->IsAccessValid(url)) |
| 116 return scoped_ptr<webkit_blob::FileStreamReader>(); |
| 117 return scoped_ptr<webkit_blob::FileStreamReader>( |
| 118 new fileapi::FileSystemFileStreamReader( |
| 119 context, url, offset, expected_modification_time)); |
| 120 } |
| 121 |
| 122 scoped_ptr<fileapi::FileStreamWriter> |
| 123 SyncFileSystemBackend::CreateFileStreamWriter( |
| 124 const fileapi::FileSystemURL& url, |
| 125 int64 offset, |
| 126 fileapi::FileSystemContext* context) const { |
| 127 DCHECK(CanHandleType(url.type())); |
| 128 if (!sandbox_context_->IsAccessValid(url)) |
| 129 return scoped_ptr<fileapi::FileStreamWriter>(); |
| 130 return scoped_ptr<fileapi::FileStreamWriter>( |
| 131 new fileapi::SandboxFileStreamWriter( |
| 132 context, url, offset, update_observers_)); |
| 133 } |
| 134 |
| 135 fileapi::FileSystemQuotaUtil* SyncFileSystemBackend::GetQuotaUtil() { |
| 136 return this; |
| 137 } |
| 138 |
| 139 base::PlatformFileError SyncFileSystemBackend::DeleteOriginDataOnFileThread( |
| 140 fileapi::FileSystemContext* context, |
| 141 quota::QuotaManagerProxy* proxy, |
| 142 const GURL& origin_url, |
| 143 fileapi::FileSystemType type) { |
| 144 DCHECK(CanHandleType(type)); |
| 145 DCHECK(sandbox_context_); |
| 146 return sandbox_context_->DeleteOriginDataOnFileThread( |
| 147 context, proxy, origin_url, type); |
| 148 } |
| 149 |
| 150 void SyncFileSystemBackend::GetOriginsForTypeOnFileThread( |
| 151 fileapi::FileSystemType type, |
| 152 std::set<GURL>* origins) { |
| 153 DCHECK(CanHandleType(type)); |
| 154 DCHECK(sandbox_context_); |
| 155 sandbox_context_->GetOriginsForTypeOnFileThread(type, origins); |
| 156 } |
| 157 |
| 158 void SyncFileSystemBackend::GetOriginsForHostOnFileThread( |
| 159 fileapi::FileSystemType type, |
| 160 const std::string& host, |
| 161 std::set<GURL>* origins) { |
| 162 DCHECK(CanHandleType(type)); |
| 163 DCHECK(sandbox_context_); |
| 164 sandbox_context_->GetOriginsForHostOnFileThread(type, host, origins); |
| 165 } |
| 166 |
| 167 int64 SyncFileSystemBackend::GetOriginUsageOnFileThread( |
| 168 fileapi::FileSystemContext* context, |
| 169 const GURL& origin_url, |
| 170 fileapi::FileSystemType type) { |
| 171 DCHECK(CanHandleType(type)); |
| 172 DCHECK(sandbox_context_); |
| 173 return sandbox_context_->GetOriginUsageOnFileThread( |
| 174 context, origin_url, type); |
| 175 } |
| 176 |
| 177 void SyncFileSystemBackend::InvalidateUsageCache( |
| 178 const GURL& origin_url, |
| 179 fileapi::FileSystemType type) { |
| 180 DCHECK(CanHandleType(type)); |
| 181 DCHECK(sandbox_context_); |
| 182 sandbox_context_->InvalidateUsageCache(origin_url, type); |
| 183 } |
| 184 |
| 185 void SyncFileSystemBackend::StickyInvalidateUsageCache( |
| 186 const GURL& origin_url, |
| 187 fileapi::FileSystemType type) { |
| 188 DCHECK(CanHandleType(type)); |
| 189 DCHECK(sandbox_context_); |
| 190 sandbox_context_->StickyInvalidateUsageCache(origin_url, type); |
| 191 } |
| 192 |
| 193 void SyncFileSystemBackend::AddFileUpdateObserver( |
| 194 fileapi::FileSystemType type, |
| 195 fileapi::FileUpdateObserver* observer, |
| 196 base::SequencedTaskRunner* task_runner) { |
| 197 DCHECK_EQ(fileapi::kFileSystemTypeSyncable, type); |
| 198 fileapi::UpdateObserverList* list = &syncable_update_observers_; |
| 199 *list = list->AddObserver(observer, task_runner); |
| 200 } |
| 201 |
| 202 void SyncFileSystemBackend::AddFileChangeObserver( |
| 203 fileapi::FileSystemType type, |
| 204 fileapi::FileChangeObserver* observer, |
| 205 base::SequencedTaskRunner* task_runner) { |
| 206 DCHECK_EQ(fileapi::kFileSystemTypeSyncable, type); |
| 207 fileapi::ChangeObserverList* list = &syncable_change_observers_; |
| 208 *list = list->AddObserver(observer, task_runner); |
| 209 } |
| 210 |
| 211 void SyncFileSystemBackend::AddFileAccessObserver( |
| 212 fileapi::FileSystemType type, |
| 213 fileapi::FileAccessObserver* observer, |
| 214 base::SequencedTaskRunner* task_runner) { |
| 215 NOTREACHED() << "SyncFileSystemBackend does not support access observers."; |
| 216 } |
| 217 |
| 218 const fileapi::UpdateObserverList* SyncFileSystemBackend::GetUpdateObservers( |
| 219 fileapi::FileSystemType type) const { |
| 220 DCHECK(CanHandleType(type)); |
| 221 if (type != fileapi::kFileSystemTypeSyncable) |
| 222 return NULL; |
| 223 return &syncable_update_observers_; |
| 224 } |
| 225 |
| 226 const fileapi::ChangeObserverList* SyncFileSystemBackend::GetChangeObservers( |
| 227 fileapi::FileSystemType type) const { |
| 228 DCHECK(CanHandleType(type)); |
| 229 DCHECK_EQ(fileapi::kFileSystemTypeSyncable, type); |
| 230 if (type != fileapi::kFileSystemTypeSyncable) |
| 231 return NULL; |
| 232 return &syncable_change_observers_; |
| 233 } |
| 234 |
| 235 const fileapi::AccessObserverList* SyncFileSystemBackend::GetAccessObservers( |
| 236 fileapi::FileSystemType type) const { |
| 237 return NULL; |
| 238 } |
| 239 |
| 240 } // namespace sync_file_system |
OLD | NEW |