Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / base / filename_util_icu.cc
blob7436a811a8f55fbb94cad7c826475842f9bcbf23
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/base/filename_util.h"
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/i18n/file_util_icu.h"
10 #include "base/strings/string16.h"
11 #include "net/base/filename_util_internal.h"
13 class GURL;
15 namespace net {
17 bool IsSafePortablePathComponent(const base::FilePath& component) {
18 base::string16 component16;
19 base::FilePath::StringType sanitized = component.value();
20 SanitizeGeneratedFileName(&sanitized, true);
21 base::FilePath::StringType extension = component.Extension();
22 if (!extension.empty())
23 extension.erase(extension.begin()); // Erase preceding '.'.
24 return !component.empty() && (component == component.BaseName()) &&
25 (component == component.StripTrailingSeparators()) &&
26 FilePathToString16(component, &component16) &&
27 base::i18n::IsFilenameLegal(component16) &&
28 !IsShellIntegratedExtension(extension) &&
29 (sanitized == component.value()) &&
30 !IsReservedNameOnWindows(component.value());
33 bool IsSafePortableRelativePath(const base::FilePath& path) {
34 if (path.empty() || path.IsAbsolute() || path.EndsWithSeparator())
35 return false;
36 std::vector<base::FilePath::StringType> components;
37 path.GetComponents(&components);
38 if (components.empty())
39 return false;
40 for (size_t i = 0; i < components.size() - 1; ++i) {
41 if (!IsSafePortablePathComponent(base::FilePath(components[i])))
42 return false;
44 return IsSafePortablePathComponent(path.BaseName());
47 base::string16 GetSuggestedFilename(const GURL& url,
48 const std::string& content_disposition,
49 const std::string& referrer_charset,
50 const std::string& suggested_name,
51 const std::string& mime_type,
52 const std::string& default_name) {
53 return GetSuggestedFilenameImpl(
54 url,
55 content_disposition,
56 referrer_charset,
57 suggested_name,
58 mime_type,
59 default_name,
60 base::Bind(&base::i18n::ReplaceIllegalCharactersInPath));
63 base::FilePath GenerateFileName(const GURL& url,
64 const std::string& content_disposition,
65 const std::string& referrer_charset,
66 const std::string& suggested_name,
67 const std::string& mime_type,
68 const std::string& default_file_name) {
69 base::FilePath generated_name(GenerateFileNameImpl(
70 url,
71 content_disposition,
72 referrer_charset,
73 suggested_name,
74 mime_type,
75 default_file_name,
76 base::Bind(&base::i18n::ReplaceIllegalCharactersInPath)));
78 #if defined(OS_CHROMEOS)
79 // When doing file manager operations on ChromeOS, the file paths get
80 // normalized in WebKit layer, so let's ensure downloaded files have
81 // normalized names. Otherwise, we won't be able to handle files with NFD
82 // utf8 encoded characters in name.
83 base::i18n::NormalizeFileNameEncoding(&generated_name);
84 #endif
86 DCHECK(!generated_name.empty());
88 return generated_name;
91 } // namespace net