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 / get_metadata.cc
blobb67c0ebc35c4d2227ec4fd1cf3a9fc175ad0f42a
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/get_metadata.h"
7 #include <algorithm>
8 #include <string>
10 #include "chrome/common/extensions/api/file_system_provider.h"
11 #include "chrome/common/extensions/api/file_system_provider_internal.h"
13 namespace chromeos {
14 namespace file_system_provider {
15 namespace operations {
16 namespace {
18 // Convert |value| into |output|. If parsing fails, then returns false.
19 bool ConvertRequestValueToFileInfo(scoped_ptr<RequestValue> value,
20 bool root_entry,
21 EntryMetadata* output) {
22 using extensions::api::file_system_provider::EntryMetadata;
23 using extensions::api::file_system_provider_internal::
24 GetMetadataRequestedSuccess::Params;
26 const Params* params = value->get_metadata_success_params();
27 if (!params)
28 return false;
30 if (!ValidateIDLEntryMetadata(params->metadata, root_entry))
31 return false;
33 output->name = params->metadata.name;
34 output->is_directory = params->metadata.is_directory;
35 output->size = static_cast<int64>(params->metadata.size);
37 std::string input_modification_time;
38 if (!params->metadata.modification_time.additional_properties.GetString(
39 "value", &input_modification_time)) {
40 NOTREACHED();
43 // Allow to pass invalid modification time, since there is no way to verify
44 // it easily on any earlier stage.
45 base::Time::FromString(input_modification_time.c_str(),
46 &output->modification_time);
48 if (params->metadata.mime_type.get())
49 output->mime_type = *params->metadata.mime_type.get();
51 if (params->metadata.thumbnail.get())
52 output->thumbnail = *params->metadata.thumbnail.get();
54 return true;
57 } // namespace
59 bool ValidateIDLEntryMetadata(
60 const extensions::api::file_system_provider::EntryMetadata& metadata,
61 bool root_entry) {
62 using extensions::api::file_system_provider::EntryMetadata;
64 if (!ValidateName(metadata.name, root_entry))
65 return false;
67 std::string input_modification_time;
68 if (!metadata.modification_time.additional_properties.GetString(
69 "value", &input_modification_time)) {
70 return false;
73 if (metadata.thumbnail.get()) {
74 // Sanity check for the thumbnail format. Note, that another, more granural
75 // check is done in custom bindings. Note, this is an extra check only for
76 // the security reasons.
77 const std::string expected_prefix = "data:";
78 std::string thumbnail_prefix =
79 metadata.thumbnail.get()->substr(0, expected_prefix.size());
80 std::transform(thumbnail_prefix.begin(),
81 thumbnail_prefix.end(),
82 thumbnail_prefix.begin(),
83 ::tolower);
85 if (expected_prefix != thumbnail_prefix)
86 return false;
89 return true;
92 bool ValidateName(const std::string& name, bool root_entry) {
93 if (root_entry)
94 return name.empty();
95 return !name.empty() && name.find('/') == std::string::npos;
98 GetMetadata::GetMetadata(
99 extensions::EventRouter* event_router,
100 const ProvidedFileSystemInfo& file_system_info,
101 const base::FilePath& entry_path,
102 ProvidedFileSystemInterface::MetadataFieldMask fields,
103 const ProvidedFileSystemInterface::GetMetadataCallback& callback)
104 : Operation(event_router, file_system_info),
105 entry_path_(entry_path),
106 fields_(fields),
107 callback_(callback) {
110 GetMetadata::~GetMetadata() {
113 bool GetMetadata::Execute(int request_id) {
114 using extensions::api::file_system_provider::GetMetadataRequestedOptions;
116 GetMetadataRequestedOptions options;
117 options.file_system_id = file_system_info_.file_system_id();
118 options.request_id = request_id;
119 options.entry_path = entry_path_.AsUTF8Unsafe();
120 options.thumbnail =
121 fields_ & ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL;
123 return SendEvent(
124 request_id,
125 extensions::api::file_system_provider::OnGetMetadataRequested::kEventName,
126 extensions::api::file_system_provider::OnGetMetadataRequested::Create(
127 options));
130 void GetMetadata::OnSuccess(int /* request_id */,
131 scoped_ptr<RequestValue> result,
132 bool has_more) {
133 scoped_ptr<EntryMetadata> metadata(new EntryMetadata);
134 const bool convert_result = ConvertRequestValueToFileInfo(
135 result.Pass(), entry_path_.AsUTF8Unsafe() == FILE_PATH_LITERAL("/"),
136 metadata.get());
138 if (!convert_result) {
139 LOG(ERROR) << "Failed to parse a response for the get metadata operation.";
140 callback_.Run(make_scoped_ptr<EntryMetadata>(NULL),
141 base::File::FILE_ERROR_IO);
142 return;
145 callback_.Run(metadata.Pass(), base::File::FILE_OK);
148 void GetMetadata::OnError(int /* request_id */,
149 scoped_ptr<RequestValue> /* result */,
150 base::File::Error error) {
151 callback_.Run(make_scoped_ptr<EntryMetadata>(NULL), error);
153 } // namespace operations
154 } // namespace file_system_provider
155 } // namespace chromeos