File: file_system_access_observer_host.h

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 (127 lines) | stat: -rw-r--r-- 5,099 bytes parent folder | download | duplicates (4)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_FILE_SYSTEM_ACCESS_FILE_SYSTEM_ACCESS_OBSERVER_HOST_H_
#define CONTENT_BROWSER_FILE_SYSTEM_ACCESS_FILE_SYSTEM_ACCESS_OBSERVER_HOST_H_

#include <memory>
#include <variant>

#include "base/containers/flat_set.h"
#include "base/containers/unique_ptr_adapters.h"
#include "base/sequence_checker.h"
#include "content/browser/file_system_access/file_system_access_manager_impl.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "third_party/blink/public/mojom/file_system_access/file_system_access_observer_host.mojom.h"

namespace content {
class FileSystemAccessObserverObservation;
class FileSystemAccessWatcherManager;

// Stores the state associated with each FileSystemAccessObserverHost mojo
// connection.
//
// The bulk of the FileSystemObserver implementation is in the
// FileSystemAccessWatcherManager class. Each StoragePartition has a single
// associated FileSystemAccessWatcherManager instance. By contrast, each
// FileSystemAccessObserverHost mojo connection has an associated
// FileSystemAccessObserverHost instance, which stores the per-connection state.
//
// Instances of this class must be accessed exclusively on the UI thread,
// because they call into FileSystemAccessWatcherManager directly.
class FileSystemAccessObserverHost
    : public blink::mojom::FileSystemAccessObserverHost {
 public:
  using BindingContext = FileSystemAccessManagerImpl::BindingContext;

  FileSystemAccessObserverHost(
      FileSystemAccessManagerImpl* manager,
      FileSystemAccessWatcherManager* watcher_manager,
      const BindingContext& binding_context,
      mojo::PendingReceiver<blink::mojom::FileSystemAccessObserverHost>
          host_receiver);
  ~FileSystemAccessObserverHost() override;

  // blink::mojom::FileSystemObserverHost:
  void Observe(
      mojo::PendingRemote<blink::mojom::FileSystemAccessTransferToken> token,
      bool is_recursive,
      ObserveCallback callback) override;
  void Unobserve(
      mojo::PendingRemote<blink::mojom::FileSystemAccessTransferToken> token)
      override;

  void RemoveObservation(FileSystemAccessObserverObservation* observation);

  const BindingContext& binding_context() const { return binding_context_; }
  FileSystemAccessManagerImpl* manager() const { return manager_; }
  FileSystemAccessWatcherManager* watcher_manager() const {
    return watcher_manager_;
  }

 private:
  void DidResolveTransferTokenToObserve(
      bool is_recursive,
      ObserveCallback callback,
      FileSystemAccessTransferTokenImpl* resolved_token);

  void DidCheckIfSymlinkOrJunction(
      std::variant<std::unique_ptr<FileSystemAccessDirectoryHandleImpl>,
                   std::unique_ptr<FileSystemAccessFileHandleImpl>> handle,
      ObserveCallback callback,
      storage::FileSystemURL url,
      bool is_recursive,
      FileSystemAccessPermissionContext::HandleType handle_type,
      bool is_symlink_or_junction);

  void DidCheckItemExists(
      std::variant<std::unique_ptr<FileSystemAccessDirectoryHandleImpl>,
                   std::unique_ptr<FileSystemAccessFileHandleImpl>> handle,
      ObserveCallback callback,
      storage::FileSystemURL url,
      bool is_recursive,
      base::File::Error result);

  void DidResolveTransferTokenToUnobserve(
      FileSystemAccessTransferTokenImpl* resolved_token);

  void GotObservation(
      std::variant<std::unique_ptr<FileSystemAccessDirectoryHandleImpl>,
                   std::unique_ptr<FileSystemAccessFileHandleImpl>> handle,
      ObserveCallback callback,
      base::expected<
          std::unique_ptr<FileSystemAccessObservationGroup::Observer>,
          blink::mojom::FileSystemAccessErrorPtr> observation_or_error);

  void OnHostReceiverDisconnect();

  SEQUENCE_CHECKER(sequence_checker_);

  // The manager which owns `watcher_manager_`.
  const raw_ptr<FileSystemAccessManagerImpl> manager_ = nullptr;
  // The watcher manager which owns this instance.
  const raw_ptr<FileSystemAccessWatcherManager> watcher_manager_ = nullptr;
  const BindingContext binding_context_;

  // Observations which maintain mojo pipes that send file change notifications
  // back to the renderer. Each connection corresponds to a file system watch
  // set up with `Observe()`.
  base::flat_set<std::unique_ptr<FileSystemAccessObserverObservation>,
                 base::UniquePtrComparator>
      observations_;

  // Connection owned by a FileSystemObserver object. When the
  // FileSystemObserver is destroyed, this instance will remove itself from the
  // manager.
  // TODO(crbug.com/40283779): Make the lifetime not depend on GC.
  mojo::Receiver<blink::mojom::FileSystemAccessObserverHost> host_receiver_
      GUARDED_BY_CONTEXT(sequence_checker_);

  base::WeakPtrFactory<FileSystemAccessObserverHost> weak_factory_
      GUARDED_BY_CONTEXT(sequence_checker_){this};
};

}  // namespace content

#endif  // CONTENT_BROWSER_FILE_SYSTEM_ACCESS_FILE_SYSTEM_ACCESS_OBSERVER_HOST_H_