| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "webkit/fileapi/syncable/local_file_sync_context.h" | 5 #include "webkit/fileapi/syncable/local_file_sync_context.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | |
| 9 #include "base/location.h" | 8 #include "base/location.h" |
| 10 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 11 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 12 #include "base/task_runner_util.h" | 11 #include "base/task_runner_util.h" |
| 13 #include "webkit/fileapi/file_system_context.h" | 12 #include "webkit/fileapi/file_system_context.h" |
| 14 #include "webkit/fileapi/file_system_task_runners.h" | 13 #include "webkit/fileapi/file_system_task_runners.h" |
| 14 #include "webkit/fileapi/syncable/file_change.h" |
| 15 #include "webkit/fileapi/syncable/local_file_change_tracker.h" | 15 #include "webkit/fileapi/syncable/local_file_change_tracker.h" |
| 16 #include "webkit/fileapi/syncable/syncable_file_operation_runner.h" | 16 #include "webkit/fileapi/syncable/syncable_file_operation_runner.h" |
| 17 | 17 |
| 18 namespace fileapi { | 18 namespace fileapi { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 const int kMaxConcurrentSyncableOperation = 3; | 21 const int kMaxConcurrentSyncableOperation = 3; |
| 22 } // namespace | 22 } // namespace |
| 23 | 23 |
| 24 LocalFileSyncContext::LocalFileSyncContext( | 24 LocalFileSyncContext::LocalFileSyncContext( |
| 25 base::SingleThreadTaskRunner* ui_task_runner, | 25 base::SingleThreadTaskRunner* ui_task_runner, |
| 26 base::SingleThreadTaskRunner* io_task_runner) | 26 base::SingleThreadTaskRunner* io_task_runner) |
| 27 : ui_task_runner_(ui_task_runner), | 27 : ui_task_runner_(ui_task_runner), |
| 28 io_task_runner_(io_task_runner) { | 28 io_task_runner_(io_task_runner), |
| 29 shutdown_on_ui_(false) { |
| 29 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 30 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 30 } | 31 } |
| 31 | 32 |
| 32 void LocalFileSyncContext::MaybeInitializeFileSystemContext( | 33 void LocalFileSyncContext::MaybeInitializeFileSystemContext( |
| 33 const GURL& source_url, | 34 const GURL& source_url, |
| 34 FileSystemContext* file_system_context, | 35 FileSystemContext* file_system_context, |
| 35 const StatusCallback& callback) { | 36 const StatusCallback& callback) { |
| 36 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 37 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 37 if (ContainsKey(file_system_contexts_, file_system_context)) { | 38 if (ContainsKey(file_system_contexts_, file_system_context)) { |
| 38 DCHECK(!ContainsKey(origin_to_contexts_, source_url) || | 39 DCHECK(!ContainsKey(origin_to_contexts_, source_url) || |
| (...skipping 12 matching lines...) Expand all Loading... |
| 51 return; | 52 return; |
| 52 | 53 |
| 53 io_task_runner_->PostTask( | 54 io_task_runner_->PostTask( |
| 54 FROM_HERE, | 55 FROM_HERE, |
| 55 base::Bind(&LocalFileSyncContext::InitializeFileSystemContextOnIOThread, | 56 base::Bind(&LocalFileSyncContext::InitializeFileSystemContextOnIOThread, |
| 56 this, source_url, make_scoped_refptr(file_system_context))); | 57 this, source_url, make_scoped_refptr(file_system_context))); |
| 57 } | 58 } |
| 58 | 59 |
| 59 void LocalFileSyncContext::ShutdownOnUIThread() { | 60 void LocalFileSyncContext::ShutdownOnUIThread() { |
| 60 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 61 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 62 shutdown_on_ui_ = true; |
| 61 io_task_runner_->PostTask( | 63 io_task_runner_->PostTask( |
| 62 FROM_HERE, | 64 FROM_HERE, |
| 63 base::Bind(&LocalFileSyncContext::ShutdownOnIOThread, | 65 base::Bind(&LocalFileSyncContext::ShutdownOnIOThread, |
| 64 this)); | 66 this)); |
| 65 } | 67 } |
| 66 | 68 |
| 69 void LocalFileSyncContext::PrepareForSync( |
| 70 const FileSystemURL& url, |
| 71 const ChangeListCallback& callback) { |
| 72 // This is initially called on UI thread and to be relayed to IO thread. |
| 73 if (!io_task_runner_->RunsTasksOnCurrentThread()) { |
| 74 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 75 io_task_runner_->PostTask( |
| 76 FROM_HERE, |
| 77 base::Bind(&LocalFileSyncContext::PrepareForSync, this, url, callback)); |
| 78 return; |
| 79 } |
| 80 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 81 if (sync_status()->IsWriting(url)) { |
| 82 ui_task_runner_->PostTask( |
| 83 FROM_HERE, |
| 84 base::Bind(callback, SYNC_STATUS_FILE_BUSY, FileChangeList())); |
| 85 return; |
| 86 } |
| 87 sync_status()->StartSyncing(url); |
| 88 ui_task_runner_->PostTask( |
| 89 FROM_HERE, |
| 90 base::Bind(&LocalFileSyncContext::DidDisabledWritesForPrepareForSync, |
| 91 this, url, callback)); |
| 92 } |
| 93 |
| 94 void LocalFileSyncContext::RegisterURLForWaitingSync( |
| 95 const FileSystemURL& url, |
| 96 const base::Closure& on_syncable_callback) { |
| 97 // This is initially called on UI thread and to be relayed to IO thread. |
| 98 if (!io_task_runner_->RunsTasksOnCurrentThread()) { |
| 99 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 100 io_task_runner_->PostTask( |
| 101 FROM_HERE, |
| 102 base::Bind(&LocalFileSyncContext::RegisterURLForWaitingSync, |
| 103 this, url, on_syncable_callback)); |
| 104 return; |
| 105 } |
| 106 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 107 if (sync_status()->IsWritable(url)) { |
| 108 // No need to register; fire the callback now. |
| 109 ui_task_runner_->PostTask(FROM_HERE, on_syncable_callback); |
| 110 return; |
| 111 } |
| 112 url_waiting_sync_on_io_ = url; |
| 113 url_syncable_callback_ = on_syncable_callback; |
| 114 } |
| 115 |
| 67 base::WeakPtr<SyncableFileOperationRunner> | 116 base::WeakPtr<SyncableFileOperationRunner> |
| 68 LocalFileSyncContext::operation_runner() const { | 117 LocalFileSyncContext::operation_runner() const { |
| 69 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 118 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 70 if (operation_runner_.get()) | 119 if (operation_runner_.get()) |
| 71 return operation_runner_->AsWeakPtr(); | 120 return operation_runner_->AsWeakPtr(); |
| 72 return base::WeakPtr<SyncableFileOperationRunner>(); | 121 return base::WeakPtr<SyncableFileOperationRunner>(); |
| 73 } | 122 } |
| 74 | 123 |
| 75 LocalFileSyncStatus* LocalFileSyncContext::sync_status() const { | 124 LocalFileSyncStatus* LocalFileSyncContext::sync_status() const { |
| 76 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 125 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 77 return sync_status_.get(); | 126 return sync_status_.get(); |
| 78 } | 127 } |
| 79 | 128 |
| 129 void LocalFileSyncContext::OnSyncEnabled(const FileSystemURL& url) { |
| 130 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 131 if (url_syncable_callback_.is_null() || |
| 132 sync_status()->IsWriting(url_waiting_sync_on_io_)) |
| 133 return; |
| 134 // TODO(kinuko): may want to check how many pending tasks we have. |
| 135 sync_status()->StartSyncing(url_waiting_sync_on_io_); |
| 136 ui_task_runner_->PostTask(FROM_HERE, url_syncable_callback_); |
| 137 url_syncable_callback_.Reset(); |
| 138 } |
| 139 |
| 140 void LocalFileSyncContext::OnWriteEnabled(const FileSystemURL& url) { |
| 141 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 142 // Nothing to do for now. |
| 143 } |
| 144 |
| 80 LocalFileSyncContext::~LocalFileSyncContext() { | 145 LocalFileSyncContext::~LocalFileSyncContext() { |
| 81 } | 146 } |
| 82 | 147 |
| 83 void LocalFileSyncContext::ShutdownOnIOThread() { | 148 void LocalFileSyncContext::ShutdownOnIOThread() { |
| 84 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 149 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 85 operation_runner_.reset(); | 150 operation_runner_.reset(); |
| 86 sync_status_.reset(); | 151 sync_status_.reset(); |
| 87 } | 152 } |
| 88 | 153 |
| 89 void LocalFileSyncContext::InitializeFileSystemContextOnIOThread( | 154 void LocalFileSyncContext::InitializeFileSystemContextOnIOThread( |
| (...skipping 17 matching lines...) Expand all Loading... |
| 107 source_url, | 172 source_url, |
| 108 make_scoped_refptr(file_system_context))); | 173 make_scoped_refptr(file_system_context))); |
| 109 return; | 174 return; |
| 110 } | 175 } |
| 111 if (!operation_runner_.get()) { | 176 if (!operation_runner_.get()) { |
| 112 DCHECK(!sync_status_.get()); | 177 DCHECK(!sync_status_.get()); |
| 113 sync_status_.reset(new LocalFileSyncStatus); | 178 sync_status_.reset(new LocalFileSyncStatus); |
| 114 operation_runner_.reset(new SyncableFileOperationRunner( | 179 operation_runner_.reset(new SyncableFileOperationRunner( |
| 115 kMaxConcurrentSyncableOperation, | 180 kMaxConcurrentSyncableOperation, |
| 116 sync_status_.get())); | 181 sync_status_.get())); |
| 182 sync_status_->AddObserver(this); |
| 117 } | 183 } |
| 118 file_system_context->set_sync_context(this); | 184 file_system_context->set_sync_context(this); |
| 119 DidInitialize(source_url, file_system_context, SYNC_STATUS_OK); | 185 DidInitialize(source_url, file_system_context, SYNC_STATUS_OK); |
| 120 } | 186 } |
| 121 | 187 |
| 122 SyncStatusCode LocalFileSyncContext::InitializeChangeTrackerOnFileThread( | 188 SyncStatusCode LocalFileSyncContext::InitializeChangeTrackerOnFileThread( |
| 123 scoped_ptr<LocalFileChangeTracker>* tracker_ptr, | 189 scoped_ptr<LocalFileChangeTracker>* tracker_ptr, |
| 124 FileSystemContext* file_system_context) { | 190 FileSystemContext* file_system_context) { |
| 125 DCHECK(file_system_context); | 191 DCHECK(file_system_context); |
| 126 DCHECK(tracker_ptr); | 192 DCHECK(tracker_ptr); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 | 234 |
| 169 StatusCallbackQueue& callback_queue = | 235 StatusCallbackQueue& callback_queue = |
| 170 pending_initialize_callbacks_[file_system_context]; | 236 pending_initialize_callbacks_[file_system_context]; |
| 171 for (StatusCallbackQueue::iterator iter = callback_queue.begin(); | 237 for (StatusCallbackQueue::iterator iter = callback_queue.begin(); |
| 172 iter != callback_queue.end(); ++iter) { | 238 iter != callback_queue.end(); ++iter) { |
| 173 ui_task_runner_->PostTask(FROM_HERE, base::Bind(*iter, status)); | 239 ui_task_runner_->PostTask(FROM_HERE, base::Bind(*iter, status)); |
| 174 } | 240 } |
| 175 pending_initialize_callbacks_.erase(file_system_context); | 241 pending_initialize_callbacks_.erase(file_system_context); |
| 176 } | 242 } |
| 177 | 243 |
| 244 void LocalFileSyncContext::DidDisabledWritesForPrepareForSync( |
| 245 const FileSystemURL& url, |
| 246 const ChangeListCallback& callback) { |
| 247 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 248 if (shutdown_on_ui_) { |
| 249 callback.Run(SYNC_STATUS_ABORT, FileChangeList()); |
| 250 return; |
| 251 } |
| 252 DCHECK(ContainsKey(origin_to_contexts_, url.origin())); |
| 253 FileSystemContext* context = origin_to_contexts_[url.origin()]; |
| 254 DCHECK(context); |
| 255 DCHECK(context->change_tracker()); |
| 256 |
| 257 FileChangeList changes; |
| 258 context->change_tracker()->GetChangesForURL(url, &changes); |
| 259 callback.Run(SYNC_STATUS_OK, changes); |
| 260 } |
| 261 |
| 178 } // namespace fileapi | 262 } // namespace fileapi |
| OLD | NEW |