File: recent_disk_source.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (325 lines) | stat: -rw-r--r-- 11,520 bytes parent folder | download | duplicates (5)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2017 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/fileapi/recent_disk_source.h"

#include <utility>

#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/mime_util.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/file_system_operation.h"
#include "storage/browser/file_system/file_system_operation_runner.h"
#include "storage/browser/file_system/file_system_url.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "ui/file_manager/file_types_data.h"
#include "url/origin.h"

using content::BrowserThread;

namespace ash {

namespace {

constexpr char kAudioMimeType[] = "audio/*";
constexpr char kImageMimeType[] = "image/*";
constexpr char kVideoMimeType[] = "video/*";

void OnReadDirectoryOnIOThread(
    const storage::FileSystemOperation::ReadDirectoryCallback& callback,
    base::File::Error result,
    storage::FileSystemOperation::FileEntryList entries,
    bool has_more) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  content::GetUIThreadTaskRunner({})->PostTask(
      FROM_HERE,
      base::BindOnce(callback, result, std::move(entries), has_more));
}

void ReadDirectoryOnIOThread(
    scoped_refptr<storage::FileSystemContext> file_system_context,
    const storage::FileSystemURL& url,
    const storage::FileSystemOperation::ReadDirectoryCallback& callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  file_system_context->operation_runner()->ReadDirectory(
      url, base::BindRepeating(&OnReadDirectoryOnIOThread, callback));
}

void OnGetMetadataOnIOThread(
    storage::FileSystemOperation::GetMetadataCallback callback,
    base::File::Error result,
    const base::File::Info& info) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  content::GetUIThreadTaskRunner({})->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback), result, info));
}

void GetMetadataOnIOThread(
    scoped_refptr<storage::FileSystemContext> file_system_context,
    const storage::FileSystemURL& url,
    storage::FileSystemOperation::GetMetadataFieldSet fields,
    storage::FileSystemOperation::GetMetadataCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  file_system_context->operation_runner()->GetMetadata(
      url, fields,
      base::BindOnce(&OnGetMetadataOnIOThread, std::move(callback)));
}

}  // namespace

RecentDiskSource::RecentDiskSource::CallContext::CallContext(
    const Params& params,
    GetRecentFilesCallback callback)
    : params(params),
      callback(std::move(callback)),
      build_start_time(base::TimeTicks::Now()),
      accumulator(params.max_files()) {}

RecentDiskSource::RecentDiskSource::CallContext::CallContext(
    CallContext&& context)
    : params(context.params),
      callback(std::move(context.callback)),
      build_start_time(context.build_start_time),
      inflight_readdirs(context.inflight_readdirs),
      inflight_stats(context.inflight_stats),
      accumulator(std::move(context.accumulator)) {}

RecentDiskSource::RecentDiskSource::CallContext::~CallContext() = default;

RecentDiskSource::RecentDiskSource(
    extensions::api::file_manager_private::VolumeType volume_type,
    std::string mount_point_name,
    bool ignore_dotfiles,
    int max_depth,
    std::string uma_histogram_name)
    : RecentSource(volume_type),
      mount_point_name_(std::move(mount_point_name)),
      ignore_dotfiles_(ignore_dotfiles),
      max_depth_(max_depth),
      uma_histogram_name_(std::move(uma_histogram_name)) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
}

RecentDiskSource::~RecentDiskSource() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
}

void RecentDiskSource::GetRecentFiles(const Params& params,
                                      GetRecentFilesCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  DCHECK(context_map_.Lookup(params.call_id()) == nullptr);

  // Return immediately if mount point does not exist.
  storage::ExternalMountPoints* mount_points =
      storage::ExternalMountPoints::GetSystemInstance();
  base::FilePath path;
  if (!mount_points->GetRegisteredPath(mount_point_name_, &path)) {
    std::move(callback).Run({});
    return;
  }

  // Create a unique context for this call.
  auto context = std::make_unique<CallContext>(params, std::move(callback));
  context_map_.AddWithID(std::move(context), params.call_id());

  ScanDirectory(params.call_id(), base::FilePath(), 1);
}

std::vector<RecentFile> RecentDiskSource::Stop(const int32_t call_id) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  CallContext* context = context_map_.Lookup(call_id);
  if (context == nullptr) {
    // The Stop method was called after we already responded. Just return empty
    // list of files.
    return {};
  }
  // Proper stop; get the files and erase the context.
  const std::vector<RecentFile> files = context->accumulator.Get();
  context_map_.Remove(call_id);
  return files;
}

void RecentDiskSource::ScanDirectory(const int32_t call_id,
                                     const base::FilePath& path,
                                     int depth) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  // If context is gone, that is Stop() has been called, exit immediately.
  CallContext* context = context_map_.Lookup(call_id);
  if (context == nullptr) {
    return;
  }

  storage::FileSystemURL url = BuildDiskURL(context->params, path);

  ++context->inflight_readdirs;
  content::GetIOThreadTaskRunner({})->PostTask(
      FROM_HERE,
      base::BindOnce(
          &ReadDirectoryOnIOThread,
          base::WrapRefCounted(context->params.file_system_context()), url,
          base::BindRepeating(&RecentDiskSource::OnReadDirectory,
                              weak_ptr_factory_.GetWeakPtr(), call_id, path,
                              depth)));
}

void RecentDiskSource::OnReadDirectory(
    const int32_t call_id,
    const base::FilePath& path,
    const int depth,
    base::File::Error result,
    storage::FileSystemOperation::FileEntryList entries,
    bool has_more) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  // If context is gone, that is Stop() has been called, exit immediately.
  CallContext* context = context_map_.Lookup(call_id);
  if (context == nullptr) {
    return;
  }

  const std::u16string q16 = base::UTF8ToUTF16(context->params.query());
  for (const auto& entry : entries) {
    // Ignore directories and files that start with dot.
    if (ignore_dotfiles_ &&
        base::StartsWith(entry.name.value(), ".",
                         base::CompareCase::INSENSITIVE_ASCII)) {
      continue;
    }
    base::FilePath subpath = path.Append(entry.name);

    if (entry.type == filesystem::mojom::FsFileType::DIRECTORY) {
      if ((max_depth_ > 0 && depth >= max_depth_) || context->params.IsLate()) {
        continue;
      }
      ScanDirectory(call_id, subpath, depth + 1);
    } else {
      if (!MatchesFileType(entry.name.path(), context->params.file_type())) {
        continue;
      }
      if (!FileNameMatches(base::UTF8ToUTF16(entry.name.value()), q16)) {
        continue;
      }
      storage::FileSystemURL url = BuildDiskURL(context->params, subpath);
      ++context->inflight_stats;
      content::GetIOThreadTaskRunner({})->PostTask(
          FROM_HERE,
          base::BindOnce(
              &GetMetadataOnIOThread,
              base::WrapRefCounted(context->params.file_system_context()), url,
              storage::FileSystemOperation::GetMetadataFieldSet(
                  {storage::FileSystemOperation::GetMetadataField::
                       kLastModified}),
              base::BindOnce(&RecentDiskSource::OnGotMetadata,
                             weak_ptr_factory_.GetWeakPtr(), call_id, url)));
    }
  }

  if (has_more) {
    return;
  }

  --context->inflight_readdirs;
  if (context->inflight_stats == 0 && context->inflight_readdirs == 0) {
    OnReadOrStatFinished(call_id);
  }
}

void RecentDiskSource::OnGotMetadata(const int32_t call_id,
                                     const storage::FileSystemURL& url,
                                     base::File::Error result,
                                     const base::File::Info& info) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  // If context is gone, that is Stop() has been called, exit immediately.
  CallContext* context = context_map_.Lookup(call_id);
  if (context == nullptr) {
    return;
  }

  if (result == base::File::FILE_OK &&
      info.last_modified >= context->params.cutoff_time()) {
    context->accumulator.Add(RecentFile(url, info.last_modified));
  }

  --context->inflight_stats;
  if (context->inflight_stats == 0 && context->inflight_readdirs == 0) {
    OnReadOrStatFinished(call_id);
  }
}

void RecentDiskSource::OnReadOrStatFinished(const int32_t call_id) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  CallContext* context = context_map_.Lookup(call_id);
  // If context is gone, that is Stop() has been called, exit immediately.
  if (context == nullptr) {
    return;
  }

  DCHECK(context->inflight_stats == 0);
  DCHECK(context->inflight_readdirs == 0);
  DCHECK(!context->build_start_time.is_null());

  // All reads/scans completed.
  UmaHistogramTimes(uma_histogram_name_,
                    base::TimeTicks::Now() - context->build_start_time);

  std::move(context->callback).Run(context->accumulator.Get());
  context_map_.Remove(call_id);
}

storage::FileSystemURL RecentDiskSource::BuildDiskURL(
    const Params& params,
    const base::FilePath& path) const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  storage::ExternalMountPoints* mount_points =
      storage::ExternalMountPoints::GetSystemInstance();
  return mount_points->CreateExternalFileSystemURL(
      blink::StorageKey::CreateFirstParty(url::Origin::Create(params.origin())),
      mount_point_name_, path);
}

bool RecentDiskSource::MatchesFileType(const base::FilePath& path,
                                       RecentSource::FileType file_type) {
  if (file_type == RecentSource::FileType::kAll) {
    return true;
  }

  // File type for |path| is guessed by data generated from file_types.json5.
  // It guesses mime types based on file extensions, but it has a limited set
  // of file extensions.
  // TODO(fukino): It is better to have better coverage of file extensions to be
  // consistent with file-type detection on Android system. crbug.com/1034874.
  const auto ext = base::ToLowerASCII(path.Extension());
  if (!file_types_data::kExtensionToMIME.contains(ext)) {
    return false;
  }
  std::string mime_type = file_types_data::kExtensionToMIME.at(ext);

  switch (file_type) {
    case RecentSource::FileType::kAudio:
      return net::MatchesMimeType(kAudioMimeType, mime_type);
    case RecentSource::FileType::kImage:
      return net::MatchesMimeType(kImageMimeType, mime_type);
    case RecentSource::FileType::kVideo:
      return net::MatchesMimeType(kVideoMimeType, mime_type);
    case RecentSource::FileType::kDocument:
      return file_types_data::kDocumentMIMETypes.contains(mime_type);
    default:
      return false;
  }
}

}  // namespace ash