File: io_task.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 (245 lines) | stat: -rw-r--r-- 7,185 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
// Copyright 2021 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_manager/io_task.h"

#include <type_traits>
#include <vector>

#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ash/file_manager/volume_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "storage/browser/file_system/file_system_url.h"

namespace file_manager::io_task {

std::ostream& operator<<(std::ostream& out, const State state) {
  switch (state) {
#define PRINT(s)    \
  case State::k##s: \
    return out << #s;
    PRINT(Queued)
    PRINT(Scanning)
    PRINT(InProgress)
    PRINT(Paused)
    PRINT(Success)
    PRINT(Error)
    PRINT(NeedPassword)
    PRINT(Cancelled)
#undef PRINT
  }

  return out << "State(" << static_cast<std::underlying_type_t<State>>(state)
             << ")";
}

std::ostream& operator<<(std::ostream& out, OperationType op) {
  switch (op) {
#define PRINT(s)            \
  case OperationType::k##s: \
    return out << #s;
    PRINT(Copy)
    PRINT(Delete)
    PRINT(EmptyTrash)
    PRINT(Extract)
    PRINT(Move)
    PRINT(Restore)
    PRINT(RestoreToDestination)
    PRINT(Trash)
    PRINT(Zip)
#undef PRINT
  }

  return out << "OperationType("
             << static_cast<std::underlying_type_t<OperationType>>(op) << ")";
}

void IOTask::Pause(PauseParams params) {}

void IOTask::Resume(ResumeParams) {}

void IOTask::CompleteWithError(PolicyError policy_error) {}

bool PolicyError::operator==(const PolicyError& other) const = default;

bool ConflictPauseParams::operator==(const ConflictPauseParams& other) const =
    default;

bool PolicyPauseParams::operator==(const PolicyPauseParams& other) const =
    default;

PauseParams::PauseParams() = default;

PauseParams::PauseParams(const PauseParams& other) = default;

PauseParams& PauseParams::operator=(const PauseParams& other) = default;

PauseParams::PauseParams(PauseParams&& other) = default;

PauseParams& PauseParams::operator=(PauseParams&& other) = default;

bool PauseParams::operator==(const PauseParams& other) const {
  return (conflict_params == other.conflict_params) &&
         (policy_params == other.policy_params);
}

PauseParams::~PauseParams() = default;

ResumeParams::ResumeParams() = default;

ResumeParams::ResumeParams(const ResumeParams& other) = default;

ResumeParams& ResumeParams::operator=(const ResumeParams& other) = default;

ResumeParams::ResumeParams(ResumeParams&& other) = default;

ResumeParams& ResumeParams::operator=(ResumeParams&& other) = default;

ResumeParams::~ResumeParams() = default;

EntryStatus::EntryStatus(storage::FileSystemURL file_url,
                         std::optional<base::File::Error> file_error,
                         std::optional<storage::FileSystemURL> source_url)
    : url(file_url), error(file_error), source_url(source_url) {}

EntryStatus::~EntryStatus() = default;

EntryStatus::EntryStatus(EntryStatus&& other) = default;
EntryStatus& EntryStatus::operator=(EntryStatus&& other) = default;

ProgressStatus::ProgressStatus() = default;
ProgressStatus::~ProgressStatus() = default;

ProgressStatus::ProgressStatus(ProgressStatus&& other) = default;
ProgressStatus& ProgressStatus::operator=(ProgressStatus&& other) = default;

bool ProgressStatus::IsPaused() const {
  return state == State::kPaused;
}

bool ProgressStatus::IsCompleted() const {
  return state == State::kSuccess || state == State::kError ||
         state == State::kCancelled;
}

bool ProgressStatus::HasWarning() const {
  // We should show a warning if the task is paused because of policy.
  return IsPaused() && pause_params.policy_params.has_value();
}

bool ProgressStatus::HasPolicyError() const {
  return state == State::kError && policy_error.has_value();
}

bool ProgressStatus::IsScanning() const {
  return state == State::kScanning;
}

std::string ProgressStatus::GetSourceName(Profile* profile) const {
  if (!source_name.empty()) {
    return source_name;
  }

  if (sources.size() == 0) {
    return {};
  }

  return util::GetDisplayablePath(profile, sources.front().url)
      .value_or(base::FilePath())
      .BaseName()
      .value();
}

void ProgressStatus::SetDestinationFolder(storage::FileSystemURL folder,
                                          Profile* const profile) {
  destination_folder_ = std::move(folder);
  destination_volume_id_.clear();
  if (profile) {
    if (VolumeManager* const volume_manager = VolumeManager::Get(profile)) {
      if (const base::WeakPtr<const Volume> volume =
              volume_manager->FindVolumeFromPath(destination_folder_.path())) {
        destination_volume_id_ = volume->volume_id();
      }
    }
  }
}

DummyIOTask::DummyIOTask(std::vector<storage::FileSystemURL> source_urls,
                         storage::FileSystemURL destination_folder,
                         OperationType type,
                         bool show_notifications,
                         bool progress_succeeds)
    : IOTask(show_notifications), progress_succeeds_(progress_succeeds) {
  progress_.state = State::kQueued;
  progress_.type = type;
  progress_.SetDestinationFolder(std::move(destination_folder));
  progress_.bytes_transferred = 0;
  progress_.total_bytes = 2;

  for (auto& url : source_urls) {
    progress_.sources.emplace_back(url, std::nullopt);
  }
}

DummyIOTask::~DummyIOTask() = default;

void DummyIOTask::Execute(IOTask::ProgressCallback progress_callback,
                          IOTask::CompleteCallback complete_callback) {
  progress_callback_ = std::move(progress_callback);
  complete_callback_ = std::move(complete_callback);

  progress_.state = State::kInProgress;
  progress_callback_.Run(progress_);

  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(&DummyIOTask::DoProgress, weak_ptr_factory_.GetWeakPtr()));
}

void DummyIOTask::Pause(PauseParams pause_params) {
  progress_.state = State::kPaused;
  progress_.pause_params = pause_params;
}

void DummyIOTask::Resume(ResumeParams resume_params) {
  progress_.state = State::kInProgress;
}

void DummyIOTask::Cancel() {
  progress_.state = State::kCancelled;
}

void DummyIOTask::CompleteWithError(PolicyError policy_error) {
  progress_.state = State::kError;
  progress_.policy_error.emplace(std::move(policy_error));
}

void DummyIOTask::DoProgress() {
  if (progress_.IsPaused()) {
    return;
  }

  progress_.bytes_transferred = 1;
  progress_callback_.Run(progress_);

  if (progress_succeeds_) {
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(&DummyIOTask::DoComplete,
                                  weak_ptr_factory_.GetWeakPtr()));
  }
}

void DummyIOTask::DoComplete() {
  progress_.state = State::kSuccess;
  progress_.bytes_transferred = 2;
  for (auto& source : progress_.sources) {
    source.error.emplace(base::File::FILE_OK);
  }
  std::move(complete_callback_).Run(std::move(progress_));
}

}  // namespace file_manager::io_task