File: fake_recent_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 (158 lines) | stat: -rw-r--r-- 5,125 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
// 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/test/fake_recent_source.h"

#include <iterator>
#include <memory>
#include <string>
#include <vector>

#include "base/containers/extend.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ash/fileapi/recent_file.h"
#include "fake_recent_source.h"
#include "net/base/mime_util.h"

namespace ash {

// Helper method for matching file type against the desired `file_type`.
bool MatchesFileType(const RecentFile& file, RecentSource::FileType file_type) {
  if (file_type == RecentSource::FileType::kAll) {
    return true;
  }

  std::string mime_type;
  if (!net::GetMimeTypeFromFile(file.url().path(), &mime_type)) {
    return false;
  }

  switch (file_type) {
    case RecentSource::FileType::kAudio:
      return net::MatchesMimeType("audio/*", mime_type);
    case RecentSource::FileType::kImage:
      return net::MatchesMimeType("image/*", mime_type);
    case RecentSource::FileType::kVideo:
      return net::MatchesMimeType("video/*", mime_type);
    default:
      return false;
  }
}

FileProducer::FileProducer(const base::TimeDelta& lag,
                           std::vector<RecentFile> files)
    : lag_(lag), files_(std::move(files)) {}

FileProducer::~FileProducer() = default;

void FileProducer::GetFiles(RecentSource::GetRecentFilesCallback callback) {
  timers_.emplace_back(std::make_unique<base::OneShotTimer>());
  timers_.back()->Start(
      FROM_HERE, lag_,
      base::BindOnce(&FileProducer::OnFilesReady, base::Unretained(this),
                     std::move(callback)));
}

void FileProducer::OnFilesReady(RecentSource::GetRecentFilesCallback callback) {
  std::move(callback).Run(files_);
}

FakeRecentSource::CallContext::CallContext(GetRecentFilesCallback callback,
                                           const Params& params)
    : callback(std::move(callback)), params(params) {}
FakeRecentSource::CallContext::CallContext(CallContext&& context)
    : callback(std::move(context.callback)),
      params(context.params),
      accumulator(std::move(context.accumulator)),
      active_producer_count(context.active_producer_count) {}
FakeRecentSource::CallContext::~CallContext() = default;

FakeRecentSource::FakeRecentSource()
    : FakeRecentSource(
          extensions::api::file_manager_private::VolumeType::kTesting) {}

FakeRecentSource::FakeRecentSource(
    extensions::api::file_manager_private::VolumeType volume_type)
    : RecentSource(volume_type) {}

FakeRecentSource::~FakeRecentSource() = default;

void FakeRecentSource::AddProducer(std::unique_ptr<FileProducer> producer) {
  producers_.emplace_back(std::move(producer));
}

void FakeRecentSource::GetRecentFiles(const Params& params,
                                      GetRecentFilesCallback callback) {
  const auto& [it, _] = context_map_.emplace(
      params.call_id(), CallContext(std::move(callback), params));

  if (producers_.empty()) {
    OnAllProducersDone(params.call_id());
    return;
  }

  it->second.active_producer_count = producers_.size();
  for (const auto& producer : producers_) {
    producer->GetFiles(base::BindOnce(&FakeRecentSource::OnProducerReady,
                                      base::Unretained(this),
                                      params.call_id()));
  }
}

std::vector<RecentFile> FakeRecentSource::Stop(const int32_t call_id) {
  auto it = context_map_.find(call_id);
  if (it == context_map_.end()) {
    LOG(INFO) << "Received files after results delivered to recent model";
    return {};
  }

  std::vector<RecentFile> files =
      GetMatchingFiles(it->second.accumulator, it->second.params);
  context_map_.erase(it);
  return files;
}

void FakeRecentSource::OnProducerReady(const int32_t call_id,
                                       std::vector<RecentFile> files) {
  auto it = context_map_.find(call_id);
  if (it == context_map_.end()) {
    LOG(INFO) << "Producer ready after source stopped";
    return;
  }
  base::Extend(it->second.accumulator, files);
  if (--it->second.active_producer_count <= 0) {
    OnAllProducersDone(call_id);
  }
}

void FakeRecentSource::OnAllProducersDone(int32_t call_id) {
  auto it = context_map_.find(call_id);
  if (it == context_map_.end()) {
    LOG(WARNING) << "FakeRecentSource ready after it was stopped";
    return;
  }
  std::move(it->second.callback)
      .Run(GetMatchingFiles(it->second.accumulator, it->second.params));
  context_map_.erase(it);
}

std::vector<RecentFile> FakeRecentSource::GetMatchingFiles(
    const std::vector<RecentFile>& accumulator,
    const Params& params) {
  const std::u16string q16 = base::UTF8ToUTF16(params.query());
  std::vector<RecentFile> result;
  for (const auto& file : accumulator) {
    if (!MatchesFileType(file, params.file_type())) {
      continue;
    }
    if (!FileNameMatches(file.url().path().LossyDisplayName(), q16)) {
      continue;
    }
    result.push_back(file);
  }
  return result;
}

}  // namespace ash