Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h" | 5 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h" | 8 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h" |
| 9 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h" | 9 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h" |
| 10 #include "chrome/browser/sync_file_system/local/syncable_file_system_operation.h " | 10 #include "chrome/browser/sync_file_system/local/syncable_file_system_operation.h " |
| 11 #include "chrome/browser/sync_file_system/sync_file_system_service.h" | |
| 12 #include "chrome/browser/sync_file_system/sync_file_system_service_factory.h" | |
| 11 #include "chrome/browser/sync_file_system/syncable_file_system_util.h" | 13 #include "chrome/browser/sync_file_system/syncable_file_system_util.h" |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "webkit/browser/fileapi/async_file_util_adapter.h" | |
| 12 #include "webkit/browser/fileapi/file_system_context.h" | 16 #include "webkit/browser/fileapi/file_system_context.h" |
| 13 #include "webkit/browser/fileapi/file_system_file_stream_reader.h" | 17 #include "webkit/browser/fileapi/file_system_file_stream_reader.h" |
| 14 #include "webkit/browser/fileapi/file_system_operation_impl.h" | 18 #include "webkit/browser/fileapi/file_system_operation_impl.h" |
| 15 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h" | 19 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h" |
| 16 #include "webkit/browser/fileapi/sandbox_quota_observer.h" | 20 #include "webkit/browser/fileapi/sandbox_quota_observer.h" |
| 17 #include "webkit/common/fileapi/file_system_util.h" | 21 #include "webkit/common/fileapi/file_system_util.h" |
| 18 | 22 |
| 23 using content::BrowserThread; | |
| 24 | |
| 19 namespace sync_file_system { | 25 namespace sync_file_system { |
| 20 | 26 |
| 21 SyncFileSystemBackend::SyncFileSystemBackend() | 27 SyncFileSystemBackend::SyncFileSystemBackend(Profile* profile) |
| 22 : delegate_(NULL) { | 28 : context_(NULL), |
| 29 profile_(profile), | |
| 30 skip_initialize_syncfs_service_for_testing_(false) { | |
|
kinuko
2013/08/23 10:02:03
This is called on UI thread.. is it correct? Can w
nhiroki
2013/08/28 05:37:42
Added an assertion.
I found some tests using Cann
kinuko
2013/08/28 06:51:28
Which test fails? In most cases I expect we could
nhiroki
2013/08/28 07:46:46
AFAIK, the following tests failed:
- LocalFileCha
kinuko
2013/08/28 08:23:57
Sorry I've overlooked this part. I think it's ok
| |
| 23 } | 31 } |
| 24 | 32 |
| 25 SyncFileSystemBackend::~SyncFileSystemBackend() { | 33 SyncFileSystemBackend::~SyncFileSystemBackend() { |
| 26 if (change_tracker_) { | 34 if (change_tracker_) { |
| 27 delegate_->file_task_runner()->DeleteSoon( | 35 GetDelegate()->file_task_runner()->DeleteSoon( |
| 28 FROM_HERE, change_tracker_.release()); | 36 FROM_HERE, change_tracker_.release()); |
| 29 } | 37 } |
| 30 } | 38 } |
| 31 | 39 |
| 32 bool SyncFileSystemBackend::CanHandleType( | 40 bool SyncFileSystemBackend::CanHandleType( |
| 33 fileapi::FileSystemType type) const { | 41 fileapi::FileSystemType type) const { |
| 34 return type == fileapi::kFileSystemTypeSyncable || | 42 return type == fileapi::kFileSystemTypeSyncable || |
| 35 type == fileapi::kFileSystemTypeSyncableForInternalSync; | 43 type == fileapi::kFileSystemTypeSyncableForInternalSync; |
| 36 } | 44 } |
| 37 | 45 |
| 38 void SyncFileSystemBackend::Initialize(fileapi::FileSystemContext* context) { | 46 void SyncFileSystemBackend::Initialize(fileapi::FileSystemContext* context) { |
| 39 DCHECK(context); | 47 DCHECK(context); |
| 40 DCHECK(!delegate_); | 48 DCHECK(!context_); |
| 41 delegate_ = context->sandbox_delegate(); | 49 context_ = context; |
| 42 | 50 |
| 43 delegate_->AddFileUpdateObserver( | 51 fileapi::SandboxFileSystemBackendDelegate* delegate = GetDelegate(); |
| 52 delegate->AddFileUpdateObserver( | |
| 44 fileapi::kFileSystemTypeSyncable, | 53 fileapi::kFileSystemTypeSyncable, |
| 45 delegate_->quota_observer(), | 54 delegate->quota_observer(), |
| 46 delegate_->file_task_runner()); | 55 delegate->file_task_runner()); |
| 47 delegate_->AddFileUpdateObserver( | 56 delegate->AddFileUpdateObserver( |
| 48 fileapi::kFileSystemTypeSyncableForInternalSync, | 57 fileapi::kFileSystemTypeSyncableForInternalSync, |
| 49 delegate_->quota_observer(), | 58 delegate->quota_observer(), |
| 50 delegate_->file_task_runner()); | 59 delegate->file_task_runner()); |
| 51 } | 60 } |
| 52 | 61 |
| 53 void SyncFileSystemBackend::OpenFileSystem( | 62 void SyncFileSystemBackend::OpenFileSystem( |
| 54 const GURL& origin_url, | 63 const GURL& origin_url, |
| 55 fileapi::FileSystemType type, | 64 fileapi::FileSystemType type, |
| 56 fileapi::OpenFileSystemMode mode, | 65 fileapi::OpenFileSystemMode mode, |
| 57 const OpenFileSystemCallback& callback) { | 66 const OpenFileSystemCallback& callback) { |
| 58 DCHECK(CanHandleType(type)); | 67 DCHECK(CanHandleType(type)); |
| 59 DCHECK(delegate_); | 68 |
| 60 delegate_->OpenFileSystem(origin_url, type, mode, callback, | 69 if (skip_initialize_syncfs_service_for_testing_) { |
| 61 GetSyncableFileSystemRootURI(origin_url)); | 70 GetDelegate()->OpenFileSystem(origin_url, type, mode, callback, |
| 71 GetSyncableFileSystemRootURI(origin_url)); | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 SyncStatusCallback initialize_callback = | |
| 76 base::Bind(&SyncFileSystemBackend::DidInitializeSyncFileSystemService, | |
| 77 base::Unretained(this), origin_url, type, mode, callback); | |
| 78 InitializeSyncFileSystemService(origin_url, initialize_callback); | |
| 62 } | 79 } |
| 63 | 80 |
| 64 fileapi::FileSystemFileUtil* SyncFileSystemBackend::GetFileUtil( | 81 fileapi::FileSystemFileUtil* SyncFileSystemBackend::GetFileUtil( |
| 65 fileapi::FileSystemType type) { | 82 fileapi::FileSystemType type) { |
| 66 DCHECK(delegate_); | 83 return GetDelegate()->sync_file_util(); |
| 67 return delegate_->sync_file_util(); | |
| 68 } | 84 } |
| 69 | 85 |
| 70 fileapi::AsyncFileUtil* SyncFileSystemBackend::GetAsyncFileUtil( | 86 fileapi::AsyncFileUtil* SyncFileSystemBackend::GetAsyncFileUtil( |
| 71 fileapi::FileSystemType type) { | 87 fileapi::FileSystemType type) { |
| 72 DCHECK(delegate_); | 88 return GetDelegate()->file_util(); |
| 73 return delegate_->file_util(); | |
| 74 } | 89 } |
| 75 | 90 |
| 76 fileapi::CopyOrMoveFileValidatorFactory* | 91 fileapi::CopyOrMoveFileValidatorFactory* |
| 77 SyncFileSystemBackend::GetCopyOrMoveFileValidatorFactory( | 92 SyncFileSystemBackend::GetCopyOrMoveFileValidatorFactory( |
| 78 fileapi::FileSystemType type, | 93 fileapi::FileSystemType type, |
| 79 base::PlatformFileError* error_code) { | 94 base::PlatformFileError* error_code) { |
| 80 DCHECK(error_code); | 95 DCHECK(error_code); |
| 81 *error_code = base::PLATFORM_FILE_OK; | 96 *error_code = base::PLATFORM_FILE_OK; |
| 82 return NULL; | 97 return NULL; |
| 83 } | 98 } |
| 84 | 99 |
| 85 fileapi::FileSystemOperation* | 100 fileapi::FileSystemOperation* |
| 86 SyncFileSystemBackend::CreateFileSystemOperation( | 101 SyncFileSystemBackend::CreateFileSystemOperation( |
| 87 const fileapi::FileSystemURL& url, | 102 const fileapi::FileSystemURL& url, |
| 88 fileapi::FileSystemContext* context, | 103 fileapi::FileSystemContext* context, |
| 89 base::PlatformFileError* error_code) const { | 104 base::PlatformFileError* error_code) const { |
| 90 DCHECK(CanHandleType(url.type())); | 105 DCHECK(CanHandleType(url.type())); |
| 91 DCHECK(context); | 106 DCHECK(context); |
| 92 DCHECK(error_code); | 107 DCHECK(error_code); |
| 93 | 108 |
| 94 DCHECK(delegate_); | |
| 95 scoped_ptr<fileapi::FileSystemOperationContext> operation_context = | 109 scoped_ptr<fileapi::FileSystemOperationContext> operation_context = |
| 96 delegate_->CreateFileSystemOperationContext(url, context, error_code); | 110 GetDelegate()->CreateFileSystemOperationContext(url, context, error_code); |
| 97 if (!operation_context) | 111 if (!operation_context) |
| 98 return NULL; | 112 return NULL; |
| 99 | 113 |
| 100 if (url.type() == fileapi::kFileSystemTypeSyncableForInternalSync) { | 114 if (url.type() == fileapi::kFileSystemTypeSyncableForInternalSync) { |
| 101 return new fileapi::FileSystemOperationImpl( | 115 return new fileapi::FileSystemOperationImpl( |
| 102 url, context, operation_context.Pass()); | 116 url, context, operation_context.Pass()); |
| 103 } | 117 } |
| 104 | 118 |
| 105 return new SyncableFileSystemOperation( | 119 return new SyncableFileSystemOperation( |
| 106 url, context, operation_context.Pass()); | 120 url, context, operation_context.Pass()); |
| 107 } | 121 } |
| 108 | 122 |
| 109 scoped_ptr<webkit_blob::FileStreamReader> | 123 scoped_ptr<webkit_blob::FileStreamReader> |
| 110 SyncFileSystemBackend::CreateFileStreamReader( | 124 SyncFileSystemBackend::CreateFileStreamReader( |
| 111 const fileapi::FileSystemURL& url, | 125 const fileapi::FileSystemURL& url, |
| 112 int64 offset, | 126 int64 offset, |
| 113 const base::Time& expected_modification_time, | 127 const base::Time& expected_modification_time, |
| 114 fileapi::FileSystemContext* context) const { | 128 fileapi::FileSystemContext* context) const { |
| 115 DCHECK(CanHandleType(url.type())); | 129 DCHECK(CanHandleType(url.type())); |
| 116 DCHECK(delegate_); | 130 return GetDelegate()->CreateFileStreamReader( |
| 117 return delegate_->CreateFileStreamReader( | |
| 118 url, offset, expected_modification_time, context); | 131 url, offset, expected_modification_time, context); |
| 119 } | 132 } |
| 120 | 133 |
| 121 scoped_ptr<fileapi::FileStreamWriter> | 134 scoped_ptr<fileapi::FileStreamWriter> |
| 122 SyncFileSystemBackend::CreateFileStreamWriter( | 135 SyncFileSystemBackend::CreateFileStreamWriter( |
| 123 const fileapi::FileSystemURL& url, | 136 const fileapi::FileSystemURL& url, |
| 124 int64 offset, | 137 int64 offset, |
| 125 fileapi::FileSystemContext* context) const { | 138 fileapi::FileSystemContext* context) const { |
| 126 DCHECK(CanHandleType(url.type())); | 139 DCHECK(CanHandleType(url.type())); |
| 127 DCHECK(delegate_); | 140 return GetDelegate()->CreateFileStreamWriter( |
| 128 return delegate_->CreateFileStreamWriter( | |
| 129 url, offset, context, fileapi::kFileSystemTypeSyncableForInternalSync); | 141 url, offset, context, fileapi::kFileSystemTypeSyncableForInternalSync); |
| 130 } | 142 } |
| 131 | 143 |
| 132 fileapi::FileSystemQuotaUtil* SyncFileSystemBackend::GetQuotaUtil() { | 144 fileapi::FileSystemQuotaUtil* SyncFileSystemBackend::GetQuotaUtil() { |
| 133 return delegate_; | 145 return GetDelegate(); |
| 134 } | 146 } |
| 135 | 147 |
| 136 // static | 148 // static |
| 137 SyncFileSystemBackend* SyncFileSystemBackend::GetBackend( | 149 SyncFileSystemBackend* SyncFileSystemBackend::GetBackend( |
| 138 const fileapi::FileSystemContext* file_system_context) { | 150 const fileapi::FileSystemContext* file_system_context) { |
| 139 DCHECK(file_system_context); | 151 DCHECK(file_system_context); |
| 140 return static_cast<SyncFileSystemBackend*>( | 152 return static_cast<SyncFileSystemBackend*>( |
| 141 file_system_context->GetFileSystemBackend( | 153 file_system_context->GetFileSystemBackend( |
| 142 fileapi::kFileSystemTypeSyncable)); | 154 fileapi::kFileSystemTypeSyncable)); |
| 143 } | 155 } |
| 144 | 156 |
| 145 void SyncFileSystemBackend::SetLocalFileChangeTracker( | 157 void SyncFileSystemBackend::SetLocalFileChangeTracker( |
| 146 scoped_ptr<LocalFileChangeTracker> tracker) { | 158 scoped_ptr<LocalFileChangeTracker> tracker) { |
| 147 DCHECK(!change_tracker_); | 159 DCHECK(!change_tracker_); |
| 148 DCHECK(tracker); | 160 DCHECK(tracker); |
| 149 change_tracker_ = tracker.Pass(); | 161 change_tracker_ = tracker.Pass(); |
| 150 | 162 |
| 151 DCHECK(delegate_); | 163 fileapi::SandboxFileSystemBackendDelegate* delegate = GetDelegate(); |
| 152 delegate_->AddFileUpdateObserver( | 164 delegate->AddFileUpdateObserver( |
| 153 fileapi::kFileSystemTypeSyncable, | 165 fileapi::kFileSystemTypeSyncable, |
| 154 change_tracker_.get(), | 166 change_tracker_.get(), |
| 155 delegate_->file_task_runner()); | 167 delegate->file_task_runner()); |
| 156 delegate_->AddFileChangeObserver( | 168 delegate->AddFileChangeObserver( |
| 157 fileapi::kFileSystemTypeSyncable, | 169 fileapi::kFileSystemTypeSyncable, |
| 158 change_tracker_.get(), | 170 change_tracker_.get(), |
| 159 delegate_->file_task_runner()); | 171 delegate->file_task_runner()); |
| 160 } | 172 } |
| 161 | 173 |
| 162 void SyncFileSystemBackend::set_sync_context( | 174 void SyncFileSystemBackend::set_sync_context( |
| 163 LocalFileSyncContext* sync_context) { | 175 LocalFileSyncContext* sync_context) { |
| 164 DCHECK(!sync_context_); | 176 DCHECK(!sync_context_); |
| 165 sync_context_ = sync_context; | 177 sync_context_ = sync_context; |
| 166 } | 178 } |
| 167 | 179 |
| 180 fileapi::SandboxFileSystemBackendDelegate* | |
| 181 SyncFileSystemBackend::GetDelegate() const { | |
| 182 DCHECK(context_); | |
| 183 DCHECK(context_->sandbox_delegate()); | |
| 184 return context_->sandbox_delegate(); | |
| 185 } | |
| 186 | |
| 187 void SyncFileSystemBackend::InitializeSyncFileSystemService( | |
| 188 const GURL& origin_url, | |
| 189 const SyncStatusCallback& callback) { | |
| 190 // Repost to switch from IO thread to UI thread. | |
| 191 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 193 BrowserThread::PostTask( | |
| 194 BrowserThread::UI, FROM_HERE, | |
| 195 base::Bind(&SyncFileSystemBackend::InitializeSyncFileSystemService, | |
| 196 base::Unretained(this), origin_url, callback)); | |
| 197 return; | |
| 198 } | |
| 199 | |
| 200 DCHECK(profile_); | |
| 201 SyncFileSystemService* service = | |
| 202 SyncFileSystemServiceFactory::GetForProfile(profile_); | |
|
kinuko
2013/08/23 10:02:03
I'm not 100% sure at this point profile_ is always
nhiroki
2013/08/28 05:37:42
Right. |profile_| could be invalidated before usin
| |
| 203 DCHECK(service); | |
| 204 service->InitializeForApp(context_, origin_url, callback); | |
| 205 } | |
| 206 | |
| 207 void SyncFileSystemBackend::DidInitializeSyncFileSystemService( | |
| 208 const GURL& origin_url, | |
| 209 fileapi::FileSystemType type, | |
| 210 fileapi::OpenFileSystemMode mode, | |
| 211 const OpenFileSystemCallback& callback, | |
| 212 SyncStatusCode status) { | |
| 213 // Repost to switch from UI thread to IO thread. | |
| 214 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
| 215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 216 BrowserThread::PostTask( | |
| 217 BrowserThread::IO, FROM_HERE, | |
| 218 base::Bind(&SyncFileSystemBackend::DidInitializeSyncFileSystemService, | |
| 219 base::Unretained(this), | |
| 220 origin_url, type, mode, callback, status)); | |
| 221 return; | |
| 222 } | |
| 223 | |
| 224 if (status != sync_file_system::SYNC_STATUS_OK) { | |
| 225 callback.Run(GURL(), std::string(), | |
| 226 PlatformFileErrorToSyncStatusCode(status)); | |
| 227 return; | |
| 228 } | |
| 229 | |
| 230 GetDelegate()->OpenFileSystem(origin_url, type, mode, callback, | |
| 231 GetSyncableFileSystemRootURI(origin_url)); | |
| 232 } | |
| 233 | |
| 168 } // namespace sync_file_system | 234 } // namespace sync_file_system |
| OLD | NEW |