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 "base/files/file.h" | 7 #include "base/files/file.h" |
8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
10 | 10 |
11 namespace chromeos { | 11 namespace chromeos { |
12 namespace file_system_provider { | 12 namespace file_system_provider { |
13 namespace { | 13 namespace { |
14 | 14 |
15 const char kFakeFileName[] = "hello.txt"; | 15 const char kFakeFileName[] = "hello.txt"; |
16 const char kFakeFileText[] = | 16 const char kFakeFileText[] = |
17 "This is a testing file. Lorem ipsum dolor sit amet est."; | 17 "This is a testing file. Lorem ipsum dolor sit amet est."; |
18 const size_t kFakeFileSize = sizeof(kFakeFileText) - 1u; | 18 const size_t kFakeFileSize = sizeof(kFakeFileText) - 1u; |
19 const char kFakeFileModificationTime[] = "Fri Apr 25 01:47:53 UTC 2014"; | 19 const char kFakeFileModificationTime[] = "Fri Apr 25 01:47:53 UTC 2014"; |
20 const char kFakeFileMimeType[] = "text/plain"; | 20 const char kFakeFileMimeType[] = "text/plain"; |
21 | 21 |
22 } // namespace | 22 } // namespace |
23 | 23 |
24 const char kFakeFilePath[] = "/hello.txt"; | 24 const char kFakeFilePath[] = "/hello.txt"; |
25 | 25 |
| 26 FakeEntry::FakeEntry() { |
| 27 } |
| 28 |
| 29 FakeEntry::FakeEntry(scoped_ptr<EntryMetadata> metadata, |
| 30 const std::string& contents) |
| 31 : metadata(metadata.Pass()), contents(contents) { |
| 32 } |
| 33 |
| 34 FakeEntry::~FakeEntry() { |
| 35 } |
| 36 |
26 FakeProvidedFileSystem::FakeProvidedFileSystem( | 37 FakeProvidedFileSystem::FakeProvidedFileSystem( |
27 const ProvidedFileSystemInfo& file_system_info) | 38 const ProvidedFileSystemInfo& file_system_info) |
28 : file_system_info_(file_system_info), | 39 : file_system_info_(file_system_info), |
29 last_file_handle_(0), | 40 last_file_handle_(0), |
30 weak_ptr_factory_(this) { | 41 weak_ptr_factory_(this) { |
31 AddEntry( | 42 AddEntry( |
32 base::FilePath::FromUTF8Unsafe("/"), true, "", 0, base::Time(), "", ""); | 43 base::FilePath::FromUTF8Unsafe("/"), true, "", 0, base::Time(), "", ""); |
33 | 44 |
34 base::Time modification_time; | 45 base::Time modification_time; |
35 DCHECK(base::Time::FromString(kFakeFileModificationTime, &modification_time)); | 46 DCHECK(base::Time::FromString(kFakeFileModificationTime, &modification_time)); |
36 AddEntry(base::FilePath::FromUTF8Unsafe(kFakeFilePath), | 47 AddEntry(base::FilePath::FromUTF8Unsafe(kFakeFilePath), |
37 false, | 48 false, |
38 kFakeFileName, | 49 kFakeFileName, |
39 kFakeFileSize, | 50 kFakeFileSize, |
40 modification_time, | 51 modification_time, |
41 kFakeFileMimeType, | 52 kFakeFileMimeType, |
42 kFakeFileText); | 53 kFakeFileText); |
43 } | 54 } |
44 | 55 |
45 FakeProvidedFileSystem::~FakeProvidedFileSystem() {} | 56 FakeProvidedFileSystem::~FakeProvidedFileSystem() {} |
46 | 57 |
47 void FakeProvidedFileSystem::AddEntry(const base::FilePath& entry_path, | 58 void FakeProvidedFileSystem::AddEntry(const base::FilePath& entry_path, |
48 bool is_directory, | 59 bool is_directory, |
49 const std::string& name, | 60 const std::string& name, |
50 int64 size, | 61 int64 size, |
51 base::Time modification_time, | 62 base::Time modification_time, |
52 std::string mime_type, | 63 std::string mime_type, |
53 std::string contents) { | 64 std::string contents) { |
54 DCHECK(entries_.find(entry_path) == entries_.end()); | 65 DCHECK(entries_.find(entry_path) == entries_.end()); |
55 EntryMetadata metadata; | 66 scoped_ptr<EntryMetadata> metadata(new EntryMetadata); |
56 | 67 |
57 metadata.is_directory = is_directory; | 68 metadata->is_directory = is_directory; |
58 metadata.name = name; | 69 metadata->name = name; |
59 metadata.size = size; | 70 metadata->size = size; |
60 metadata.modification_time = modification_time; | 71 metadata->modification_time = modification_time; |
61 metadata.mime_type = mime_type; | 72 metadata->mime_type = mime_type; |
62 | 73 |
63 entries_[entry_path] = FakeEntry(metadata, contents); | 74 entries_[entry_path] = |
| 75 make_linked_ptr(new FakeEntry(metadata.Pass(), contents)); |
64 } | 76 } |
65 | 77 |
66 bool FakeProvidedFileSystem::GetEntry(const base::FilePath& entry_path, | 78 const FakeEntry* FakeProvidedFileSystem::GetEntry( |
67 FakeEntry* fake_entry) const { | 79 const base::FilePath& entry_path) const { |
68 const Entries::const_iterator entry_it = entries_.find(entry_path); | 80 const Entries::const_iterator entry_it = entries_.find(entry_path); |
69 if (entry_it == entries_.end()) | 81 if (entry_it == entries_.end()) |
70 return false; | 82 return NULL; |
71 | 83 |
72 *fake_entry = entry_it->second; | 84 return entry_it->second.get(); |
73 return true; | |
74 } | 85 } |
75 | 86 |
76 ProvidedFileSystemInterface::AbortCallback | 87 ProvidedFileSystemInterface::AbortCallback |
77 FakeProvidedFileSystem::RequestUnmount( | 88 FakeProvidedFileSystem::RequestUnmount( |
78 const storage::AsyncFileUtil::StatusCallback& callback) { | 89 const storage::AsyncFileUtil::StatusCallback& callback) { |
79 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); | 90 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); |
80 } | 91 } |
81 | 92 |
82 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::GetMetadata( | 93 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::GetMetadata( |
83 const base::FilePath& entry_path, | 94 const base::FilePath& entry_path, |
| 95 ProvidedFileSystemInterface::MetadataFieldMask fields, |
84 const ProvidedFileSystemInterface::GetMetadataCallback& callback) { | 96 const ProvidedFileSystemInterface::GetMetadataCallback& callback) { |
85 const Entries::const_iterator entry_it = entries_.find(entry_path); | 97 const Entries::const_iterator entry_it = entries_.find(entry_path); |
86 | 98 |
87 if (entry_it == entries_.end()) { | 99 if (entry_it == entries_.end()) { |
88 return PostAbortableTask(base::Bind( | 100 return PostAbortableTask( |
89 callback, EntryMetadata(), base::File::FILE_ERROR_NOT_FOUND)); | 101 base::Bind(callback, |
| 102 base::Passed(make_scoped_ptr<EntryMetadata>(NULL)), |
| 103 base::File::FILE_ERROR_NOT_FOUND)); |
90 } | 104 } |
91 | 105 |
| 106 scoped_ptr<EntryMetadata> metadata(new EntryMetadata); |
| 107 metadata->is_directory = entry_it->second->metadata->is_directory; |
| 108 metadata->name = entry_it->second->metadata->name; |
| 109 metadata->size = entry_it->second->metadata->size; |
| 110 metadata->modification_time = entry_it->second->metadata->modification_time; |
| 111 metadata->mime_type = entry_it->second->metadata->mime_type; |
| 112 metadata->thumbnail = entry_it->second->metadata->thumbnail; |
| 113 |
92 return PostAbortableTask( | 114 return PostAbortableTask( |
93 base::Bind(callback, entry_it->second.metadata, base::File::FILE_OK)); | 115 base::Bind(callback, base::Passed(&metadata), base::File::FILE_OK)); |
94 } | 116 } |
95 | 117 |
96 ProvidedFileSystemInterface::AbortCallback | 118 ProvidedFileSystemInterface::AbortCallback |
97 FakeProvidedFileSystem::ReadDirectory( | 119 FakeProvidedFileSystem::ReadDirectory( |
98 const base::FilePath& directory_path, | 120 const base::FilePath& directory_path, |
99 const storage::AsyncFileUtil::ReadDirectoryCallback& callback) { | 121 const storage::AsyncFileUtil::ReadDirectoryCallback& callback) { |
100 storage::AsyncFileUtil::EntryList entry_list; | 122 storage::AsyncFileUtil::EntryList entry_list; |
101 | 123 |
102 for (Entries::const_iterator it = entries_.begin(); it != entries_.end(); | 124 for (Entries::const_iterator it = entries_.begin(); it != entries_.end(); |
103 ++it) { | 125 ++it) { |
104 const base::FilePath file_path = it->first; | 126 const base::FilePath file_path = it->first; |
105 if (file_path == directory_path || directory_path.IsParent(file_path)) { | 127 if (file_path == directory_path || directory_path.IsParent(file_path)) { |
106 const EntryMetadata& metadata = it->second.metadata; | 128 const EntryMetadata* const metadata = it->second->metadata.get(); |
107 entry_list.push_back(storage::DirectoryEntry( | 129 entry_list.push_back(storage::DirectoryEntry( |
108 metadata.name, | 130 metadata->name, |
109 metadata.is_directory ? storage::DirectoryEntry::DIRECTORY | 131 metadata->is_directory ? storage::DirectoryEntry::DIRECTORY |
110 : storage::DirectoryEntry::FILE, | 132 : storage::DirectoryEntry::FILE, |
111 metadata.size, | 133 metadata->size, |
112 metadata.modification_time)); | 134 metadata->modification_time)); |
113 } | 135 } |
114 } | 136 } |
115 | 137 |
116 return PostAbortableTask(base::Bind( | 138 return PostAbortableTask(base::Bind( |
117 callback, base::File::FILE_OK, entry_list, false /* has_more */)); | 139 callback, base::File::FILE_OK, entry_list, false /* has_more */)); |
118 } | 140 } |
119 | 141 |
120 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::OpenFile( | 142 ProvidedFileSystemInterface::AbortCallback FakeProvidedFileSystem::OpenFile( |
121 const base::FilePath& entry_path, | 143 const base::FilePath& entry_path, |
122 OpenFileMode mode, | 144 OpenFileMode mode, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 0 /* chunk_length */, | 197 0 /* chunk_length */, |
176 false /* has_more */, | 198 false /* has_more */, |
177 base::File::FILE_ERROR_INVALID_OPERATION)); | 199 base::File::FILE_ERROR_INVALID_OPERATION)); |
178 } | 200 } |
179 | 201 |
180 // Send the response byte by byte. | 202 // Send the response byte by byte. |
181 int64 current_offset = offset; | 203 int64 current_offset = offset; |
182 int current_length = length; | 204 int current_length = length; |
183 | 205 |
184 // Reading behind EOF is fine, it will just return 0 bytes. | 206 // Reading behind EOF is fine, it will just return 0 bytes. |
185 if (current_offset >= entry_it->second.metadata.size || !current_length) { | 207 if (current_offset >= entry_it->second->metadata->size || !current_length) { |
186 return PostAbortableTask(base::Bind(callback, | 208 return PostAbortableTask(base::Bind(callback, |
187 0 /* chunk_length */, | 209 0 /* chunk_length */, |
188 false /* has_more */, | 210 false /* has_more */, |
189 base::File::FILE_OK)); | 211 base::File::FILE_OK)); |
190 } | 212 } |
191 | 213 |
192 const FakeEntry& entry = entry_it->second; | 214 const FakeEntry* const entry = entry_it->second.get(); |
193 std::vector<int> task_ids; | 215 std::vector<int> task_ids; |
194 while (current_offset < entry.metadata.size && current_length) { | 216 while (current_offset < entry->metadata->size && current_length) { |
195 buffer->data()[current_offset - offset] = entry.contents[current_offset]; | 217 buffer->data()[current_offset - offset] = entry->contents[current_offset]; |
196 const bool has_more = | 218 const bool has_more = |
197 (current_offset + 1 < entry.metadata.size) && (current_length - 1); | 219 (current_offset + 1 < entry->metadata->size) && (current_length - 1); |
198 const int task_id = tracker_.PostTask( | 220 const int task_id = tracker_.PostTask( |
199 base::MessageLoopProxy::current(), | 221 base::MessageLoopProxy::current(), |
200 FROM_HERE, | 222 FROM_HERE, |
201 base::Bind( | 223 base::Bind( |
202 callback, 1 /* chunk_length */, has_more, base::File::FILE_OK)); | 224 callback, 1 /* chunk_length */, has_more, base::File::FILE_OK)); |
203 task_ids.push_back(task_id); | 225 task_ids.push_back(task_id); |
204 current_offset++; | 226 current_offset++; |
205 current_length--; | 227 current_length--; |
206 } | 228 } |
207 | 229 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 return PostAbortableTask( | 298 return PostAbortableTask( |
277 base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION)); | 299 base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION)); |
278 } | 300 } |
279 | 301 |
280 const Entries::iterator entry_it = entries_.find(opened_file_it->second); | 302 const Entries::iterator entry_it = entries_.find(opened_file_it->second); |
281 if (entry_it == entries_.end()) { | 303 if (entry_it == entries_.end()) { |
282 return PostAbortableTask( | 304 return PostAbortableTask( |
283 base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION)); | 305 base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION)); |
284 } | 306 } |
285 | 307 |
286 FakeEntry* const entry = &entry_it->second; | 308 FakeEntry* const entry = entry_it->second.get(); |
287 if (offset > entry->metadata.size) { | 309 if (offset > entry->metadata->size) { |
288 return PostAbortableTask( | 310 return PostAbortableTask( |
289 base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION)); | 311 base::Bind(callback, base::File::FILE_ERROR_INVALID_OPERATION)); |
290 } | 312 } |
291 | 313 |
292 // Allocate the string size in advance. | 314 // Allocate the string size in advance. |
293 if (offset + length > entry->metadata.size) { | 315 if (offset + length > entry->metadata->size) { |
294 entry->metadata.size = offset + length; | 316 entry->metadata->size = offset + length; |
295 entry->contents.resize(entry->metadata.size); | 317 entry->contents.resize(entry->metadata->size); |
296 } | 318 } |
297 | 319 |
298 entry->contents.replace(offset, length, buffer->data(), length); | 320 entry->contents.replace(offset, length, buffer->data(), length); |
299 | 321 |
300 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); | 322 return PostAbortableTask(base::Bind(callback, base::File::FILE_OK)); |
301 } | 323 } |
302 | 324 |
303 const ProvidedFileSystemInfo& FakeProvidedFileSystem::GetFileSystemInfo() | 325 const ProvidedFileSystemInfo& FakeProvidedFileSystem::GetFileSystemInfo() |
304 const { | 326 const { |
305 return file_system_info_; | 327 return file_system_info_; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 const std::vector<int>& task_ids, | 362 const std::vector<int>& task_ids, |
341 const storage::AsyncFileUtil::StatusCallback& callback) { | 363 const storage::AsyncFileUtil::StatusCallback& callback) { |
342 for (size_t i = 0; i < task_ids.size(); ++i) { | 364 for (size_t i = 0; i < task_ids.size(); ++i) { |
343 tracker_.TryCancel(task_ids[i]); | 365 tracker_.TryCancel(task_ids[i]); |
344 } | 366 } |
345 callback.Run(base::File::FILE_OK); | 367 callback.Run(base::File::FILE_OK); |
346 } | 368 } |
347 | 369 |
348 } // namespace file_system_provider | 370 } // namespace file_system_provider |
349 } // namespace chromeos | 371 } // namespace chromeos |
OLD | NEW |