OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/file_system_provider/operations/read_directory
.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/memory/linked_ptr.h" |
| 10 #include "chrome/common/extensions/api/file_system_provider.h" |
| 11 #include "chrome/common/extensions/api/file_system_provider_internal.h" |
| 12 |
| 13 namespace chromeos { |
| 14 namespace file_system_provider { |
| 15 namespace operations { |
| 16 namespace { |
| 17 |
| 18 // Convert |input| into |output|. If parsing fails, then returns false. |
| 19 bool ConvertRequestValueToEntryList(scoped_ptr<RequestValue> value, |
| 20 fileapi::AsyncFileUtil::EntryList* output) { |
| 21 using extensions::api::file_system_provider::EntryMetadata; |
| 22 using extensions::api::file_system_provider_internal:: |
| 23 ReadDirectoryRequestedSuccess::Params; |
| 24 |
| 25 const Params* params = value->read_directory_success_params(); |
| 26 if (!params) |
| 27 return false; |
| 28 |
| 29 for (size_t i = 0; i < params->entries.size(); ++i) { |
| 30 const linked_ptr<EntryMetadata> entry_metadata = params->entries[i]; |
| 31 |
| 32 fileapi::DirectoryEntry output_entry; |
| 33 output_entry.is_directory = entry_metadata->is_directory; |
| 34 output_entry.name = entry_metadata->name; |
| 35 output_entry.size = static_cast<int64>(entry_metadata->size); |
| 36 |
| 37 std::string input_modification_time; |
| 38 if (!entry_metadata->modification_time.additional_properties.GetString( |
| 39 "value", &input_modification_time)) { |
| 40 return false; |
| 41 } |
| 42 if (!base::Time::FromString(input_modification_time.c_str(), |
| 43 &output_entry.last_modified_time)) { |
| 44 return false; |
| 45 } |
| 46 |
| 47 output->push_back(output_entry); |
| 48 } |
| 49 |
| 50 return true; |
| 51 } |
| 52 |
| 53 } // namespace |
| 54 |
| 55 ReadDirectory::ReadDirectory( |
| 56 extensions::EventRouter* event_router, |
| 57 const ProvidedFileSystemInfo& file_system_info, |
| 58 const base::FilePath& directory_path, |
| 59 const fileapi::AsyncFileUtil::ReadDirectoryCallback& callback) |
| 60 : Operation(event_router, file_system_info), |
| 61 directory_path_(directory_path), |
| 62 callback_(callback) { |
| 63 } |
| 64 |
| 65 ReadDirectory::~ReadDirectory() { |
| 66 } |
| 67 |
| 68 bool ReadDirectory::Execute(int request_id) { |
| 69 scoped_ptr<base::ListValue> values(new base::ListValue); |
| 70 values->AppendString(directory_path_.AsUTF8Unsafe()); |
| 71 return SendEvent(request_id, |
| 72 extensions::api::file_system_provider:: |
| 73 OnReadDirectoryRequested::kEventName, |
| 74 values.Pass()); |
| 75 } |
| 76 |
| 77 void ReadDirectory::OnSuccess(int /* request_id */, |
| 78 scoped_ptr<RequestValue> result, |
| 79 bool has_next) { |
| 80 fileapi::AsyncFileUtil::EntryList entry_list; |
| 81 const bool convert_result = |
| 82 ConvertRequestValueToEntryList(result.Pass(), &entry_list); |
| 83 DCHECK(convert_result); |
| 84 callback_.Run(base::File::FILE_OK, entry_list, has_next); |
| 85 } |
| 86 |
| 87 void ReadDirectory::OnError(int /* request_id */, base::File::Error error) { |
| 88 callback_.Run( |
| 89 error, fileapi::AsyncFileUtil::EntryList(), false /* has_next */); |
| 90 } |
| 91 |
| 92 } // namespace operations |
| 93 } // namespace file_system_provider |
| 94 } // namespace chromeos |
OLD | NEW |