| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/chromeos/arc/fileapi/arc_file_system_operation_runner_u
til.h" | 5 #include "chrome/browser/chromeos/arc/fileapi/arc_file_system_operation_runner_u
til.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "chrome/browser/chromeos/arc/fileapi/arc_file_system_operation_runner.h
" | |
| 11 #include "components/arc/arc_service_manager.h" | 10 #include "components/arc/arc_service_manager.h" |
| 12 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 13 #include "url/gurl.h" | 12 #include "url/gurl.h" |
| 14 | 13 |
| 15 using content::BrowserThread; | 14 using content::BrowserThread; |
| 16 | 15 |
| 17 namespace arc { | 16 namespace arc { |
| 18 | 17 |
| 18 ObserverIOThreadWrapper::ObserverIOThreadWrapper( |
| 19 ArcFileSystemOperationRunner::Observer* underlying_observer) |
| 20 : underlying_observer_(underlying_observer) {} |
| 21 |
| 22 ObserverIOThreadWrapper::~ObserverIOThreadWrapper() = default; |
| 23 |
| 24 void ObserverIOThreadWrapper::Disable() { |
| 25 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 26 disabled_ = true; |
| 27 } |
| 28 |
| 29 void ObserverIOThreadWrapper::OnWatchersCleared() { |
| 30 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 31 BrowserThread::PostTask( |
| 32 BrowserThread::IO, FROM_HERE, |
| 33 base::Bind(&ObserverIOThreadWrapper::OnWatchersClearedOnIOThread, this)); |
| 34 } |
| 35 |
| 36 void ObserverIOThreadWrapper::OnWatchersClearedOnIOThread() { |
| 37 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 38 if (disabled_) |
| 39 return; |
| 40 underlying_observer_->OnWatchersCleared(); |
| 41 } |
| 42 |
| 19 namespace file_system_operation_runner_util { | 43 namespace file_system_operation_runner_util { |
| 20 | 44 |
| 21 namespace { | 45 namespace { |
| 22 | 46 |
| 23 template <typename T> | 47 template <typename T> |
| 24 void PostToIOThread(const base::Callback<void(T)>& callback, T result) { | 48 void PostToIOThread(const base::Callback<void(T)>& callback, T result) { |
| 25 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 49 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 26 BrowserThread::PostTask( | 50 BrowserThread::PostTask( |
| 27 BrowserThread::IO, FROM_HERE, | 51 BrowserThread::IO, FROM_HERE, |
| 28 base::Bind(callback, base::Passed(std::move(result)))); | 52 base::Bind(callback, base::Passed(std::move(result)))); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 ArcServiceManager::GetGlobalService<ArcFileSystemOperationRunner>(); | 103 ArcServiceManager::GetGlobalService<ArcFileSystemOperationRunner>(); |
| 80 if (!runner) { | 104 if (!runner) { |
| 81 LOG(ERROR) << "ArcFileSystemOperationRunner unavailable. " | 105 LOG(ERROR) << "ArcFileSystemOperationRunner unavailable. " |
| 82 << "File system operations are dropped."; | 106 << "File system operations are dropped."; |
| 83 callback.Run(base::nullopt); | 107 callback.Run(base::nullopt); |
| 84 return; | 108 return; |
| 85 } | 109 } |
| 86 runner->GetChildDocuments(authority, parent_document_id, callback); | 110 runner->GetChildDocuments(authority, parent_document_id, callback); |
| 87 } | 111 } |
| 88 | 112 |
| 113 void AddWatcherOnUIThread(const std::string& authority, |
| 114 const std::string& document_id, |
| 115 const WatcherCallback& watcher_callback, |
| 116 const AddWatcherCallback& callback) { |
| 117 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 118 auto* runner = |
| 119 ArcServiceManager::GetGlobalService<ArcFileSystemOperationRunner>(); |
| 120 if (!runner) { |
| 121 LOG(ERROR) << "ArcFileSystemOperationRunner unavailable. " |
| 122 << "File system operations are dropped."; |
| 123 callback.Run(-1); |
| 124 return; |
| 125 } |
| 126 runner->AddWatcher(authority, document_id, watcher_callback, callback); |
| 127 } |
| 128 |
| 129 void RemoveWatcherOnUIThread(int64_t watcher_id, |
| 130 const RemoveWatcherCallback& callback) { |
| 131 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 132 auto* runner = |
| 133 ArcServiceManager::GetGlobalService<ArcFileSystemOperationRunner>(); |
| 134 if (!runner) { |
| 135 LOG(ERROR) << "ArcFileSystemOperationRunner unavailable. " |
| 136 << "File system operations are dropped."; |
| 137 callback.Run(false); |
| 138 return; |
| 139 } |
| 140 runner->RemoveWatcher(watcher_id, callback); |
| 141 } |
| 142 |
| 143 void AddObserverOnUIThread(scoped_refptr<ObserverIOThreadWrapper> observer) { |
| 144 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 145 auto* runner = |
| 146 ArcServiceManager::GetGlobalService<ArcFileSystemOperationRunner>(); |
| 147 if (!runner) { |
| 148 LOG(ERROR) << "ArcFileSystemOperationRunner unavailable. " |
| 149 << "File system operations are dropped."; |
| 150 return; |
| 151 } |
| 152 runner->AddObserver(observer.get()); |
| 153 } |
| 154 |
| 155 void RemoveObserverOnUIThread(scoped_refptr<ObserverIOThreadWrapper> observer) { |
| 156 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 157 auto* runner = |
| 158 ArcServiceManager::GetGlobalService<ArcFileSystemOperationRunner>(); |
| 159 if (!runner) { |
| 160 LOG(ERROR) << "ArcFileSystemOperationRunner unavailable. " |
| 161 << "File system operations are dropped."; |
| 162 return; |
| 163 } |
| 164 runner->RemoveObserver(observer.get()); |
| 165 } |
| 166 |
| 89 } // namespace | 167 } // namespace |
| 90 | 168 |
| 91 void GetFileSizeOnIOThread(const GURL& url, | 169 void GetFileSizeOnIOThread(const GURL& url, |
| 92 const GetFileSizeCallback& callback) { | 170 const GetFileSizeCallback& callback) { |
| 93 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 171 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 94 BrowserThread::PostTask( | 172 BrowserThread::PostTask( |
| 95 BrowserThread::UI, FROM_HERE, | 173 BrowserThread::UI, FROM_HERE, |
| 96 base::Bind(&GetFileSizeOnUIThread, url, | 174 base::Bind(&GetFileSizeOnUIThread, url, |
| 97 base::Bind(&PostToIOThread<int64_t>, callback))); | 175 base::Bind(&PostToIOThread<int64_t>, callback))); |
| 98 } | 176 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 122 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 200 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 123 BrowserThread::PostTask( | 201 BrowserThread::PostTask( |
| 124 BrowserThread::UI, FROM_HERE, | 202 BrowserThread::UI, FROM_HERE, |
| 125 base::Bind( | 203 base::Bind( |
| 126 &GetChildDocumentsOnUIThread, authority, parent_document_id, | 204 &GetChildDocumentsOnUIThread, authority, parent_document_id, |
| 127 base::Bind( | 205 base::Bind( |
| 128 &PostToIOThread<base::Optional<std::vector<mojom::DocumentPtr>>>, | 206 &PostToIOThread<base::Optional<std::vector<mojom::DocumentPtr>>>, |
| 129 callback))); | 207 callback))); |
| 130 } | 208 } |
| 131 | 209 |
| 210 void AddWatcherOnIOThread(const std::string& authority, |
| 211 const std::string& document_id, |
| 212 const WatcherCallback& watcher_callback, |
| 213 const AddWatcherCallback& callback) { |
| 214 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 215 BrowserThread::PostTask( |
| 216 BrowserThread::UI, FROM_HERE, |
| 217 base::Bind( |
| 218 &AddWatcherOnUIThread, authority, document_id, |
| 219 base::Bind(&PostToIOThread<ArcFileSystemOperationRunner::ChangeType>, |
| 220 watcher_callback), |
| 221 base::Bind(&PostToIOThread<int64_t>, callback))); |
| 222 } |
| 223 |
| 224 void RemoveWatcherOnIOThread(int64_t watcher_id, |
| 225 const RemoveWatcherCallback& callback) { |
| 226 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 227 BrowserThread::PostTask( |
| 228 BrowserThread::UI, FROM_HERE, |
| 229 base::Bind(&RemoveWatcherOnUIThread, watcher_id, |
| 230 base::Bind(&PostToIOThread<bool>, callback))); |
| 231 } |
| 232 |
| 233 void AddObserverOnIOThread(scoped_refptr<ObserverIOThreadWrapper> observer) { |
| 234 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 235 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 236 base::Bind(&AddObserverOnUIThread, observer)); |
| 237 } |
| 238 |
| 239 void RemoveObserverOnIOThread(scoped_refptr<ObserverIOThreadWrapper> observer) { |
| 240 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 241 // Disable the observer now to guarantee the underlying observer is never |
| 242 // called after this function returns. |
| 243 observer->Disable(); |
| 244 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 245 base::Bind(&RemoveObserverOnUIThread, observer)); |
| 246 } |
| 247 |
| 132 } // namespace file_system_operation_runner_util | 248 } // namespace file_system_operation_runner_util |
| 133 | 249 |
| 134 } // namespace arc | 250 } // namespace arc |
| OLD | NEW |