Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.h " | 5 #include "chrome/browser/chromeos/arc/fileapi/arc_file_system_operation_runner.h " |
| 6 | 6 |
| 7 #include <utility> | |
| 8 | |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/location.h" | 10 #include "base/location.h" |
| 9 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 10 #include "base/optional.h" | 12 #include "base/optional.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "components/arc/arc_bridge_service.h" | 14 #include "components/arc/arc_bridge_service.h" |
| 13 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 14 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 15 | 17 |
| 16 using content::BrowserThread; | 18 using content::BrowserThread; |
| 17 | 19 |
| 18 namespace arc { | 20 namespace arc { |
| 19 | 21 |
| 22 namespace { | |
| 23 | |
| 24 ArcFileSystemOperationRunner::ChangeType FromMojoChangeType( | |
| 25 mojom::ChangeType type) { | |
| 26 switch (type) { | |
| 27 case mojom::ChangeType::CHANGED: | |
| 28 return ArcFileSystemOperationRunner::ChangeType::CHANGED; | |
| 29 case mojom::ChangeType::DELETED: | |
| 30 return ArcFileSystemOperationRunner::ChangeType::DELETED; | |
| 31 default: | |
| 32 NOTREACHED(); | |
|
hidehiko
2017/02/23 11:55:28
Could you move this block after switch so that (so
Shuhei Takahashi
2017/02/24 06:10:39
Done.
| |
| 33 return ArcFileSystemOperationRunner::ChangeType::CHANGED; | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 20 // static | 39 // static |
| 21 const char ArcFileSystemOperationRunner::kArcServiceName[] = | 40 const char ArcFileSystemOperationRunner::kArcServiceName[] = |
| 22 "arc::ArcFileSystemOperationRunner"; | 41 "arc::ArcFileSystemOperationRunner"; |
| 23 | 42 |
| 24 // static | 43 // static |
| 25 std::unique_ptr<ArcFileSystemOperationRunner> | 44 std::unique_ptr<ArcFileSystemOperationRunner> |
| 26 ArcFileSystemOperationRunner::CreateForTesting( | 45 ArcFileSystemOperationRunner::CreateForTesting( |
| 27 ArcBridgeService* bridge_service) { | 46 ArcBridgeService* bridge_service) { |
| 28 // We can't use base::MakeUnique() here because we are calling a private | 47 // We can't use base::MakeUnique() here because we are calling a private |
| 29 // constructor. | 48 // constructor. |
| 30 return base::WrapUnique<ArcFileSystemOperationRunner>( | 49 return base::WrapUnique<ArcFileSystemOperationRunner>( |
| 31 new ArcFileSystemOperationRunner(bridge_service, false)); | 50 new ArcFileSystemOperationRunner(bridge_service, false)); |
| 32 } | 51 } |
| 33 | 52 |
| 34 ArcFileSystemOperationRunner::ArcFileSystemOperationRunner( | 53 ArcFileSystemOperationRunner::ArcFileSystemOperationRunner( |
| 35 ArcBridgeService* bridge_service) | 54 ArcBridgeService* bridge_service) |
| 36 : ArcFileSystemOperationRunner(bridge_service, true) {} | 55 : ArcFileSystemOperationRunner(bridge_service, true) {} |
| 37 | 56 |
| 38 ArcFileSystemOperationRunner::ArcFileSystemOperationRunner( | 57 ArcFileSystemOperationRunner::ArcFileSystemOperationRunner( |
| 39 ArcBridgeService* bridge_service, | 58 ArcBridgeService* bridge_service, |
| 40 bool observe_events) | 59 bool set_should_defer_by_events) |
| 41 : ArcService(bridge_service), | 60 : ArcService(bridge_service), |
| 42 observe_events_(observe_events), | 61 set_should_defer_by_events_(set_should_defer_by_events), |
| 62 binding_(this), | |
| 43 weak_ptr_factory_(this) { | 63 weak_ptr_factory_(this) { |
| 44 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 64 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 45 | 65 |
| 46 if (observe_events_) { | 66 // We need to observe FileSystemInstance even in unit tests to call Init(). |
| 67 arc_bridge_service()->file_system()->AddObserver(this); | |
| 68 // ArcSessionManager may not exist in unit tests. | |
|
hidehiko
2017/02/23 11:55:28
auto* arc_session_manager = ArcSessionManager::Get
Shuhei Takahashi
2017/02/24 06:10:39
Sounds good, done.
| |
| 69 if (set_should_defer_by_events_) | |
| 47 ArcSessionManager::Get()->AddObserver(this); | 70 ArcSessionManager::Get()->AddObserver(this); |
| 48 arc_bridge_service()->file_system()->AddObserver(this); | 71 |
| 49 OnStateChanged(); | 72 OnStateChanged(); |
| 50 } | |
| 51 } | 73 } |
| 52 | 74 |
| 53 ArcFileSystemOperationRunner::~ArcFileSystemOperationRunner() { | 75 ArcFileSystemOperationRunner::~ArcFileSystemOperationRunner() { |
| 54 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 76 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 55 | 77 |
| 56 if (observe_events_) { | 78 if (set_should_defer_by_events_) |
| 57 ArcSessionManager::Get()->RemoveObserver(this); | 79 ArcSessionManager::Get()->RemoveObserver(this); |
| 58 arc_bridge_service()->file_system()->RemoveObserver(this); | 80 arc_bridge_service()->file_system()->RemoveObserver(this); |
| 59 } | |
| 60 // On destruction, deferred operations are discarded. | 81 // On destruction, deferred operations are discarded. |
| 61 } | 82 } |
| 62 | 83 |
| 63 void ArcFileSystemOperationRunner::GetFileSize( | 84 void ArcFileSystemOperationRunner::GetFileSize( |
| 64 const GURL& url, | 85 const GURL& url, |
| 65 const GetFileSizeCallback& callback) { | 86 const GetFileSizeCallback& callback) { |
| 66 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 87 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 67 if (should_defer_) { | 88 if (should_defer_) { |
| 68 deferred_operations_.emplace_back( | 89 deferred_operations_.emplace_back( |
| 69 base::Bind(&ArcFileSystemOperationRunner::GetFileSize, | 90 base::Bind(&ArcFileSystemOperationRunner::GetFileSize, |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 arc_bridge_service()->file_system(), GetChildDocuments); | 158 arc_bridge_service()->file_system(), GetChildDocuments); |
| 138 if (!file_system_instance) { | 159 if (!file_system_instance) { |
| 139 base::ThreadTaskRunnerHandle::Get()->PostTask( | 160 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 140 FROM_HERE, base::Bind(callback, base::nullopt)); | 161 FROM_HERE, base::Bind(callback, base::nullopt)); |
| 141 return; | 162 return; |
| 142 } | 163 } |
| 143 file_system_instance->GetChildDocuments(authority, parent_document_id, | 164 file_system_instance->GetChildDocuments(authority, parent_document_id, |
| 144 callback); | 165 callback); |
| 145 } | 166 } |
| 146 | 167 |
| 168 void ArcFileSystemOperationRunner::AddWatcher( | |
| 169 const std::string& authority, | |
| 170 const std::string& document_id, | |
| 171 const WatcherCallback& watcher_callback, | |
| 172 const AddWatcherCallback& callback) { | |
| 173 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 174 if (should_defer_) { | |
| 175 deferred_operations_.emplace_back( | |
| 176 base::Bind(&ArcFileSystemOperationRunner::AddWatcher, | |
| 177 weak_ptr_factory_.GetWeakPtr(), authority, document_id, | |
| 178 watcher_callback, callback)); | |
| 179 return; | |
| 180 } | |
| 181 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( | |
| 182 arc_bridge_service()->file_system(), AddWatcher); | |
| 183 if (!file_system_instance) { | |
| 184 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 185 base::Bind(callback, -1)); | |
| 186 return; | |
| 187 } | |
| 188 file_system_instance->AddWatcher( | |
| 189 authority, document_id, | |
| 190 base::Bind(&ArcFileSystemOperationRunner::AddWatcherDone, | |
| 191 weak_ptr_factory_.GetWeakPtr(), watcher_callback, callback)); | |
| 192 } | |
| 193 | |
| 194 void ArcFileSystemOperationRunner::RemoveWatcher( | |
| 195 int64_t watcher_id, | |
| 196 const RemoveWatcherCallback& callback) { | |
| 197 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 198 // RemoveWatcher() is never deferred since watchers do not persist across | |
| 199 // container reboots. | |
| 200 if (should_defer_) { | |
| 201 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 202 base::Bind(callback, false)); | |
| 203 return; | |
| 204 } | |
| 205 | |
| 206 // Unregister from |watcher_callbacks_| before calling the remote method. | |
| 207 auto iter = watcher_callbacks_.find(watcher_id); | |
| 208 if (iter == watcher_callbacks_.end()) { | |
| 209 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 210 base::Bind(callback, false)); | |
| 211 return; | |
| 212 } | |
| 213 watcher_callbacks_.erase(iter); | |
|
hidehiko
2017/02/23 11:55:28
This hides the OnDocumentChanged event between Rem
Shuhei Takahashi
2017/02/24 06:10:39
On 2017/02/23 11:55:28, hidehiko wrote:
> This hid
| |
| 214 | |
| 215 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( | |
| 216 arc_bridge_service()->file_system(), AddWatcher); | |
| 217 if (!file_system_instance) { | |
| 218 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 219 base::Bind(callback, false)); | |
| 220 return; | |
| 221 } | |
| 222 file_system_instance->RemoveWatcher(watcher_id, callback); | |
| 223 } | |
| 224 | |
| 225 void ArcFileSystemOperationRunner::OnDocumentChanged(int64_t watcher_id, | |
| 226 mojom::ChangeType type) { | |
| 227 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 228 auto iter = watcher_callbacks_.find(watcher_id); | |
| 229 if (iter == watcher_callbacks_.end()) { | |
| 230 return; | |
|
hidehiko
2017/02/23 11:55:28
Could you comment when this happens?
Shuhei Takahashi
2017/02/24 06:10:39
Done.
| |
| 231 } | |
| 232 WatcherCallback watcher_callback = iter->second; | |
| 233 if (type == mojom::ChangeType::DELETED) { | |
|
hidehiko
2017/02/23 11:55:28
Could you elide brace of one line if stmt, for con
Shuhei Takahashi
2017/02/24 06:10:39
Done.
| |
| 234 watcher_callbacks_.erase(watcher_id); | |
| 235 } | |
| 236 watcher_callback.Run(FromMojoChangeType(type)); | |
| 237 } | |
| 238 | |
| 147 void ArcFileSystemOperationRunner::OnArcPlayStoreEnabledChanged(bool enabled) { | 239 void ArcFileSystemOperationRunner::OnArcPlayStoreEnabledChanged(bool enabled) { |
| 148 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 240 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 149 OnStateChanged(); | 241 OnStateChanged(); |
| 150 } | 242 } |
| 151 | 243 |
| 152 void ArcFileSystemOperationRunner::OnInstanceReady() { | 244 void ArcFileSystemOperationRunner::OnInstanceReady() { |
| 153 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 245 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 246 auto* file_system_instance = | |
| 247 ARC_GET_INSTANCE_FOR_METHOD(arc_bridge_service()->file_system(), Init); | |
| 248 if (file_system_instance) { | |
|
hidehiko
2017/02/23 11:55:28
ditto
Shuhei Takahashi
2017/02/24 06:10:39
Done.
| |
| 249 file_system_instance->Init(binding_.CreateInterfacePtrAndBind()); | |
| 250 } | |
| 154 OnStateChanged(); | 251 OnStateChanged(); |
| 155 } | 252 } |
| 156 | 253 |
| 157 void ArcFileSystemOperationRunner::OnInstanceClosed() { | 254 void ArcFileSystemOperationRunner::OnInstanceClosed() { |
| 158 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 255 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 159 OnStateChanged(); | 256 OnStateChanged(); |
| 160 } | 257 } |
| 161 | 258 |
| 259 void ArcFileSystemOperationRunner::AddWatcherDone( | |
| 260 const WatcherCallback& watcher_callback, | |
| 261 const AddWatcherCallback& callback, | |
| 262 int64_t watcher_id) { | |
| 263 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 264 if (watcher_id < 0) { | |
| 265 callback.Run(-1); | |
| 266 return; | |
| 267 } | |
| 268 watcher_callbacks_.insert(std::make_pair(watcher_id, watcher_callback)); | |
|
hidehiko
2017/02/23 11:55:28
Optional: Just in case how about
DCHECK_EQ(watch
Shuhei Takahashi
2017/02/24 06:10:39
Done.
| |
| 269 callback.Run(watcher_id); | |
| 270 } | |
| 271 | |
| 162 void ArcFileSystemOperationRunner::OnStateChanged() { | 272 void ArcFileSystemOperationRunner::OnStateChanged() { |
| 163 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 273 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 164 SetShouldDefer(ArcSessionManager::Get()->IsArcPlayStoreEnabled() && | 274 if (set_should_defer_by_events_) { |
| 165 !arc_bridge_service()->file_system()->has_instance()); | 275 SetShouldDefer(ArcSessionManager::Get()->IsArcPlayStoreEnabled() && |
| 276 !arc_bridge_service()->file_system()->has_instance()); | |
| 277 } | |
| 166 } | 278 } |
| 167 | 279 |
| 168 void ArcFileSystemOperationRunner::SetShouldDefer(bool should_defer) { | 280 void ArcFileSystemOperationRunner::SetShouldDefer(bool should_defer) { |
| 169 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 281 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 170 | 282 |
| 171 should_defer_ = should_defer; | 283 should_defer_ = should_defer; |
| 172 | 284 |
| 173 if (should_defer_) | 285 if (should_defer_) { |
| 286 watcher_callbacks_.clear(); | |
|
hidehiko
2017/02/23 11:55:28
Could you comment why this is the timing for watch
Shuhei Takahashi
2017/02/24 06:10:39
Hm, I thought it's better to clear it because |sho
hidehiko
2017/02/24 16:16:17
Sorry, I'm confused. AFAIK, even if the container
hidehiko
2017/02/25 14:41:30
Oh, one thing I was wrong here. You do not actuall
Shuhei Takahashi
2017/03/01 09:10:08
Thanks for explanation. I had a misunderstanding t
| |
| 174 return; | 287 return; |
| 288 } | |
| 175 | 289 |
| 176 // Run deferred operations. | 290 // Run deferred operations. |
| 177 std::vector<base::Closure> deferred_operations; | 291 std::vector<base::Closure> deferred_operations; |
| 178 deferred_operations.swap(deferred_operations_); | 292 deferred_operations.swap(deferred_operations_); |
| 179 for (const base::Closure& operation : deferred_operations) { | 293 for (const base::Closure& operation : deferred_operations) { |
| 180 operation.Run(); | 294 operation.Run(); |
| 181 } | 295 } |
| 182 | 296 |
| 183 // No deferred operations should be left at this point. | 297 // No deferred operations should be left at this point. |
| 184 DCHECK(deferred_operations_.empty()); | 298 DCHECK(deferred_operations_.empty()); |
| 185 } | 299 } |
| 186 | 300 |
| 187 } // namespace arc | 301 } // namespace arc |
| OLD | NEW |