OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/arc/fileapi/arc_deferred_file_system_operation
_runner.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "base/optional.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "components/arc/arc_bridge_service.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "url/gurl.h" |
| 14 |
| 15 using content::BrowserThread; |
| 16 |
| 17 namespace arc { |
| 18 |
| 19 ArcDeferredFileSystemOperationRunner::ArcDeferredFileSystemOperationRunner( |
| 20 ArcBridgeService* bridge_service) |
| 21 : ArcDeferredFileSystemOperationRunner(bridge_service, true) {} |
| 22 |
| 23 ArcDeferredFileSystemOperationRunner::ArcDeferredFileSystemOperationRunner( |
| 24 ArcBridgeService* bridge_service, |
| 25 bool observe_events) |
| 26 : ArcFileSystemOperationRunner(bridge_service), |
| 27 observe_events_(observe_events), |
| 28 weak_ptr_factory_(this) { |
| 29 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 30 |
| 31 if (observe_events_) { |
| 32 ArcSessionManager::Get()->AddObserver(this); |
| 33 arc_bridge_service()->file_system()->AddObserver(this); |
| 34 OnStateChanged(); |
| 35 } |
| 36 } |
| 37 |
| 38 ArcDeferredFileSystemOperationRunner::~ArcDeferredFileSystemOperationRunner() { |
| 39 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 40 |
| 41 if (observe_events_) { |
| 42 ArcSessionManager::Get()->RemoveObserver(this); |
| 43 arc_bridge_service()->file_system()->RemoveObserver(this); |
| 44 } |
| 45 // On destruction, deferred operations are discarded. |
| 46 } |
| 47 |
| 48 void ArcDeferredFileSystemOperationRunner::GetFileSize( |
| 49 const GURL& url, |
| 50 const GetFileSizeCallback& callback) { |
| 51 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 52 if (should_defer_) { |
| 53 deferred_operations_.emplace_back( |
| 54 base::Bind(&ArcDeferredFileSystemOperationRunner::GetFileSize, |
| 55 weak_ptr_factory_.GetWeakPtr(), url, callback)); |
| 56 return; |
| 57 } |
| 58 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( |
| 59 arc_bridge_service()->file_system(), GetFileSize); |
| 60 if (!file_system_instance) { |
| 61 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 62 base::Bind(callback, -1)); |
| 63 return; |
| 64 } |
| 65 file_system_instance->GetFileSize(url.spec(), callback); |
| 66 } |
| 67 |
| 68 void ArcDeferredFileSystemOperationRunner::OpenFileToRead( |
| 69 const GURL& url, |
| 70 const OpenFileToReadCallback& callback) { |
| 71 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 72 if (should_defer_) { |
| 73 deferred_operations_.emplace_back( |
| 74 base::Bind(&ArcDeferredFileSystemOperationRunner::OpenFileToRead, |
| 75 weak_ptr_factory_.GetWeakPtr(), url, callback)); |
| 76 return; |
| 77 } |
| 78 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( |
| 79 arc_bridge_service()->file_system(), OpenFileToRead); |
| 80 if (!file_system_instance) { |
| 81 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 82 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle()))); |
| 83 return; |
| 84 } |
| 85 file_system_instance->OpenFileToRead(url.spec(), callback); |
| 86 } |
| 87 |
| 88 void ArcDeferredFileSystemOperationRunner::GetDocument( |
| 89 const std::string& authority, |
| 90 const std::string& document_id, |
| 91 const GetDocumentCallback& callback) { |
| 92 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 93 if (should_defer_) { |
| 94 deferred_operations_.emplace_back(base::Bind( |
| 95 &ArcDeferredFileSystemOperationRunner::GetDocument, |
| 96 weak_ptr_factory_.GetWeakPtr(), authority, document_id, callback)); |
| 97 return; |
| 98 } |
| 99 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( |
| 100 arc_bridge_service()->file_system(), GetDocument); |
| 101 if (!file_system_instance) { |
| 102 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 103 FROM_HERE, base::Bind(callback, base::Passed(mojom::DocumentPtr()))); |
| 104 return; |
| 105 } |
| 106 file_system_instance->GetDocument(authority, document_id, callback); |
| 107 } |
| 108 |
| 109 void ArcDeferredFileSystemOperationRunner::GetChildDocuments( |
| 110 const std::string& authority, |
| 111 const std::string& parent_document_id, |
| 112 const GetChildDocumentsCallback& callback) { |
| 113 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 114 if (should_defer_) { |
| 115 deferred_operations_.emplace_back( |
| 116 base::Bind(&ArcDeferredFileSystemOperationRunner::GetChildDocuments, |
| 117 weak_ptr_factory_.GetWeakPtr(), authority, |
| 118 parent_document_id, callback)); |
| 119 return; |
| 120 } |
| 121 auto* file_system_instance = ARC_GET_INSTANCE_FOR_METHOD( |
| 122 arc_bridge_service()->file_system(), GetChildDocuments); |
| 123 if (!file_system_instance) { |
| 124 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 125 FROM_HERE, base::Bind(callback, base::nullopt)); |
| 126 return; |
| 127 } |
| 128 file_system_instance->GetChildDocuments(authority, parent_document_id, |
| 129 callback); |
| 130 } |
| 131 |
| 132 void ArcDeferredFileSystemOperationRunner::OnArcOptInChanged(bool enabled) { |
| 133 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 134 OnStateChanged(); |
| 135 } |
| 136 |
| 137 void ArcDeferredFileSystemOperationRunner::OnInstanceReady() { |
| 138 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 139 OnStateChanged(); |
| 140 } |
| 141 |
| 142 void ArcDeferredFileSystemOperationRunner::OnInstanceClosed() { |
| 143 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 144 OnStateChanged(); |
| 145 } |
| 146 |
| 147 void ArcDeferredFileSystemOperationRunner::OnStateChanged() { |
| 148 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 149 SetShouldDefer(ArcSessionManager::Get()->IsArcEnabled() && |
| 150 !arc_bridge_service()->file_system()->has_instance()); |
| 151 } |
| 152 |
| 153 void ArcDeferredFileSystemOperationRunner::SetShouldDefer(bool should_defer) { |
| 154 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 155 |
| 156 should_defer_ = should_defer; |
| 157 |
| 158 if (should_defer_) |
| 159 return; |
| 160 |
| 161 // Run deferred operations. |
| 162 std::vector<base::Closure> deferred_operations; |
| 163 deferred_operations.swap(deferred_operations_); |
| 164 for (const base::Closure& operation : deferred_operations) { |
| 165 operation.Run(); |
| 166 } |
| 167 |
| 168 // No deferred operations should be left at this point. |
| 169 DCHECK(deferred_operations_.empty()); |
| 170 } |
| 171 |
| 172 } // namespace arc |
OLD | NEW |