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 "components/arc/test/fake_file_system_instance.h"
6 
7 #include <string.h>
8 #include <unistd.h>
9 
10 #include <limits>
11 #include <sstream>
12 #include <utility>
13 
14 #include "base/bind.h"
15 #include "base/callback_helpers.h"
16 #include "base/files/file.h"
17 #include "base/files/file_path.h"
18 #include "base/files/file_util.h"
19 #include "base/files/scoped_file.h"
20 #include "base/location.h"
21 #include "base/logging.h"
22 #include "base/optional.h"
23 #include "base/threading/thread_task_runner_handle.h"
24 #include "mojo/public/cpp/system/platform_handle.h"
25 
26 namespace arc {
27 
28 namespace {
29 
MakeDocument(const FakeFileSystemInstance::Document & doc)30 mojom::DocumentPtr MakeDocument(const FakeFileSystemInstance::Document& doc) {
31   mojom::DocumentPtr document = mojom::Document::New();
32   document->document_id = doc.document_id;
33   document->display_name = doc.display_name;
34   document->mime_type = doc.mime_type;
35   document->size = doc.size;
36   document->last_modified = doc.last_modified;
37   document->supports_delete = doc.supports_delete;
38   document->supports_rename = doc.supports_rename;
39   document->dir_supports_create = doc.dir_supports_create;
40   document->supports_thumbnail = doc.supports_thumbnail;
41   return document;
42 }
43 
MakeRoot(const FakeFileSystemInstance::Root & in_root)44 mojom::RootPtr MakeRoot(const FakeFileSystemInstance::Root& in_root) {
45   mojom::RootPtr root = mojom::Root::New();
46   root->authority = in_root.authority;
47   root->root_id = in_root.root_id;
48   root->document_id = in_root.document_id;
49   root->title = in_root.title;
50   return root;
51 }
52 
53 // Generates unique document ID on each calls.
GenerateDocumentId()54 std::string GenerateDocumentId() {
55   static int count = 0;
56   std::ostringstream ss;
57   ss << "doc_" << count++;
58   return ss.str();
59 }
60 
61 // Maximum size in bytes to read FD from PIPE.
62 constexpr size_t kMaxBytesToReadFromPipe = 8 * 1024;  // 8KB;
63 
64 }  // namespace
65 
66 constexpr base::FilePath::CharType FakeFileSystemInstance::kFakeAndroidPath[];
67 
68 constexpr gfx::Size FakeFileSystemInstance::kDefaultThumbnailSize;
69 
File(const std::string & url,const std::string & content,const std::string & mime_type,Seekable seekable)70 FakeFileSystemInstance::File::File(const std::string& url,
71                                    const std::string& content,
72                                    const std::string& mime_type,
73                                    Seekable seekable)
74     : url(url), content(content), mime_type(mime_type), seekable(seekable) {}
75 
76 FakeFileSystemInstance::File::File(const File& that) = default;
77 
78 FakeFileSystemInstance::File::~File() = default;
79 
Document(const std::string & authority,const std::string & document_id,const std::string & parent_document_id,const std::string & display_name,const std::string & mime_type,int64_t size,uint64_t last_modified)80 FakeFileSystemInstance::Document::Document(
81     const std::string& authority,
82     const std::string& document_id,
83     const std::string& parent_document_id,
84     const std::string& display_name,
85     const std::string& mime_type,
86     int64_t size,
87     uint64_t last_modified)
88     : Document(authority,
89                document_id,
90                parent_document_id,
91                display_name,
92                mime_type,
93                size,
94                last_modified,
95                true,
96                true,
97                true,
98                false) {}
99 
Document(const std::string & authority,const std::string & document_id,const std::string & parent_document_id,const std::string & display_name,const std::string & mime_type,int64_t size,uint64_t last_modified,bool supports_delete,bool supports_rename,bool dir_supports_create,bool supports_thumbnail)100 FakeFileSystemInstance::Document::Document(
101     const std::string& authority,
102     const std::string& document_id,
103     const std::string& parent_document_id,
104     const std::string& display_name,
105     const std::string& mime_type,
106     int64_t size,
107     uint64_t last_modified,
108     bool supports_delete,
109     bool supports_rename,
110     bool dir_supports_create,
111     bool supports_thumbnail)
112     : authority(authority),
113       document_id(document_id),
114       parent_document_id(parent_document_id),
115       display_name(display_name),
116       mime_type(mime_type),
117       size(size),
118       last_modified(last_modified),
119       supports_delete(supports_delete),
120       supports_rename(supports_rename),
121       dir_supports_create(dir_supports_create),
122       supports_thumbnail(supports_thumbnail) {}
123 
124 FakeFileSystemInstance::Document::Document(const Document& that) = default;
125 
126 FakeFileSystemInstance::Document::~Document() = default;
127 
Root(const std::string & authority,const std::string & root_id,const std::string & document_id,const std::string & title)128 FakeFileSystemInstance::Root::Root(const std::string& authority,
129                                    const std::string& root_id,
130                                    const std::string& document_id,
131                                    const std::string& title)
132     : authority(authority),
133       root_id(root_id),
134       document_id(document_id),
135       title(title) {}
136 
137 FakeFileSystemInstance::Root::Root(const Root& that) = default;
138 
139 FakeFileSystemInstance::Root::~Root() = default;
140 
FakeFileSystemInstance()141 FakeFileSystemInstance::FakeFileSystemInstance() {
142   bool temp_dir_created = temp_dir_.CreateUniqueTempDir();
143   DCHECK(temp_dir_created);
144 }
145 
~FakeFileSystemInstance()146 FakeFileSystemInstance::~FakeFileSystemInstance() {
147   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
148 }
149 
InitCalled()150 bool FakeFileSystemInstance::InitCalled() {
151   return host_remote_.is_bound();
152 }
153 
AddFile(const File & file)154 void FakeFileSystemInstance::AddFile(const File& file) {
155   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
156   DCHECK_EQ(0u, files_.count(std::string(file.url)));
157   files_.insert(std::make_pair(std::string(file.url), file));
158 }
159 
AddDocument(const Document & document)160 void FakeFileSystemInstance::AddDocument(const Document& document) {
161   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
162   DocumentKey key(document.authority, document.document_id);
163   DCHECK_EQ(0u, documents_.count(key));
164   documents_.insert(std::make_pair(key, document));
165   child_documents_[key];  // Allocate a vector.
166   if (!document.parent_document_id.empty()) {
167     DocumentKey parent_key(document.authority, document.parent_document_id);
168     DCHECK_EQ(1u, documents_.count(parent_key));
169     child_documents_[parent_key].push_back(key);
170   }
171 }
172 
AddRecentDocument(const std::string & root_id,const Document & document)173 void FakeFileSystemInstance::AddRecentDocument(const std::string& root_id,
174                                                const Document& document) {
175   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
176   RootKey key(document.authority, root_id);
177   recent_documents_[key].push_back(document);
178 }
179 
AddRoot(const Root & root)180 void FakeFileSystemInstance::AddRoot(const Root& root) {
181   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
182   roots_.push_back(root);
183 }
184 
SetGetLastChangeTimeCallback(GetLastChangeTimeCallback ctime_callback)185 void FakeFileSystemInstance::SetGetLastChangeTimeCallback(
186     GetLastChangeTimeCallback ctime_callback) {
187   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
188   ctime_callback_ = ctime_callback;
189 }
190 
SetCrosDir(const base::FilePath & cros_dir)191 void FakeFileSystemInstance::SetCrosDir(const base::FilePath& cros_dir) {
192   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
193   cros_dir_ = cros_dir;
194 }
195 
SetMediaStore(const std::map<base::FilePath,base::Time> & media_store)196 void FakeFileSystemInstance::SetMediaStore(
197     const std::map<base::FilePath, base::Time>& media_store) {
198   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
199   media_store_ = media_store;
200 }
201 
TriggerWatchers(const std::string & authority,const std::string & document_id,storage::WatcherManager::ChangeType type)202 void FakeFileSystemInstance::TriggerWatchers(
203     const std::string& authority,
204     const std::string& document_id,
205     storage::WatcherManager::ChangeType type) {
206   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
207   if (!host_remote_) {
208     LOG(ERROR) << "FileSystemHost is not available.";
209     return;
210   }
211   auto iter = document_to_watchers_.find(DocumentKey(authority, document_id));
212   if (iter == document_to_watchers_.end())
213     return;
214   for (int64_t watcher_id : iter->second) {
215     host_remote_->OnDocumentChanged(watcher_id, type);
216   }
217 }
218 
DocumentExists(const std::string & authority,const std::string & document_id)219 bool FakeFileSystemInstance::DocumentExists(const std::string& authority,
220                                             const std::string& document_id) {
221   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
222   DocumentKey key(authority, document_id);
223   return documents_.find(key) != documents_.end();
224 }
225 
DocumentExists(const std::string & authority,const std::string & root_document_id,const base::FilePath & path)226 bool FakeFileSystemInstance::DocumentExists(const std::string& authority,
227                                             const std::string& root_document_id,
228                                             const base::FilePath& path) {
229   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
230   std::vector<std::string> path_components;
231   path.GetComponents(&path_components);
232   std::string document_id =
233       FindChildDocumentId(authority, root_document_id, path_components);
234   return DocumentExists(authority, document_id);
235 }
236 
GetDocument(const std::string & authority,const std::string & document_id)237 FakeFileSystemInstance::Document FakeFileSystemInstance::GetDocument(
238     const std::string& authority,
239     const std::string& document_id) {
240   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
241   DocumentKey key(authority, document_id);
242   auto iter = documents_.find(key);
243   DCHECK(iter != documents_.end());
244   return iter->second;
245 }
246 
GetDocument(const std::string & authority,const std::string & root_document_id,const base::FilePath & path)247 FakeFileSystemInstance::Document FakeFileSystemInstance::GetDocument(
248     const std::string& authority,
249     const std::string& root_document_id,
250     const base::FilePath& path) {
251   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
252   std::vector<std::string> path_components;
253   path.GetComponents(&path_components);
254   std::string document_id =
255       FindChildDocumentId(authority, root_document_id, path_components);
256   return GetDocument(authority, document_id);
257 }
258 
GetFileContent(const std::string & url)259 std::string FakeFileSystemInstance::GetFileContent(const std::string& url) {
260   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
261   return GetFileContent(url, std::numeric_limits<size_t>::max());
262 }
263 
GetFileContent(const std::string & url,size_t bytes)264 std::string FakeFileSystemInstance::GetFileContent(const std::string& url,
265                                                    size_t bytes) {
266   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
267   auto regular_file_paths_it = regular_file_paths_.find(url);
268   if (regular_file_paths_it != regular_file_paths_.end()) {
269     base::FilePath path = regular_file_paths_it->second;
270     std::string content;
271     if (base::ReadFileToStringWithMaxSize(path, &content, bytes))
272       return content;
273   } else {
274     auto pipe_read_ends_it = pipe_read_ends_.find(url);
275     if (pipe_read_ends_it != pipe_read_ends_.end()) {
276       if (bytes > kMaxBytesToReadFromPipe) {
277         LOG(ERROR) << "Trying to read too many bytes from pipe. " << url;
278         return std::string();
279       }
280       std::string result;
281       result.resize(bytes);
282       bool success =
283           base::ReadFromFD(pipe_read_ends_it->second.get(), &result[0], bytes);
284       DCHECK(success);
285       return result;
286     }
287   }
288   LOG(ERROR) << "A file to read content not found. " << url;
289   return std::string();
290 }
291 
AddWatcher(const std::string & authority,const std::string & document_id,AddWatcherCallback callback)292 void FakeFileSystemInstance::AddWatcher(const std::string& authority,
293                                         const std::string& document_id,
294                                         AddWatcherCallback callback) {
295   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
296   DocumentKey key(authority, document_id);
297   auto iter = documents_.find(key);
298   if (iter == documents_.end()) {
299     base::ThreadTaskRunnerHandle::Get()->PostTask(
300         FROM_HERE, base::BindOnce(std::move(callback), -1));
301     return;
302   }
303   int64_t watcher_id = next_watcher_id_++;
304   document_to_watchers_[key].insert(watcher_id);
305   watcher_to_document_.insert(std::make_pair(watcher_id, key));
306   base::ThreadTaskRunnerHandle::Get()->PostTask(
307       FROM_HERE, base::BindOnce(std::move(callback), watcher_id));
308 }
309 
GetFileSize(const std::string & url,GetFileSizeCallback callback)310 void FakeFileSystemInstance::GetFileSize(const std::string& url,
311                                          GetFileSizeCallback callback) {
312   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
313   auto iter = files_.find(url);
314   if (iter == files_.end()) {
315     base::ThreadTaskRunnerHandle::Get()->PostTask(
316         FROM_HERE, base::BindOnce(std::move(callback), -1));
317     return;
318   }
319   const File& file = iter->second;
320   base::ThreadTaskRunnerHandle::Get()->PostTask(
321       FROM_HERE, base::BindOnce(std::move(callback), file.content.size()));
322 }
323 
GetMimeType(const std::string & url,GetMimeTypeCallback callback)324 void FakeFileSystemInstance::GetMimeType(const std::string& url,
325                                          GetMimeTypeCallback callback) {
326   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
327   auto iter = files_.find(url);
328   if (iter == files_.end()) {
329     base::ThreadTaskRunnerHandle::Get()->PostTask(
330         FROM_HERE, base::BindOnce(std::move(callback), base::nullopt));
331     return;
332   }
333   const File& file = iter->second;
334   base::ThreadTaskRunnerHandle::Get()->PostTask(
335       FROM_HERE, base::BindOnce(std::move(callback), file.mime_type));
336 }
337 
OpenFileToRead(const std::string & url,OpenFileToReadCallback callback)338 void FakeFileSystemInstance::OpenFileToRead(const std::string& url,
339                                             OpenFileToReadCallback callback) {
340   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
341   auto iter = files_.find(url);
342   if (iter == files_.end()) {
343     base::ThreadTaskRunnerHandle::Get()->PostTask(
344         FROM_HERE, base::BindOnce(std::move(callback), mojo::ScopedHandle()));
345     return;
346   }
347   const File& file = iter->second;
348   base::ScopedFD fd =
349       file.seekable == File::Seekable::YES
350           ? CreateRegularFileDescriptor(file, base::File::Flags::FLAG_OPEN |
351                                                   base::File::Flags::FLAG_READ)
352           : CreateStreamFileDescriptorToRead(file.content);
353   mojo::ScopedHandle wrapped_handle =
354       mojo::WrapPlatformHandle(mojo::PlatformHandle(std::move(fd)));
355   DCHECK(wrapped_handle.is_valid());
356   base::ThreadTaskRunnerHandle::Get()->PostTask(
357       FROM_HERE,
358       base::BindOnce(std::move(callback), std::move(wrapped_handle)));
359 }
360 
OpenFileToWrite(const std::string & url,OpenFileToWriteCallback callback)361 void FakeFileSystemInstance::OpenFileToWrite(const std::string& url,
362                                              OpenFileToWriteCallback callback) {
363   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
364   auto iter = files_.find(url);
365   if (iter == files_.end()) {
366     base::ThreadTaskRunnerHandle::Get()->PostTask(
367         FROM_HERE, base::BindOnce(std::move(callback), mojo::ScopedHandle()));
368     return;
369   }
370   const File& file = iter->second;
371   base::ScopedFD fd =
372       file.seekable == File::Seekable::YES
373           ? CreateRegularFileDescriptor(file, base::File::Flags::FLAG_OPEN |
374                                                   base::File::Flags::FLAG_WRITE)
375           : CreateStreamFileDescriptorToWrite(file.url);
376   mojo::ScopedHandle wrapped_handle =
377       mojo::WrapPlatformHandle(mojo::PlatformHandle(std::move(fd)));
378   DCHECK(wrapped_handle.is_valid());
379   base::ThreadTaskRunnerHandle::Get()->PostTask(
380       FROM_HERE,
381       base::BindOnce(std::move(callback), std::move(wrapped_handle)));
382 }
383 
OpenThumbnail(const std::string & url,const gfx::Size & size_hint,OpenThumbnailCallback callback)384 void FakeFileSystemInstance::OpenThumbnail(const std::string& url,
385                                            const gfx::Size& size_hint,
386                                            OpenThumbnailCallback callback) {
387   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
388   auto iter = files_.find(url);
389   if (iter == files_.end()) {
390     base::ThreadTaskRunnerHandle::Get()->PostTask(
391         FROM_HERE, base::BindOnce(std::move(callback), mojo::ScopedHandle()));
392     return;
393   }
394   const File& file = iter->second;
395   if (file.thumbnail_content.empty()) {
396     base::ThreadTaskRunnerHandle::Get()->PostTask(
397         FROM_HERE, base::BindOnce(std::move(callback), mojo::ScopedHandle()));
398     return;
399   }
400   // This validates that size_hint parameter is propagated properly from the
401   // client, so OpenThumbnail should always be called with same default value in
402   // tests.
403   if (size_hint != kDefaultThumbnailSize) {
404     LOG(ERROR) << "Unexpected thumbnail size hint: " << size_hint.width() << "x"
405                << size_hint.height();
406     base::ThreadTaskRunnerHandle::Get()->PostTask(
407         FROM_HERE, base::BindOnce(std::move(callback), mojo::ScopedHandle()));
408     return;
409   }
410   base::ScopedFD fd = CreateStreamFileDescriptorToRead(file.thumbnail_content);
411   mojo::ScopedHandle wrapped_handle =
412       mojo::WrapPlatformHandle(mojo::PlatformHandle(std::move(fd)));
413   DCHECK(wrapped_handle.is_valid());
414   base::ThreadTaskRunnerHandle::Get()->PostTask(
415       FROM_HERE,
416       base::BindOnce(std::move(callback), std::move(wrapped_handle)));
417 }
418 
GetDocument(const std::string & authority,const std::string & document_id,GetDocumentCallback callback)419 void FakeFileSystemInstance::GetDocument(const std::string& authority,
420                                          const std::string& document_id,
421                                          GetDocumentCallback callback) {
422   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
423   auto iter = documents_.find(DocumentKey(authority, document_id));
424   if (iter == documents_.end()) {
425     base::ThreadTaskRunnerHandle::Get()->PostTask(
426         FROM_HERE, base::BindOnce(std::move(callback), mojom::DocumentPtr()));
427     return;
428   }
429   base::ThreadTaskRunnerHandle::Get()->PostTask(
430       FROM_HERE,
431       base::BindOnce(std::move(callback), MakeDocument(iter->second)));
432 }
433 
GetChildDocuments(const std::string & authority,const std::string & parent_document_id,GetChildDocumentsCallback callback)434 void FakeFileSystemInstance::GetChildDocuments(
435     const std::string& authority,
436     const std::string& parent_document_id,
437     GetChildDocumentsCallback callback) {
438   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
439   ++get_child_documents_count_;
440   auto child_iter =
441       child_documents_.find(DocumentKey(authority, parent_document_id));
442   if (child_iter == child_documents_.end()) {
443     base::ThreadTaskRunnerHandle::Get()->PostTask(
444         FROM_HERE, base::BindOnce(std::move(callback), base::nullopt));
445     return;
446   }
447   std::vector<mojom::DocumentPtr> children;
448   for (const auto& child_key : child_iter->second) {
449     auto doc_iter = documents_.find(child_key);
450     DCHECK(doc_iter != documents_.end());
451     children.emplace_back(MakeDocument(doc_iter->second));
452   }
453   base::ThreadTaskRunnerHandle::Get()->PostTask(
454       FROM_HERE, base::BindOnce(std::move(callback),
455                                 base::make_optional(std::move(children))));
456 }
457 
GetRecentDocuments(const std::string & authority,const std::string & root_id,GetRecentDocumentsCallback callback)458 void FakeFileSystemInstance::GetRecentDocuments(
459     const std::string& authority,
460     const std::string& root_id,
461     GetRecentDocumentsCallback callback) {
462   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
463   auto recent_iter = recent_documents_.find(RootKey(authority, root_id));
464   if (recent_iter == recent_documents_.end()) {
465     base::ThreadTaskRunnerHandle::Get()->PostTask(
466         FROM_HERE, base::BindOnce(std::move(callback), base::nullopt));
467     return;
468   }
469   std::vector<mojom::DocumentPtr> recents;
470   for (const Document& document : recent_iter->second)
471     recents.emplace_back(MakeDocument(document));
472   base::ThreadTaskRunnerHandle::Get()->PostTask(
473       FROM_HERE, base::BindOnce(std::move(callback),
474                                 base::make_optional(std::move(recents))));
475 }
476 
GetRoots(GetRootsCallback callback)477 void FakeFileSystemInstance::GetRoots(GetRootsCallback callback) {
478   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
479   std::vector<mojom::RootPtr> roots;
480   for (const Root& root : roots_)
481     roots.emplace_back(MakeRoot(root));
482   base::ThreadTaskRunnerHandle::Get()->PostTask(
483       FROM_HERE, base::BindOnce(std::move(callback),
484                                 base::make_optional(std::move(roots))));
485 }
486 
DeleteDocument(const std::string & authority,const std::string & document_id,DeleteDocumentCallback callback)487 void FakeFileSystemInstance::DeleteDocument(const std::string& authority,
488                                             const std::string& document_id,
489                                             DeleteDocumentCallback callback) {
490   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
491   DocumentKey key(authority, document_id);
492   auto iter = documents_.find(key);
493   if (iter == documents_.end() || iter->second.supports_delete == false) {
494     base::ThreadTaskRunnerHandle::Get()->PostTask(
495         FROM_HERE, base::BindOnce(std::move(callback), false));
496     return;
497   }
498   documents_.erase(iter);
499   size_t erased = child_documents_.erase(key);
500   DCHECK_NE(0u, erased);
501   base::ThreadTaskRunnerHandle::Get()->PostTask(
502       FROM_HERE, base::BindOnce(std::move(callback), true));
503 }
504 
RenameDocument(const std::string & authority,const std::string & document_id,const std::string & display_name,RenameDocumentCallback callback)505 void FakeFileSystemInstance::RenameDocument(const std::string& authority,
506                                             const std::string& document_id,
507                                             const std::string& display_name,
508                                             RenameDocumentCallback callback) {
509   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
510   DocumentKey key(authority, document_id);
511   auto iter = documents_.find(key);
512   if (iter == documents_.end() || iter->second.supports_rename == false) {
513     base::ThreadTaskRunnerHandle::Get()->PostTask(
514         FROM_HERE, base::BindOnce(std::move(callback), mojom::DocumentPtr()));
515     return;
516   }
517   iter->second.display_name = display_name;
518   base::ThreadTaskRunnerHandle::Get()->PostTask(
519       FROM_HERE,
520       base::BindOnce(std::move(callback), MakeDocument(iter->second)));
521 }
522 
CreateDocument(const std::string & authority,const std::string & parent_document_id,const std::string & mime_type,const std::string & display_name,CreateDocumentCallback callback)523 void FakeFileSystemInstance::CreateDocument(
524     const std::string& authority,
525     const std::string& parent_document_id,
526     const std::string& mime_type,
527     const std::string& display_name,
528     CreateDocumentCallback callback) {
529   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
530   DocumentKey parent_key(authority, parent_document_id);
531   auto iter = documents_.find(parent_key);
532   DCHECK(iter != documents_.end());
533   if (iter->second.dir_supports_create == false) {
534     base::ThreadTaskRunnerHandle::Get()->PostTask(
535         FROM_HERE, base::BindOnce(std::move(callback), mojom::DocumentPtr()));
536     return;
537   }
538   std::string document_id = GenerateDocumentId();
539   Document document(authority, document_id, parent_document_id, display_name,
540                     mime_type, 0, 0);
541   AddDocument(document);
542   base::ThreadTaskRunnerHandle::Get()->PostTask(
543       FROM_HERE, base::BindOnce(std::move(callback), MakeDocument(document)));
544 }
545 
CopyDocument(const std::string & authority,const std::string & source_document_id,const std::string & target_parent_document_id,CopyDocumentCallback callback)546 void FakeFileSystemInstance::CopyDocument(
547     const std::string& authority,
548     const std::string& source_document_id,
549     const std::string& target_parent_document_id,
550     CopyDocumentCallback callback) {
551   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
552   DocumentKey source_key(authority, source_document_id);
553   auto iter = documents_.find(source_key);
554   DCHECK(iter != documents_.end());
555   Document target_document(iter->second);
556   target_document.document_id = target_document.display_name;
557   target_document.parent_document_id = target_parent_document_id;
558   AddDocument(target_document);
559   base::ThreadTaskRunnerHandle::Get()->PostTask(
560       FROM_HERE,
561       base::BindOnce(std::move(callback), MakeDocument(target_document)));
562 }
563 
MoveDocument(const std::string & authority,const std::string & source_document_id,const std::string & source_parent_document_id,const std::string & target_parent_document_id,MoveDocumentCallback callback)564 void FakeFileSystemInstance::MoveDocument(
565     const std::string& authority,
566     const std::string& source_document_id,
567     const std::string& source_parent_document_id,
568     const std::string& target_parent_document_id,
569     MoveDocumentCallback callback) {
570   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
571   DocumentKey source_key(authority, source_document_id);
572   DocumentKey source_parent_key(authority, source_parent_document_id);
573   DocumentKey target_parent_key(authority, target_parent_document_id);
574   for (auto iter = child_documents_[source_parent_key].begin();
575        iter != child_documents_[source_parent_key].end(); iter++) {
576     if (*iter == source_key) {
577       child_documents_[source_parent_key].erase(iter);
578       break;
579     }
580   }
581   child_documents_[target_parent_key].push_back(source_key);
582   auto iter = documents_.find(source_key);
583   DCHECK(iter != documents_.end());
584   iter->second.parent_document_id = target_parent_document_id;
585   base::ThreadTaskRunnerHandle::Get()->PostTask(
586       FROM_HERE,
587       base::BindOnce(std::move(callback), MakeDocument(iter->second)));
588 }
589 
InitDeprecated(mojo::PendingRemote<mojom::FileSystemHost> host_remote)590 void FakeFileSystemInstance::InitDeprecated(
591     mojo::PendingRemote<mojom::FileSystemHost> host_remote) {
592   Init(std::move(host_remote), base::DoNothing());
593 }
594 
Init(mojo::PendingRemote<mojom::FileSystemHost> host_remote,InitCallback callback)595 void FakeFileSystemInstance::Init(
596     mojo::PendingRemote<mojom::FileSystemHost> host_remote,
597     InitCallback callback) {
598   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
599   DCHECK(host_remote);
600   DCHECK(!host_remote_);
601   host_remote_.Bind(std::move(host_remote));
602   std::move(callback).Run();
603 }
604 
RemoveWatcher(int64_t watcher_id,RemoveWatcherCallback callback)605 void FakeFileSystemInstance::RemoveWatcher(int64_t watcher_id,
606                                            RemoveWatcherCallback callback) {
607   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
608   auto iter = watcher_to_document_.find(watcher_id);
609   if (iter == watcher_to_document_.end()) {
610     base::ThreadTaskRunnerHandle::Get()->PostTask(
611         FROM_HERE, base::BindOnce(std::move(callback), false));
612     return;
613   }
614   document_to_watchers_[iter->second].erase(watcher_id);
615   watcher_to_document_.erase(iter);
616   base::ThreadTaskRunnerHandle::Get()->PostTask(
617       FROM_HERE, base::BindOnce(std::move(callback), true));
618 }
619 
620 // TODO(risan): "Added" directory might not be handled. Please double check
621 // this.
RequestMediaScan(const std::vector<std::string> & paths)622 void FakeFileSystemInstance::RequestMediaScan(
623     const std::vector<std::string>& paths) {
624   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
625   // TODO(risan): This is to prevent crashing other tests that expect nothing
626   // from RequestMediaScan, e.g., the following:
627   // FilesAppBrowserTest.Test/dirContextMenuDocumentsProvider_DocumentsProvider
628   if (cros_dir_.empty())
629     return;
630   for (const auto& path : paths) {
631     base::FilePath file_path = base::FilePath(path);
632     base::FilePath cros_path = GetCrosPath(file_path);
633     if (PathExists(cros_path)) {
634       // For each existing path, index itself and all parent directories of
635       // it.
636       base::Time ctime;
637       if (!DirectoryExists(cros_path))
638         ctime = ctime_callback_.Run(cros_path);
639       media_store_[file_path] = ctime;
640       file_path = file_path.DirName();
641       while (file_path != base::FilePath(kFakeAndroidPath).DirName()) {
642         media_store_[file_path] = base::Time();
643         file_path = file_path.DirName();
644       }
645     } else {
646       // When a file or directory does not exist, it means it has been
647       // deleted. So we need to erase its index entry in |media_store_|, and
648       // also the entries of all files/directories underneath it if it is a
649       // directory.
650       for (auto it = media_store_.begin(); it != media_store_.end();) {
651         if (it->first == file_path || file_path.IsParent(it->first))
652           media_store_.erase(it++);
653         else
654           ++it;
655       }
656     }
657   }
658 }
659 
RequestFileRemovalScan(const std::vector<std::string> & directory_paths)660 void FakeFileSystemInstance::RequestFileRemovalScan(
661     const std::vector<std::string>& directory_paths) {
662   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
663   ReindexDirectory(kFakeAndroidPath);
664 }
665 
ReindexDirectory(const std::string & directory_path)666 void FakeFileSystemInstance::ReindexDirectory(
667     const std::string& directory_path) {
668   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
669   std::vector<std::string> paths = {directory_path};
670   base::FilePath directory_file_path(directory_path);
671   for (const auto& entry : media_store_) {
672     base::FilePath entry_path = entry.first;
673     if (!directory_file_path.IsParent(entry_path)) {
674       continue;
675     }
676     paths.push_back(entry_path.value());
677   }
678   RequestMediaScan(paths);
679 }
680 
OpenUrlsWithPermission(mojom::OpenUrlsRequestPtr request,OpenUrlsWithPermissionCallback callback)681 void FakeFileSystemInstance::OpenUrlsWithPermission(
682     mojom::OpenUrlsRequestPtr request,
683     OpenUrlsWithPermissionCallback callback) {
684   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
685   handled_url_requests_.emplace_back(std::move(request));
686 }
687 
FindChildDocumentId(const std::string & authority,const std::string & parent_document_id,const std::vector<std::string> & components)688 std::string FakeFileSystemInstance::FindChildDocumentId(
689     const std::string& authority,
690     const std::string& parent_document_id,
691     const std::vector<std::string>& components) {
692   if (components.empty())
693     return parent_document_id;
694 
695   auto children_iter =
696       child_documents_.find(DocumentKey(authority, parent_document_id));
697   if (children_iter == child_documents_.end())
698     return std::string();
699 
700   for (DocumentKey key : children_iter->second) {
701     auto iter = documents_.find(key);
702     if (iter == documents_.end())
703       continue;
704 
705     if (iter->second.display_name == components[0]) {
706       std::vector<std::string> next_components(components.begin() + 1,
707                                                components.end());
708       return FindChildDocumentId(authority, iter->second.document_id,
709                                  next_components);
710     }
711   }
712   return std::string();
713 }
714 
CreateRegularFileDescriptor(const File & file,uint32_t flags)715 base::ScopedFD FakeFileSystemInstance::CreateRegularFileDescriptor(
716     const File& file,
717     uint32_t flags) {
718   if (regular_file_paths_.find(file.url) == regular_file_paths_.end()) {
719     base::FilePath path;
720     bool create_success =
721         base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &path);
722     DCHECK(create_success);
723     int written_size =
724         base::WriteFile(path, file.content.data(), file.content.size());
725     DCHECK_EQ(static_cast<int>(file.content.size()), written_size);
726     regular_file_paths_[file.url] = path;
727   }
728   base::File regular_file(regular_file_paths_[file.url], flags);
729   DCHECK(regular_file.IsValid());
730   return base::ScopedFD(regular_file.TakePlatformFile());
731 }
732 
CreateStreamFileDescriptorToRead(const std::string & content)733 base::ScopedFD FakeFileSystemInstance::CreateStreamFileDescriptorToRead(
734     const std::string& content) {
735   int fds[2];
736   int ret = pipe(fds);
737   DCHECK_EQ(0, ret);
738   base::ScopedFD fd_read(fds[0]);
739   base::ScopedFD fd_write(fds[1]);
740   bool write_success =
741       base::WriteFileDescriptor(fd_write.get(), content.data(), content.size());
742   DCHECK(write_success);
743   return fd_read;
744 }
745 
CreateStreamFileDescriptorToWrite(const std::string & url)746 base::ScopedFD FakeFileSystemInstance::CreateStreamFileDescriptorToWrite(
747     const std::string& url) {
748   int fds[2];
749   int ret = pipe(fds);
750   DCHECK_EQ(0, ret);
751   base::ScopedFD fd_read(fds[0]);
752   base::ScopedFD fd_write(fds[1]);
753   pipe_read_ends_.emplace(url, std::move(fd_read));
754   return fd_write;
755 }
756 
GetCrosPath(const base::FilePath & android_path) const757 base::FilePath FakeFileSystemInstance::GetCrosPath(
758     const base::FilePath& android_path) const {
759   base::FilePath cros_path(cros_dir_);
760   base::FilePath android_dir(kFakeAndroidPath);
761   android_dir.AppendRelativePath(android_path, &cros_path);
762   return cros_path;
763 }
764 
765 }  // namespace arc
766