Chromium Code Reviews| Index: chrome/browser/chromeos/file_system_provider/fake_provided_file_system.cc |
| diff --git a/chrome/browser/chromeos/file_system_provider/fake_provided_file_system.cc b/chrome/browser/chromeos/file_system_provider/fake_provided_file_system.cc |
| index 45a8a0ba4546dd7122d5768acbe53bc23d9f3aab..e07772c176ea139d29fe97fe9221e6e280ff9e40 100644 |
| --- a/chrome/browser/chromeos/file_system_provider/fake_provided_file_system.cc |
| +++ b/chrome/browser/chromeos/file_system_provider/fake_provided_file_system.cc |
| @@ -14,38 +14,67 @@ namespace chromeos { |
| namespace file_system_provider { |
| namespace { |
| -// Adds a fake entry to the entry list. |
| -void AddDirectoryEntry(fileapi::AsyncFileUtil::EntryList* entry_list, |
| - const std::string& name, |
| - fileapi::DirectoryEntry::DirectoryEntryType type, |
| - int64 size, |
| - std::string last_modified_time_string) { |
| - base::Time last_modified_time; |
| - const bool result = base::Time::FromString(last_modified_time_string.c_str(), |
| - &last_modified_time); |
| - DCHECK(result); |
| - entry_list->push_back( |
| - fileapi::DirectoryEntry(name, type, size, last_modified_time)); |
| -} |
| - |
| -} // namespace |
| - |
| const char kFakeFileName[] = "hello.txt"; |
| -const char kFakeFilePath[] = "/hello.txt"; |
| const char kFakeFileText[] = |
| "This is a testing file. Lorem ipsum dolor sit amet est."; |
| const size_t kFakeFileSize = sizeof(kFakeFileText) - 1u; |
| const char kFakeFileModificationTime[] = "Fri Apr 25 01:47:53 UTC 2014"; |
| +const char kFakeFileMimeType[] = "text/plain"; |
| + |
| +} // namespace |
| + |
| +const char kFakeFilePath[] = "/hello.txt"; |
| FakeProvidedFileSystem::FakeProvidedFileSystem( |
| const ProvidedFileSystemInfo& file_system_info) |
| : file_system_info_(file_system_info), |
| last_file_handle_(0), |
| weak_ptr_factory_(this) { |
| + AddEntry( |
| + base::FilePath::FromUTF8Unsafe("/"), true, "", 0, base::Time(), "", ""); |
| + |
| + base::Time modification_time; |
| + DCHECK(base::Time::FromString(kFakeFileModificationTime, &modification_time)); |
| + AddEntry(base::FilePath::FromUTF8Unsafe(kFakeFilePath), |
| + false, |
| + kFakeFileName, |
| + kFakeFileSize, |
| + modification_time, |
| + kFakeFileMimeType, |
| + kFakeFileText); |
| } |
| FakeProvidedFileSystem::~FakeProvidedFileSystem() {} |
| +void FakeProvidedFileSystem::AddEntry(const base::FilePath& entry_path, |
| + bool is_directory, |
| + const std::string& name, |
| + int64 size, |
| + base::Time modification_time, |
| + std::string mime_type, |
| + std::string contents) { |
| + DCHECK(entries_.find(entry_path) == entries_.end()); |
| + EntryMetadata metadata; |
| + |
| + metadata.is_directory = is_directory; |
| + metadata.name = name; |
| + metadata.size = size; |
| + metadata.modification_time = modification_time; |
| + metadata.mime_type = mime_type; |
| + |
| + entries_[entry_path] = FakeEntry(metadata, contents); |
| +} |
| + |
| +bool FakeProvidedFileSystem::GetEntry(const base::FilePath& entry_path, |
| + FakeEntry* fake_entry) const { |
| + Entries::const_iterator entry_it = entries_.find(entry_path); |
|
hirono
2014/07/16 06:30:00
nit: const Entries::const_iterator
mtomasz
2014/07/16 07:29:15
Done.
|
| + if (entry_it == entries_.end()) |
| + return false; |
| + |
| + *fake_entry = entry_it->second; |
| + return true; |
| +} |
| + |
| void FakeProvidedFileSystem::RequestUnmount( |
| const fileapi::AsyncFileUtil::StatusCallback& callback) { |
| base::MessageLoopProxy::current()->PostTask( |
| @@ -55,103 +84,52 @@ void FakeProvidedFileSystem::RequestUnmount( |
| void FakeProvidedFileSystem::GetMetadata( |
| const base::FilePath& entry_path, |
| const ProvidedFileSystemInterface::GetMetadataCallback& callback) { |
| - if (entry_path.AsUTF8Unsafe() == "/") { |
| - EntryMetadata metadata; |
| - metadata.size = 0; |
| - metadata.is_directory = true; |
| - base::Time modification_time; |
| - const bool result = base::Time::FromString("Thu Apr 24 00:46:52 UTC 2014", |
| - &modification_time); |
| - DCHECK(result); |
| - metadata.modification_time = modification_time; |
| + const Entries::const_iterator entry_it = entries_.find(entry_path); |
| + if (entry_it == entries_.end()) { |
| base::MessageLoopProxy::current()->PostTask( |
| - FROM_HERE, base::Bind(callback, metadata, base::File::FILE_OK)); |
| - return; |
| - } |
| - |
| - if (entry_path.AsUTF8Unsafe() == kFakeFilePath) { |
| - EntryMetadata metadata; |
| - metadata.size = kFakeFileSize; |
| - metadata.is_directory = false; |
| - base::Time modification_time; |
| - const bool result = |
| - base::Time::FromString(kFakeFileModificationTime, &modification_time); |
| - DCHECK(result); |
| - metadata.modification_time = modification_time; |
| - metadata.mime_type = "text/plain"; |
| - |
| - base::MessageLoopProxy::current()->PostTask( |
| - FROM_HERE, base::Bind(callback, metadata, base::File::FILE_OK)); |
| + FROM_HERE, |
| + base::Bind( |
| + callback, EntryMetadata(), base::File::FILE_ERROR_NOT_FOUND)); |
| return; |
| } |
| base::MessageLoopProxy::current()->PostTask( |
| FROM_HERE, |
| - base::Bind(callback, EntryMetadata(), base::File::FILE_ERROR_NOT_FOUND)); |
| + base::Bind(callback, entry_it->second.metadata, base::File::FILE_OK)); |
| } |
| void FakeProvidedFileSystem::ReadDirectory( |
| const base::FilePath& directory_path, |
| const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) { |
| - // Return fake contents for the root directory only. |
| - if (directory_path.AsUTF8Unsafe() != "/") { |
| - base::MessageLoopProxy::current()->PostTask( |
| - FROM_HERE, |
| - base::Bind(callback, |
| - base::File::FILE_ERROR_NOT_FOUND, |
| - fileapi::AsyncFileUtil::EntryList(), |
| - false /* has_more */)); |
| - return; |
| - } |
| - |
| - { |
| - fileapi::AsyncFileUtil::EntryList entry_list; |
| - AddDirectoryEntry(&entry_list, |
| - kFakeFileName, |
| - fileapi::DirectoryEntry::FILE, |
| - kFakeFileSize, |
| - "Thu Apr 24 00:46:52 UTC 2014"); |
| - |
| - AddDirectoryEntry(&entry_list, |
| - "world.txt", |
| - fileapi::DirectoryEntry::FILE, |
| - 1024 /* size */, |
| - "Wed Apr 23 00:20:30 UTC 2014"); |
| - |
| - base::MessageLoopProxy::current()->PostTask( |
| - FROM_HERE, |
| - base::Bind( |
| - callback, base::File::FILE_OK, entry_list, true /* has_more */)); |
| + fileapi::AsyncFileUtil::EntryList entry_list; |
| + |
| + for (Entries::const_iterator it = entries_.begin(); it != entries_.end(); |
| + ++it) { |
| + const base::FilePath file_path = it->first; |
| + if (file_path == directory_path || directory_path.IsParent(file_path)) { |
| + const EntryMetadata& metadata = it->second.metadata; |
| + entry_list.push_back(fileapi::DirectoryEntry( |
| + metadata.name, |
| + metadata.is_directory ? fileapi::DirectoryEntry::DIRECTORY |
| + : fileapi::DirectoryEntry::FILE, |
| + metadata.size, |
| + metadata.modification_time)); |
| + } |
| } |
| - { |
| - fileapi::AsyncFileUtil::EntryList entry_list; |
| - AddDirectoryEntry(&entry_list, |
| - "pictures", |
| - fileapi::DirectoryEntry::DIRECTORY, |
| - 0 /* size */, |
| - "Tue May 22 00:40:50 UTC 2014"); |
| - |
| - base::MessageLoopProxy::current()->PostTask( |
| - FROM_HERE, |
| - base::Bind( |
| - callback, base::File::FILE_OK, entry_list, false /* has_more */)); |
| - } |
| + base::MessageLoopProxy::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind( |
| + callback, base::File::FILE_OK, entry_list, false /* has_more */)); |
| } |
| -void FakeProvidedFileSystem::OpenFile(const base::FilePath& file_path, |
| +void FakeProvidedFileSystem::OpenFile(const base::FilePath& entry_path, |
| OpenFileMode mode, |
| const OpenFileCallback& callback) { |
| - if (mode == OPEN_FILE_MODE_WRITE) { |
| - base::MessageLoopProxy::current()->PostTask( |
| - FROM_HERE, |
| - base::Bind(callback, |
| - 0 /* file_handle */, |
| - base::File::FILE_ERROR_ACCESS_DENIED)); |
| - } |
| + const Entries::const_iterator entry_it = entries_.find(entry_path); |
| - if (file_path.AsUTF8Unsafe() != "/hello.txt") { |
| + if (entry_it == entries_.end()) { |
| base::MessageLoopProxy::current()->PostTask( |
| FROM_HERE, |
| base::Bind( |
| @@ -160,7 +138,7 @@ void FakeProvidedFileSystem::OpenFile(const base::FilePath& file_path, |
| } |
| const int file_handle = ++last_file_handle_; |
| - opened_files_[file_handle] = file_path; |
| + opened_files_[file_handle] = entry_path; |
| base::MessageLoopProxy::current()->PostTask( |
| FROM_HERE, base::Bind(callback, file_handle, base::File::FILE_OK)); |
| } |
| @@ -170,6 +148,7 @@ void FakeProvidedFileSystem::CloseFile( |
| const fileapi::AsyncFileUtil::StatusCallback& callback) { |
| const OpenedFilesMap::iterator opened_file_it = |
| opened_files_.find(file_handle); |
| + |
| if (opened_file_it == opened_files_.end()) { |
| base::MessageLoopProxy::current()->PostTask( |
| FROM_HERE, base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); |
| @@ -189,6 +168,7 @@ void FakeProvidedFileSystem::ReadFile( |
| const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) { |
| const OpenedFilesMap::iterator opened_file_it = |
| opened_files_.find(file_handle); |
| + |
| if (opened_file_it == opened_files_.end() || |
| opened_file_it->second.AsUTF8Unsafe() != kFakeFilePath) { |
| base::MessageLoopProxy::current()->PostTask( |
| @@ -200,12 +180,24 @@ void FakeProvidedFileSystem::ReadFile( |
| return; |
| } |
| + const Entries::const_iterator entry_it = |
| + entries_.find(opened_file_it->second); |
| + if (entry_it == entries_.end()) { |
| + base::MessageLoopProxy::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(callback, |
| + 0 /* chunk_length */, |
| + false /* has_more */, |
| + base::File::FILE_ERROR_INVALID_OPERATION)); |
| + return; |
| + } |
| + |
| // Send the response byte by byte. |
| - size_t current_offset = static_cast<size_t>(offset); |
| - size_t current_length = static_cast<size_t>(length); |
| + int64 current_offset = offset; |
| + int current_length = length; |
| // Reading behind EOF is fine, it will just return 0 bytes. |
| - if (current_offset >= kFakeFileSize || !current_length) { |
| + if (current_offset >= entry_it->second.metadata.size || !current_length) { |
| base::MessageLoopProxy::current()->PostTask( |
| FROM_HERE, |
| base::Bind(callback, |
| @@ -214,10 +206,11 @@ void FakeProvidedFileSystem::ReadFile( |
| base::File::FILE_OK)); |
| } |
| - while (current_offset < kFakeFileSize && current_length) { |
| - buffer->data()[current_offset - offset] = kFakeFileText[current_offset]; |
| + const FakeEntry& entry = entry_it->second; |
| + while (current_offset < entry.metadata.size && current_length) { |
| + buffer->data()[current_offset - offset] = entry.contents[current_offset]; |
| const bool has_more = |
| - (current_offset + 1 < kFakeFileSize) && (current_length - 1); |
| + (current_offset + 1 < entry.metadata.size) && (current_length - 1); |
| base::MessageLoopProxy::current()->PostTask( |
| FROM_HERE, |
| base::Bind( |
| @@ -232,6 +225,7 @@ void FakeProvidedFileSystem::CreateDirectory( |
| bool exclusive, |
| bool recursive, |
| const fileapi::AsyncFileUtil::StatusCallback& callback) { |
| + // TODO(mtomasz): Implement it once needed. |
|
hirono
2014/07/16 06:30:00
Cannot we use NOTIMPLEMENTED()?
mtomasz
2014/07/16 07:29:15
This is implemented, but the implementation is sup
|
| base::MessageLoopProxy::current()->PostTask( |
| FROM_HERE, base::Bind(callback, base::File::FILE_OK)); |
| } |
| @@ -240,6 +234,7 @@ void FakeProvidedFileSystem::DeleteEntry( |
| const base::FilePath& entry_path, |
| bool recursive, |
| const fileapi::AsyncFileUtil::StatusCallback& callback) { |
| + // TODO(mtomasz): Implement it once needed. |
| base::MessageLoopProxy::current()->PostTask( |
| FROM_HERE, base::Bind(callback, base::File::FILE_OK)); |
| } |