Chromium Code Reviews| 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()); | |
|
tzik
2012/10/24 03:45:13
I think this is a bit too redundant.
kinuko
2012/10/24 09:39:56
I may prefer keeping this as we chatted offline.
| |
| 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 URLCallback& 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, base::Bind(on_syncable_callback, url)); | |
| 110 return; | |
| 111 } | |
| 112 DCHECK(url_syncable_callback_.is_null()); | |
| 113 url_waiting_sync_on_io_ = url; | |
| 114 url_syncable_callback_ = on_syncable_callback; | |
| 115 } | |
| 116 | |
| 67 base::WeakPtr<SyncableFileOperationRunner> | 117 base::WeakPtr<SyncableFileOperationRunner> |
| 68 LocalFileSyncContext::operation_runner() const { | 118 LocalFileSyncContext::operation_runner() const { |
| 69 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 119 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 70 if (operation_runner_.get()) | 120 if (operation_runner_.get()) |
| 71 return operation_runner_->AsWeakPtr(); | 121 return operation_runner_->AsWeakPtr(); |
| 72 return base::WeakPtr<SyncableFileOperationRunner>(); | 122 return base::WeakPtr<SyncableFileOperationRunner>(); |
| 73 } | 123 } |
| 74 | 124 |
| 75 LocalFileSyncStatus* LocalFileSyncContext::sync_status() const { | 125 LocalFileSyncStatus* LocalFileSyncContext::sync_status() const { |
| 76 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 126 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 77 return sync_status_.get(); | 127 return sync_status_.get(); |
| 78 } | 128 } |
| 79 | 129 |
| 130 void LocalFileSyncContext::OnSyncEnabled(const FileSystemURL& url) { | |
| 131 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | |
| 132 if (url_syncable_callback_.is_null() || | |
| 133 sync_status()->IsWriting(url_waiting_sync_on_io_)) | |
| 134 return; | |
| 135 // TODO(kinuko): may want to check how many pending tasks we have. | |
| 136 sync_status()->StartSyncing(url_waiting_sync_on_io_); | |
| 137 ui_task_runner_->PostTask(FROM_HERE, | |
| 138 base::Bind(url_syncable_callback_, | |
| 139 url_waiting_sync_on_io_)); | |
| 140 url_syncable_callback_.Reset(); | |
| 141 } | |
| 142 | |
| 143 void LocalFileSyncContext::OnWriteEnabled(const FileSystemURL& url) { | |
| 144 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | |
|
tzik
2012/10/24 03:45:13
could you add TODO comment?
kinuko
2012/10/24 09:39:56
I have no plan to add anything for now.. :) will a
| |
| 145 } | |
| 146 | |
| 80 LocalFileSyncContext::~LocalFileSyncContext() { | 147 LocalFileSyncContext::~LocalFileSyncContext() { |
| 81 } | 148 } |
| 82 | 149 |
| 83 void LocalFileSyncContext::ShutdownOnIOThread() { | 150 void LocalFileSyncContext::ShutdownOnIOThread() { |
| 84 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 151 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 85 operation_runner_.reset(); | 152 operation_runner_.reset(); |
| 86 sync_status_.reset(); | 153 sync_status_.reset(); |
| 87 } | 154 } |
| 88 | 155 |
| 89 void LocalFileSyncContext::InitializeFileSystemContextOnIOThread( | 156 void LocalFileSyncContext::InitializeFileSystemContextOnIOThread( |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 107 source_url, | 174 source_url, |
| 108 make_scoped_refptr(file_system_context))); | 175 make_scoped_refptr(file_system_context))); |
| 109 return; | 176 return; |
| 110 } | 177 } |
| 111 if (!operation_runner_.get()) { | 178 if (!operation_runner_.get()) { |
| 112 DCHECK(!sync_status_.get()); | 179 DCHECK(!sync_status_.get()); |
| 113 sync_status_.reset(new LocalFileSyncStatus); | 180 sync_status_.reset(new LocalFileSyncStatus); |
| 114 operation_runner_.reset(new SyncableFileOperationRunner( | 181 operation_runner_.reset(new SyncableFileOperationRunner( |
| 115 kMaxConcurrentSyncableOperation, | 182 kMaxConcurrentSyncableOperation, |
| 116 sync_status_.get())); | 183 sync_status_.get())); |
| 184 sync_status_->AddObserver(this); | |
| 117 } | 185 } |
| 118 file_system_context->set_sync_context(this); | 186 file_system_context->set_sync_context(this); |
| 119 DidInitialize(source_url, file_system_context, SYNC_STATUS_OK); | 187 DidInitialize(source_url, file_system_context, SYNC_STATUS_OK); |
| 120 } | 188 } |
| 121 | 189 |
| 122 SyncStatusCode LocalFileSyncContext::InitializeChangeTrackerOnFileThread( | 190 SyncStatusCode LocalFileSyncContext::InitializeChangeTrackerOnFileThread( |
| 123 scoped_ptr<LocalFileChangeTracker>* tracker_ptr, | 191 scoped_ptr<LocalFileChangeTracker>* tracker_ptr, |
| 124 FileSystemContext* file_system_context) { | 192 FileSystemContext* file_system_context) { |
| 125 DCHECK(file_system_context); | 193 DCHECK(file_system_context); |
| 126 DCHECK(tracker_ptr); | 194 DCHECK(tracker_ptr); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 | 236 |
| 169 StatusCallbackQueue& callback_queue = | 237 StatusCallbackQueue& callback_queue = |
| 170 pending_initialize_callbacks_[file_system_context]; | 238 pending_initialize_callbacks_[file_system_context]; |
| 171 for (StatusCallbackQueue::iterator iter = callback_queue.begin(); | 239 for (StatusCallbackQueue::iterator iter = callback_queue.begin(); |
| 172 iter != callback_queue.end(); ++iter) { | 240 iter != callback_queue.end(); ++iter) { |
| 173 ui_task_runner_->PostTask(FROM_HERE, base::Bind(*iter, status)); | 241 ui_task_runner_->PostTask(FROM_HERE, base::Bind(*iter, status)); |
| 174 } | 242 } |
| 175 pending_initialize_callbacks_.erase(file_system_context); | 243 pending_initialize_callbacks_.erase(file_system_context); |
| 176 } | 244 } |
| 177 | 245 |
| 246 void LocalFileSyncContext::DidDisabledWritesForPrepareForSync( | |
| 247 const FileSystemURL& url, | |
| 248 const ChangeListCallback& callback) { | |
| 249 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
| 250 if (shutdown_on_ui_) { | |
| 251 callback.Run(SYNC_STATUS_ABORT, FileChangeList()); | |
| 252 return; | |
| 253 } | |
| 254 DCHECK(ContainsKey(origin_to_contexts_, url.origin())); | |
| 255 FileSystemContext* context = origin_to_contexts_[url.origin()]; | |
| 256 DCHECK(context); | |
| 257 DCHECK(context->change_tracker()); | |
| 258 | |
| 259 FileChangeList changes; | |
| 260 context->change_tracker()->GetChangesForURL(url, &changes); | |
| 261 callback.Run(SYNC_STATUS_OK, changes); | |
| 262 } | |
| 263 | |
| 178 } // namespace fileapi | 264 } // namespace fileapi |
| OLD | NEW |