netwerk/protocol/file/nsFileProtocolHandler.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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: 4 -*- */
michael@0 2 // vim:ts=4 sw=4 sts=4 et cin:
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 #include "nsFileProtocolHandler.h"
michael@0 8 #include "nsFileChannel.h"
michael@0 9 #include "nsStandardURL.h"
michael@0 10 #include "nsURLHelper.h"
michael@0 11
michael@0 12 #include "nsNetUtil.h"
michael@0 13
michael@0 14 // URL file handling, copied and modified from xpfe/components/bookmarks/src/nsBookmarksService.cpp
michael@0 15 #ifdef XP_WIN
michael@0 16 #include <shlobj.h>
michael@0 17 #include <intshcut.h>
michael@0 18 #include "nsIFileURL.h"
michael@0 19 #ifdef CompareString
michael@0 20 #undef CompareString
michael@0 21 #endif
michael@0 22 #endif
michael@0 23
michael@0 24 // URL file handling for freedesktop.org
michael@0 25 #ifdef XP_UNIX
michael@0 26 #include "nsINIParser.h"
michael@0 27 #define DESKTOP_ENTRY_SECTION "Desktop Entry"
michael@0 28 #endif
michael@0 29
michael@0 30 //-----------------------------------------------------------------------------
michael@0 31
michael@0 32 nsFileProtocolHandler::nsFileProtocolHandler()
michael@0 33 {
michael@0 34 }
michael@0 35
michael@0 36 nsresult
michael@0 37 nsFileProtocolHandler::Init()
michael@0 38 {
michael@0 39 return NS_OK;
michael@0 40 }
michael@0 41
michael@0 42 NS_IMPL_ISUPPORTS(nsFileProtocolHandler,
michael@0 43 nsIFileProtocolHandler,
michael@0 44 nsIProtocolHandler,
michael@0 45 nsISupportsWeakReference)
michael@0 46
michael@0 47 //-----------------------------------------------------------------------------
michael@0 48 // nsIProtocolHandler methods:
michael@0 49
michael@0 50 #if defined(XP_WIN)
michael@0 51 NS_IMETHODIMP
michael@0 52 nsFileProtocolHandler::ReadURLFile(nsIFile* aFile, nsIURI** aURI)
michael@0 53 {
michael@0 54 nsAutoString path;
michael@0 55 nsresult rv = aFile->GetPath(path);
michael@0 56 if (NS_FAILED(rv))
michael@0 57 return rv;
michael@0 58
michael@0 59 if (path.Length() < 4)
michael@0 60 return NS_ERROR_NOT_AVAILABLE;
michael@0 61 if (!StringTail(path, 4).LowerCaseEqualsLiteral(".url"))
michael@0 62 return NS_ERROR_NOT_AVAILABLE;
michael@0 63
michael@0 64 HRESULT result;
michael@0 65
michael@0 66 rv = NS_ERROR_NOT_AVAILABLE;
michael@0 67
michael@0 68 IUniformResourceLocatorW* urlLink = nullptr;
michael@0 69 result = ::CoCreateInstance(CLSID_InternetShortcut, nullptr, CLSCTX_INPROC_SERVER,
michael@0 70 IID_IUniformResourceLocatorW, (void**)&urlLink);
michael@0 71 if (SUCCEEDED(result) && urlLink) {
michael@0 72 IPersistFile* urlFile = nullptr;
michael@0 73 result = urlLink->QueryInterface(IID_IPersistFile, (void**)&urlFile);
michael@0 74 if (SUCCEEDED(result) && urlFile) {
michael@0 75 result = urlFile->Load(path.get(), STGM_READ);
michael@0 76 if (SUCCEEDED(result) ) {
michael@0 77 LPWSTR lpTemp = nullptr;
michael@0 78
michael@0 79 // The URL this method will give us back seems to be already
michael@0 80 // escaped. Hence, do not do escaping of our own.
michael@0 81 result = urlLink->GetURL(&lpTemp);
michael@0 82 if (SUCCEEDED(result) && lpTemp) {
michael@0 83 rv = NS_NewURI(aURI, nsDependentString(lpTemp));
michael@0 84 // free the string that GetURL alloc'd
michael@0 85 CoTaskMemFree(lpTemp);
michael@0 86 }
michael@0 87 }
michael@0 88 urlFile->Release();
michael@0 89 }
michael@0 90 urlLink->Release();
michael@0 91 }
michael@0 92 return rv;
michael@0 93 }
michael@0 94
michael@0 95 #elif defined(XP_UNIX)
michael@0 96 NS_IMETHODIMP
michael@0 97 nsFileProtocolHandler::ReadURLFile(nsIFile* aFile, nsIURI** aURI)
michael@0 98 {
michael@0 99 // We only support desktop files that end in ".desktop" like the spec says:
michael@0 100 // http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s02.html
michael@0 101 nsAutoCString leafName;
michael@0 102 nsresult rv = aFile->GetNativeLeafName(leafName);
michael@0 103 if (NS_FAILED(rv) ||
michael@0 104 !StringEndsWith(leafName, NS_LITERAL_CSTRING(".desktop")))
michael@0 105 return NS_ERROR_NOT_AVAILABLE;
michael@0 106
michael@0 107 nsINIParser parser;
michael@0 108 rv = parser.Init(aFile);
michael@0 109 if (NS_FAILED(rv))
michael@0 110 return rv;
michael@0 111
michael@0 112 nsAutoCString type;
michael@0 113 parser.GetString(DESKTOP_ENTRY_SECTION, "Type", type);
michael@0 114 if (!type.EqualsLiteral("Link"))
michael@0 115 return NS_ERROR_NOT_AVAILABLE;
michael@0 116
michael@0 117 nsAutoCString url;
michael@0 118 rv = parser.GetString(DESKTOP_ENTRY_SECTION, "URL", url);
michael@0 119 if (NS_FAILED(rv) || url.IsEmpty())
michael@0 120 return NS_ERROR_NOT_AVAILABLE;
michael@0 121
michael@0 122 return NS_NewURI(aURI, url);
michael@0 123 }
michael@0 124
michael@0 125 #else // other platforms
michael@0 126 NS_IMETHODIMP
michael@0 127 nsFileProtocolHandler::ReadURLFile(nsIFile* aFile, nsIURI** aURI)
michael@0 128 {
michael@0 129 return NS_ERROR_NOT_AVAILABLE;
michael@0 130 }
michael@0 131 #endif // ReadURLFile()
michael@0 132
michael@0 133 NS_IMETHODIMP
michael@0 134 nsFileProtocolHandler::GetScheme(nsACString &result)
michael@0 135 {
michael@0 136 result.AssignLiteral("file");
michael@0 137 return NS_OK;
michael@0 138 }
michael@0 139
michael@0 140 NS_IMETHODIMP
michael@0 141 nsFileProtocolHandler::GetDefaultPort(int32_t *result)
michael@0 142 {
michael@0 143 *result = -1; // no port for file: URLs
michael@0 144 return NS_OK;
michael@0 145 }
michael@0 146
michael@0 147 NS_IMETHODIMP
michael@0 148 nsFileProtocolHandler::GetProtocolFlags(uint32_t *result)
michael@0 149 {
michael@0 150 *result = URI_NOAUTH | URI_IS_LOCAL_FILE | URI_IS_LOCAL_RESOURCE;
michael@0 151 return NS_OK;
michael@0 152 }
michael@0 153
michael@0 154 NS_IMETHODIMP
michael@0 155 nsFileProtocolHandler::NewURI(const nsACString &spec,
michael@0 156 const char *charset,
michael@0 157 nsIURI *baseURI,
michael@0 158 nsIURI **result)
michael@0 159 {
michael@0 160 nsCOMPtr<nsIStandardURL> url = new nsStandardURL(true);
michael@0 161 if (!url)
michael@0 162 return NS_ERROR_OUT_OF_MEMORY;
michael@0 163
michael@0 164 const nsACString *specPtr = &spec;
michael@0 165
michael@0 166 #if defined(XP_WIN)
michael@0 167 nsAutoCString buf;
michael@0 168 if (net_NormalizeFileURL(spec, buf))
michael@0 169 specPtr = &buf;
michael@0 170 #endif
michael@0 171
michael@0 172 nsresult rv = url->Init(nsIStandardURL::URLTYPE_NO_AUTHORITY, -1,
michael@0 173 *specPtr, charset, baseURI);
michael@0 174 if (NS_FAILED(rv)) return rv;
michael@0 175
michael@0 176 return CallQueryInterface(url, result);
michael@0 177 }
michael@0 178
michael@0 179 NS_IMETHODIMP
michael@0 180 nsFileProtocolHandler::NewChannel(nsIURI *uri, nsIChannel **result)
michael@0 181 {
michael@0 182 nsFileChannel *chan = new nsFileChannel(uri);
michael@0 183 if (!chan)
michael@0 184 return NS_ERROR_OUT_OF_MEMORY;
michael@0 185 NS_ADDREF(chan);
michael@0 186
michael@0 187 nsresult rv = chan->Init();
michael@0 188 if (NS_FAILED(rv)) {
michael@0 189 NS_RELEASE(chan);
michael@0 190 return rv;
michael@0 191 }
michael@0 192
michael@0 193 *result = chan;
michael@0 194 return NS_OK;
michael@0 195 }
michael@0 196
michael@0 197 NS_IMETHODIMP
michael@0 198 nsFileProtocolHandler::AllowPort(int32_t port, const char *scheme, bool *result)
michael@0 199 {
michael@0 200 // don't override anything.
michael@0 201 *result = false;
michael@0 202 return NS_OK;
michael@0 203 }
michael@0 204
michael@0 205 //-----------------------------------------------------------------------------
michael@0 206 // nsIFileProtocolHandler methods:
michael@0 207
michael@0 208 NS_IMETHODIMP
michael@0 209 nsFileProtocolHandler::NewFileURI(nsIFile *file, nsIURI **result)
michael@0 210 {
michael@0 211 NS_ENSURE_ARG_POINTER(file);
michael@0 212 nsresult rv;
michael@0 213
michael@0 214 nsCOMPtr<nsIFileURL> url = new nsStandardURL(true);
michael@0 215 if (!url)
michael@0 216 return NS_ERROR_OUT_OF_MEMORY;
michael@0 217
michael@0 218 // NOTE: the origin charset is assigned the value of the platform
michael@0 219 // charset by the SetFile method.
michael@0 220 rv = url->SetFile(file);
michael@0 221 if (NS_FAILED(rv)) return rv;
michael@0 222
michael@0 223 return CallQueryInterface(url, result);
michael@0 224 }
michael@0 225
michael@0 226 NS_IMETHODIMP
michael@0 227 nsFileProtocolHandler::GetURLSpecFromFile(nsIFile *file, nsACString &result)
michael@0 228 {
michael@0 229 NS_ENSURE_ARG_POINTER(file);
michael@0 230 return net_GetURLSpecFromFile(file, result);
michael@0 231 }
michael@0 232
michael@0 233 NS_IMETHODIMP
michael@0 234 nsFileProtocolHandler::GetURLSpecFromActualFile(nsIFile *file,
michael@0 235 nsACString &result)
michael@0 236 {
michael@0 237 NS_ENSURE_ARG_POINTER(file);
michael@0 238 return net_GetURLSpecFromActualFile(file, result);
michael@0 239 }
michael@0 240
michael@0 241 NS_IMETHODIMP
michael@0 242 nsFileProtocolHandler::GetURLSpecFromDir(nsIFile *file, nsACString &result)
michael@0 243 {
michael@0 244 NS_ENSURE_ARG_POINTER(file);
michael@0 245 return net_GetURLSpecFromDir(file, result);
michael@0 246 }
michael@0 247
michael@0 248 NS_IMETHODIMP
michael@0 249 nsFileProtocolHandler::GetFileFromURLSpec(const nsACString &spec, nsIFile **result)
michael@0 250 {
michael@0 251 return net_GetFileFromURLSpec(spec, result);
michael@0 252 }

mercurial