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