File: scoped_file_access_delegate.h

package info (click to toggle)
chromium 138.0.7204.92-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,576 kB
  • sloc: cpp: 34,933,512; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,956; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (215 lines) | stat: -rw-r--r-- 9,741 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
// 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.

#ifndef COMPONENTS_FILE_ACCESS_SCOPED_FILE_ACCESS_DELEGATE_H_
#define COMPONENTS_FILE_ACCESS_SCOPED_FILE_ACCESS_DELEGATE_H_

#include <memory>
#include <vector>

#include "base/component_export.h"
#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "base/location.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "components/file_access/scoped_file_access.h"
#include "url/gurl.h"

namespace file_access {

// This class is mainly a interface and used to delegate DLP checks to
// appropriate proxy. It is used for managed ChromeOs only in the implementation
// DlpScopedfileAccessDelegate. Only one instance of a class which extends
// this class can exist at a time. The class itself also manages this one
// instance. When it is replaced the old instance is destructed. This instance
// is constructed and destructed on the UI thread. So all methods should only be
// called from the UI thread. The exception is RequestDefaultFilesAccessIO,
// which takes care of hopping correctly between the threads and providing this
// to packages without direct access to the UI thread.
class COMPONENT_EXPORT(FILE_ACCESS) ScopedFileAccessDelegate {
 public:
  using RequestFilesAccessIOCallback =
      base::RepeatingCallback<void(const std::vector<base::FilePath>&,
                                   base::OnceCallback<void(ScopedFileAccess)>)>;

  using RequestFilesAccessCheckDefaultCallback =
      base::RepeatingCallback<void(const std::vector<base::FilePath>&,
                                   base::OnceCallback<void(ScopedFileAccess)>,
                                   bool check_default)>;
  // When new entries are added, EnterpriseDlpFilesDefaultAccess enum in
  // histograms/enums.xml should be updated.
  enum class DefaultAccess {
    kMyFilesAllow = 0,
    kMyFilesDeny = 1,
    kSystemFilesAllow = 2,
    kSystemFilesDeny = 3,
    kMaxValue = kSystemFilesDeny
  };

  ScopedFileAccessDelegate(const ScopedFileAccessDelegate&) = delete;
  ScopedFileAccessDelegate& operator=(const ScopedFileAccessDelegate&) = delete;

  // Returns a pointer to the existing instance of the class.
  static ScopedFileAccessDelegate* Get();

  // Returns true if an instance exists, without forcing an initialization.
  static bool HasInstance();

  // Deletes the existing instance of the class if it's already created.
  // Indicates that restricting data transfer is no longer required.
  // The instance will be deconstructed
  static void DeleteInstance();

  // Requests access to |files| in order to be sent to |destination_url|.
  // |callback| is called with a token that should be hold until
  // `open()` operation on the files finished.
  virtual void RequestFilesAccess(
      const std::vector<base::FilePath>& files,
      const GURL& destination_url,
      base::OnceCallback<void(file_access::ScopedFileAccess)> callback) = 0;

  // Requests access to |files| in order to be sent to a system process.
  // |callback| is called with a token that should be hold until
  // `open()` operation on the files finished.
  virtual void RequestFilesAccessForSystem(
      const std::vector<base::FilePath>& files,
      base::OnceCallback<void(file_access::ScopedFileAccess)> callback) = 0;

  // If feature DataControlsDefaultDeny is not set, requests access to |files|.
  // |callback| is called with a token that should be hold until `open()`
  // operation on the files finished. If the feature is set the `callback` is
  // called with a dummy `ScopedFileAccess`, which will lead the daemon to deny
  // access, if the file is restricted.
  virtual void RequestDefaultFilesAccess(
      const std::vector<base::FilePath>& files,
      base::OnceCallback<void(file_access::ScopedFileAccess)> callback) = 0;

  // Creates a callback to gain file access for the given `destination`. The
  // callback should be called on the IO thread. The method itself from the UI
  // thread.
  virtual RequestFilesAccessIOCallback CreateFileAccessCallback(
      const GURL& destination) const = 0;

  // Called from the IO thread. Depending on the default behaviour
  // (kDataControlsDefaultDeny feature) executes the callback without requesting
  // file access (denying access to managed files) or switch to the UI thread
  // and call RequestFilesAccessForSystem there. The `callback` is run on the IO
  // thread in both cases. The feature might get removed at some point
  // (b/306619855); the behaviour after that will like executing the `callback`
  // directly.
  static void RequestDefaultFilesAccessIO(
      const std::vector<base::FilePath>& files,
      base::OnceCallback<void(ScopedFileAccess)> callback);

  // Called from the IO thread. The method will switch to the UI thread and call
  // RequestFilesAccessForSystem there. The `callback` is run on the IO thread
  // with the gained file access token.
  static void RequestFilesAccessForSystemIO(
      const std::vector<base::FilePath>& files,
      base::OnceCallback<void(ScopedFileAccess)> callback);

  // Calls base::ThreadPool::PostTaskAndReplyWithResult but `task` is run with
  // file access to `path`. The file access is hold until the call to `reply`
  // returns.
  template <typename T>
  void AccessScopedPostTaskAndReplyWithResult(
      const base::FilePath& path,
      const base::Location& from_here,
      const base::TaskTraits& traits,
      base::OnceCallback<T()> task,
      base::OnceCallback<void(T)> reply) {
    file_access::ScopedFileAccessDelegate::Get()->RequestFilesAccessForSystem(
        {path},
        base::BindOnce(
            [](const base::FilePath path, const base::Location& from_here,
               const base::TaskTraits traits, base::OnceCallback<T()> task,
               base::OnceCallback<void(T)> reply,
               file_access::ScopedFileAccess file_access) {
              base::ThreadPool::PostTaskAndReplyWithResult(
                  from_here, traits, std::move(task),
                  base::BindOnce([](base::OnceCallback<void(T)> reply,
                                    file_access::ScopedFileAccess file_access,
                                    T arg) { std::move(reply).Run(arg); },
                                 std::move(reply), std::move(file_access)));
            },
            path, from_here, traits, std::move(task), std::move(reply)));
  }

  // This class sets the callback forwarding the RequestFilesAccessForSystem
  // call from IO to UI thread.
  class COMPONENT_EXPORT(FILE_ACCESS)
      ScopedRequestFilesAccessCallbackForTesting {
   public:
    // If `restore_original_callback` is set, it restores the original callback.
    // Otherwise, it destroys the original callback when this class is
    // destroyed.
    explicit ScopedRequestFilesAccessCallbackForTesting(
        RequestFilesAccessIOCallback callback,
        bool restore_original_callback = true);

    virtual ~ScopedRequestFilesAccessCallbackForTesting();

    ScopedRequestFilesAccessCallbackForTesting(
        const ScopedRequestFilesAccessCallbackForTesting&) = delete;
    ScopedRequestFilesAccessCallbackForTesting& operator=(
        const ScopedRequestFilesAccessCallbackForTesting&) = delete;

    void RunOriginalCallback(
        const std::vector<base::FilePath>& path,
        base::OnceCallback<void(file_access::ScopedFileAccess)> callback);

   private:
    bool restore_original_callback_;
    std::unique_ptr<RequestFilesAccessCheckDefaultCallback> original_callback_ =
        nullptr;
  };
  // Get a callback to get file access to files for system component
  // destination. Can be called from IO or UI thread. The callback should be
  // called on IO thread only.
  static RequestFilesAccessIOCallback GetCallbackForSystem();

 protected:
  ScopedFileAccessDelegate();

  virtual ~ScopedFileAccessDelegate();

  // A single instance of ScopedFileAccessDelegate. Equals nullptr when there's
  // not any data transfer restrictions required.
  static ScopedFileAccessDelegate* scoped_file_access_delegate_;

  // A single instance for a callback living on the IO thread which switches to
  // the UI thread to call RequestFilesAccessForSystem from there and switch
  // back to IO thread handing the ScopedFileAccess to another (given) callback.
  static RequestFilesAccessCheckDefaultCallback*
      request_files_access_for_system_io_callback_;
};

// Calls ScopedFilesAccessDelegate::RequestFilesAccess if
// ScopedFilesAccessDelegate::HasInstance returns true, immediately calls the
// callback with a ScopedFileAccess::Allowed object otherwise.
COMPONENT_EXPORT(FILE_ACCESS)
void RequestFilesAccess(
    const std::vector<base::FilePath>& files,
    const GURL& destination_url,
    base::OnceCallback<void(file_access::ScopedFileAccess)> callback);

// Calls ScopedFilesAccessDelegate::RequestFilesAccessForSystem if
// ScopedFilesAccessDelegate::HasInstance returns true, immediately calls the
// callback with a ScopedFileAccess::Allowed object otherwise.
COMPONENT_EXPORT(FILE_ACCESS)
void RequestFilesAccessForSystem(
    const std::vector<base::FilePath>& files,
    base::OnceCallback<void(file_access::ScopedFileAccess)> callback);

// Calls ScopedFilesAccessDelegate::CreateFileAccessCallback if
// ScopedFilesAccessDelegate::HasInstance returns true, returns a callback with
// a ScopedFileAccess::Allowed object otherwise.
COMPONENT_EXPORT(FILE_ACCESS)
ScopedFileAccessDelegate::RequestFilesAccessIOCallback CreateFileAccessCallback(
    const GURL& destination);

}  // namespace file_access

#endif  // COMPONENTS_FILE_ACCESS_SCOPED_FILE_ACCESS_DELEGATE_H_