File: scoped_file_access_delegate.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; 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 (162 lines) | stat: -rw-r--r-- 5,705 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
// 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 "components/file_access/scoped_file_access_delegate.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "components/file_access/scoped_file_access.h"

namespace file_access {
// static
ScopedFileAccessDelegate* ScopedFileAccessDelegate::Get() {
  return scoped_file_access_delegate_;
}

// static
bool ScopedFileAccessDelegate::HasInstance() {
  return scoped_file_access_delegate_;
}

// static
void ScopedFileAccessDelegate::DeleteInstance() {
  if (scoped_file_access_delegate_) {
    delete scoped_file_access_delegate_;
    scoped_file_access_delegate_ = nullptr;
  }
}

// static
void ScopedFileAccessDelegate::RequestDefaultFilesAccessIO(
    const std::vector<base::FilePath>& files,
    base::OnceCallback<void(ScopedFileAccess)> callback) {
  if (request_files_access_for_system_io_callback_) {
    request_files_access_for_system_io_callback_->Run(
        files, std::move(callback), /*check_default=*/true);
  } else {
    std::move(callback).Run(ScopedFileAccess::Allowed());
  }
}

// static
void ScopedFileAccessDelegate::RequestFilesAccessForSystemIO(
    const std::vector<base::FilePath>& files,
    base::OnceCallback<void(ScopedFileAccess)> callback) {
  if (request_files_access_for_system_io_callback_) {
    request_files_access_for_system_io_callback_->Run(
        files, std::move(callback), /*check_default=*/false);
  } else {
    std::move(callback).Run(ScopedFileAccess::Allowed());
  }
}

// static
ScopedFileAccessDelegate::RequestFilesAccessIOCallback
ScopedFileAccessDelegate::GetCallbackForSystem() {
  return base::BindRepeating(
      [](const std::vector<base::FilePath>& file_paths,
         base::OnceCallback<void(ScopedFileAccess)> callback) {
        if (request_files_access_for_system_io_callback_) {
          request_files_access_for_system_io_callback_->Run(
              file_paths, std::move(callback), /*check_default=*/false);
        } else {
          std::move(callback).Run(ScopedFileAccess::Allowed());
        }
      });
}

ScopedFileAccessDelegate::ScopedFileAccessDelegate() {
  if (scoped_file_access_delegate_) {
    delete scoped_file_access_delegate_;
  }
  scoped_file_access_delegate_ = this;
}

ScopedFileAccessDelegate::~ScopedFileAccessDelegate() {
  if (scoped_file_access_delegate_ == this) {
    scoped_file_access_delegate_ = nullptr;
  }
}

// static
ScopedFileAccessDelegate*
    ScopedFileAccessDelegate::scoped_file_access_delegate_ = nullptr;

// static
ScopedFileAccessDelegate::RequestFilesAccessCheckDefaultCallback*
    ScopedFileAccessDelegate::request_files_access_for_system_io_callback_ =
        nullptr;

ScopedFileAccessDelegate::ScopedRequestFilesAccessCallbackForTesting::
    ScopedRequestFilesAccessCallbackForTesting(
        RequestFilesAccessIOCallback callback,
        bool restore_original_callback)
    : restore_original_callback_(restore_original_callback) {
  original_callback_ =
      base::WrapUnique(request_files_access_for_system_io_callback_);
  request_files_access_for_system_io_callback_ =
      new RequestFilesAccessCheckDefaultCallback(base::BindRepeating(
          [](RequestFilesAccessIOCallback callback,
             const std::vector<base::FilePath>& files,
             base::OnceCallback<void(ScopedFileAccess)> cb,
             bool check_default) { callback.Run(files, std::move(cb)); },
          std::move(callback)));
}

ScopedFileAccessDelegate::ScopedRequestFilesAccessCallbackForTesting::
    ~ScopedRequestFilesAccessCallbackForTesting() {
  if (request_files_access_for_system_io_callback_) {
    delete request_files_access_for_system_io_callback_;
  }
  if (!restore_original_callback_ && original_callback_) {
    original_callback_.reset();
  }
  request_files_access_for_system_io_callback_ = original_callback_.release();
}

void ScopedFileAccessDelegate::ScopedRequestFilesAccessCallbackForTesting::
    RunOriginalCallback(
        const std::vector<base::FilePath>& path,
        base::OnceCallback<void(file_access::ScopedFileAccess)> callback) {
  original_callback_->Run(path, std::move(callback), /*check_default=*/false);
}

void RequestFilesAccess(
    const std::vector<base::FilePath>& files,
    const GURL& destination_url,
    base::OnceCallback<void(file_access::ScopedFileAccess)> callback) {
  if (ScopedFileAccessDelegate::HasInstance()) {
    ScopedFileAccessDelegate::Get()->RequestFilesAccess(files, destination_url,
                                                        std::move(callback));
  } else {
    std::move(callback).Run(ScopedFileAccess::Allowed());
  }
}

void RequestFilesAccessForSystem(
    const std::vector<base::FilePath>& files,
    base::OnceCallback<void(file_access::ScopedFileAccess)> callback) {
  if (ScopedFileAccessDelegate::HasInstance()) {
    ScopedFileAccessDelegate::Get()->RequestFilesAccessForSystem(
        files, std::move(callback));
  } else {
    std::move(callback).Run(ScopedFileAccess::Allowed());
  }
}

ScopedFileAccessDelegate::RequestFilesAccessIOCallback CreateFileAccessCallback(
    const GURL& destination) {
  if (ScopedFileAccessDelegate::HasInstance()) {
    return ScopedFileAccessDelegate::Get()->CreateFileAccessCallback(
        destination);
  }
  return base::BindRepeating(
      [](const GURL& destination, const std::vector<base::FilePath>& files,
         base::OnceCallback<void(file_access::ScopedFileAccess)> callback) {
        std::move(callback).Run(file_access::ScopedFileAccess::Allowed());
      },
      destination);
}

}  // namespace file_access