File: arc_file_system_bridge.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 (736 lines) | stat: -rw-r--r-- 28,814 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// 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/arc/fileapi/arc_file_system_bridge.h"

#include <fcntl.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <utility>
#include <vector>

#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/posix/eintr_wrapper.h"
#include "base/strings/escape.h"
#include "base/system/sys_info.h"
#include "base/task/bind_post_task.h"
#include "base/task/thread_pool.h"
#include "base/threading/scoped_blocking_call.h"
#include "chrome/browser/ash/arc/fileapi/arc_select_files_handler.h"
#include "chrome/browser/ash/arc/fileapi/chrome_content_provider_url_util.h"
#include "chrome/browser/ash/arc/fileapi/file_stream_forwarder.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/drive/file_system_util.h"
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ash/fileapi/external_file_url_util.h"
#include "chrome/browser/ash/fusebox/fusebox_moniker.h"
#include "chrome/browser/ash/fusebox/fusebox_server.h"
#include "chrome/browser/ash/guest_os/guest_os_session_tracker.h"
#include "chrome/browser/ash/guest_os/guest_os_session_tracker_factory.h"
#include "chrome/browser/ash/guest_os/guest_os_share_path.h"
#include "chrome/browser/ash/guest_os/guest_os_share_path_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/ash/components/dbus/virtual_file_provider/virtual_file_provider_client.h"
#include "chromeos/ash/components/dbus/vm_concierge/concierge_service.pb.h"
#include "chromeos/ash/experiences/arc/arc_browser_context_keyed_service_factory_base.h"
#include "chromeos/ash/experiences/arc/arc_util.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/url_constants.h"
#include "extensions/browser/api/file_handlers/mime_util.h"
#include "mojo/public/cpp/platform/platform_handle.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "storage/browser/file_system/file_system_context.h"
#include "url/gurl.h"

namespace {
constexpr char kChromeOSReleaseTrack[] = "CHROMEOS_RELEASE_TRACK";
constexpr char kTestImageRelease[] = "testimage-channel";
}  // namespace

namespace arc {

namespace {

// The maximum number of Fusebox Monikers that can be shared concurrently. When
// this number is reached, CreateMoniker() destroys the oldest Moniker. A large
// number is randomly chosen not to block usual user flows.
constexpr size_t kMaxNumberOfSharedMonikers = 1024;

// Returns true if it's OK to allow ARC apps to read the given URL.
bool IsUrlAllowed(const GURL& url) {
  // Currently, only externalfile URLs are allowed.
  return url.SchemeIs(content::kExternalFileScheme);
}

// Returns true if this is a testimage build.
bool IsTestImageBuild() {
  std::string track;
  return base::SysInfo::GetLsbReleaseValue(kChromeOSReleaseTrack, &track) &&
         track.find(kTestImageRelease) != std::string::npos;
}

// Returns true if `download` passes validation.
bool IsMediaStoreDownloadMetadataValid(
    const mojom::MediaStoreDownloadMetadataPtr& download) {
  // Download should have non-empty display name and owner package name.
  if (download->display_name.empty() || download->owner_package_name.empty())
    return false;

  // Download should have path relative to "Download/" which is the download
  // directory for the associated profile.
  const base::FilePath download_path("Download/");
  return download_path == download->relative_path ||
         (!download->relative_path.IsAbsolute() &&
          !download->relative_path.ReferencesParent() &&
          download_path.IsParent(download->relative_path));
}

// Returns FileSystemContext.
scoped_refptr<storage::FileSystemContext> GetFileSystemContext(
    content::BrowserContext* context) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  content::StoragePartition* storage = context->GetDefaultStoragePartition();
  return storage->GetFileSystemContext();
}

// Converts the given URL to a FileSystemURL.
file_manager::util::FileSystemURLAndHandle GetFileSystemURL(
    const storage::FileSystemContext& context,
    const GURL& url) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  return file_manager::util::CreateIsolatedURLFromVirtualPath(
      context, url::Origin(), ash::ExternalFileURLToVirtualPath(url));
}

// Retrieves file's metadata on the IO thread, and runs the callback on the UI
// thread.
void GetMetadataOnIOThread(
    scoped_refptr<storage::FileSystemContext> context,
    const storage::FileSystemURL& url,
    storage::FileSystemOperation::GetMetadataFieldSet flags,
    storage::FileSystemOperation::GetMetadataCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
  context->operation_runner()->GetMetadata(
      url, flags,
      base::BindPostTask(content::GetUIThreadTaskRunner({}),
                         std::move(callback)));
}

// TODO(risan): Write test.
// Open a file from a VFS (vs Chrome-only) filesystem.
mojo::ScopedHandle OpenVFSFileToRead(const base::FilePath& fs_path) {
  base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
                                                base::BlockingType::WILL_BLOCK);
  // Open the file and wrap the fd to be returned through mojo.
  base::ScopedFD fd(HANDLE_EINTR(
      open(fs_path.value().c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW)));
  if (!fd.is_valid()) {
    PLOG(WARNING) << "Invalid FD for fs_path: " << fs_path;
    return mojo::ScopedHandle();
  }
  return mojo::WrapPlatformHandle(mojo::PlatformHandle(std::move(fd)));
}

// Factory of ArcFileSystemBridge.
class ArcFileSystemBridgeFactory
    : public internal::ArcBrowserContextKeyedServiceFactoryBase<
          ArcFileSystemBridge,
          ArcFileSystemBridgeFactory> {
 public:
  static constexpr const char* kName = "ArcFileSystemBridgeFactory";

  static ArcFileSystemBridgeFactory* GetInstance() {
    return base::Singleton<ArcFileSystemBridgeFactory>::get();
  }

 private:
  friend base::DefaultSingletonTraits<ArcFileSystemBridgeFactory>;
  ArcFileSystemBridgeFactory() = default;
  ~ArcFileSystemBridgeFactory() override = default;
};

}  // namespace

ArcFileSystemBridge::ArcFileSystemBridge(content::BrowserContext* context,
                                         ArcBridgeService* bridge_service)
    : profile_(Profile::FromBrowserContext(context)),
      bridge_service_(bridge_service),
      max_number_of_shared_monikers_(kMaxNumberOfSharedMonikers),
      select_files_handlers_manager_(
          std::make_unique<ArcSelectFilesHandlersManager>(context)) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  bridge_service_->file_system()->SetHost(this);
  bridge_service_->file_system()->AddObserver(this);
}

ArcFileSystemBridge::~ArcFileSystemBridge() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  bridge_service_->file_system()->RemoveObserver(this);
  bridge_service_->file_system()->SetHost(nullptr);
  auto* fusebox_server = fusebox::Server::GetInstance();
  if (fusebox_server) {
    for (const auto& [moniker, _] : shared_monikers_) {
      fusebox_server->DestroyMoniker(moniker);
    }
  }
}

// static
BrowserContextKeyedServiceFactory* ArcFileSystemBridge::GetFactory() {
  return ArcFileSystemBridgeFactory::GetInstance();
}

// static
ArcFileSystemBridge* ArcFileSystemBridge::GetForBrowserContext(
    content::BrowserContext* context) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  return ArcFileSystemBridgeFactory::GetForBrowserContext(context);
}

// static
ArcFileSystemBridge* ArcFileSystemBridge::GetForBrowserContextForTesting(
    content::BrowserContext* context) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  return ArcFileSystemBridgeFactory::GetForBrowserContextForTesting(context);
}

void ArcFileSystemBridge::AddObserver(Observer* observer) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  observer_list_.AddObserver(observer);
}

void ArcFileSystemBridge::RemoveObserver(Observer* observer) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  observer_list_.RemoveObserver(observer);
}

void ArcFileSystemBridge::GetFileName(const std::string& url,
                                      GetFileNameCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  GURL url_decoded = DecodeFromChromeContentProviderUrl(GURL(url));
  std::string unescaped_file_name;
  // It's generally not safe to unescape path separators in strings to be used
  // in file paths.
  if (url_decoded.is_empty() || !IsUrlAllowed(url_decoded) ||
      !base::UnescapeBinaryURLComponentSafe(url_decoded.ExtractFileName(),
                                            true /* fail_on_path_separators */,
                                            &unescaped_file_name)) {
    LOG(ERROR) << "Invalid URL: " << url << " " << url_decoded;
    std::move(callback).Run(std::nullopt);
    return;
  }
  std::move(callback).Run(unescaped_file_name);
}

void ArcFileSystemBridge::GetFileSize(const std::string& url,
                                      GetFileSizeCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  GURL url_decoded = DecodeFromChromeContentProviderUrl(GURL(url));
  if (url_decoded.is_empty() || !IsUrlAllowed(url_decoded)) {
    LOG(ERROR) << "Invalid URL: " << url << " " << url_decoded;
    std::move(callback).Run(-1);
    return;
  }

  GetFileSizeInternal(url_decoded, std::move(callback));
}

void ArcFileSystemBridge::GetFileSizeInternal(const GURL& url_decoded,
                                              GetFileSizeCallback callback) {
  GetMetadata(url_decoded,
              {storage::FileSystemOperation::GetMetadataField::kIsDirectory,
               storage::FileSystemOperation::GetMetadataField::kSize},
              base::BindOnce([](base::File::Error result,
                                const base::File::Info& file_info) -> int64_t {
                if (result == base::File::FILE_OK && !file_info.is_directory &&
                    file_info.size >= 0) {
                  return file_info.size;
                }
                return -1;
              }).Then(std::move(callback)));
}

void ArcFileSystemBridge::GetLastModified(const GURL& url,
                                          GetLastModifiedCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  GURL url_decoded = DecodeFromChromeContentProviderUrl(url);
  if (url_decoded.is_empty() || !IsUrlAllowed(url_decoded)) {
    LOG(ERROR) << "Invalid URL: " << url << " " << url_decoded;
    std::move(callback).Run(std::nullopt);
    return;
  }

  GetMetadata(url_decoded,
              {storage::FileSystemOperation::GetMetadataField::kLastModified},
              base::BindOnce([](base::File::Error result,
                                const base::File::Info& file_info)
                                 -> std::optional<base::Time> {
                if (result != base::File::FILE_OK) {
                  return std::nullopt;
                }
                return std::make_optional(file_info.last_modified);
              }).Then(std::move(callback)));
}

void ArcFileSystemBridge::GetMetadata(
    const GURL& url_decoded,
    storage::FileSystemOperation::GetMetadataFieldSet flags,
    storage::FileSystemOperation::GetMetadataCallback callback) {
  scoped_refptr<storage::FileSystemContext> context =
      GetFileSystemContext(profile_);
  file_manager::util::FileSystemURLAndHandle file_system_url_and_handle =
      GetFileSystemURL(*context, url_decoded);
  content::GetIOThreadTaskRunner({})->PostTask(
      FROM_HERE,
      base::BindOnce(
          &GetMetadataOnIOThread, std::move(context),
          file_system_url_and_handle.url, flags,
          base::BindOnce(
              [](const std::string& file_system_id,
                 storage::FileSystemOperation::GetMetadataCallback callback,
                 base::File::Error result, const base::File::Info& file_info) {
                storage::IsolatedContext::GetInstance()->RemoveReference(
                    file_system_id);
                std::move(callback).Run(result, file_info);
              },
              file_system_url_and_handle.handle.id(), std::move(callback))));

  storage::IsolatedContext::GetInstance()->AddReference(
      file_system_url_and_handle.handle.id());
}

void ArcFileSystemBridge::GetFileType(const std::string& url,
                                      GetFileTypeCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  GURL url_decoded = DecodeFromChromeContentProviderUrl(GURL(url));
  if (url_decoded.is_empty() || !IsUrlAllowed(url_decoded)) {
    LOG(ERROR) << "Invalid URL: " << url << " " << url_decoded;
    std::move(callback).Run(std::nullopt);
    return;
  }
  scoped_refptr<storage::FileSystemContext> context =
      GetFileSystemContext(profile_);
  file_manager::util::FileSystemURLAndHandle file_system_url_and_handle =
      GetFileSystemURL(*context, url_decoded);
  extensions::app_file_handler_util::GetMimeTypeForLocalPath(
      profile_, file_system_url_and_handle.url.path(),
      base::BindOnce([](const std::string& mime_type) {
        return mime_type.empty() ? std::nullopt : std::make_optional(mime_type);
      }).Then(std::move(callback)));
}

void ArcFileSystemBridge::OnDocumentChanged(
    int64_t watcher_id,
    storage::WatcherManager::ChangeType type) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  for (auto& observer : observer_list_)
    observer.OnDocumentChanged(watcher_id, type);
}

void ArcFileSystemBridge::OnRootsChanged() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  for (auto& observer : observer_list_)
    observer.OnRootsChanged();
}

void ArcFileSystemBridge::GetVirtualFileId(const std::string& url,
                                           GetVirtualFileIdCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  GURL url_decoded = DecodeFromChromeContentProviderUrl(GURL(url));
  if (url_decoded.is_empty() || !IsUrlAllowed(url_decoded)) {
    LOG(ERROR) << "Invalid URL: " << url << " " << url_decoded;
    std::move(callback).Run(std::nullopt);
    return;
  }

  GetVirtualFileIdInternal(url_decoded, std::move(callback));
}

void ArcFileSystemBridge::HandleIdReleased(const std::string& id,
                                           HandleIdReleasedCallback callback) {
  std::move(callback).Run(HandleIdReleased(id));
}

void ArcFileSystemBridge::OpenFileToRead(const std::string& url,
                                         OpenFileToReadCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  GURL url_decoded = DecodeFromChromeContentProviderUrl(GURL(url));
  if (url_decoded.is_empty() || !IsUrlAllowed(url_decoded)) {
    LOG(ERROR) << "Invalid URL: " << url << " " << url_decoded;
    std::move(callback).Run(mojo::ScopedHandle());
    return;
  }

  base::FilePath fs_path =
      GetLinuxVFSPathFromExternalFileURL(profile_, url_decoded);
  // If the URL represents a file on a virtual (Chrome-only, e.g., FSP,
  // MTP) filesystem, use VirtualFileProvider instead.
  if (fs_path.empty()) {
    GetVirtualFileIdInternal(
        url_decoded, base::BindOnce(&ArcFileSystemBridge::OpenFileById,
                                    weak_ptr_factory_.GetWeakPtr(), url_decoded,
                                    std::move(callback)));
    return;
  }

  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock()},
      base::BindOnce(&OpenVFSFileToRead, fs_path), std::move(callback));
}

void ArcFileSystemBridge::GetVirtualFileIdInternal(
    const GURL& url_decoded,
    GetVirtualFileIdCallback callback) {
  GetFileSizeInternal(
      url_decoded, base::BindOnce(&ArcFileSystemBridge::GenerateVirtualFileId,
                                  weak_ptr_factory_.GetWeakPtr(), url_decoded,
                                  std::move(callback)));
}

void ArcFileSystemBridge::SelectFiles(mojom::SelectFilesRequestPtr request,
                                      SelectFilesCallback callback) {
  select_files_handlers_manager_->SelectFiles(std::move(request),
                                              std::move(callback));
}

void ArcFileSystemBridge::OnFileSelectorEvent(
    mojom::FileSelectorEventPtr event,
    ArcFileSystemBridge::OnFileSelectorEventCallback callback) {
  std::string track;
  select_files_handlers_manager_->OnFileSelectorEvent(std::move(event),
                                                      std::move(callback));
}

void ArcFileSystemBridge::GetFileSelectorElements(
    mojom::GetFileSelectorElementsRequestPtr request,
    GetFileSelectorElementsCallback callback) {
  if (!IsTestImageBuild()) {
    LOG(ERROR)
        << "GetFileSelectorElements is only allowed under test conditions";
    std::move(callback).Run(mojom::FileSelectorElements::New());
    return;
  }
  select_files_handlers_manager_->GetFileSelectorElements(std::move(request),
                                                          std::move(callback));
}

void ArcFileSystemBridge::OnMediaStoreUriAdded(
    const GURL& uri,
    mojom::MediaStoreMetadataPtr metadata) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  // Validate `metadata`.
  bool is_valid = false;
  if (metadata->is_download())
    is_valid = IsMediaStoreDownloadMetadataValid(metadata->get_download());

  if (!is_valid) {
    LOG(ERROR) << "`OnMediaStoreUriAdded()` called with invalid payload.";
    return;
  }

  for (auto& observer : observer_list_)
    observer.OnMediaStoreUriAdded(uri, *metadata);
}

void ArcFileSystemBridge::CreateMoniker(const GURL& content_uri,
                                        bool read_only,
                                        CreateMonikerCallback callback) {
  CHECK_CURRENTLY_ON(content::BrowserThread::UI);

  const GURL url_decoded = DecodeFromChromeContentProviderUrl(content_uri);
  if (url_decoded.is_empty() || !IsUrlAllowed(url_decoded)) {
    LOG(ERROR) << "Invalid ChromeContentProvider URI: " << content_uri;
    std::move(callback).Run(std::nullopt);
    return;
  }

  scoped_refptr<storage::FileSystemContext> context =
      GetFileSystemContext(profile_);
  const file_manager::util::FileSystemURLAndHandle fs_url_and_handle =
      GetFileSystemURL(*context, url_decoded);
  if (!fs_url_and_handle.url.is_valid()) {
    LOG(ERROR) << "Failed to get FileSystemURL for URL: " << url_decoded;
    std::move(callback).Run(std::nullopt);
    return;
  }

  const auto& vm_info =
      guest_os::GuestOsSessionTrackerFactory::GetForProfile(profile_)
          ->GetVmInfo(kArcVmName);
  if (!vm_info) {
    LOG(ERROR) << "ARCVM is not running";
    std::move(callback).Run(std::nullopt);
    return;
  }

  auto* fusebox_server = fusebox::Server::GetInstance();
  if (!fusebox_server) {
    LOG(ERROR) << "Failed to get Fusebox server";
    std::move(callback).Run(std::nullopt);
    return;
  }

  if (shared_monikers_.size() >= max_number_of_shared_monikers_) {
    // Destroy the oldest Fusebox Moniker when the maximum number of shared
    // Monikers is reached.
    CHECK(!moniker_indices_.empty());
    const fusebox::Moniker oldest_moniker = moniker_indices_.begin()->second;
    LOG(WARNING) << "Destroying the oldest Fusebox Moniker: "
                 << oldest_moniker.ToString();
    DestroyMoniker(oldest_moniker, base::DoNothing());
  }

  const fusebox::Moniker moniker =
      fusebox_server->CreateMoniker(fs_url_and_handle.url, read_only);
  const int index =
      moniker_indices_.empty() ? 0 : (moniker_indices_.rbegin()->first + 1);
  shared_monikers_.insert({moniker, index});
  moniker_indices_.insert({index, moniker});
  CHECK_EQ(shared_monikers_.size(), moniker_indices_.size());

  guest_os::GuestOsSharePathFactory::GetForProfile(profile_)->SharePath(
      kArcVmName, vm_info->seneschal_server_handle(),
      base::FilePath(fusebox::MonikerMap::GetFilename(moniker)),
      base::BindOnce(&ArcFileSystemBridge::OnShareMonikerPath,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback),
                     moniker));
}

void ArcFileSystemBridge::OnShareMonikerPath(
    CreateMonikerCallback callback,
    const fusebox::Moniker& moniker,
    const base::FilePath& path,
    bool success,
    const std::string& failure_reason) {
  if (!success) {
    LOG(ERROR) << "Failed to share Fusebox Moniker path with ARCVM: reason: "
               << failure_reason << ", path: " << path;
    std::move(callback).Run(std::nullopt);
    DestroyMoniker(moniker, base::DoNothing());
    return;
  }
  std::move(callback).Run(moniker);
}

void ArcFileSystemBridge::DestroyMoniker(const fusebox::Moniker& moniker,
                                         DestroyMonikerCallback callback) {
  CHECK_CURRENTLY_ON(content::BrowserThread::UI);

  const auto iter = shared_monikers_.find(moniker);
  if (iter == shared_monikers_.end()) {
    LOG(WARNING) << "Cannot destroy unknown Fusebox Moniker: "
                 << moniker.ToString();
    std::move(callback).Run(false);
    return;
  }
  const int index = iter->second;
  shared_monikers_.erase(iter);
  moniker_indices_.erase(index);
  CHECK_EQ(shared_monikers_.size(), moniker_indices_.size());

  auto* fusebox_server = fusebox::Server::GetInstance();
  if (fusebox_server) {
    fusebox_server->DestroyMoniker(moniker);
  } else {
    LOG(ERROR) << "Failed to get Fusebox server";
    // Not returning false, since the Moniker and the resources held by the
    // Fusebox server should have already gone.
  }
  // No need to call GuestOsSharePath::UnsharePath(), because the method is
  // eventually called from GuestOsSharePath::PathDeleted().
  std::move(callback).Run(true);
}

void ArcFileSystemBridge::GenerateVirtualFileId(
    const GURL& url_decoded,
    GenerateVirtualFileIdCallback callback,
    int64_t size) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  if (size < 0) {
    LOG(ERROR) << "Failed to get file size " << url_decoded;
    std::move(callback).Run(std::nullopt);
    return;
  }
  ash::VirtualFileProviderClient::Get()->GenerateVirtualFileId(
      size, base::BindOnce(&ArcFileSystemBridge::OnGenerateVirtualFileId,
                           weak_ptr_factory_.GetWeakPtr(), url_decoded,
                           std::move(callback)));
}

void ArcFileSystemBridge::OnGenerateVirtualFileId(
    const GURL& url_decoded,
    GenerateVirtualFileIdCallback callback,
    const std::optional<std::string>& id) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(id.has_value());
  DCHECK_EQ(id_to_url_.count(id.value()), 0u);
  id_to_url_[id.value()] = url_decoded;

  std::move(callback).Run(std::move(id));
}

void ArcFileSystemBridge::OpenFileById(const GURL& url_decoded,
                                       OpenFileToReadCallback callback,
                                       const std::optional<std::string>& id) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  if (!id.has_value()) {
    LOG(ERROR) << "Missing ID";
    std::move(callback).Run(mojo::ScopedHandle());
    return;
  }

  ash::VirtualFileProviderClient::Get()->OpenFileById(
      id.value(), base::BindOnce(&ArcFileSystemBridge::OnOpenFileById,
                                 weak_ptr_factory_.GetWeakPtr(), url_decoded,
                                 std::move(callback), id.value()));
}

void ArcFileSystemBridge::OnOpenFileById(const GURL& url_decoded,
                                         OpenFileToReadCallback callback,
                                         const std::string& id,
                                         base::ScopedFD fd) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  if (!fd.is_valid()) {
    LOG(ERROR) << "Invalid FD";
    if (!HandleIdReleased(id))
      LOG(ERROR) << "Cannot release ID: " << id;
    std::move(callback).Run(mojo::ScopedHandle());
    return;
  }

  mojo::ScopedHandle wrapped_handle =
      mojo::WrapPlatformHandle(mojo::PlatformHandle(std::move(fd)));
  if (!wrapped_handle.is_valid()) {
    LOG(ERROR) << "Failed to wrap handle";
    if (!HandleIdReleased(id))
      LOG(ERROR) << "Cannot release ID: " << id;
    std::move(callback).Run(mojo::ScopedHandle());
    return;
  }
  std::move(callback).Run(std::move(wrapped_handle));
}

bool ArcFileSystemBridge::HandleReadRequest(const std::string& id,
                                            int64_t offset,
                                            int64_t size,
                                            base::ScopedFD pipe_write_end) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  auto it_url = id_to_url_.find(id);
  if (it_url == id_to_url_.end()) {
    LOG(ERROR) << "Invalid ID: " << id;
    return false;
  }

  // Add a new element to the list, and get an iterator.
  // NOTE: std::list iterators never get invalidated as long as the pointed
  // element is alive.
  file_stream_forwarders_.emplace_front();
  auto it_forwarder = file_stream_forwarders_.begin();

  const GURL& url = it_url->second;
  scoped_refptr<storage::FileSystemContext> context =
      GetFileSystemContext(profile_);
  file_manager::util::FileSystemURLAndHandle file_system_url_and_handle =
      GetFileSystemURL(*context, url);
  *it_forwarder = FileStreamForwarderPtr(new FileStreamForwarder(
      std::move(context), file_system_url_and_handle.url, offset, size,
      std::move(pipe_write_end),
      base::BindOnce(&ArcFileSystemBridge::OnReadRequestCompleted,
                     weak_ptr_factory_.GetWeakPtr(), id, it_forwarder,
                     file_system_url_and_handle.handle.id())));
  storage::IsolatedContext::GetInstance()->AddReference(
      file_system_url_and_handle.handle.id());
  return true;
}

bool ArcFileSystemBridge::HandleIdReleased(const std::string& id) {
  return id_to_url_.erase(id) != 0;
}

void ArcFileSystemBridge::OnReadRequestCompleted(
    const std::string& id,
    std::list<FileStreamForwarderPtr>::iterator it,
    const std::string& file_system_id,
    bool result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  LOG_IF(ERROR, !result) << "Failed to read " << id;
  storage::IsolatedContext::GetInstance()->RemoveReference(file_system_id);
  file_stream_forwarders_.erase(it);
}

void ArcFileSystemBridge::OnConnectionClosed() {
  LOG(WARNING) << "FileSystem connection has been closed. "
               << "Closing SelectFileDialogs owned by ARC apps, if any.";
  select_files_handlers_manager_->DeleteAllHandlers();
}

base::FilePath ArcFileSystemBridge::GetLinuxVFSPathFromExternalFileURL(
    Profile* const profile,
    const GURL& url) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  base::FilePath virtual_path = ash::ExternalFileURLToVirtualPath(url);

  std::string mount_name, cracked_id;
  storage::FileSystemType file_system_type;
  base::FilePath absolute_path;
  storage::FileSystemMountOption mount_option;

  if (!storage::ExternalMountPoints::GetSystemInstance()->CrackVirtualPath(
          virtual_path, &mount_name, &file_system_type, &cracked_id,
          &absolute_path, &mount_option)) {
    LOG(WARNING) << "Couldn't find mount point for: " << url;
    return base::FilePath();
  }

  return GetLinuxVFSPathForPathOnFileSystemType(profile, absolute_path,
                                                file_system_type);
}

base::FilePath ArcFileSystemBridge::GetLinuxVFSPathForPathOnFileSystemType(
    Profile* const profile,
    const base::FilePath& path,
    storage::FileSystemType file_system_type) {
  switch (file_system_type) {
    case storage::FileSystemType::kFileSystemTypeDriveFs:
    case storage::FileSystemType::kFileSystemTypeSmbFs:
    case storage::FileSystemType::kFileSystemTypeFuseBox:
      return path;
    case storage::FileSystemType::kFileSystemTypeLocal: {
      base::FilePath crostini_mount_path =
          file_manager::util::GetCrostiniMountDirectory(profile);
      if (crostini_mount_path == path || crostini_mount_path.IsParent(path))
        return path;

      // fuse-zip, rar2fs.
      base::FilePath archive_mount_path =
          base::FilePath(file_manager::util::kArchiveMountPath);
      if (archive_mount_path.IsParent(path))
        return path;

      break;
    }
    default:
      break;
  }

  // The path is not representable on the Linux VFS.
  return base::FilePath();
}

void ArcFileSystemBridge::SetMaxNumberOfSharedMonikersForTesting(size_t value) {
  max_number_of_shared_monikers_ = value;
}

}  // namespace arc