File: get_metadata.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (238 lines) | stat: -rw-r--r-- 8,607 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/file_system_provider/operations/get_metadata.h"

#include <stdint.h>

#include <algorithm>
#include <memory>
#include <string>
#include <tuple>
#include <utility>

#include "base/time/time.h"
#include "chrome/browser/ash/file_system_provider/provided_file_system_interface.h"
#include "chrome/common/extensions/api/file_system_provider.h"
#include "chrome/common/extensions/api/file_system_provider_internal.h"

namespace ash::file_system_provider::operations {
namespace {

// Convert |value| into |output|. If parsing fails, then returns false.
bool ConvertRequestValueToFileInfo(const RequestValue& value,
                                   int fields,
                                   bool root_entry,
                                   EntryMetadata* output) {
  using extensions::api::file_system_provider::EntryMetadata;
  using extensions::api::file_system_provider_internal::
      GetMetadataRequestedSuccess::Params;

  const Params* params = value.get_metadata_success_params();
  if (!params)
    return false;

  if (!ValidateIDLEntryMetadata(params->metadata, fields, root_entry))
    return false;

  if (fields & ProvidedFileSystemInterface::METADATA_FIELD_NAME)
    output->name = std::make_unique<std::string>(*params->metadata.name);

  if (fields & ProvidedFileSystemInterface::METADATA_FIELD_IS_DIRECTORY)
    output->is_directory =
        std::make_unique<bool>(*params->metadata.is_directory);

  if (fields & ProvidedFileSystemInterface::METADATA_FIELD_SIZE)
    output->size =
        std::make_unique<int64_t>(static_cast<int64_t>(*params->metadata.size));

  if (fields & ProvidedFileSystemInterface::METADATA_FIELD_MODIFICATION_TIME) {
    const std::string* input_modification_time =
        params->metadata.modification_time->additional_properties.FindString(
            "value");

    if (input_modification_time) {
      // Allow to pass invalid modification time, since there is no way to
      // verify it easily on any earlier stage.
      base::Time output_modification_time;
      std::ignore = base::Time::FromString(input_modification_time->c_str(),
                                           &output_modification_time);
      output->modification_time =
          std::make_unique<base::Time>(output_modification_time);
    }
  }

  if (fields & ProvidedFileSystemInterface::METADATA_FIELD_MIME_TYPE &&
      params->metadata.mime_type) {
    output->mime_type =
        std::make_unique<std::string>(*params->metadata.mime_type);
  }

  if (fields & ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL &&
      params->metadata.thumbnail) {
    output->thumbnail =
        std::make_unique<std::string>(*params->metadata.thumbnail);
  }

  if (fields & ProvidedFileSystemInterface::METADATA_FIELD_CLOUD_IDENTIFIER &&
      params->metadata.cloud_identifier) {
    output->cloud_identifier = std::make_unique<CloudIdentifier>(
        params->metadata.cloud_identifier->provider_name,
        params->metadata.cloud_identifier->id);
  }

  if (fields & ProvidedFileSystemInterface::METADATA_FIELD_CLOUD_FILE_INFO &&
      params->metadata.cloud_file_info &&
      params->metadata.cloud_file_info->version_tag.has_value()) {
    output->cloud_file_info = std::make_unique<CloudFileInfo>(
        params->metadata.cloud_file_info->version_tag.value());
  }

  return true;
}

}  // namespace

bool ValidateIDLEntryMetadata(
    const extensions::api::file_system_provider::EntryMetadata& metadata,
    int fields,
    bool root_entry) {
  using extensions::api::file_system_provider::EntryMetadata;

  if (fields & ProvidedFileSystemInterface::METADATA_FIELD_IS_DIRECTORY &&
      !metadata.is_directory) {
    return false;
  }

  if (fields & ProvidedFileSystemInterface::METADATA_FIELD_NAME &&
      (!metadata.name || !ValidateName(*metadata.name, root_entry))) {
    return false;
  }

  if (fields & ProvidedFileSystemInterface::METADATA_FIELD_SIZE &&
      !metadata.size) {
    return false;
  }

  if (fields & ProvidedFileSystemInterface::METADATA_FIELD_MODIFICATION_TIME) {
    if (!metadata.modification_time)
      return false;
    const std::string* input_modification_time =
        metadata.modification_time->additional_properties.FindString("value");
    if (!input_modification_time) {
      return false;
    }
  }

  // Empty MIME type is not allowed, but for backward compability it's
  // accepted. Note, that there is a warning in custom bindings for it.

  if (fields & ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL &&
      metadata.thumbnail) {
    // Sanity check for the thumbnail format. Note, that another, more
    // granural check is done in custom bindings. Note, this is an extra check
    // only for the security reasons.
    const std::string expected_prefix = "data:";
    std::string thumbnail_prefix =
        metadata.thumbnail->substr(0, expected_prefix.size());
    std::ranges::transform(thumbnail_prefix, thumbnail_prefix.begin(),
                           ::tolower);

    if (expected_prefix != thumbnail_prefix)
      return false;
  }

  if (fields & ProvidedFileSystemInterface::METADATA_FIELD_CLOUD_IDENTIFIER &&
      (!metadata.cloud_identifier ||
       !ValidateCloudIdentifier(*metadata.cloud_identifier))) {
    return false;
  }

  return true;
}

bool ValidateName(const std::string& name, bool root_entry) {
  if (root_entry)
    return name.empty();
  return !name.empty() && name.find('/') == std::string::npos;
}

bool ValidateCloudIdentifier(
    const extensions::api::file_system_provider::CloudIdentifier&
        cloud_identifier) {
  return !cloud_identifier.provider_name.empty() &&
         !cloud_identifier.id.empty();
}

GetMetadata::GetMetadata(
    RequestDispatcher* dispatcher,
    const ProvidedFileSystemInfo& file_system_info,
    const base::FilePath& entry_path,
    ProvidedFileSystemInterface::MetadataFieldMask fields,
    ProvidedFileSystemInterface::GetMetadataCallback callback)
    : Operation(dispatcher, file_system_info),
      entry_path_(entry_path),
      fields_(fields),
      callback_(std::move(callback)) {
  DCHECK_NE(0, fields_);
}

GetMetadata::~GetMetadata() = default;

bool GetMetadata::Execute(int request_id) {
  using extensions::api::file_system_provider::GetMetadataRequestedOptions;

  GetMetadataRequestedOptions options;
  options.file_system_id = file_system_info_.file_system_id();
  options.request_id = request_id;
  options.entry_path = entry_path_.AsUTF8Unsafe();
  options.is_directory =
      fields_ & ProvidedFileSystemInterface::METADATA_FIELD_IS_DIRECTORY;
  options.name = fields_ & ProvidedFileSystemInterface::METADATA_FIELD_NAME;
  options.size = fields_ & ProvidedFileSystemInterface::METADATA_FIELD_SIZE;
  options.modification_time =
      fields_ & ProvidedFileSystemInterface::METADATA_FIELD_MODIFICATION_TIME;
  options.mime_type =
      fields_ & ProvidedFileSystemInterface::METADATA_FIELD_MIME_TYPE;
  options.thumbnail =
      fields_ & ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL;
  options.cloud_identifier =
      fields_ & ProvidedFileSystemInterface::METADATA_FIELD_CLOUD_IDENTIFIER;
  options.cloud_file_info =
      fields_ & ProvidedFileSystemInterface::METADATA_FIELD_CLOUD_FILE_INFO;

  return SendEvent(
      request_id,
      extensions::events::FILE_SYSTEM_PROVIDER_ON_GET_METADATA_REQUESTED,
      extensions::api::file_system_provider::OnGetMetadataRequested::kEventName,
      extensions::api::file_system_provider::OnGetMetadataRequested::Create(
          options));
}

void GetMetadata::OnSuccess(/*request_id=*/int,
                            const RequestValue& result,
                            bool has_more) {
  DCHECK(callback_);
  std::unique_ptr<EntryMetadata> metadata(new EntryMetadata);
  const bool convert_result = ConvertRequestValueToFileInfo(
      result, fields_, entry_path_.AsUTF8Unsafe() == FILE_PATH_LITERAL("/"),
      metadata.get());

  if (!convert_result) {
    LOG(ERROR) << "Failed to parse a response for the get metadata operation.";
    std::move(callback_).Run(nullptr, base::File::FILE_ERROR_IO);
    return;
  }

  std::move(callback_).Run(std::move(metadata), base::File::FILE_OK);
}

void GetMetadata::OnError(/*request_id=*/int,
                          /*result=*/const RequestValue&,
                          base::File::Error error) {
  DCHECK(callback_);
  std::move(callback_).Run(nullptr, error);
}

}  // namespace ash::file_system_provider::operations