Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* Mac OS X-specific local file uri parsing */ |
michael@0 | 8 | #include "nsURLHelper.h" |
michael@0 | 9 | #include "nsEscape.h" |
michael@0 | 10 | #include "nsIFile.h" |
michael@0 | 11 | #include "nsTArray.h" |
michael@0 | 12 | #include "nsReadableUtils.h" |
michael@0 | 13 | #include <Carbon/Carbon.h> |
michael@0 | 14 | |
michael@0 | 15 | static nsTArray<nsCString> *gVolumeList = nullptr; |
michael@0 | 16 | |
michael@0 | 17 | static bool pathBeginsWithVolName(const nsACString& path, nsACString& firstPathComponent) |
michael@0 | 18 | { |
michael@0 | 19 | // Return whether the 1st path component in path (escaped) is equal to the name |
michael@0 | 20 | // of a mounted volume. Return the 1st path component (unescaped) in any case. |
michael@0 | 21 | // This needs to be done as quickly as possible, so we cache a list of volume names. |
michael@0 | 22 | // XXX Register an event handler to detect drives being mounted/unmounted? |
michael@0 | 23 | |
michael@0 | 24 | if (!gVolumeList) { |
michael@0 | 25 | gVolumeList = new nsTArray<nsCString>; |
michael@0 | 26 | if (!gVolumeList) { |
michael@0 | 27 | return false; // out of memory |
michael@0 | 28 | } |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | // Cache a list of volume names |
michael@0 | 32 | if (!gVolumeList->Length()) { |
michael@0 | 33 | OSErr err; |
michael@0 | 34 | ItemCount volumeIndex = 1; |
michael@0 | 35 | |
michael@0 | 36 | do { |
michael@0 | 37 | HFSUniStr255 volName; |
michael@0 | 38 | FSRef rootDirectory; |
michael@0 | 39 | err = ::FSGetVolumeInfo(0, volumeIndex, nullptr, kFSVolInfoNone, nullptr, |
michael@0 | 40 | &volName, &rootDirectory); |
michael@0 | 41 | if (err == noErr) { |
michael@0 | 42 | NS_ConvertUTF16toUTF8 volNameStr(Substring((char16_t *)volName.unicode, |
michael@0 | 43 | (char16_t *)volName.unicode + volName.length)); |
michael@0 | 44 | gVolumeList->AppendElement(volNameStr); |
michael@0 | 45 | volumeIndex++; |
michael@0 | 46 | } |
michael@0 | 47 | } while (err == noErr); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | // Extract the first component of the path |
michael@0 | 51 | nsACString::const_iterator start; |
michael@0 | 52 | path.BeginReading(start); |
michael@0 | 53 | start.advance(1); // path begins with '/' |
michael@0 | 54 | nsACString::const_iterator directory_end; |
michael@0 | 55 | path.EndReading(directory_end); |
michael@0 | 56 | nsACString::const_iterator component_end(start); |
michael@0 | 57 | FindCharInReadable('/', component_end, directory_end); |
michael@0 | 58 | |
michael@0 | 59 | nsAutoCString flatComponent((Substring(start, component_end))); |
michael@0 | 60 | NS_UnescapeURL(flatComponent); |
michael@0 | 61 | int32_t foundIndex = gVolumeList->IndexOf(flatComponent); |
michael@0 | 62 | firstPathComponent = flatComponent; |
michael@0 | 63 | return (foundIndex != -1); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | void |
michael@0 | 67 | net_ShutdownURLHelperOSX() |
michael@0 | 68 | { |
michael@0 | 69 | delete gVolumeList; |
michael@0 | 70 | gVolumeList = nullptr; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | static nsresult convertHFSPathtoPOSIX(const nsACString& hfsPath, nsACString& posixPath) |
michael@0 | 74 | { |
michael@0 | 75 | // Use CFURL to do the conversion. We don't want to do this by simply |
michael@0 | 76 | // using SwapSlashColon - we need the charset mapped from MacRoman |
michael@0 | 77 | // to UTF-8, and we need "/Volumes" (or whatever - Apple says this is subject to change) |
michael@0 | 78 | // prepended if the path is not on the boot drive. |
michael@0 | 79 | |
michael@0 | 80 | CFStringRef pathStrRef = CFStringCreateWithCString(nullptr, |
michael@0 | 81 | PromiseFlatCString(hfsPath).get(), |
michael@0 | 82 | kCFStringEncodingMacRoman); |
michael@0 | 83 | if (!pathStrRef) |
michael@0 | 84 | return NS_ERROR_FAILURE; |
michael@0 | 85 | |
michael@0 | 86 | nsresult rv = NS_ERROR_FAILURE; |
michael@0 | 87 | CFURLRef urlRef = CFURLCreateWithFileSystemPath(nullptr, |
michael@0 | 88 | pathStrRef, kCFURLHFSPathStyle, true); |
michael@0 | 89 | if (urlRef) { |
michael@0 | 90 | UInt8 pathBuf[PATH_MAX]; |
michael@0 | 91 | if (CFURLGetFileSystemRepresentation(urlRef, true, pathBuf, sizeof(pathBuf))) { |
michael@0 | 92 | posixPath = (char *)pathBuf; |
michael@0 | 93 | rv = NS_OK; |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | CFRelease(pathStrRef); |
michael@0 | 97 | if (urlRef) |
michael@0 | 98 | CFRelease(urlRef); |
michael@0 | 99 | return rv; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | static void SwapSlashColon(char *s) |
michael@0 | 103 | { |
michael@0 | 104 | while (*s) { |
michael@0 | 105 | if (*s == '/') |
michael@0 | 106 | *s = ':'; |
michael@0 | 107 | else if (*s == ':') |
michael@0 | 108 | *s = '/'; |
michael@0 | 109 | s++; |
michael@0 | 110 | } |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | nsresult |
michael@0 | 114 | net_GetURLSpecFromActualFile(nsIFile *aFile, nsACString &result) |
michael@0 | 115 | { |
michael@0 | 116 | // NOTE: This is identical to the implementation in nsURLHelperUnix.cpp |
michael@0 | 117 | |
michael@0 | 118 | nsresult rv; |
michael@0 | 119 | nsAutoCString ePath; |
michael@0 | 120 | |
michael@0 | 121 | // construct URL spec from native file path |
michael@0 | 122 | rv = aFile->GetNativePath(ePath); |
michael@0 | 123 | if (NS_FAILED(rv)) |
michael@0 | 124 | return rv; |
michael@0 | 125 | |
michael@0 | 126 | nsAutoCString escPath; |
michael@0 | 127 | NS_NAMED_LITERAL_CSTRING(prefix, "file://"); |
michael@0 | 128 | |
michael@0 | 129 | // Escape the path with the directory mask |
michael@0 | 130 | if (NS_EscapeURL(ePath.get(), ePath.Length(), esc_Directory+esc_Forced, escPath)) |
michael@0 | 131 | escPath.Insert(prefix, 0); |
michael@0 | 132 | else |
michael@0 | 133 | escPath.Assign(prefix + ePath); |
michael@0 | 134 | |
michael@0 | 135 | // esc_Directory does not escape the semicolons, so if a filename |
michael@0 | 136 | // contains semicolons we need to manually escape them. |
michael@0 | 137 | // This replacement should be removed in bug #473280 |
michael@0 | 138 | escPath.ReplaceSubstring(";", "%3b"); |
michael@0 | 139 | |
michael@0 | 140 | result = escPath; |
michael@0 | 141 | return NS_OK; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | nsresult |
michael@0 | 145 | net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result) |
michael@0 | 146 | { |
michael@0 | 147 | // NOTE: See also the implementation in nsURLHelperUnix.cpp |
michael@0 | 148 | // This matches it except for the HFS path handling. |
michael@0 | 149 | |
michael@0 | 150 | nsresult rv; |
michael@0 | 151 | |
michael@0 | 152 | nsCOMPtr<nsIFile> localFile; |
michael@0 | 153 | rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(localFile)); |
michael@0 | 154 | if (NS_FAILED(rv)) |
michael@0 | 155 | return rv; |
michael@0 | 156 | |
michael@0 | 157 | nsAutoCString directory, fileBaseName, fileExtension, path; |
michael@0 | 158 | bool bHFSPath = false; |
michael@0 | 159 | |
michael@0 | 160 | rv = net_ParseFileURL(aURL, directory, fileBaseName, fileExtension); |
michael@0 | 161 | if (NS_FAILED(rv)) |
michael@0 | 162 | return rv; |
michael@0 | 163 | |
michael@0 | 164 | if (!directory.IsEmpty()) { |
michael@0 | 165 | NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path); |
michael@0 | 166 | |
michael@0 | 167 | // The canonical form of file URLs on OSX use POSIX paths: |
michael@0 | 168 | // file:///path-name. |
michael@0 | 169 | // But, we still encounter file URLs that use HFS paths: |
michael@0 | 170 | // file:///volume-name/path-name |
michael@0 | 171 | // Determine that here and normalize HFS paths to POSIX. |
michael@0 | 172 | nsAutoCString possibleVolName; |
michael@0 | 173 | if (pathBeginsWithVolName(directory, possibleVolName)) { |
michael@0 | 174 | // Though we know it begins with a volume name, it could still |
michael@0 | 175 | // be a valid POSIX path if the boot drive is named "Mac HD" |
michael@0 | 176 | // and there is a directory "Mac HD" at its root. If such a |
michael@0 | 177 | // directory doesn't exist, we'll assume this is an HFS path. |
michael@0 | 178 | FSRef testRef; |
michael@0 | 179 | possibleVolName.Insert("/", 0); |
michael@0 | 180 | if (::FSPathMakeRef((UInt8*)possibleVolName.get(), &testRef, nullptr) != noErr) |
michael@0 | 181 | bHFSPath = true; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | if (bHFSPath) { |
michael@0 | 185 | // "%2F"s need to become slashes, while all other slashes need to |
michael@0 | 186 | // become colons. If we start out by changing "%2F"s to colons, we |
michael@0 | 187 | // can reply on SwapSlashColon() to do what we need |
michael@0 | 188 | path.ReplaceSubstring("%2F", ":"); |
michael@0 | 189 | path.Cut(0, 1); // directory begins with '/' |
michael@0 | 190 | SwapSlashColon((char *)path.get()); |
michael@0 | 191 | // At this point, path is an HFS path made using the same |
michael@0 | 192 | // algorithm as nsURLHelperMac. We'll convert to POSIX below. |
michael@0 | 193 | } |
michael@0 | 194 | } |
michael@0 | 195 | if (!fileBaseName.IsEmpty()) |
michael@0 | 196 | NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path); |
michael@0 | 197 | if (!fileExtension.IsEmpty()) { |
michael@0 | 198 | path += '.'; |
michael@0 | 199 | NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | NS_UnescapeURL(path); |
michael@0 | 203 | if (path.Length() != strlen(path.get())) |
michael@0 | 204 | return NS_ERROR_FILE_INVALID_PATH; |
michael@0 | 205 | |
michael@0 | 206 | if (bHFSPath) |
michael@0 | 207 | convertHFSPathtoPOSIX(path, path); |
michael@0 | 208 | |
michael@0 | 209 | // assuming path is encoded in the native charset |
michael@0 | 210 | rv = localFile->InitWithNativePath(path); |
michael@0 | 211 | if (NS_FAILED(rv)) |
michael@0 | 212 | return rv; |
michael@0 | 213 | |
michael@0 | 214 | NS_ADDREF(*result = localFile); |
michael@0 | 215 | return NS_OK; |
michael@0 | 216 | } |