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