xref: /aosp_15_r20/external/cronet/base/files/safe_base_name.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #include "base/files/safe_base_name.h"
6 
7 namespace base {
8 
9 // static
Create(const FilePath & path)10 std::optional<SafeBaseName> SafeBaseName::Create(const FilePath& path) {
11   auto basename = path.BaseName();
12 
13   if (!basename.IsAbsolute() && !basename.ReferencesParent() &&
14       !basename.EndsWithSeparator()) {
15     return std::make_optional(SafeBaseName(basename));
16   }
17 
18   return std::nullopt;
19 }
20 
21 // static
Create(FilePath::StringPieceType path)22 std::optional<SafeBaseName> SafeBaseName::Create(
23     FilePath::StringPieceType path) {
24   return Create(FilePath(path));
25 }
26 
SafeBaseName(const FilePath & path)27 SafeBaseName::SafeBaseName(const FilePath& path) : path_(path) {}
28 
operator ==(const SafeBaseName & that) const29 bool SafeBaseName::operator==(const SafeBaseName& that) const {
30   return path_ == that.path_;
31 }
32 
33 }  // namespace base
34