File: devtools_file_storage.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (95 lines) | stat: -rw-r--r-- 3,815 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
// Copyright 2025 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/devtools/devtools_file_storage.h"

#include <vector>

#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/browser/file_system/isolated_context.h"
#include "storage/common/file_system/file_system_util.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"

using content::BrowserThread;
using content::RenderViewHost;
using content::WebContents;
using storage::IsolatedContext;

namespace {

static const char kRootName[] = "<root>";

IsolatedContext* isolated_context() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  IsolatedContext* isolated_context = IsolatedContext::GetInstance();
  DCHECK(isolated_context);
  return isolated_context;
}

}  // namespace

DevToolsFileStorage::DevToolsFileStorage(WebContents* web_contents)
    : web_contents_(web_contents) {}

DevToolsFileStorage::~DevToolsFileStorage() = default;

DevToolsFileHelper::FileSystem DevToolsFileStorage::RegisterFileSystem(
    const base::FilePath& path,
    const std::string& type) {
  CHECK(web_contents_->GetLastCommittedURL().SchemeIs(
      content::kChromeDevToolsScheme));
  std::string root_name(kRootName);
  auto file_system = isolated_context()->RegisterFileSystemForPath(
      storage::kFileSystemTypeLocal, std::string(), path, &root_name);

  content::ChildProcessSecurityPolicy* policy =
      content::ChildProcessSecurityPolicy::GetInstance();
  RenderViewHost* render_view_host =
      web_contents_->GetPrimaryMainFrame()->GetRenderViewHost();
  int renderer_id = render_view_host->GetProcess()->GetDeprecatedID();
  policy->GrantReadFileSystem(renderer_id, file_system.id());
  policy->GrantWriteFileSystem(renderer_id, file_system.id());
  policy->GrantCreateFileForFileSystem(renderer_id, file_system.id());
  policy->GrantDeleteFromFileSystem(renderer_id, file_system.id());

  // We only need file level access for reading FileEntries. Saving FileEntries
  // just needs the file system to have read/write access, which is granted
  // above if required.
  if (!policy->CanReadFile(renderer_id, path)) {
    policy->GrantReadFile(renderer_id, path);
  }
  GURL origin = web_contents_->GetLastCommittedURL().DeprecatedGetOriginAsURL();
  std::string file_system_name =
      storage::GetIsolatedFileSystemName(origin, file_system.id());
  std::string root_url = storage::GetIsolatedFileSystemRootURIString(
      origin, file_system.id(), root_name);
  return DevToolsFileHelper::FileSystem(type, file_system_name, root_url,
                                        path.AsUTF8Unsafe());
}

void DevToolsFileStorage::UnregisterFileSystem(const base::FilePath& path) {
  isolated_context()->RevokeFileSystemByPath(path);
}

std::vector<base::FilePath> DevToolsFileStorage::GetDraggedFileSystemPaths(
    const GURL& file_system_url) {
  auto root_url = isolated_context()->CrackURL(
      file_system_url, blink::StorageKey::CreateFirstParty(
                           url::Origin::Create(file_system_url)));
  std::vector<base::FilePath> file_system_paths;
  if (root_url.is_valid() && root_url.path().empty()) {
    std::vector<storage::MountPoints::MountPointInfo> mount_points;
    isolated_context()->GetDraggedFileInfo(root_url.filesystem_id(),
                                           &mount_points);
    for (const auto& mount_point : mount_points) {
      file_system_paths.push_back(mount_point.path);
    }
  }
  return file_system_paths;
}