OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/fileapi/syncable/local_file_sync_context.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/location.h" |
| 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/stl_util.h" |
| 12 #include "base/task_runner_util.h" |
| 13 #include "webkit/fileapi/file_system_context.h" |
| 14 #include "webkit/fileapi/file_system_task_runners.h" |
| 15 #include "webkit/fileapi/syncable/local_file_change_tracker.h" |
| 16 |
| 17 namespace fileapi { |
| 18 |
| 19 LocalFileSyncContext::LocalFileSyncContext( |
| 20 base::SingleThreadTaskRunner* ui_task_runner, |
| 21 base::SingleThreadTaskRunner* io_task_runner) |
| 22 : ui_task_runner_(ui_task_runner), |
| 23 io_task_runner_(io_task_runner), |
| 24 shutdown_(false) { |
| 25 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 26 } |
| 27 |
| 28 void LocalFileSyncContext::MaybeInitializeFileSystemContext( |
| 29 const GURL& source_url, |
| 30 FileSystemContext* file_system_context, |
| 31 const StatusCallback& callback) { |
| 32 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 33 if (ContainsKey(file_system_contexts_, file_system_context)) { |
| 34 DCHECK(!ContainsKey(origin_to_contexts_, source_url) || |
| 35 origin_to_contexts_[source_url] == file_system_context); |
| 36 origin_to_contexts_[source_url] = file_system_context; |
| 37 // The context has been already initialized. Just dispatch the callback |
| 38 // with SYNC_STATUS_OK. |
| 39 ui_task_runner_->PostTask(FROM_HERE, base::Bind(callback, SYNC_STATUS_OK)); |
| 40 return; |
| 41 } |
| 42 |
| 43 StatusCallbackQueue& callback_queue = |
| 44 pending_initialize_callbacks_[file_system_context]; |
| 45 callback_queue.push_back(callback); |
| 46 if (callback_queue.size() > 1) |
| 47 return; |
| 48 |
| 49 io_task_runner_->PostTask( |
| 50 FROM_HERE, |
| 51 base::Bind(&LocalFileSyncContext::InitializeFileSystemContextOnIOThread, |
| 52 this, source_url, make_scoped_refptr(file_system_context))); |
| 53 } |
| 54 |
| 55 void LocalFileSyncContext::ShutdownOnUIThread() { |
| 56 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 57 shutdown_ = true; |
| 58 io_task_runner_->PostTask( |
| 59 FROM_HERE, |
| 60 base::Bind(&LocalFileSyncContext::ShutdownOnIOThread, |
| 61 this)); |
| 62 } |
| 63 |
| 64 LocalFileSyncContext::~LocalFileSyncContext() { |
| 65 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 66 DCHECK(shutdown_); |
| 67 } |
| 68 |
| 69 void LocalFileSyncContext::ShutdownOnIOThread() { |
| 70 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 71 } |
| 72 |
| 73 void LocalFileSyncContext::InitializeFileSystemContextOnIOThread( |
| 74 const GURL& source_url, |
| 75 FileSystemContext* file_system_context) { |
| 76 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 77 DCHECK(file_system_context); |
| 78 if (!file_system_context->change_tracker()) { |
| 79 // Create and initialize LocalFileChangeTracker and call back this method |
| 80 // later again. |
| 81 scoped_ptr<LocalFileChangeTracker>* tracker_ptr( |
| 82 new scoped_ptr<LocalFileChangeTracker>); |
| 83 base::PostTaskAndReplyWithResult( |
| 84 file_system_context->task_runners()->file_task_runner(), |
| 85 FROM_HERE, |
| 86 base::Bind(&LocalFileSyncContext::InitializeChangeTrackerOnFileThread, |
| 87 this, tracker_ptr, |
| 88 make_scoped_refptr(file_system_context)), |
| 89 base::Bind(&LocalFileSyncContext::DidInitializeChangeTracker, this, |
| 90 base::Owned(tracker_ptr), |
| 91 source_url, |
| 92 make_scoped_refptr(file_system_context))); |
| 93 return; |
| 94 } |
| 95 file_system_context->set_sync_context(this); |
| 96 DidInitialize(source_url, file_system_context, SYNC_STATUS_OK); |
| 97 } |
| 98 |
| 99 SyncStatusCode LocalFileSyncContext::InitializeChangeTrackerOnFileThread( |
| 100 scoped_ptr<LocalFileChangeTracker>* tracker_ptr, |
| 101 FileSystemContext* file_system_context) { |
| 102 DCHECK(file_system_context); |
| 103 DCHECK(tracker_ptr); |
| 104 tracker_ptr->reset(new LocalFileChangeTracker( |
| 105 file_system_context->partition_path(), |
| 106 file_system_context->task_runners()->file_task_runner())); |
| 107 // TODO(kinuko,nhiroki): Uncomment this once http://crbug.com/154028 is fixed. |
| 108 // return change_tracker->Initialize(file_system_context); |
| 109 return SYNC_STATUS_OK; |
| 110 } |
| 111 |
| 112 void LocalFileSyncContext::DidInitializeChangeTracker( |
| 113 scoped_ptr<LocalFileChangeTracker>* tracker_ptr, |
| 114 const GURL& source_url, |
| 115 FileSystemContext* file_system_context, |
| 116 SyncStatusCode status) { |
| 117 DCHECK(file_system_context); |
| 118 if (status != SYNC_STATUS_OK) { |
| 119 DidInitialize(source_url, file_system_context, status); |
| 120 return; |
| 121 } |
| 122 file_system_context->SetLocalFileChangeTracker(tracker_ptr->Pass()); |
| 123 InitializeFileSystemContextOnIOThread(source_url, file_system_context); |
| 124 } |
| 125 |
| 126 void LocalFileSyncContext::DidInitialize( |
| 127 const GURL& source_url, |
| 128 FileSystemContext* file_system_context, |
| 129 SyncStatusCode status) { |
| 130 if (!ui_task_runner_->RunsTasksOnCurrentThread()) { |
| 131 ui_task_runner_->PostTask( |
| 132 FROM_HERE, |
| 133 base::Bind(&LocalFileSyncContext::DidInitialize, |
| 134 this, source_url, |
| 135 make_scoped_refptr(file_system_context), status)); |
| 136 return; |
| 137 } |
| 138 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 139 DCHECK(!ContainsKey(file_system_contexts_, file_system_context)); |
| 140 DCHECK(ContainsKey(pending_initialize_callbacks_, file_system_context)); |
| 141 DCHECK(file_system_context->change_tracker()); |
| 142 |
| 143 file_system_contexts_.insert(file_system_context); |
| 144 |
| 145 DCHECK(!ContainsKey(origin_to_contexts_, source_url)); |
| 146 origin_to_contexts_[source_url] = file_system_context; |
| 147 |
| 148 StatusCallbackQueue& callback_queue = |
| 149 pending_initialize_callbacks_[file_system_context]; |
| 150 for (StatusCallbackQueue::iterator iter = callback_queue.begin(); |
| 151 iter != callback_queue.end(); ++iter) { |
| 152 ui_task_runner_->PostTask(FROM_HERE, base::Bind(*iter, status)); |
| 153 } |
| 154 pending_initialize_callbacks_.erase(file_system_context); |
| 155 } |
| 156 |
| 157 } // namespace fileapi |
OLD | NEW |