• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_FILES_SAFE_BASE_NAME_H_
6 #define BASE_FILES_SAFE_BASE_NAME_H_
7 
8 #include "base/base_export.h"
9 #include "base/files/file_path.h"
10 #include "third_party/abseil-cpp/absl/types/optional.h"
11 
12 namespace base {
13 
14 // Represents the last path component of a FilePath object, either a file or a
15 // directory. This type does not allow absolute paths or references to parent
16 // directories and is considered safe to be passed over IPC. See
17 // FilePath::BaseName().
18 // Usage examples:
19 // absl::optional<SafeBaseName> a
20 //     (SafeBaseName::Create(FILE_PATH_LITERAL("file.txt")));
21 // FilePath dir(FILE_PATH_LITERAL("foo")); dir.Append(*a);
22 class BASE_EXPORT SafeBaseName {
23  public:
24   // TODO(crbug.com/1269986): Change to only be exposed to Mojo.
25   SafeBaseName() = default;
26 
27   // Factory method that returns a valid SafeBaseName or absl::nullopt.
28   static absl::optional<SafeBaseName> Create(const FilePath&);
29 
30   // Same as above, but takes a StringPieceType for convenience.
31   static absl::optional<SafeBaseName> Create(FilePath::StringPieceType);
path()32   const FilePath& path() const { return path_; }
33 
34   bool operator==(const SafeBaseName& that) const;
35 
36  private:
37   // Constructs a new SafeBaseName from the given FilePath.
38   explicit SafeBaseName(const FilePath&);
39   FilePath path_;
40 };
41 
42 }  // namespace base
43 
44 #endif  // BASE_FILES_SAFE_BASE_NAME_H_
45