Home | History | Annotate | Download | only in operations
      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/get_metadata.h"
      6 
      7 #include <string>
      8 
      9 #include "chrome/common/extensions/api/file_system_provider.h"
     10 #include "chrome/common/extensions/api/file_system_provider_internal.h"
     11 
     12 namespace chromeos {
     13 namespace file_system_provider {
     14 namespace operations {
     15 namespace {
     16 
     17 // Convert |value| into |output|. If parsing fails, then returns false.
     18 bool ConvertRequestValueToFileInfo(scoped_ptr<RequestValue> value,
     19                                    base::File::Info* output) {
     20   using extensions::api::file_system_provider::EntryMetadata;
     21   using extensions::api::file_system_provider_internal::
     22       GetMetadataRequestedSuccess::Params;
     23 
     24   const Params* params = value->get_metadata_success_params();
     25   if (!params)
     26     return false;
     27 
     28   output->is_directory = params->metadata.is_directory;
     29   output->size = static_cast<int64>(params->metadata.size);
     30   output->is_symbolic_link = false;  // Not supported.
     31 
     32   std::string input_modification_time;
     33   if (!params->metadata.modification_time.additional_properties.GetString(
     34           "value", &input_modification_time)) {
     35     return false;
     36   }
     37 
     38   // Allow to pass invalid modification time, since there is no way to verify
     39   // it easily on any earlier stage.
     40   base::Time::FromString(input_modification_time.c_str(),
     41                          &output->last_modified);
     42 
     43   return true;
     44 }
     45 
     46 }  // namespace
     47 
     48 GetMetadata::GetMetadata(
     49     extensions::EventRouter* event_router,
     50     const ProvidedFileSystemInfo& file_system_info,
     51     const base::FilePath& entry_path,
     52     const fileapi::AsyncFileUtil::GetFileInfoCallback& callback)
     53     : Operation(event_router, file_system_info),
     54       entry_path_(entry_path),
     55       callback_(callback) {
     56 }
     57 
     58 GetMetadata::~GetMetadata() {
     59 }
     60 
     61 bool GetMetadata::Execute(int request_id) {
     62   scoped_ptr<base::DictionaryValue> values(new base::DictionaryValue);
     63   values->SetString("entryPath", entry_path_.AsUTF8Unsafe());
     64   return SendEvent(
     65       request_id,
     66       extensions::api::file_system_provider::OnGetMetadataRequested::kEventName,
     67       values.Pass());
     68 }
     69 
     70 void GetMetadata::OnSuccess(int /* request_id */,
     71                             scoped_ptr<RequestValue> result,
     72                             bool has_more) {
     73   base::File::Info file_info;
     74   const bool convert_result =
     75       ConvertRequestValueToFileInfo(result.Pass(), &file_info);
     76   DCHECK(convert_result);
     77   callback_.Run(base::File::FILE_OK, file_info);
     78 }
     79 
     80 void GetMetadata::OnError(int /* request_id */, base::File::Error error) {
     81   callback_.Run(error, base::File::Info());
     82 }
     83 
     84 }  // namespace operations
     85 }  // namespace file_system_provider
     86 }  // namespace chromeos
     87