| 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_instance_util.h" | 5 #include "chrome/browser/chromeos/arc/fileapi/arc_file_system_instance_util.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/optional.h" | 11 #include "base/optional.h" |
| 12 #include "components/arc/arc_bridge_service.h" | 12 #include "components/arc/arc_bridge_service.h" |
| 13 #include "components/arc/arc_service_manager.h" | 13 #include "components/arc/arc_service_manager.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 16 | 16 |
| 17 #define GET_FILE_SYSTEM_INSTANCE(method_name) \ |
| 18 (arc::ArcServiceManager::Get() \ |
| 19 ? ARC_GET_INSTANCE_FOR_METHOD(arc::ArcServiceManager::Get() \ |
| 20 ->arc_bridge_service() \ |
| 21 ->file_system(), \ |
| 22 method_name) \ |
| 23 : nullptr) |
| 24 |
| 17 using content::BrowserThread; | 25 using content::BrowserThread; |
| 18 | 26 |
| 19 namespace arc { | 27 namespace arc { |
| 20 | 28 |
| 21 namespace file_system_instance_util { | 29 namespace file_system_instance_util { |
| 22 | 30 |
| 23 namespace { | 31 namespace { |
| 24 | 32 |
| 25 constexpr uint32_t kGetFileSizeVersion = 1; | |
| 26 constexpr uint32_t kOpenFileToReadVersion = 1; | |
| 27 constexpr uint32_t kGetDocumentVersion = 2; | |
| 28 constexpr uint32_t kGetChildDocumentsVersion = 2; | |
| 29 | |
| 30 // Returns FileSystemInstance for the given |min_version|, if found. | |
| 31 // Otherwise, nullptr. | |
| 32 mojom::FileSystemInstance* GetFileSystemInstance( | |
| 33 const std::string& method_name_for_logging, | |
| 34 uint32_t min_version) { | |
| 35 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 36 auto* arc_service_manager = arc::ArcServiceManager::Get(); | |
| 37 if (!arc_service_manager) { | |
| 38 LOG(ERROR) << "Failed to get ArcServiceManager."; | |
| 39 return nullptr; | |
| 40 } | |
| 41 // TODO(lhchavez): Stop calling GetInstanceForVersion() directly. | |
| 42 return arc_service_manager->arc_bridge_service() | |
| 43 ->file_system() | |
| 44 ->GetInstanceForVersion(min_version, method_name_for_logging.c_str()); | |
| 45 } | |
| 46 | |
| 47 template <typename T> | 33 template <typename T> |
| 48 void PostToIOThread(const base::Callback<void(T)>& callback, T result) { | 34 void PostToIOThread(const base::Callback<void(T)>& callback, T result) { |
| 49 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 35 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 50 BrowserThread::PostTask( | 36 BrowserThread::PostTask( |
| 51 BrowserThread::IO, FROM_HERE, | 37 BrowserThread::IO, FROM_HERE, |
| 52 base::Bind(callback, base::Passed(std::move(result)))); | 38 base::Bind(callback, base::Passed(std::move(result)))); |
| 53 } | 39 } |
| 54 | 40 |
| 55 void GetFileSizeOnUIThread(const GURL& arc_url, | 41 void GetFileSizeOnUIThread(const GURL& arc_url, |
| 56 const GetFileSizeCallback& callback) { | 42 const GetFileSizeCallback& callback) { |
| 57 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 43 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 58 auto* file_system_instance = | 44 auto* file_system_instance = GET_FILE_SYSTEM_INSTANCE(GetFileSize); |
| 59 GetFileSystemInstance("GetFileSize", kGetFileSizeVersion); | |
| 60 if (!file_system_instance) { | 45 if (!file_system_instance) { |
| 61 callback.Run(-1); | 46 callback.Run(-1); |
| 62 return; | 47 return; |
| 63 } | 48 } |
| 64 file_system_instance->GetFileSize(arc_url.spec(), callback); | 49 file_system_instance->GetFileSize(arc_url.spec(), callback); |
| 65 } | 50 } |
| 66 | 51 |
| 67 void OpenFileToReadOnUIThread(const GURL& arc_url, | 52 void OpenFileToReadOnUIThread(const GURL& arc_url, |
| 68 const OpenFileToReadCallback& callback) { | 53 const OpenFileToReadCallback& callback) { |
| 69 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 54 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 70 auto* file_system_instance = | 55 auto* file_system_instance = GET_FILE_SYSTEM_INSTANCE(OpenFileToRead); |
| 71 GetFileSystemInstance("OpenFileToRead", kOpenFileToReadVersion); | |
| 72 if (!file_system_instance) { | 56 if (!file_system_instance) { |
| 73 callback.Run(mojo::ScopedHandle()); | 57 callback.Run(mojo::ScopedHandle()); |
| 74 return; | 58 return; |
| 75 } | 59 } |
| 76 file_system_instance->OpenFileToRead(arc_url.spec(), callback); | 60 file_system_instance->OpenFileToRead(arc_url.spec(), callback); |
| 77 } | 61 } |
| 78 | 62 |
| 79 void GetDocumentOnUIThread(const std::string& authority, | 63 void GetDocumentOnUIThread(const std::string& authority, |
| 80 const std::string& document_id, | 64 const std::string& document_id, |
| 81 const GetDocumentCallback& callback) { | 65 const GetDocumentCallback& callback) { |
| 82 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 66 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 83 auto* file_system_instance = | 67 auto* file_system_instance = GET_FILE_SYSTEM_INSTANCE(GetDocument); |
| 84 GetFileSystemInstance("GetDocument", kGetDocumentVersion); | |
| 85 if (!file_system_instance) { | 68 if (!file_system_instance) { |
| 86 callback.Run(mojom::DocumentPtr()); | 69 callback.Run(mojom::DocumentPtr()); |
| 87 return; | 70 return; |
| 88 } | 71 } |
| 89 file_system_instance->GetDocument(authority, document_id, callback); | 72 file_system_instance->GetDocument(authority, document_id, callback); |
| 90 } | 73 } |
| 91 | 74 |
| 92 void GetChildDocumentsOnUIThread(const std::string& authority, | 75 void GetChildDocumentsOnUIThread(const std::string& authority, |
| 93 const std::string& parent_document_id, | 76 const std::string& parent_document_id, |
| 94 const GetChildDocumentsCallback& callback) { | 77 const GetChildDocumentsCallback& callback) { |
| 95 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 78 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 96 auto* file_system_instance = | 79 auto* file_system_instance = GET_FILE_SYSTEM_INSTANCE(GetChildDocuments); |
| 97 GetFileSystemInstance("GetChildDocuments", kGetChildDocumentsVersion); | |
| 98 if (!file_system_instance) { | 80 if (!file_system_instance) { |
| 99 callback.Run(base::nullopt); | 81 callback.Run(base::nullopt); |
| 100 return; | 82 return; |
| 101 } | 83 } |
| 102 file_system_instance->GetChildDocuments(authority, parent_document_id, | 84 file_system_instance->GetChildDocuments(authority, parent_document_id, |
| 103 callback); | 85 callback); |
| 104 } | 86 } |
| 105 | 87 |
| 106 } // namespace | 88 } // namespace |
| 107 | 89 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 base::Bind( | 124 base::Bind( |
| 143 &GetChildDocumentsOnUIThread, authority, parent_document_id, | 125 &GetChildDocumentsOnUIThread, authority, parent_document_id, |
| 144 base::Bind( | 126 base::Bind( |
| 145 &PostToIOThread<base::Optional<std::vector<mojom::DocumentPtr>>>, | 127 &PostToIOThread<base::Optional<std::vector<mojom::DocumentPtr>>>, |
| 146 callback))); | 128 callback))); |
| 147 } | 129 } |
| 148 | 130 |
| 149 } // namespace file_system_instance_util | 131 } // namespace file_system_instance_util |
| 150 | 132 |
| 151 } // namespace arc | 133 } // namespace arc |
| OLD | NEW |