OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/arc/fileapi/arc_file_system_instance_util.h" | |
6 | |
7 #include <string> | |
8 #include <utility> | |
9 #include <vector> | |
10 | |
11 #include "base/optional.h" | |
12 #include "components/arc/arc_bridge_service.h" | |
13 #include "components/arc/arc_service_manager.h" | |
14 #include "content/public/browser/browser_thread.h" | |
15 #include "url/gurl.h" | |
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 | |
25 using content::BrowserThread; | |
26 | |
27 namespace arc { | |
28 | |
29 namespace file_system_instance_util { | |
30 | |
31 namespace { | |
32 | |
33 template <typename T> | |
34 void PostToIOThread(const base::Callback<void(T)>& callback, T result) { | |
35 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
36 BrowserThread::PostTask( | |
37 BrowserThread::IO, FROM_HERE, | |
38 base::Bind(callback, base::Passed(std::move(result)))); | |
39 } | |
40 | |
41 void GetFileSizeOnUIThread(const GURL& arc_url, | |
42 const GetFileSizeCallback& callback) { | |
43 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
44 auto* file_system_instance = GET_FILE_SYSTEM_INSTANCE(GetFileSize); | |
45 if (!file_system_instance) { | |
46 callback.Run(-1); | |
47 return; | |
48 } | |
49 file_system_instance->GetFileSize(arc_url.spec(), callback); | |
50 } | |
51 | |
52 void OpenFileToReadOnUIThread(const GURL& arc_url, | |
53 const OpenFileToReadCallback& callback) { | |
54 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
55 auto* file_system_instance = GET_FILE_SYSTEM_INSTANCE(OpenFileToRead); | |
56 if (!file_system_instance) { | |
57 callback.Run(mojo::ScopedHandle()); | |
58 return; | |
59 } | |
60 file_system_instance->OpenFileToRead(arc_url.spec(), callback); | |
61 } | |
62 | |
63 void GetDocumentOnUIThread(const std::string& authority, | |
64 const std::string& document_id, | |
65 const GetDocumentCallback& callback) { | |
66 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
67 auto* file_system_instance = GET_FILE_SYSTEM_INSTANCE(GetDocument); | |
68 if (!file_system_instance) { | |
69 callback.Run(mojom::DocumentPtr()); | |
70 return; | |
71 } | |
72 file_system_instance->GetDocument(authority, document_id, callback); | |
73 } | |
74 | |
75 void GetChildDocumentsOnUIThread(const std::string& authority, | |
76 const std::string& parent_document_id, | |
77 const GetChildDocumentsCallback& callback) { | |
78 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
79 auto* file_system_instance = GET_FILE_SYSTEM_INSTANCE(GetChildDocuments); | |
80 if (!file_system_instance) { | |
81 callback.Run(base::nullopt); | |
82 return; | |
83 } | |
84 file_system_instance->GetChildDocuments(authority, parent_document_id, | |
85 callback); | |
86 } | |
87 | |
88 } // namespace | |
89 | |
90 void GetFileSizeOnIOThread(const GURL& arc_url, | |
91 const GetFileSizeCallback& callback) { | |
92 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
93 BrowserThread::PostTask( | |
94 BrowserThread::UI, FROM_HERE, | |
95 base::Bind(&GetFileSizeOnUIThread, arc_url, | |
96 base::Bind(&PostToIOThread<int64_t>, callback))); | |
97 } | |
98 | |
99 void OpenFileToReadOnIOThread(const GURL& arc_url, | |
100 const OpenFileToReadCallback& callback) { | |
101 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
102 BrowserThread::PostTask( | |
103 BrowserThread::UI, FROM_HERE, | |
104 base::Bind(&OpenFileToReadOnUIThread, arc_url, | |
105 base::Bind(&PostToIOThread<mojo::ScopedHandle>, callback))); | |
106 } | |
107 | |
108 void GetDocumentOnIOThread(const std::string& authority, | |
109 const std::string& document_id, | |
110 const GetDocumentCallback& callback) { | |
111 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
112 BrowserThread::PostTask( | |
113 BrowserThread::UI, FROM_HERE, | |
114 base::Bind(&GetDocumentOnUIThread, authority, document_id, | |
115 base::Bind(&PostToIOThread<mojom::DocumentPtr>, callback))); | |
116 } | |
117 | |
118 void GetChildDocumentsOnIOThread(const std::string& authority, | |
119 const std::string& parent_document_id, | |
120 const GetChildDocumentsCallback& callback) { | |
121 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
122 BrowserThread::PostTask( | |
123 BrowserThread::UI, FROM_HERE, | |
124 base::Bind( | |
125 &GetChildDocumentsOnUIThread, authority, parent_document_id, | |
126 base::Bind( | |
127 &PostToIOThread<base::Optional<std::vector<mojom::DocumentPtr>>>, | |
128 callback))); | |
129 } | |
130 | |
131 } // namespace file_system_instance_util | |
132 | |
133 } // namespace arc | |
OLD | NEW |