| 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/chrome_notification_types.h" |
| 8 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h" | 9 #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" | 10 #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
" | 11 #include "chrome/browser/sync_file_system/local/syncable_file_system_operation.h
" |
| 12 #include "chrome/browser/sync_file_system/sync_file_system_service.h" |
| 13 #include "chrome/browser/sync_file_system/sync_file_system_service_factory.h" |
| 11 #include "chrome/browser/sync_file_system/syncable_file_system_util.h" | 14 #include "chrome/browser/sync_file_system/syncable_file_system_util.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/notification_service.h" |
| 12 #include "webkit/browser/fileapi/file_system_context.h" | 17 #include "webkit/browser/fileapi/file_system_context.h" |
| 13 #include "webkit/browser/fileapi/file_system_file_stream_reader.h" | 18 #include "webkit/browser/fileapi/file_system_file_stream_reader.h" |
| 14 #include "webkit/browser/fileapi/file_system_operation_impl.h" | 19 #include "webkit/browser/fileapi/file_system_operation_impl.h" |
| 15 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h" | 20 #include "webkit/browser/fileapi/sandbox_file_stream_writer.h" |
| 16 #include "webkit/browser/fileapi/sandbox_quota_observer.h" | 21 #include "webkit/browser/fileapi/sandbox_quota_observer.h" |
| 17 #include "webkit/common/fileapi/file_system_util.h" | 22 #include "webkit/common/fileapi/file_system_util.h" |
| 18 | 23 |
| 24 using content::BrowserThread; |
| 25 |
| 19 namespace sync_file_system { | 26 namespace sync_file_system { |
| 20 | 27 |
| 21 SyncFileSystemBackend::SyncFileSystemBackend() | 28 SyncFileSystemBackend::ProfileHolder::ProfileHolder(Profile* profile) |
| 22 : delegate_(NULL) { | 29 : profile_(profile) { |
| 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 31 registrar_.Add(this, |
| 32 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 33 content::NotificationService::AllSources()); |
| 34 } |
| 35 |
| 36 void SyncFileSystemBackend::ProfileHolder::Observe( |
| 37 int type, |
| 38 const content::NotificationSource& source, |
| 39 const content::NotificationDetails& details) { |
| 40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 41 switch (type) { |
| 42 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
| 43 Profile* profile = content::Source<Profile>(source).ptr(); |
| 44 if (profile_ == profile) |
| 45 profile_ = NULL; |
| 46 break; |
| 47 } |
| 48 } |
| 49 } |
| 50 |
| 51 Profile* SyncFileSystemBackend::ProfileHolder::GetProfile() { |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 53 return profile_; |
| 54 } |
| 55 |
| 56 SyncFileSystemBackend::SyncFileSystemBackend(Profile* profile) |
| 57 : context_(NULL), |
| 58 skip_initialize_syncfs_service_for_testing_(false) { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 60 profile_holder_.reset(new ProfileHolder(profile)); |
| 23 } | 61 } |
| 24 | 62 |
| 25 SyncFileSystemBackend::~SyncFileSystemBackend() { | 63 SyncFileSystemBackend::~SyncFileSystemBackend() { |
| 26 if (change_tracker_) { | 64 if (change_tracker_) { |
| 27 delegate_->file_task_runner()->DeleteSoon( | 65 GetDelegate()->file_task_runner()->DeleteSoon( |
| 28 FROM_HERE, change_tracker_.release()); | 66 FROM_HERE, change_tracker_.release()); |
| 29 } | 67 } |
| 68 |
| 69 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 70 BrowserThread::DeleteSoon( |
| 71 BrowserThread::UI, FROM_HERE, profile_holder_.release()); |
| 72 } |
| 30 } | 73 } |
| 31 | 74 |
| 75 // static |
| 76 SyncFileSystemBackend* SyncFileSystemBackend::CreateForTesting() { |
| 77 return new SyncFileSystemBackend; |
| 78 } |
| 79 |
| 32 bool SyncFileSystemBackend::CanHandleType( | 80 bool SyncFileSystemBackend::CanHandleType( |
| 33 fileapi::FileSystemType type) const { | 81 fileapi::FileSystemType type) const { |
| 34 return type == fileapi::kFileSystemTypeSyncable || | 82 return type == fileapi::kFileSystemTypeSyncable || |
| 35 type == fileapi::kFileSystemTypeSyncableForInternalSync; | 83 type == fileapi::kFileSystemTypeSyncableForInternalSync; |
| 36 } | 84 } |
| 37 | 85 |
| 38 void SyncFileSystemBackend::Initialize(fileapi::FileSystemContext* context) { | 86 void SyncFileSystemBackend::Initialize(fileapi::FileSystemContext* context) { |
| 39 DCHECK(context); | 87 DCHECK(context); |
| 40 DCHECK(!delegate_); | 88 DCHECK(!context_); |
| 41 delegate_ = context->sandbox_delegate(); | 89 context_ = context; |
| 42 | 90 |
| 43 delegate_->AddFileUpdateObserver( | 91 fileapi::SandboxFileSystemBackendDelegate* delegate = GetDelegate(); |
| 92 delegate->AddFileUpdateObserver( |
| 44 fileapi::kFileSystemTypeSyncable, | 93 fileapi::kFileSystemTypeSyncable, |
| 45 delegate_->quota_observer(), | 94 delegate->quota_observer(), |
| 46 delegate_->file_task_runner()); | 95 delegate->file_task_runner()); |
| 47 delegate_->AddFileUpdateObserver( | 96 delegate->AddFileUpdateObserver( |
| 48 fileapi::kFileSystemTypeSyncableForInternalSync, | 97 fileapi::kFileSystemTypeSyncableForInternalSync, |
| 49 delegate_->quota_observer(), | 98 delegate->quota_observer(), |
| 50 delegate_->file_task_runner()); | 99 delegate->file_task_runner()); |
| 51 } | 100 } |
| 52 | 101 |
| 53 void SyncFileSystemBackend::OpenFileSystem( | 102 void SyncFileSystemBackend::OpenFileSystem( |
| 54 const GURL& origin_url, | 103 const GURL& origin_url, |
| 55 fileapi::FileSystemType type, | 104 fileapi::FileSystemType type, |
| 56 fileapi::OpenFileSystemMode mode, | 105 fileapi::OpenFileSystemMode mode, |
| 57 const OpenFileSystemCallback& callback) { | 106 const OpenFileSystemCallback& callback) { |
| 58 DCHECK(CanHandleType(type)); | 107 DCHECK(CanHandleType(type)); |
| 59 DCHECK(delegate_); | 108 |
| 60 delegate_->OpenFileSystem(origin_url, type, mode, callback, | 109 if (skip_initialize_syncfs_service_for_testing_) { |
| 61 GetSyncableFileSystemRootURI(origin_url)); | 110 GetDelegate()->OpenFileSystem(origin_url, type, mode, callback, |
| 111 GetSyncableFileSystemRootURI(origin_url)); |
| 112 return; |
| 113 } |
| 114 |
| 115 SyncStatusCallback initialize_callback = |
| 116 base::Bind(&SyncFileSystemBackend::DidInitializeSyncFileSystemService, |
| 117 base::Unretained(this), origin_url, type, mode, callback); |
| 118 InitializeSyncFileSystemService(origin_url, initialize_callback); |
| 62 } | 119 } |
| 63 | 120 |
| 64 fileapi::FileSystemFileUtil* SyncFileSystemBackend::GetFileUtil( | 121 fileapi::FileSystemFileUtil* SyncFileSystemBackend::GetFileUtil( |
| 65 fileapi::FileSystemType type) { | 122 fileapi::FileSystemType type) { |
| 66 DCHECK(delegate_); | 123 return GetDelegate()->sync_file_util(); |
| 67 return delegate_->sync_file_util(); | |
| 68 } | 124 } |
| 69 | 125 |
| 70 fileapi::AsyncFileUtil* SyncFileSystemBackend::GetAsyncFileUtil( | 126 fileapi::AsyncFileUtil* SyncFileSystemBackend::GetAsyncFileUtil( |
| 71 fileapi::FileSystemType type) { | 127 fileapi::FileSystemType type) { |
| 72 DCHECK(delegate_); | 128 return GetDelegate()->file_util(); |
| 73 return delegate_->file_util(); | |
| 74 } | 129 } |
| 75 | 130 |
| 76 fileapi::CopyOrMoveFileValidatorFactory* | 131 fileapi::CopyOrMoveFileValidatorFactory* |
| 77 SyncFileSystemBackend::GetCopyOrMoveFileValidatorFactory( | 132 SyncFileSystemBackend::GetCopyOrMoveFileValidatorFactory( |
| 78 fileapi::FileSystemType type, | 133 fileapi::FileSystemType type, |
| 79 base::PlatformFileError* error_code) { | 134 base::PlatformFileError* error_code) { |
| 80 DCHECK(error_code); | 135 DCHECK(error_code); |
| 81 *error_code = base::PLATFORM_FILE_OK; | 136 *error_code = base::PLATFORM_FILE_OK; |
| 82 return NULL; | 137 return NULL; |
| 83 } | 138 } |
| 84 | 139 |
| 85 fileapi::FileSystemOperation* | 140 fileapi::FileSystemOperation* |
| 86 SyncFileSystemBackend::CreateFileSystemOperation( | 141 SyncFileSystemBackend::CreateFileSystemOperation( |
| 87 const fileapi::FileSystemURL& url, | 142 const fileapi::FileSystemURL& url, |
| 88 fileapi::FileSystemContext* context, | 143 fileapi::FileSystemContext* context, |
| 89 base::PlatformFileError* error_code) const { | 144 base::PlatformFileError* error_code) const { |
| 90 DCHECK(CanHandleType(url.type())); | 145 DCHECK(CanHandleType(url.type())); |
| 91 DCHECK(context); | 146 DCHECK(context); |
| 92 DCHECK(error_code); | 147 DCHECK(error_code); |
| 93 | 148 |
| 94 DCHECK(delegate_); | |
| 95 scoped_ptr<fileapi::FileSystemOperationContext> operation_context = | 149 scoped_ptr<fileapi::FileSystemOperationContext> operation_context = |
| 96 delegate_->CreateFileSystemOperationContext(url, context, error_code); | 150 GetDelegate()->CreateFileSystemOperationContext(url, context, error_code); |
| 97 if (!operation_context) | 151 if (!operation_context) |
| 98 return NULL; | 152 return NULL; |
| 99 | 153 |
| 100 if (url.type() == fileapi::kFileSystemTypeSyncableForInternalSync) { | 154 if (url.type() == fileapi::kFileSystemTypeSyncableForInternalSync) { |
| 101 return new fileapi::FileSystemOperationImpl( | 155 return new fileapi::FileSystemOperationImpl( |
| 102 url, context, operation_context.Pass()); | 156 url, context, operation_context.Pass()); |
| 103 } | 157 } |
| 104 | 158 |
| 105 return new SyncableFileSystemOperation( | 159 return new SyncableFileSystemOperation( |
| 106 url, context, operation_context.Pass()); | 160 url, context, operation_context.Pass()); |
| 107 } | 161 } |
| 108 | 162 |
| 109 scoped_ptr<webkit_blob::FileStreamReader> | 163 scoped_ptr<webkit_blob::FileStreamReader> |
| 110 SyncFileSystemBackend::CreateFileStreamReader( | 164 SyncFileSystemBackend::CreateFileStreamReader( |
| 111 const fileapi::FileSystemURL& url, | 165 const fileapi::FileSystemURL& url, |
| 112 int64 offset, | 166 int64 offset, |
| 113 const base::Time& expected_modification_time, | 167 const base::Time& expected_modification_time, |
| 114 fileapi::FileSystemContext* context) const { | 168 fileapi::FileSystemContext* context) const { |
| 115 DCHECK(CanHandleType(url.type())); | 169 DCHECK(CanHandleType(url.type())); |
| 116 DCHECK(delegate_); | 170 return GetDelegate()->CreateFileStreamReader( |
| 117 return delegate_->CreateFileStreamReader( | |
| 118 url, offset, expected_modification_time, context); | 171 url, offset, expected_modification_time, context); |
| 119 } | 172 } |
| 120 | 173 |
| 121 scoped_ptr<fileapi::FileStreamWriter> | 174 scoped_ptr<fileapi::FileStreamWriter> |
| 122 SyncFileSystemBackend::CreateFileStreamWriter( | 175 SyncFileSystemBackend::CreateFileStreamWriter( |
| 123 const fileapi::FileSystemURL& url, | 176 const fileapi::FileSystemURL& url, |
| 124 int64 offset, | 177 int64 offset, |
| 125 fileapi::FileSystemContext* context) const { | 178 fileapi::FileSystemContext* context) const { |
| 126 DCHECK(CanHandleType(url.type())); | 179 DCHECK(CanHandleType(url.type())); |
| 127 DCHECK(delegate_); | 180 return GetDelegate()->CreateFileStreamWriter( |
| 128 return delegate_->CreateFileStreamWriter( | |
| 129 url, offset, context, fileapi::kFileSystemTypeSyncableForInternalSync); | 181 url, offset, context, fileapi::kFileSystemTypeSyncableForInternalSync); |
| 130 } | 182 } |
| 131 | 183 |
| 132 fileapi::FileSystemQuotaUtil* SyncFileSystemBackend::GetQuotaUtil() { | 184 fileapi::FileSystemQuotaUtil* SyncFileSystemBackend::GetQuotaUtil() { |
| 133 return delegate_; | 185 return GetDelegate(); |
| 134 } | 186 } |
| 135 | 187 |
| 136 // static | 188 // static |
| 137 SyncFileSystemBackend* SyncFileSystemBackend::GetBackend( | 189 SyncFileSystemBackend* SyncFileSystemBackend::GetBackend( |
| 138 const fileapi::FileSystemContext* file_system_context) { | 190 const fileapi::FileSystemContext* file_system_context) { |
| 139 DCHECK(file_system_context); | 191 DCHECK(file_system_context); |
| 140 return static_cast<SyncFileSystemBackend*>( | 192 return static_cast<SyncFileSystemBackend*>( |
| 141 file_system_context->GetFileSystemBackend( | 193 file_system_context->GetFileSystemBackend( |
| 142 fileapi::kFileSystemTypeSyncable)); | 194 fileapi::kFileSystemTypeSyncable)); |
| 143 } | 195 } |
| 144 | 196 |
| 145 void SyncFileSystemBackend::SetLocalFileChangeTracker( | 197 void SyncFileSystemBackend::SetLocalFileChangeTracker( |
| 146 scoped_ptr<LocalFileChangeTracker> tracker) { | 198 scoped_ptr<LocalFileChangeTracker> tracker) { |
| 147 DCHECK(!change_tracker_); | 199 DCHECK(!change_tracker_); |
| 148 DCHECK(tracker); | 200 DCHECK(tracker); |
| 149 change_tracker_ = tracker.Pass(); | 201 change_tracker_ = tracker.Pass(); |
| 150 | 202 |
| 151 DCHECK(delegate_); | 203 fileapi::SandboxFileSystemBackendDelegate* delegate = GetDelegate(); |
| 152 delegate_->AddFileUpdateObserver( | 204 delegate->AddFileUpdateObserver( |
| 153 fileapi::kFileSystemTypeSyncable, | 205 fileapi::kFileSystemTypeSyncable, |
| 154 change_tracker_.get(), | 206 change_tracker_.get(), |
| 155 delegate_->file_task_runner()); | 207 delegate->file_task_runner()); |
| 156 delegate_->AddFileChangeObserver( | 208 delegate->AddFileChangeObserver( |
| 157 fileapi::kFileSystemTypeSyncable, | 209 fileapi::kFileSystemTypeSyncable, |
| 158 change_tracker_.get(), | 210 change_tracker_.get(), |
| 159 delegate_->file_task_runner()); | 211 delegate->file_task_runner()); |
| 160 } | 212 } |
| 161 | 213 |
| 162 void SyncFileSystemBackend::set_sync_context( | 214 void SyncFileSystemBackend::set_sync_context( |
| 163 LocalFileSyncContext* sync_context) { | 215 LocalFileSyncContext* sync_context) { |
| 164 DCHECK(!sync_context_); | 216 DCHECK(!sync_context_); |
| 165 sync_context_ = sync_context; | 217 sync_context_ = sync_context; |
| 166 } | 218 } |
| 167 | 219 |
| 220 SyncFileSystemBackend::SyncFileSystemBackend() |
| 221 : context_(NULL), |
| 222 skip_initialize_syncfs_service_for_testing_(true) { |
| 223 } |
| 224 |
| 225 fileapi::SandboxFileSystemBackendDelegate* |
| 226 SyncFileSystemBackend::GetDelegate() const { |
| 227 DCHECK(context_); |
| 228 DCHECK(context_->sandbox_delegate()); |
| 229 return context_->sandbox_delegate(); |
| 230 } |
| 231 |
| 232 void SyncFileSystemBackend::InitializeSyncFileSystemService( |
| 233 const GURL& origin_url, |
| 234 const SyncStatusCallback& callback) { |
| 235 // Repost to switch from IO thread to UI thread. |
| 236 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 238 BrowserThread::PostTask( |
| 239 BrowserThread::UI, FROM_HERE, |
| 240 base::Bind(&SyncFileSystemBackend::InitializeSyncFileSystemService, |
| 241 base::Unretained(this), origin_url, callback)); |
| 242 return; |
| 243 } |
| 244 |
| 245 if (!profile_holder_->GetProfile()) { |
| 246 // Profile was destroyed. |
| 247 callback.Run(SYNC_FILE_ERROR_FAILED); |
| 248 return; |
| 249 } |
| 250 |
| 251 SyncFileSystemService* service = SyncFileSystemServiceFactory::GetForProfile( |
| 252 profile_holder_->GetProfile()); |
| 253 DCHECK(service); |
| 254 service->InitializeForApp(context_, origin_url, callback); |
| 255 } |
| 256 |
| 257 void SyncFileSystemBackend::DidInitializeSyncFileSystemService( |
| 258 const GURL& origin_url, |
| 259 fileapi::FileSystemType type, |
| 260 fileapi::OpenFileSystemMode mode, |
| 261 const OpenFileSystemCallback& callback, |
| 262 SyncStatusCode status) { |
| 263 // Repost to switch from UI thread to IO thread. |
| 264 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 266 BrowserThread::PostTask( |
| 267 BrowserThread::IO, FROM_HERE, |
| 268 base::Bind(&SyncFileSystemBackend::DidInitializeSyncFileSystemService, |
| 269 base::Unretained(this), |
| 270 origin_url, type, mode, callback, status)); |
| 271 return; |
| 272 } |
| 273 |
| 274 if (status != sync_file_system::SYNC_STATUS_OK) { |
| 275 callback.Run(GURL(), std::string(), |
| 276 SyncStatusCodeToPlatformFileError(status)); |
| 277 return; |
| 278 } |
| 279 |
| 280 GetDelegate()->OpenFileSystem(origin_url, type, mode, callback, |
| 281 GetSyncableFileSystemRootURI(origin_url)); |
| 282 } |
| 283 |
| 168 } // namespace sync_file_system | 284 } // namespace sync_file_system |
| OLD | NEW |