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"
10 #include "chrome/common/extensions/api/file_system_provider.h"
11 #include "chrome/common/extensions/api/file_system_provider_internal.h"
14 namespace file_system_provider
{
15 namespace operations
{
18 // Convert |value| into |output|. If parsing fails, then returns false.
19 bool ConvertRequestValueToFileInfo(scoped_ptr
<RequestValue
> value
,
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();
30 if (!ValidateIDLEntryMetadata(params
->metadata
, root_entry
))
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
)) {
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();
59 bool ValidateIDLEntryMetadata(
60 const extensions::api::file_system_provider::EntryMetadata
& metadata
,
62 using extensions::api::file_system_provider::EntryMetadata
;
64 if (!ValidateName(metadata
.name
, root_entry
))
67 std::string input_modification_time
;
68 if (!metadata
.modification_time
.additional_properties
.GetString(
69 "value", &input_modification_time
)) {
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(),
85 if (expected_prefix
!= thumbnail_prefix
)
92 bool ValidateName(const std::string
& name
, bool root_entry
) {
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
),
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();
121 fields_
& ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL
;
125 extensions::events::FILE_SYSTEM_PROVIDER_ON_GET_METADATA_REQUESTED
,
126 extensions::api::file_system_provider::OnGetMetadataRequested::kEventName
,
127 extensions::api::file_system_provider::OnGetMetadataRequested::Create(
131 void GetMetadata::OnSuccess(int /* request_id */,
132 scoped_ptr
<RequestValue
> result
,
134 scoped_ptr
<EntryMetadata
> metadata(new EntryMetadata
);
135 const bool convert_result
= ConvertRequestValueToFileInfo(
136 result
.Pass(), entry_path_
.AsUTF8Unsafe() == FILE_PATH_LITERAL("/"),
139 if (!convert_result
) {
140 LOG(ERROR
) << "Failed to parse a response for the get metadata operation.";
141 callback_
.Run(make_scoped_ptr
<EntryMetadata
>(NULL
),
142 base::File::FILE_ERROR_IO
);
146 callback_
.Run(metadata
.Pass(), base::File::FILE_OK
);
149 void GetMetadata::OnError(int /* request_id */,
150 scoped_ptr
<RequestValue
> /* result */,
151 base::File::Error error
) {
152 callback_
.Run(make_scoped_ptr
<EntryMetadata
>(NULL
), error
);
154 } // namespace operations
155 } // namespace file_system_provider
156 } // namespace chromeos