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