File: private_api_mount.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 (329 lines) | stat: -rw-r--r-- 12,401 bytes parent folder | download | duplicates (6)
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
// Copyright 2013 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/extensions/file_manager/private_api_mount.h"

#include <memory>
#include <string_view>
#include <utility>

#include "base/feature_list.h"
#include "base/files/file_util.h"
#include "base/format_macros.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "chrome/browser/ash/drive/file_system_util.h"
#include "chrome/browser/ash/extensions/file_manager/event_router.h"
#include "chrome/browser/ash/extensions/file_manager/private_api_util.h"
#include "chrome/browser/ash/file_manager/file_tasks_notifier.h"
#include "chrome/browser/ash/file_manager/file_tasks_notifier_factory.h"
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/browser/ash/file_manager/volume_manager.h"
#include "chrome/browser/ash/fileapi/file_system_backend.h"
#include "chrome/browser/ash/fusebox/fusebox_server.h"
#include "chrome/browser/ash/smb_client/smb_service.h"
#include "chrome/browser/ash/smb_client/smb_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/file_manager_private.h"
#include "chromeos/ash/components/dbus/cros_disks/cros_disks_client.h"
#include "chromeos/ash/components/disks/disk_mount_manager.h"
#include "components/drive/event_logger.h"
#include "components/services/unzip/content/unzip_service.h"
#include "components/services/unzip/public/cpp/unzip.h"
#include "content/public/browser/browser_thread.h"
#include "google_apis/common/task_util.h"
#include "storage/browser/file_system/file_system_url.h"
#include "ui/shell_dialogs/selected_file_info.h"

namespace extensions {
namespace {

std::string Redact(std::string_view path) {
  return LOG_IS_ON(INFO) ? base::StrCat({"'", path, "'"}) : "(redacted)";
}

}  // namespace

using ::ash::disks::DiskMountManager;
using content::BrowserThread;
namespace file_manager_private = extensions::api::file_manager_private;

FileManagerPrivateAddMountFunction::FileManagerPrivateAddMountFunction() =
    default;

FileManagerPrivateAddMountFunction::~FileManagerPrivateAddMountFunction() =
    default;

ExtensionFunction::ResponseAction FileManagerPrivateAddMountFunction::Run() {
  using file_manager_private::AddMount::Params;
  const std::optional<Params> params = Params::Create(args());
  EXTENSION_FUNCTION_VALIDATE(params);

  Profile* const profile = Profile::FromBrowserContext(browser_context());
  if (drive::EventLogger* logger = file_manager::util::GetLogger(profile)) {
    logger->Log(logging::LOGGING_INFO, "%s[%s] called. (source: '%s')", name(),
                request_uuid().AsLowercaseString().c_str(),
                params->file_url.empty() ? "(none)" : params->file_url.c_str());
  }
  set_log_on_completion(true);

  const scoped_refptr<storage::FileSystemContext> file_system_context =
      file_manager::util::GetFileSystemContextForRenderFrameHost(
          profile, render_frame_host());
  const storage::FileSystemURL fs_url(
      file_system_context->CrackURLInFirstPartyContext(GURL(params->file_url)));
  path_ = ash::FileSystemBackend::CanHandleURL(fs_url)
              ? (fs_url.TypeImpliesPathIsReal()
                     ? fs_url.path()
                     : fusebox::Server::SubstituteFuseboxFilePath(fs_url))
              : base::FilePath();

  if (auto* notifier =
          file_manager::file_tasks::FileTasksNotifierFactory::GetForProfile(
              profile)) {
    std::vector<storage::FileSystemURL> urls;
    urls.push_back(std::move(fs_url));
    notifier->NotifyFileTasks(urls);
  }

  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  if (params->password) {
    options_.push_back("password=" + *params->password);
  }

  extension_ = base::ToLowerASCII(path_.Extension());

  // Detect the file path encoding of ZIP archives.
  if (extension_ == ".zip") {
    unzip::DetectEncoding(
        unzip::LaunchUnzipper(), path_,
        base::BindOnce(&FileManagerPrivateAddMountFunction::OnEncodingDetected,
                       this));
  } else {
    FinishMounting();
  }

  // Pass back the actual source path of the mount point.
  return RespondNow(WithArguments(path_.AsUTF8Unsafe()));
}

void FileManagerPrivateAddMountFunction::OnEncodingDetected(
    const Encoding encoding) {
  // Pass the detected ZIP encoding as a mount option.
  std::string& option = options_.emplace_back("encoding=");

  if (IsShiftJisOrVariant(encoding) || encoding == RUSSIAN_CP866) {
    option += MimeEncodingName(encoding);
  } else {
    option += "libzip";
  }

  FinishMounting();
}

void FileManagerPrivateAddMountFunction::FinishMounting() {
  DiskMountManager* const disk_mount_manager = DiskMountManager::GetInstance();
  DCHECK(disk_mount_manager);
  disk_mount_manager->MountPath(path_.AsUTF8Unsafe(), std::move(extension_),
                                path_.BaseName().AsUTF8Unsafe(),
                                std::move(options_), ash::MountType::kArchive,
                                ash::MountAccessMode::kReadWrite,
                                base::DoNothing());
}

FileManagerPrivateCancelMountingFunction::
    FileManagerPrivateCancelMountingFunction() = default;

FileManagerPrivateCancelMountingFunction::
    ~FileManagerPrivateCancelMountingFunction() = default;

ExtensionFunction::ResponseAction
FileManagerPrivateCancelMountingFunction::Run() {
  using file_manager_private::CancelMounting::Params;
  const std::optional<Params> params = Params::Create(args());
  EXTENSION_FUNCTION_VALIDATE(params);

  Profile* const profile = Profile::FromBrowserContext(browser_context());

  if (drive::EventLogger* logger = file_manager::util::GetLogger(profile)) {
    logger->Log(logging::LOGGING_INFO, "%s[%s] called. (source: '%s')", name(),
                request_uuid().AsLowercaseString().c_str(),
                params->file_url.empty() ? "(none)" : params->file_url.c_str());
  }
  set_log_on_completion(true);

  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  const scoped_refptr<storage::FileSystemContext> file_system_context =
      file_manager::util::GetFileSystemContextForRenderFrameHost(
          profile, render_frame_host());
  const storage::FileSystemURL fs_url(
      file_system_context->CrackURLInFirstPartyContext(GURL(params->file_url)));
  base::FilePath path =
      ash::FileSystemBackend::CanHandleURL(fs_url)
          ? (fs_url.TypeImpliesPathIsReal()
                 ? fs_url.path()
                 : fusebox::Server::SubstituteFuseboxFilePath(fs_url))
          : base::FilePath();

  DiskMountManager* const disk_mount_manager = DiskMountManager::GetInstance();
  DCHECK(disk_mount_manager);
  disk_mount_manager->UnmountPath(
      path.AsUTF8Unsafe(),
      base::BindOnce(&FileManagerPrivateCancelMountingFunction::OnCancelled,
                     this));

  return RespondLater();
}

void FileManagerPrivateCancelMountingFunction::OnCancelled(
    ash::MountError error) {
  if (error == ash::MountError::kSuccess) {
    Respond(NoArguments());
  } else {
    Respond(Error(file_manager_private::ToString(
        file_manager::MountErrorToMountCompletedStatus(error))));
  }
}

ExtensionFunction::ResponseAction FileManagerPrivateRemoveMountFunction::Run() {
  using file_manager_private::RemoveMount::Params;
  const std::optional<Params> params = Params::Create(args());
  EXTENSION_FUNCTION_VALIDATE(params);

  Profile* const profile = Profile::FromBrowserContext(browser_context());
  if (drive::EventLogger* logger = file_manager::util::GetLogger(profile)) {
    logger->Log(logging::LOGGING_INFO, "%s[%s] called. (volume_id: '%s')",
                name(), request_uuid().AsLowercaseString().c_str(),
                params->volume_id.c_str());
  }
  set_log_on_completion(true);

  using file_manager::Volume;
  using file_manager::VolumeManager;
  VolumeManager* const volume_manager = VolumeManager::Get(profile);
  DCHECK(volume_manager);

  std::string volume_id = params->volume_id;
  volume_manager->ConvertFuseBoxFSPVolumeIdToFSPIfNeeded(&volume_id);

  const base::WeakPtr<Volume> volume =
      volume_manager->FindVolumeById(volume_id);
  if (!volume) {
    LOG(ERROR) << "Cannot find volume " << Redact(volume_id);
    return RespondNow(Error(file_manager_private::ToString(
        api::file_manager_private::MountError::kPathNotMounted)));
  }

  switch (volume->type()) {
    case file_manager::VOLUME_TYPE_REMOVABLE_DISK_PARTITION:
    case file_manager::VOLUME_TYPE_MOUNTED_ARCHIVE_FILE:
      DiskMountManager::GetInstance()->UnmountPath(
          volume->mount_path().value(),
          base::BindOnce(
              &FileManagerPrivateRemoveMountFunction::OnDiskUnmounted, this));
      return RespondLater();

    case file_manager::VOLUME_TYPE_PROVIDED: {
      auto* service =
          ash::file_system_provider::Service::Get(browser_context());
      DCHECK(service);
      if (!service->RequestUnmount(volume->provider_id(),
                                   volume->file_system_id())) {
        return RespondNow(Error("Unmount failed"));
      }
      return RespondNow(NoArguments());
    }

    case file_manager::VOLUME_TYPE_CROSTINI:
      file_manager::VolumeManager::Get(profile)->RemoveSshfsCrostiniVolume(
          volume->mount_path(),
          base::BindOnce(
              &FileManagerPrivateRemoveMountFunction::OnSshFsUnmounted, this));
      return RespondLater();

    case file_manager::VOLUME_TYPE_SMB:
      ash::smb_client::SmbServiceFactory::Get(profile)->UnmountSmbFs(
          volume->mount_path());
      return RespondNow(NoArguments());

    case file_manager::VOLUME_TYPE_TESTING:
      file_manager::VolumeManager::Get(profile)
          ->RemoveVolumeForTesting(  // IN-TEST
              volume->mount_path(), volume->type(), volume->device_type(),
              volume->is_read_only(), volume->storage_device_path(),
              volume->drive_label(), volume->file_system_type());

      return RespondNow(NoArguments());

    case file_manager::VOLUME_TYPE_GUEST_OS:
      // TODO(crbug/1293229): Figure out if we need to support unmounting. I'm
      // not actually sure if it's possible to reach here.
      NOTREACHED();

    default:
      // Requested unmounting a device which is not unmountable.
      return RespondNow(Error("Invalid volume type"));
  }
}

void FileManagerPrivateRemoveMountFunction::OnSshFsUnmounted(bool ok) {
  if (ok) {
    Respond(NoArguments());
  } else {
    Respond(Error(file_manager_private::ToString(
        api::file_manager_private::MountError::kUnknownError)));
  }
}

void FileManagerPrivateRemoveMountFunction::OnDiskUnmounted(
    ash::MountError error) {
  if (error == ash::MountError::kSuccess) {
    Respond(NoArguments());
  } else {
    Respond(Error(file_manager_private::ToString(
        file_manager::MountErrorToMountCompletedStatus(error))));
  }
}

ExtensionFunction::ResponseAction
FileManagerPrivateGetVolumeMetadataListFunction::Run() {
  if (!args().empty()) {
    return RespondNow(Error("Invalid arguments"));
  }

  Profile* const profile = Profile::FromBrowserContext(browser_context());
  const std::vector<base::WeakPtr<file_manager::Volume>>& volume_list =
      file_manager::VolumeManager::Get(profile)->GetVolumeList();

  std::string log_string;
  std::vector<file_manager_private::VolumeMetadata> result;
  for (const auto& volume : volume_list) {
    file_manager_private::VolumeMetadata volume_metadata;
    file_manager::util::VolumeToVolumeMetadata(profile, *volume,
                                               &volume_metadata);
    result.push_back(std::move(volume_metadata));
    if (!log_string.empty()) {
      log_string += ", ";
    }
    log_string += volume->mount_path().AsUTF8Unsafe();
  }

  if (drive::EventLogger* logger = file_manager::util::GetLogger(profile)) {
    logger->Log(logging::LOGGING_INFO,
                "%s[%s] succeeded. (results: '[%s]', %" PRIuS " mount points)",
                name(), request_uuid().AsLowercaseString().c_str(),
                log_string.c_str(), result.size());
  }

  return RespondNow(ArgumentList(
      file_manager_private::GetVolumeMetadataList::Results::Create(result)));
}

}  // namespace extensions