Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / operations / read_directory.cc
blobb80e1f60aac21c734feaa73c083156968a518750
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.
5 #include "chrome/browser/chromeos/file_system_provider/operations/read_directory.h"
7 #include <string>
9 #include "base/memory/linked_ptr.h"
10 #include "chrome/browser/chromeos/file_system_provider/operations/get_metadata.h"
11 #include "chrome/common/extensions/api/file_system_provider.h"
12 #include "chrome/common/extensions/api/file_system_provider_internal.h"
14 namespace chromeos {
15 namespace file_system_provider {
16 namespace operations {
17 namespace {
19 // Convert |input| into |output|. If parsing fails, then returns false.
20 bool ConvertRequestValueToEntryList(scoped_ptr<RequestValue> value,
21 storage::AsyncFileUtil::EntryList* output) {
22 using extensions::api::file_system_provider::EntryMetadata;
23 using extensions::api::file_system_provider_internal::
24 ReadDirectoryRequestedSuccess::Params;
26 const Params* params = value->read_directory_success_params();
27 if (!params)
28 return false;
30 for (size_t i = 0; i < params->entries.size(); ++i) {
31 const linked_ptr<EntryMetadata> entry_metadata = params->entries[i];
32 if (!ValidateIDLEntryMetadata(*entry_metadata, false /* root_entry */))
33 return false;
35 storage::DirectoryEntry output_entry;
36 output_entry.is_directory = entry_metadata->is_directory;
37 output_entry.name = entry_metadata->name;
38 output_entry.size = static_cast<int64>(entry_metadata->size);
40 std::string input_modification_time;
41 if (!entry_metadata->modification_time.additional_properties.GetString(
42 "value", &input_modification_time)) {
43 NOTREACHED();
45 if (!base::Time::FromString(input_modification_time.c_str(),
46 &output_entry.last_modified_time)) {
47 NOTREACHED();
50 output->push_back(output_entry);
53 return true;
56 } // namespace
58 ReadDirectory::ReadDirectory(
59 extensions::EventRouter* event_router,
60 const ProvidedFileSystemInfo& file_system_info,
61 const base::FilePath& directory_path,
62 const storage::AsyncFileUtil::ReadDirectoryCallback& callback)
63 : Operation(event_router, file_system_info),
64 directory_path_(directory_path),
65 callback_(callback) {
68 ReadDirectory::~ReadDirectory() {
71 bool ReadDirectory::Execute(int request_id) {
72 using extensions::api::file_system_provider::ReadDirectoryRequestedOptions;
74 ReadDirectoryRequestedOptions options;
75 options.file_system_id = file_system_info_.file_system_id();
76 options.request_id = request_id;
77 options.directory_path = directory_path_.AsUTF8Unsafe();
79 return SendEvent(
80 request_id,
81 extensions::api::file_system_provider::OnReadDirectoryRequested::
82 kEventName,
83 extensions::api::file_system_provider::OnReadDirectoryRequested::Create(
84 options));
87 void ReadDirectory::OnSuccess(int /* request_id */,
88 scoped_ptr<RequestValue> result,
89 bool has_more) {
90 storage::AsyncFileUtil::EntryList entry_list;
91 const bool convert_result =
92 ConvertRequestValueToEntryList(result.Pass(), &entry_list);
94 if (!convert_result) {
95 LOG(ERROR)
96 << "Failed to parse a response for the read directory operation.";
97 callback_.Run(base::File::FILE_ERROR_IO,
98 storage::AsyncFileUtil::EntryList(),
99 false /* has_more */);
100 return;
103 callback_.Run(base::File::FILE_OK, entry_list, has_more);
106 void ReadDirectory::OnError(int /* request_id */,
107 scoped_ptr<RequestValue> /* result */,
108 base::File::Error error) {
109 callback_.Run(
110 error, storage::AsyncFileUtil::EntryList(), false /* has_more */);
113 } // namespace operations
114 } // namespace file_system_provider
115 } // namespace chromeos