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> |
| 9 #include <vector> |
8 | 10 |
| 11 #include "base/optional.h" |
9 #include "components/arc/arc_bridge_service.h" | 12 #include "components/arc/arc_bridge_service.h" |
10 #include "components/arc/arc_service_manager.h" | 13 #include "components/arc/arc_service_manager.h" |
11 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
12 #include "url/gurl.h" | 15 #include "url/gurl.h" |
13 | 16 |
| 17 using content::BrowserThread; |
| 18 |
14 namespace arc { | 19 namespace arc { |
15 | 20 |
16 namespace file_system_instance_util { | 21 namespace file_system_instance_util { |
17 | 22 |
18 namespace { | 23 namespace { |
19 | 24 |
20 constexpr uint32_t kGetFileSizeVersion = 1; | 25 constexpr uint32_t kGetFileSizeVersion = 1; |
21 constexpr uint32_t kOpenFileToReadVersion = 1; | 26 constexpr uint32_t kOpenFileToReadVersion = 1; |
| 27 constexpr uint32_t kGetDocumentVersion = 2; |
| 28 constexpr uint32_t kGetChildDocumentsVersion = 2; |
22 | 29 |
23 // Returns FileSystemInstance for the given |min_version|, if found. | 30 // Returns FileSystemInstance for the given |min_version|, if found. |
24 // Otherwise, nullptr. | 31 // Otherwise, nullptr. |
25 mojom::FileSystemInstance* GetFileSystemInstance( | 32 mojom::FileSystemInstance* GetFileSystemInstance( |
26 const std::string& method_name_for_logging, | 33 const std::string& method_name_for_logging, |
27 uint32_t min_version) { | 34 uint32_t min_version) { |
28 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 35 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
29 auto* arc_service_manager = arc::ArcServiceManager::Get(); | 36 auto* arc_service_manager = arc::ArcServiceManager::Get(); |
30 if (!arc_service_manager) { | 37 if (!arc_service_manager) { |
31 LOG(ERROR) << "Failed to get ArcServiceManager."; | 38 LOG(ERROR) << "Failed to get ArcServiceManager."; |
32 return nullptr; | 39 return nullptr; |
33 } | 40 } |
34 return arc_service_manager->arc_bridge_service() | 41 return arc_service_manager->arc_bridge_service() |
35 ->file_system() | 42 ->file_system() |
36 ->GetInstanceForMethod(method_name_for_logging, min_version); | 43 ->GetInstanceForMethod(method_name_for_logging, min_version); |
37 } | 44 } |
38 | 45 |
39 void OnGetFileSize(const GetFileSizeCallback& callback, int64_t size) { | 46 template <typename T> |
40 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 47 void PostToIOThread(const base::Callback<void(T)>& callback, T result) { |
41 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | 48 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
42 base::Bind(callback, size)); | 49 BrowserThread::PostTask( |
| 50 BrowserThread::IO, FROM_HERE, |
| 51 base::Bind(callback, base::Passed(std::move(result)))); |
43 } | 52 } |
44 | 53 |
45 void GetFileSizeOnUIThread(const GURL& arc_url, | 54 void GetFileSizeOnUIThread(const GURL& arc_url, |
46 const GetFileSizeCallback& callback) { | 55 const GetFileSizeCallback& callback) { |
47 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 56 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
48 auto* file_system_instance = | 57 auto* file_system_instance = |
49 GetFileSystemInstance("GetFileSize", kGetFileSizeVersion); | 58 GetFileSystemInstance("GetFileSize", kGetFileSizeVersion); |
50 if (!file_system_instance) { | 59 if (!file_system_instance) { |
51 OnGetFileSize(callback, -1); | 60 callback.Run(-1); |
52 return; | 61 return; |
53 } | 62 } |
54 file_system_instance->GetFileSize(arc_url.spec(), | 63 file_system_instance->GetFileSize(arc_url.spec(), callback); |
55 base::Bind(&OnGetFileSize, callback)); | |
56 } | |
57 | |
58 void OnOpenFileToRead(const OpenFileToReadCallback& callback, | |
59 mojo::ScopedHandle handle) { | |
60 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
61 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | |
62 base::Bind(callback, base::Passed(&handle))); | |
63 } | 64 } |
64 | 65 |
65 void OpenFileToReadOnUIThread(const GURL& arc_url, | 66 void OpenFileToReadOnUIThread(const GURL& arc_url, |
66 const OpenFileToReadCallback& callback) { | 67 const OpenFileToReadCallback& callback) { |
67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 68 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
68 auto* file_system_instance = | 69 auto* file_system_instance = |
69 GetFileSystemInstance("OpenFileToRead", kOpenFileToReadVersion); | 70 GetFileSystemInstance("OpenFileToRead", kOpenFileToReadVersion); |
70 if (!file_system_instance) { | 71 if (!file_system_instance) { |
71 OnOpenFileToRead(callback, mojo::ScopedHandle()); | 72 callback.Run(mojo::ScopedHandle()); |
72 return; | 73 return; |
73 } | 74 } |
74 file_system_instance->OpenFileToRead(arc_url.spec(), | 75 file_system_instance->OpenFileToRead(arc_url.spec(), callback); |
75 base::Bind(&OnOpenFileToRead, callback)); | 76 } |
| 77 |
| 78 void GetDocumentOnUIThread(const std::string& authority, |
| 79 const std::string& document_id, |
| 80 const GetDocumentCallback& callback) { |
| 81 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 82 auto* file_system_instance = |
| 83 GetFileSystemInstance("GetDocument", kGetDocumentVersion); |
| 84 if (!file_system_instance) { |
| 85 callback.Run(mojom::DocumentPtr()); |
| 86 return; |
| 87 } |
| 88 file_system_instance->GetDocument(authority, document_id, callback); |
| 89 } |
| 90 |
| 91 void GetChildDocumentsOnUIThread(const std::string& authority, |
| 92 const std::string& parent_document_id, |
| 93 const GetChildDocumentsCallback& callback) { |
| 94 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 95 auto* file_system_instance = |
| 96 GetFileSystemInstance("GetChildDocuments", kGetChildDocumentsVersion); |
| 97 if (!file_system_instance) { |
| 98 callback.Run(base::nullopt); |
| 99 return; |
| 100 } |
| 101 file_system_instance->GetChildDocuments(authority, parent_document_id, |
| 102 callback); |
76 } | 103 } |
77 | 104 |
78 } // namespace | 105 } // namespace |
79 | 106 |
80 void GetFileSizeOnIOThread(const GURL& arc_url, | 107 void GetFileSizeOnIOThread(const GURL& arc_url, |
81 const GetFileSizeCallback& callback) { | 108 const GetFileSizeCallback& callback) { |
82 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 109 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
83 content::BrowserThread::PostTask( | 110 BrowserThread::PostTask( |
84 content::BrowserThread::UI, FROM_HERE, | 111 BrowserThread::UI, FROM_HERE, |
85 base::Bind(&GetFileSizeOnUIThread, arc_url, callback)); | 112 base::Bind(&GetFileSizeOnUIThread, arc_url, |
| 113 base::Bind(&PostToIOThread<int64_t>, callback))); |
86 } | 114 } |
87 | 115 |
88 void OpenFileToReadOnIOThread(const GURL& arc_url, | 116 void OpenFileToReadOnIOThread(const GURL& arc_url, |
89 const OpenFileToReadCallback& callback) { | 117 const OpenFileToReadCallback& callback) { |
90 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 118 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
91 content::BrowserThread::PostTask( | 119 BrowserThread::PostTask( |
92 content::BrowserThread::UI, FROM_HERE, | 120 BrowserThread::UI, FROM_HERE, |
93 base::Bind(&OpenFileToReadOnUIThread, arc_url, callback)); | 121 base::Bind(&OpenFileToReadOnUIThread, arc_url, |
| 122 base::Bind(&PostToIOThread<mojo::ScopedHandle>, callback))); |
| 123 } |
| 124 |
| 125 void GetDocumentOnIOThread(const std::string& authority, |
| 126 const std::string& document_id, |
| 127 const GetDocumentCallback& callback) { |
| 128 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 129 BrowserThread::PostTask( |
| 130 BrowserThread::UI, FROM_HERE, |
| 131 base::Bind(&GetDocumentOnUIThread, authority, document_id, |
| 132 base::Bind(&PostToIOThread<mojom::DocumentPtr>, callback))); |
| 133 } |
| 134 |
| 135 void GetChildDocumentsOnIOThread(const std::string& authority, |
| 136 const std::string& parent_document_id, |
| 137 const GetChildDocumentsCallback& callback) { |
| 138 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 139 BrowserThread::PostTask( |
| 140 BrowserThread::UI, FROM_HERE, |
| 141 base::Bind( |
| 142 &GetChildDocumentsOnUIThread, authority, parent_document_id, |
| 143 base::Bind( |
| 144 &PostToIOThread<base::Optional<std::vector<mojom::DocumentPtr>>>, |
| 145 callback))); |
94 } | 146 } |
95 | 147 |
96 } // namespace file_system_instance_util | 148 } // namespace file_system_instance_util |
97 | 149 |
98 } // namespace arc | 150 } // namespace arc |
OLD | NEW |