| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/file_system_provider/fake_provided_file_system
.h" | 5 #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system
.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 base::Time last_modified_time; | 24 base::Time last_modified_time; |
| 25 const bool result = base::Time::FromString(last_modified_time_string.c_str(), | 25 const bool result = base::Time::FromString(last_modified_time_string.c_str(), |
| 26 &last_modified_time); | 26 &last_modified_time); |
| 27 DCHECK(result); | 27 DCHECK(result); |
| 28 entry_list->push_back( | 28 entry_list->push_back( |
| 29 fileapi::DirectoryEntry(name, type, size, last_modified_time)); | 29 fileapi::DirectoryEntry(name, type, size, last_modified_time)); |
| 30 } | 30 } |
| 31 | 31 |
| 32 } // namespace | 32 } // namespace |
| 33 | 33 |
| 34 const char kFakeFileName[] = "hello.txt"; |
| 35 const char kFakeFilePath[] = "/hello.txt"; |
| 36 const char kFakeFileText[] = |
| 37 "This is a testing file. Lorem ipsum dolor sit amet est."; |
| 38 const size_t kFakeFileSize = sizeof(kFakeFileText) - 1u; |
| 39 |
| 34 FakeProvidedFileSystem::FakeProvidedFileSystem( | 40 FakeProvidedFileSystem::FakeProvidedFileSystem( |
| 35 const ProvidedFileSystemInfo& file_system_info) | 41 const ProvidedFileSystemInfo& file_system_info) |
| 36 : file_system_info_(file_system_info), last_file_handle_(0) { | 42 : file_system_info_(file_system_info), |
| 43 last_file_handle_(0), |
| 44 weak_ptr_factory_(this) { |
| 37 } | 45 } |
| 38 | 46 |
| 39 FakeProvidedFileSystem::~FakeProvidedFileSystem() {} | 47 FakeProvidedFileSystem::~FakeProvidedFileSystem() {} |
| 40 | 48 |
| 41 void FakeProvidedFileSystem::RequestUnmount( | 49 void FakeProvidedFileSystem::RequestUnmount( |
| 42 const fileapi::AsyncFileUtil::StatusCallback& callback) { | 50 const fileapi::AsyncFileUtil::StatusCallback& callback) { |
| 43 base::MessageLoopProxy::current()->PostTask( | 51 base::MessageLoopProxy::current()->PostTask( |
| 44 FROM_HERE, base::Bind(callback, base::File::FILE_OK)); | 52 FROM_HERE, base::Bind(callback, base::File::FILE_OK)); |
| 45 } | 53 } |
| 46 | 54 |
| 47 void FakeProvidedFileSystem::GetMetadata( | 55 void FakeProvidedFileSystem::GetMetadata( |
| 48 const base::FilePath& entry_path, | 56 const base::FilePath& entry_path, |
| 49 const fileapi::AsyncFileUtil::GetFileInfoCallback& callback) { | 57 const fileapi::AsyncFileUtil::GetFileInfoCallback& callback) { |
| 50 // Return fake metadata for the root directory only. | 58 if (entry_path.AsUTF8Unsafe() == "/") { |
| 51 if (entry_path.AsUTF8Unsafe() != "/") { | 59 base::File::Info file_info; |
| 60 file_info.size = 0; |
| 61 file_info.is_directory = true; |
| 62 file_info.is_symbolic_link = false; |
| 63 base::Time last_modified_time; |
| 64 const bool result = base::Time::FromString("Thu Apr 24 00:46:52 UTC 2014", |
| 65 &last_modified_time); |
| 66 DCHECK(result); |
| 67 file_info.last_modified = last_modified_time; |
| 68 |
| 52 base::MessageLoopProxy::current()->PostTask( | 69 base::MessageLoopProxy::current()->PostTask( |
| 53 FROM_HERE, | 70 FROM_HERE, base::Bind(callback, base::File::FILE_OK, file_info)); |
| 54 base::Bind( | |
| 55 callback, base::File::FILE_ERROR_NOT_FOUND, base::File::Info())); | |
| 56 return; | 71 return; |
| 57 } | 72 } |
| 58 | 73 |
| 59 base::File::Info file_info; | 74 if (entry_path.AsUTF8Unsafe() == kFakeFilePath) { |
| 60 file_info.size = 0; | 75 base::File::Info file_info; |
| 61 file_info.is_directory = true; | 76 file_info.size = kFakeFileSize; |
| 62 file_info.is_symbolic_link = false; | 77 file_info.is_directory = false; |
| 63 base::Time last_modified_time; | 78 file_info.is_symbolic_link = false; |
| 64 const bool result = base::Time::FromString("Thu Apr 24 00:46:52 UTC 2014", | 79 base::Time last_modified_time; |
| 65 &last_modified_time); | 80 const bool result = base::Time::FromString("Fri Apr 25 01:47:53 UTC 2014", |
| 66 DCHECK(result); | 81 &last_modified_time); |
| 67 file_info.last_modified = last_modified_time; | 82 DCHECK(result); |
| 83 file_info.last_modified = last_modified_time; |
| 84 |
| 85 base::MessageLoopProxy::current()->PostTask( |
| 86 FROM_HERE, base::Bind(callback, base::File::FILE_OK, file_info)); |
| 87 return; |
| 88 } |
| 68 | 89 |
| 69 base::MessageLoopProxy::current()->PostTask( | 90 base::MessageLoopProxy::current()->PostTask( |
| 70 FROM_HERE, base::Bind(callback, base::File::FILE_OK, file_info)); | 91 FROM_HERE, |
| 92 base::Bind( |
| 93 callback, base::File::FILE_ERROR_NOT_FOUND, base::File::Info())); |
| 71 } | 94 } |
| 72 | 95 |
| 73 void FakeProvidedFileSystem::ReadDirectory( | 96 void FakeProvidedFileSystem::ReadDirectory( |
| 74 const base::FilePath& directory_path, | 97 const base::FilePath& directory_path, |
| 75 const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) { | 98 const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) { |
| 76 // Return fake contents for the root directory only. | 99 // Return fake contents for the root directory only. |
| 77 if (directory_path.AsUTF8Unsafe() != "/") { | 100 if (directory_path.AsUTF8Unsafe() != "/") { |
| 78 base::MessageLoopProxy::current()->PostTask( | 101 base::MessageLoopProxy::current()->PostTask( |
| 79 FROM_HERE, | 102 FROM_HERE, |
| 80 base::Bind(callback, | 103 base::Bind(callback, |
| 81 base::File::FILE_ERROR_NOT_FOUND, | 104 base::File::FILE_ERROR_NOT_FOUND, |
| 82 fileapi::AsyncFileUtil::EntryList(), | 105 fileapi::AsyncFileUtil::EntryList(), |
| 83 false /* has_more */)); | 106 false /* has_more */)); |
| 84 return; | 107 return; |
| 85 } | 108 } |
| 86 | 109 |
| 87 { | 110 { |
| 88 fileapi::AsyncFileUtil::EntryList entry_list; | 111 fileapi::AsyncFileUtil::EntryList entry_list; |
| 89 AddDirectoryEntry(&entry_list, | 112 AddDirectoryEntry(&entry_list, |
| 90 "hello.txt", | 113 kFakeFileName, |
| 91 fileapi::DirectoryEntry::FILE, | 114 fileapi::DirectoryEntry::FILE, |
| 92 1024 /* size */, | 115 kFakeFileSize, |
| 93 "Thu Apr 24 00:46:52 UTC 2014"); | 116 "Thu Apr 24 00:46:52 UTC 2014"); |
| 94 | 117 |
| 95 AddDirectoryEntry(&entry_list, | 118 AddDirectoryEntry(&entry_list, |
| 96 "world.txt", | 119 "world.txt", |
| 97 fileapi::DirectoryEntry::FILE, | 120 fileapi::DirectoryEntry::FILE, |
| 98 1024 /* size */, | 121 1024 /* size */, |
| 99 "Wed Apr 23 00:20:30 UTC 2014"); | 122 "Wed Apr 23 00:20:30 UTC 2014"); |
| 100 | 123 |
| 101 base::MessageLoopProxy::current()->PostTask( | 124 base::MessageLoopProxy::current()->PostTask( |
| 102 FROM_HERE, | 125 FROM_HERE, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 123 OpenFileMode mode, | 146 OpenFileMode mode, |
| 124 bool create, | 147 bool create, |
| 125 const OpenFileCallback& callback) { | 148 const OpenFileCallback& callback) { |
| 126 if (file_path.AsUTF8Unsafe() != "/hello.txt" || | 149 if (file_path.AsUTF8Unsafe() != "/hello.txt" || |
| 127 mode == OPEN_FILE_MODE_WRITE || create) { | 150 mode == OPEN_FILE_MODE_WRITE || create) { |
| 128 callback.Run(0 /* file_handle */, base::File::FILE_ERROR_SECURITY); | 151 callback.Run(0 /* file_handle */, base::File::FILE_ERROR_SECURITY); |
| 129 return; | 152 return; |
| 130 } | 153 } |
| 131 | 154 |
| 132 const int file_handle = ++last_file_handle_; | 155 const int file_handle = ++last_file_handle_; |
| 133 opened_files_.insert(file_handle); | 156 opened_files_[file_handle] = file_path; |
| 134 callback.Run(file_handle, base::File::FILE_OK); | 157 callback.Run(file_handle, base::File::FILE_OK); |
| 135 } | 158 } |
| 136 | 159 |
| 137 void FakeProvidedFileSystem::CloseFile( | 160 void FakeProvidedFileSystem::CloseFile( |
| 138 int file_handle, | 161 int file_handle, |
| 139 const fileapi::AsyncFileUtil::StatusCallback& callback) { | 162 const fileapi::AsyncFileUtil::StatusCallback& callback) { |
| 140 const std::set<int>::iterator opened_file_it = | 163 const OpenedFilesMap::iterator opened_file_it = |
| 141 opened_files_.find(file_handle); | 164 opened_files_.find(file_handle); |
| 142 if (opened_file_it == opened_files_.end()) { | 165 if (opened_file_it == opened_files_.end()) { |
| 143 base::MessageLoopProxy::current()->PostTask( | 166 base::MessageLoopProxy::current()->PostTask( |
| 144 FROM_HERE, base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); | 167 FROM_HERE, base::Bind(callback, base::File::FILE_ERROR_NOT_FOUND)); |
| 145 return; | 168 return; |
| 146 } | 169 } |
| 147 | 170 |
| 148 opened_files_.erase(opened_file_it); | 171 opened_files_.erase(opened_file_it); |
| 149 base::MessageLoopProxy::current()->PostTask( | 172 base::MessageLoopProxy::current()->PostTask( |
| 150 FROM_HERE, base::Bind(callback, base::File::FILE_OK)); | 173 FROM_HERE, base::Bind(callback, base::File::FILE_OK)); |
| 151 } | 174 } |
| 152 | 175 |
| 153 void FakeProvidedFileSystem::ReadFile( | 176 void FakeProvidedFileSystem::ReadFile( |
| 154 int file_handle, | 177 int file_handle, |
| 155 net::IOBuffer* buffer, | 178 net::IOBuffer* buffer, |
| 156 int64 offset, | 179 int64 offset, |
| 157 int length, | 180 int length, |
| 158 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) { | 181 const ProvidedFileSystemInterface::ReadChunkReceivedCallback& callback) { |
| 159 // TODO(mtomasz): Implement together with the FileStreamReader. | 182 const OpenedFilesMap::iterator opened_file_it = |
| 160 base::MessageLoopProxy::current()->PostTask( | 183 opened_files_.find(file_handle); |
| 161 FROM_HERE, | 184 if (opened_file_it == opened_files_.end() || |
| 162 base::Bind(callback, | 185 opened_file_it->second.AsUTF8Unsafe() != kFakeFilePath) { |
| 163 0 /* chunk_length */, | 186 base::MessageLoopProxy::current()->PostTask( |
| 164 false /* has_next */, | 187 FROM_HERE, |
| 165 base::File::FILE_ERROR_SECURITY)); | 188 base::Bind(callback, |
| 189 0 /* chunk_length */, |
| 190 false /* has_next */, |
| 191 base::File::FILE_ERROR_SECURITY)); |
| 192 return; |
| 193 } |
| 194 |
| 195 // Send the response byte by byte. |
| 196 size_t current_offset = static_cast<size_t>(offset); |
| 197 size_t current_length = static_cast<size_t>(length); |
| 198 |
| 199 // Reading behind EOF, is fine, it will just read 0 bytes. |
| 200 if (current_offset > kFakeFileSize || !current_length) { |
| 201 base::MessageLoopProxy::current()->PostTask( |
| 202 FROM_HERE, |
| 203 base::Bind(callback, |
| 204 0 /* chunk_length */, |
| 205 false /* has_next */, |
| 206 base::File::FILE_OK)); |
| 207 } |
| 208 |
| 209 while (current_offset < kFakeFileSize && current_length) { |
| 210 buffer->data()[current_offset - offset] = kFakeFileText[current_offset]; |
| 211 const bool has_next = |
| 212 (current_offset + 1 < kFakeFileSize) && (current_length - 1); |
| 213 callback.Run(1, has_next, base::File::FILE_OK); |
| 214 current_offset++; |
| 215 current_length--; |
| 216 } |
| 166 } | 217 } |
| 167 | 218 |
| 168 const ProvidedFileSystemInfo& FakeProvidedFileSystem::GetFileSystemInfo() | 219 const ProvidedFileSystemInfo& FakeProvidedFileSystem::GetFileSystemInfo() |
| 169 const { | 220 const { |
| 170 return file_system_info_; | 221 return file_system_info_; |
| 171 } | 222 } |
| 172 | 223 |
| 173 RequestManager* FakeProvidedFileSystem::GetRequestManager() { | 224 RequestManager* FakeProvidedFileSystem::GetRequestManager() { |
| 174 NOTREACHED(); | 225 NOTREACHED(); |
| 175 return NULL; | 226 return NULL; |
| 176 } | 227 } |
| 177 | 228 |
| 178 ProvidedFileSystemInterface* FakeProvidedFileSystem::Create( | 229 ProvidedFileSystemInterface* FakeProvidedFileSystem::Create( |
| 179 extensions::EventRouter* event_router, | 230 extensions::EventRouter* event_router, |
| 180 const ProvidedFileSystemInfo& file_system_info) { | 231 const ProvidedFileSystemInfo& file_system_info) { |
| 181 return new FakeProvidedFileSystem(file_system_info); | 232 return new FakeProvidedFileSystem(file_system_info); |
| 182 } | 233 } |
| 183 | 234 |
| 235 base::WeakPtr<ProvidedFileSystemInterface> |
| 236 FakeProvidedFileSystem::GetWeakPtr() { |
| 237 return weak_ptr_factory_.GetWeakPtr(); |
| 238 } |
| 239 |
| 184 } // namespace file_system_provider | 240 } // namespace file_system_provider |
| 185 } // namespace chromeos | 241 } // namespace chromeos |
| OLD | NEW |