File: source_destination_matcher_ash.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 (330 lines) | stat: -rw-r--r-- 12,127 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
326
327
328
329
330
// Copyright 2022 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/enterprise/connectors/analysis/source_destination_matcher_ash.h"

#include <optional>

#include "base/files/file_path.h"
#include "base/notreached.h"
#include "base/values.h"
#include "chrome/browser/ash/file_manager/volume_manager.h"
#include "chrome/browser/ash/guest_os/public/types.h"
#include "storage/browser/file_system/file_system_url.h"

namespace storage {
class FileSystemURL;
}

namespace enterprise_connectors {

namespace {
// Checks if the key of the sources or destinations list is known.
// This should only be extended when a key is properly supported.
bool AllowedFSInfoKey(const std::string& key) {
  // TODO(crbug.com/1340553): Also allow settings for app ids and smb.
  return key == "file_system_type";
}

// This function checks whether there are no unknown keys in the passed dict.
// The function returns true if there are no such unknown keys and false if an
// unknown key is found.
bool SourceOrDestinationEntryIsValid(const base::Value::Dict* dict) {
  DCHECK(dict);
  for (auto&& [key, _] : *dict) {
    if (!AllowedFSInfoKey(key)) {
      LOG(ERROR) << "Source or destination entry is not valid, ignoring it "
                    "because of unknown key \""
                 << key << "\" in " << dict->DebugString();
      return false;
    }
  }
  return true;
}
}  // namespace

// static
SourceDestinationMatcherAsh::FsType SourceDestinationMatcherAsh::VolumeToFsType(
    base::WeakPtr<file_manager::Volume> volume) {
  if (!volume) {
    return SourceDestinationMatcherAsh::FsType::kUnknown;
  }
  if (volume->type() == file_manager::VOLUME_TYPE_GUEST_OS) {
    if (!volume->vm_type()) {
      return SourceDestinationMatcherAsh::FsType::kUnknownVm;
    }
    switch (volume->vm_type().value()) {
      case guest_os::VmType::TERMINA:
        return SourceDestinationMatcherAsh::FsType::kCrostini;
      case guest_os::VmType::PLUGIN_VM:
        return SourceDestinationMatcherAsh::FsType::kPluginVm;
      case guest_os::VmType::BOREALIS:
        return SourceDestinationMatcherAsh::FsType::kBorealis;
      case guest_os::VmType::BRUSCHETTA:
        return SourceDestinationMatcherAsh::FsType::kBruschetta;
      case guest_os::VmType::UNKNOWN:
        return SourceDestinationMatcherAsh::FsType::kUnknownVm;
      case guest_os::VmType::ARCVM:
        return SourceDestinationMatcherAsh::FsType::kArc;
      case guest_os::VmType::BAGUETTE:
        return SourceDestinationMatcherAsh::FsType::kUnknownVm;
      case guest_os::VmType::VmType_INT_MIN_SENTINEL_DO_NOT_USE_:
      case guest_os::VmType::VmType_INT_MAX_SENTINEL_DO_NOT_USE_:
        NOTREACHED();
    }
  }
  switch (volume->type()) {
    case file_manager::VOLUME_TYPE_TESTING:
      return SourceDestinationMatcherAsh::FsType::kTesting;
    case file_manager::VOLUME_TYPE_GOOGLE_DRIVE:
      return SourceDestinationMatcherAsh::FsType::kGoogleDrive;
    case file_manager::VOLUME_TYPE_DOWNLOADS_DIRECTORY:
      return SourceDestinationMatcherAsh::FsType::kMyFiles;
    case file_manager::VOLUME_TYPE_REMOVABLE_DISK_PARTITION:
      return SourceDestinationMatcherAsh::FsType::kRemovable;
    case file_manager::VOLUME_TYPE_PROVIDED:
      return SourceDestinationMatcherAsh::FsType::kProvided;
    case file_manager::VOLUME_TYPE_MTP:
      return SourceDestinationMatcherAsh::FsType::kDeviceMediaStorage;
    case file_manager::VOLUME_TYPE_MEDIA_VIEW:
      // The media view file system is a read-only file system that allows to
      // display recent ARC files in the Files App.
      return SourceDestinationMatcherAsh::FsType::kArc;
    case file_manager::VOLUME_TYPE_CROSTINI:
      return SourceDestinationMatcherAsh::FsType::kCrostini;
    case file_manager::VOLUME_TYPE_ANDROID_FILES:
      return SourceDestinationMatcherAsh::FsType::kArc;
    case file_manager::VOLUME_TYPE_DOCUMENTS_PROVIDER:
      // This is for ARC documents provider, so we also map to ARC!
      return SourceDestinationMatcherAsh::FsType::kArc;
    case file_manager::VOLUME_TYPE_SMB:
      return SourceDestinationMatcherAsh::FsType::kSmb;
    case file_manager::VOLUME_TYPE_SYSTEM_INTERNAL:
      return SourceDestinationMatcherAsh::FsType::kUnknown;
    case file_manager::VOLUME_TYPE_MOUNTED_ARCHIVE_FILE:
    case file_manager::VOLUME_TYPE_GUEST_OS:
    case file_manager::NUM_VOLUME_TYPE:
      NOTREACHED();
  }
  NOTREACHED();
}

// static
SourceDestinationMatcherAsh::FsType SourceDestinationMatcherAsh::PathToFsType(
    content::BrowserContext* context,
    const base::FilePath& path) {
  file_manager::VolumeManager* const volume_manager =
      file_manager::VolumeManager::Get(context);

  DCHECK(volume_manager);
  base::WeakPtr<file_manager::Volume> volume =
      volume_manager->FindVolumeFromPath(path);

  if (volume &&
      volume->type() == file_manager::VOLUME_TYPE_MOUNTED_ARCHIVE_FILE) {
    // For mounted archives, we check the source file system.
    // Recursive to handle mounted archives from mounted archives.
    return PathToFsType(context, volume->source_path());
  }

  return VolumeToFsType(volume);
}

std::string SourceDestinationMatcherAsh::GetVolumeDescriptionFromPath(
    content::BrowserContext* context,
    const base::FilePath& path) {
  return FsTypeToString(PathToFsType(context, path));
}

// static
std::optional<SourceDestinationMatcherAsh::FsType>
SourceDestinationMatcherAsh::StringToFsType(const std::string& s) {
  if (s == "TESTING") {
    return SourceDestinationMatcherAsh::FsType::kTesting;
  }
  if (s == "UNKNOWN") {
    return SourceDestinationMatcherAsh::FsType::kUnknown;
  }
  if (s == "*" || s == "ANY") {
    return SourceDestinationMatcherAsh::FsType::kAny;
  }
  if (s == "MY_FILES") {
    return SourceDestinationMatcherAsh::FsType::kMyFiles;
  }
  if (s == "REMOVABLE") {
    return SourceDestinationMatcherAsh::FsType::kRemovable;
  }
  if (s == "DEVICE_MEDIA_STORAGE") {
    return SourceDestinationMatcherAsh::FsType::kDeviceMediaStorage;
  }
  if (s == "PROVIDED") {
    return SourceDestinationMatcherAsh::FsType::kProvided;
  }
  if (s == "ARC") {
    return SourceDestinationMatcherAsh::FsType::kArc;
  }
  if (s == "GOOGLE_DRIVE") {
    return SourceDestinationMatcherAsh::FsType::kGoogleDrive;
  }
  if (s == "SMB") {
    return SourceDestinationMatcherAsh::FsType::kSmb;
  }
  if (s == "CROSTINI") {
    return SourceDestinationMatcherAsh::FsType::kCrostini;
  }
  if (s == "PLUGIN_VM") {
    return SourceDestinationMatcherAsh::FsType::kPluginVm;
  }
  if (s == "BOREALIS") {
    return SourceDestinationMatcherAsh::FsType::kBorealis;
  }
  if (s == "BRUSCHETTA") {
    return SourceDestinationMatcherAsh::FsType::kBruschetta;
  }
  if (s == "UNKNOWN_VM") {
    return SourceDestinationMatcherAsh::FsType::kUnknownVm;
  }
  return std::nullopt;
}

std::set<SourceDestinationMatcherAsh::FsType>
SourceDestinationMatcherAsh::ValueListToFsTypes(
    const base::Value::List* source_or_destination_list) {
  std::set<SourceDestinationMatcherAsh::FsType> fs_types;
  for (const auto& entry : *source_or_destination_list) {
    const auto* dict = entry.GetIfDict();
    if (!dict) {
      LOG(ERROR) << "Entry is not a dict.";
      continue;
    }
    if (!SourceOrDestinationEntryIsValid(dict)) {
      // We ignore all entries that have unknown configuration values.
      // These values typically narrow down the range of matching file system
      // urls and thus are handled as a specialized case.
      // These specialized cases are ignored, if they are not yet supported.
      continue;
    }
    const auto* s = dict->FindString("file_system_type");
    if (!s || s->empty()) {
      LOG(ERROR) << "file_system_type not found.";
      continue;
    }
    auto fs_type = StringToFsType(*s);
    if (fs_type) {
      fs_types.insert(fs_type.value());
    }
  }
  return fs_types;
}

std::string SourceDestinationMatcherAsh::FsTypeToString(
    SourceDestinationMatcherAsh::FsType fs_type) {
  switch (fs_type) {
    case SourceDestinationMatcherAsh::FsType::kTesting:
      return "TESTING";
    case SourceDestinationMatcherAsh::FsType::kUnknown:
      return "UNKNOWN";
    case SourceDestinationMatcherAsh::FsType::kAny:
      return "ANY";
    case SourceDestinationMatcherAsh::FsType::kMyFiles:
      return "MY_FILES";
    case SourceDestinationMatcherAsh::FsType::kRemovable:
      return "REMOVABLE";
    case SourceDestinationMatcherAsh::FsType::kDeviceMediaStorage:
      return "DEVICE_MEDIA_STORAGE";
    case SourceDestinationMatcherAsh::FsType::kProvided:
      return "PROVIDED";
    case SourceDestinationMatcherAsh::FsType::kArc:
      return "ARC";
    case SourceDestinationMatcherAsh::FsType::kGoogleDrive:
      return "GOOGLE_DRIVE";
    case SourceDestinationMatcherAsh::FsType::kSmb:
      return "SMB";
    case SourceDestinationMatcherAsh::FsType::kCrostini:
      return "CROSTINI";
    case SourceDestinationMatcherAsh::FsType::kPluginVm:
      return "PLUGIN_VM";
    case SourceDestinationMatcherAsh::FsType::kBorealis:
      return "BOREALIS";
    case SourceDestinationMatcherAsh::FsType::kBruschetta:
      return "BRUSCHETTA";
    case SourceDestinationMatcherAsh::FsType::kUnknownVm:
      return "UNKNOWN_VM";
  }
  NOTREACHED();
}

SourceDestinationMatcherAsh::SourceDestinationMatcherAsh() = default;

SourceDestinationMatcherAsh::~SourceDestinationMatcherAsh() = default;

void SourceDestinationMatcherAsh::AddFilters(
    ID* id,
    const base::Value::List* settings_list) {
  DCHECK(id);
  // TODO(crbug.com/1340553): Adapt for app ids and smb settings
  if (!settings_list) {
    LOG(ERROR) << "No settings list found.";
    return;
  }
  for (const auto& value : *settings_list) {
    const auto* dict = value.GetIfDict();
    if (!dict) {
      LOG(ERROR) << "Settings list value is not a dict.";
      continue;
    }
    const auto* sources = dict->FindList("sources");
    const auto* destinations = dict->FindList("destinations");
    if (!sources || !destinations) {
      LOG(ERROR) << "Sources or destinations not found.";
      continue;
    }
    SourceDestinationEntry entry;
    entry.fs_sources = ValueListToFsTypes(sources);
    entry.fs_destinations = ValueListToFsTypes(destinations);
    if (entry.fs_sources.empty() || entry.fs_destinations.empty()) {
      LOG(ERROR) << "No valid sources or destinations found.";
      continue;
    }
    entry.id = ++(*id);
    source_destination_entries_.push_back(std::move(entry));
  }
}

std::set<SourceDestinationMatcherAsh::ID> SourceDestinationMatcherAsh::Match(
    content::BrowserContext* context,
    const storage::FileSystemURL& source_url,
    const storage::FileSystemURL& destination_url) const {
  FsType source_fs_type = PathToFsType(context, source_url.path());
  FsType destination_fs_type = PathToFsType(context, destination_url.path());
  VLOG(1) << "source_fs_type = " << FsTypeToString(source_fs_type)
          << ", destination_fs_type = " << FsTypeToString(destination_fs_type);

  std::set<ID> matches;
  for (const SourceDestinationEntry& entry : source_destination_entries_) {
    if (entry.Matches(source_fs_type, destination_fs_type)) {
      matches.insert(entry.id);
    }
  }
  return matches;
}

SourceDestinationMatcherAsh::SourceDestinationEntry::SourceDestinationEntry() =
    default;
SourceDestinationMatcherAsh::SourceDestinationEntry::SourceDestinationEntry(
    const SourceDestinationEntry& other) = default;
SourceDestinationMatcherAsh::SourceDestinationEntry::SourceDestinationEntry(
    SourceDestinationEntry&& other) = default;
SourceDestinationMatcherAsh::SourceDestinationEntry::~SourceDestinationEntry() =
    default;

bool SourceDestinationMatcherAsh::SourceDestinationEntry::Matches(
    SourceDestinationMatcherAsh::FsType source_type,
    SourceDestinationMatcherAsh::FsType destination_type) const {
  return (fs_sources.count(FsType::kAny) || fs_sources.count(source_type)) &&
         (fs_destinations.count(FsType::kAny) ||
          fs_destinations.count(destination_type));
}

}  // namespace enterprise_connectors