File: file_info.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,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 (166 lines) | stat: -rw-r--r-- 5,758 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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/clipboard/file_info.h"

#include <string_view>

#include "base/strings/escape.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"

namespace ui {
namespace {

constexpr char kFileUrlPrefix[] = "file://";
constexpr char kFileSchemePrefix[] = "file:";
constexpr char kURIListSeparator[] = "\r\n";

// Returns true if path starts with a letter, followed by a colon, followed
// by a path separator.
bool StartsWithDriveLetter(std::string_view path) {
  return path.length() > 2 && base::IsAsciiAlpha(path[0]) && path[1] == ':' &&
         base::FilePath::IsSeparator(path[2]);
}

// We implement our own URLToPath() and PathToURL() rather than use
// net::FileUrlToFilePath() or net::FilePathToFileURL() since //net code works
// differently on each platform and is overly strict. In particular, it doesn't
// allow Windows network paths such as //ChromeOS/MyFiles on OS_CHROMEOS.
//
// This code is a little different in nature to most other path handling in that
// we expect this code to roundtrip both posix and windows paths (local or
// network) when running on either platform.
//
// Convert file:// |url| to a FilePath. Returns empty if |url| is invalid.
// This function expects an absolute path since it is not possible to encode
// a relative path as a file:// URL. The third slash in 'file:///' is not
// mandatory, but without it, the path is considered a network path
// (file://host/path). If a drive letter followed by colon and slash is detected
// (file:///C:/path), the path is assumed to be windows path 'C:/path', but
// without the slash (file:///C:path), the path is assumed to posix '/C:path'
// rather than windows relative path 'C:path'.
base::FilePath URLToPath(std::string_view url) {
  // Must start with 'file://' with at least 1 more char.
  std::string prefix(kFileUrlPrefix);
  if (url.size() <= prefix.size() || !url.starts_with(prefix)) {
    return base::FilePath();
  }

  // Skip slashes after 'file:' if needed:
  size_t path_start;
  if (url[prefix.size()] == '/') {
    // file:///path => /path
    path_start = prefix.size();
    if (StartsWithDriveLetter(url.substr(path_start + 1))) {
      // file:///C:/path => C:/path
      ++path_start;
    }
  } else {
    // file://host/path => //host/path
    DCHECK_EQ(prefix.size(), 7u);
    path_start = prefix.size() - 2;
  }

  std::string result = base::UnescapeBinaryURLComponent(url.substr(path_start));
#if BUILDFLAG(IS_WIN)
  return base::FilePath(base::UTF8ToWide(result)).NormalizePathSeparators();
#else
  return base::FilePath(result);
#endif
}

}  // namespace

FileInfo::FileInfo() = default;

FileInfo::FileInfo(const base::FilePath& path,
                   const base::FilePath& display_name)
    : path(path), display_name(display_name) {}

FileInfo::~FileInfo() = default;

bool FileInfo::operator==(const FileInfo& other) const {
  return path == other.path && display_name == other.display_name;
}

std::vector<FileInfo> URIListToFileInfos(std::string_view uri_list) {
  std::vector<FileInfo> result;
  std::vector<std::string_view> lines =
      base::SplitStringPiece(uri_list, kURIListSeparator, base::TRIM_WHITESPACE,
                             base::SPLIT_WANT_NONEMPTY);
  for (std::string_view line : lines) {
    base::FilePath path = URLToPath(line);
    if (!path.empty()) {
      result.push_back(FileInfo(path, base::FilePath()));
    }
  }
  return result;
}

std::string FilePathToFileURL(const base::FilePath& file_path) {
  std::string url;
#if BUILDFLAG(IS_WIN)
  std::string path = base::WideToUTF8(file_path.value());
#else
  std::string path = file_path.value();
#endif
  // Allocate maximum possible size upfront:
  // 'file:' + '///' + (3 x path.size() for percent encoding).
  url.reserve((sizeof(kFileSchemePrefix) - 1) + 3 + (3 * path.size()));
  url += kFileSchemePrefix;

  // Add slashes after 'file:' if needed:
  if (path.size() > 1 && base::FilePath::IsSeparator(path[0]) &&
      base::FilePath::IsSeparator(path[1])) {
    //  //host/path    => file://host/path
  } else if (path.size() > 0 && base::FilePath::IsSeparator(path[0])) {
    //  /absolute/path => file:///absolute/path
    url += "//";
  } else {
    //  relative/path  => file:///relative/path
    //  C:/path        => file:///C:/path
    // A relative path can't be encoded as a file:// URL, so we will produce an
    // absolute path like GURL() does. We do expect input to be absolute, and
    // ideally, we would DCHECK(file_path.IsAbsolute()), but it is possible that
    // we are interpreting a windows path while running on a posix platform.
    url += "///";
  }

  for (char c : path) {
    // Encode special characters `%;#?\`.
    if (c == '%' || c == ';' || c == '#' || c == '?' ||
#if !BUILDFLAG(IS_WIN)
        // Backslash is percent-encoded on posix platforms.
        c == '\\' ||
#endif
        // Encode space and all control chars.
        c <= ' ') {
      url += '%';
      base::AppendHexEncodedByte(static_cast<uint8_t>(c), url);
#if BUILDFLAG(IS_WIN)
    } else if (c == '\\') {
      // Backslash is converted to slash on windows.
      url += '/';
#endif
    } else {
      url += c;
    }
  }

  return url;
}

std::string FileInfosToURIList(const std::vector<FileInfo>& filenames) {
  std::vector<std::string> uri_list;
  for (const FileInfo& file : filenames) {
    uri_list.push_back(FilePathToFileURL(file.path));
  }
  return base::JoinString(uri_list, kURIListSeparator);
}

}  // namespace ui