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

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

Issue 343014: Enable localization of default downloaded filename.... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
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 | Annotate | Revision Log
« 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 1029 matching lines...) Expand 10 before | Expand all | Expand 10 after
1040 1040
1041 std::wstring StripWWW(const std::wstring& text) { 1041 std::wstring StripWWW(const std::wstring& text) {
1042 const std::wstring www(L"www."); 1042 const std::wstring www(L"www.");
1043 return (text.compare(0, www.length(), www) == 0) ? 1043 return (text.compare(0, www.length(), www) == 0) ?
1044 text.substr(www.length()) : text; 1044 text.substr(www.length()) : text;
1045 } 1045 }
1046 1046
1047 FilePath GetSuggestedFilename(const GURL& url, 1047 FilePath GetSuggestedFilename(const GURL& url,
1048 const std::string& content_disposition, 1048 const std::string& content_disposition,
1049 const std::string& referrer_charset, 1049 const std::string& referrer_charset,
1050 const char* default_name) { 1050 const FilePath& default_name) {
1051 // TODO(rolandsteiner): as pointed out by darin in the code review, this is 1051 // We don't translate this fallback string, "download". If localization is
1052 // hardly ideal. "download" should be translated, or another solution found. 1052 // needed, the caller should provide localized fallback default_name.
1053 // (cf. http://code.google.com/p/chromium/issues/detail?id=25289) 1053 static const FilePath::CharType kFinalFallbackName[] =
1054 const char kFinalFallbackName[] = "download"; 1054 FILE_PATH_LITERAL("download");
1055 1055
1056 // about: and data: URLs don't have file names, but esp. data: URLs may 1056 // about: and data: URLs don't have file names, but esp. data: URLs may
1057 // contain parts that look like ones (i.e., contain a slash). 1057 // contain parts that look like ones (i.e., contain a slash).
1058 // Therefore we don't attempt to divine a file name out of them. 1058 // Therefore we don't attempt to divine a file name out of them.
1059 if (url.SchemeIs("about") || url.SchemeIs("data")) { 1059 if (url.SchemeIs("about") || url.SchemeIs("data")) {
1060 return FilePath(UTF8ToFilePathString( 1060 return default_name.empty() ? FilePath(kFinalFallbackName) : default_name;
1061 default_name && default_name[0] ? default_name : kFinalFallbackName));
1062 } 1061 }
1063 1062
1064 std::string filename = GetFileNameFromCD(content_disposition, 1063 const std::string filename_from_cd = GetFileNameFromCD(content_disposition,
1065 referrer_charset); 1064 referrer_charset);
1065 #if defined(OS_WIN)
1066 FilePath::StringType filename = UTF8ToWide(filename_from_cd);
1067 #elif defined(OS_POSIX)
1068 FilePath::StringType filename = filename_from_cd;
1069 #endif
1070
1066 if (!filename.empty()) { 1071 if (!filename.empty()) {
1067 // Remove any path information the server may have sent, take the name 1072 // Remove any path information the server may have sent, take the name
1068 // only. 1073 // only.
1069 #if defined(OS_WIN)
1070 filename = UTF16ToUTF8(FilePath(UTF8ToUTF16(filename)).BaseName().value());
1071 #else
1072 filename = FilePath(filename).BaseName().value(); 1074 filename = FilePath(filename).BaseName().value();
1073 #endif
1074 1075
1075 // Next, remove "." from the beginning and end of the file name to avoid 1076 // Next, remove "." from the beginning and end of the file name to avoid
1076 // tricks with hidden files, "..", and "." 1077 // tricks with hidden files, "..", and "."
1077 TrimString(filename, ".", &filename); 1078 TrimString(filename, FILE_PATH_LITERAL("."), &filename);
1078 } 1079 }
1079 if (filename.empty()) { 1080 if (filename.empty()) {
1080 if (url.is_valid()) { 1081 if (url.is_valid()) {
1081 filename = UnescapeURLComponent( 1082 const std::string unescaped_url_filename = UnescapeURLComponent(
1082 url.ExtractFileName(), 1083 url.ExtractFileName(),
1083 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); 1084 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS);
1085 #if defined(OS_WIN)
1086 filename = UTF8ToWide(unescaped_url_filename);
1087 #elif defined(OS_POSIX)
1088 filename = unescaped_url_filename;
1089 #endif
1084 } 1090 }
1085 } 1091 }
1086 1092
1087 // Trim '.' once more. 1093 // Trim '.' once more.
1088 TrimString(filename, ".", &filename); 1094 TrimString(filename, FILE_PATH_LITERAL("."), &filename);
1089 1095
1090 // If there's no filename or it gets trimed to be empty, use 1096 // If there's no filename or it gets trimed to be empty, use
1091 // the URL hostname or default_name 1097 // the URL hostname or default_name
1092 if (filename.empty()) { 1098 if (filename.empty()) {
1093 if (default_name && default_name[0]) { 1099 if (!default_name.empty()) {
1094 filename = default_name; 1100 filename = default_name.value();
1095 } else if (url.is_valid()) { 1101 } else if (url.is_valid()) {
1096 // Some schemes (e.g. file) do not have a hostname. Even though it's 1102 // Some schemes (e.g. file) do not have a hostname. Even though it's
1097 // not likely to reach here, let's hardcode the last fallback name. 1103 // not likely to reach here, let's hardcode the last fallback name.
1098 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451) 1104 // TODO(jungshik) : Decode a 'punycoded' IDN hostname. (bug 1264451)
1099 filename = url.host().empty() ? std::string(kFinalFallbackName) 1105 filename = url.host().empty() ? kFinalFallbackName :
1100 : url.host(); 1106 #if defined(OS_WIN)
1107 UTF8ToWide(url.host());
1108 #elif defined(OS_POSIX)
1109 url.host();
1110 #endif
1101 } else { 1111 } else {
1102 NOTREACHED(); 1112 NOTREACHED();
1103 } 1113 }
1104 } 1114 }
1105 1115
1106 #if defined(OS_WIN) 1116 file_util::ReplaceIllegalCharactersInPath(&filename, '-');
1107 FilePath::StringType file_path_string = UTF8ToWide(filename); 1117 return FilePath(filename);
1108 #else
1109 std::string& file_path_string = filename;
1110 #endif
1111 file_util::ReplaceIllegalCharactersInPath(&file_path_string, '-');
1112 return FilePath(file_path_string);
1113 } 1118 }
1114 1119
1115 bool IsPortAllowedByDefault(int port) { 1120 bool IsPortAllowedByDefault(int port) {
1116 int array_size = arraysize(kRestrictedPorts); 1121 int array_size = arraysize(kRestrictedPorts);
1117 for (int i = 0; i < array_size; i++) { 1122 for (int i = 0; i < array_size; i++) {
1118 if (kRestrictedPorts[i] == port) { 1123 if (kRestrictedPorts[i] == port) {
1119 return false; 1124 return false;
1120 } 1125 }
1121 } 1126 }
1122 return true; 1127 return true;
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
1451 if (length > 0) 1456 if (length > 0)
1452 ports.insert(StringToInt(WideToASCII( 1457 ports.insert(StringToInt(WideToASCII(
1453 allowed_ports.substr(last, length)))); 1458 allowed_ports.substr(last, length))));
1454 last = i + 1; 1459 last = i + 1;
1455 } 1460 }
1456 } 1461 }
1457 explicitly_allowed_ports = ports; 1462 explicitly_allowed_ports = ports;
1458 } 1463 }
1459 1464
1460 } // namespace net 1465 } // 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