Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: net/base/net_util.cc

Issue 367003: Enable localization of default downloaded filename. (Closed)
Patch Set: Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/net_util.h ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/net_util.h" 5 #include "net/base/net_util.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 #include <unicode/ucnv.h> 9 #include <unicode/ucnv.h>
10 #include <unicode/uidna.h> 10 #include <unicode/uidna.h>
(...skipping 1024 matching lines...) Expand 10 before | Expand all | Expand 10 after
1035 1035
1036 std::wstring StripWWW(const std::wstring& text) { 1036 std::wstring StripWWW(const std::wstring& text) {
1037 const std::wstring www(L"www."); 1037 const std::wstring www(L"www.");
1038 return (text.compare(0, www.length(), www) == 0) ? 1038 return (text.compare(0, www.length(), www) == 0) ?
1039 text.substr(www.length()) : text; 1039 text.substr(www.length()) : text;
1040 } 1040 }
1041 1041
1042 FilePath GetSuggestedFilename(const GURL& url, 1042 FilePath GetSuggestedFilename(const GURL& url,
1043 const std::string& content_disposition, 1043 const std::string& content_disposition,
1044 const std::string& referrer_charset, 1044 const std::string& referrer_charset,
1045 const char* default_name) { 1045 const FilePath& default_name) {
1046 // TODO(rolandsteiner): as pointed out by darin in the code review, this is 1046 // We don't translate this fallback string, "download". If localization is
1047 // hardly ideal. "download" should be translated, or another solution found. 1047 // needed, the caller should provide localized fallback default_name.
1048 // (cf. http://code.google.com/p/chromium/issues/detail?id=25289) 1048 static const FilePath::CharType kFinalFallbackName[] =
1049 const char kFinalFallbackName[] = "download"; 1049 FILE_PATH_LITERAL("download");
1050 1050
1051 // about: and data: URLs don't have file names, but esp. data: URLs may 1051 // about: and data: URLs don't have file names, but esp. data: URLs may
1052 // contain parts that look like ones (i.e., contain a slash). 1052 // contain parts that look like ones (i.e., contain a slash).
1053 // Therefore we don't attempt to divine a file name out of them. 1053 // Therefore we don't attempt to divine a file name out of them.
1054 if (url.SchemeIs("about") || url.SchemeIs("data")) { 1054 if (url.SchemeIs("about") || url.SchemeIs("data")) {
1055 return FilePath(UTF8ToFilePathString( 1055 return default_name.empty() ? FilePath(kFinalFallbackName) : default_name;
1056 default_name && default_name[0] ? default_name : kFinalFallbackName));
1057 } 1056 }
1058 1057
1059 std::string filename = GetFileNameFromCD(content_disposition, 1058 const std::string filename_from_cd = GetFileNameFromCD(content_disposition,
1060 referrer_charset); 1059 referrer_charset);
1060 #if defined(OS_WIN)
1061 FilePath::StringType filename = UTF8ToWide(filename_from_cd);
1062 #elif defined(OS_POSIX)
1063 FilePath::StringType filename = filename_from_cd;
1064 #endif
1065
1061 if (!filename.empty()) { 1066 if (!filename.empty()) {
1062 // Remove any path information the server may have sent, take the name 1067 // Remove any path information the server may have sent, take the name
1063 // only. 1068 // only.
1064 #if defined(OS_WIN)
1065 filename = UTF16ToUTF8(FilePath(UTF8ToUTF16(filename)).BaseName().value());
1066 #else
1067 filename = FilePath(filename).BaseName().value(); 1069 filename = FilePath(filename).BaseName().value();
1068 #endif
1069 1070
1070 // Next, remove "." from the beginning and end of the file name to avoid 1071 // Next, remove "." from the beginning and end of the file name to avoid
1071 // tricks with hidden files, "..", and "." 1072 // tricks with hidden files, "..", and "."
1072 TrimString(filename, ".", &filename); 1073 TrimString(filename, FILE_PATH_LITERAL("."), &filename);
1073 } 1074 }
1074 if (filename.empty()) { 1075 if (filename.empty()) {
1075 if (url.is_valid()) { 1076 if (url.is_valid()) {
1076 filename = UnescapeURLComponent( 1077 const std::string unescaped_url_filename = UnescapeURLComponent(
1077 url.ExtractFileName(), 1078 url.ExtractFileName(),
1078 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); 1079 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS);
1080 #if defined(OS_WIN)
1081 filename = UTF8ToWide(unescaped_url_filename);
1082 #elif defined(OS_POSIX)
1083 filename = unescaped_url_filename;
1084 #endif
1079 } 1085 }
1080 } 1086 }
1081 1087
1082 // Trim '.' once more. 1088 // Trim '.' once more.
1083 TrimString(filename, ".", &filename); 1089 TrimString(filename, FILE_PATH_LITERAL("."), &filename);
1084 1090
1085 // If there's no filename or it gets trimed to be empty, use 1091 // If there's no filename or it gets trimed to be empty, use
1086 // the URL hostname or default_name 1092 // the URL hostname or default_name
1087 if (filename.empty()) { 1093 if (filename.empty()) {
1088 if (default_name && default_name[0]) { 1094 if (!default_name.empty()) {
1089 filename = default_name; 1095 filename = default_name.value();
1090 } else if (url.is_valid()) { 1096 } else if (url.is_valid()) {
1091 // Some schemes (e.g. file) do not have a hostname. Even though it's 1097 // Some schemes (e.g. file) do not have a hostname. Even though it's
1092 // not likely to reach here, let's hardcode the last fallback name. 1098 // not likely to reach here, let's hardcode the last fallback name.
1093 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451) 1099 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451)
1094 filename = url.host().empty() ? std::string(kFinalFallbackName) 1100 filename = url.host().empty() ? kFinalFallbackName :
1095 : url.host(); 1101 #if defined(OS_WIN)
1102 UTF8ToWide(url.host());
1103 #elif defined(OS_POSIX)
1104 url.host();
1105 #endif
1096 } else { 1106 } else {
1097 NOTREACHED(); 1107 NOTREACHED();
1098 } 1108 }
1099 } 1109 }
1100 1110
1101 #if defined(OS_WIN) 1111 file_util::ReplaceIllegalCharactersInPath(&filename, '-');
1102 FilePath::StringType file_path_string = UTF8ToWide(filename); 1112 return FilePath(filename);
1103 #else
1104 std::string& file_path_string = filename;
1105 #endif
1106 file_util::ReplaceIllegalCharactersInPath(&file_path_string, '-');
1107 return FilePath(file_path_string);
1108 } 1113 }
1109 1114
1110 bool IsPortAllowedByDefault(int port) { 1115 bool IsPortAllowedByDefault(int port) {
1111 int array_size = arraysize(kRestrictedPorts); 1116 int array_size = arraysize(kRestrictedPorts);
1112 for (int i = 0; i < array_size; i++) { 1117 for (int i = 0; i < array_size; i++) {
1113 if (kRestrictedPorts[i] == port) { 1118 if (kRestrictedPorts[i] == port) {
1114 return false; 1119 return false;
1115 } 1120 }
1116 } 1121 }
1117 return true; 1122 return true;
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
1446 if (length > 0) 1451 if (length > 0)
1447 ports.insert(StringToInt(WideToASCII( 1452 ports.insert(StringToInt(WideToASCII(
1448 allowed_ports.substr(last, length)))); 1453 allowed_ports.substr(last, length))));
1449 last = i + 1; 1454 last = i + 1;
1450 } 1455 }
1451 } 1456 }
1452 explicitly_allowed_ports = ports; 1457 explicitly_allowed_ports = ports;
1453 } 1458 }
1454 1459
1455 } // namespace net 1460 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_util.h ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698