michael@0: /* vim:set ts=2 sw=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // GnomeVFS v2.2.2 is missing G_BEGIN_DECLS in gnome-vfs-module-callback.h michael@0: extern "C" { michael@0: #include michael@0: #include michael@0: #include michael@0: } michael@0: michael@0: #include michael@0: michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsComponentManagerUtils.h" michael@0: #include "mozilla/ModuleUtils.h" michael@0: #include "nsIInterfaceRequestorUtils.h" michael@0: #include "nsIPrefService.h" michael@0: #include "nsIPrefBranch.h" michael@0: #include "nsIObserver.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "nsProxyRelease.h" michael@0: #include "nsIAuthPrompt.h" michael@0: #include "nsIStringBundle.h" michael@0: #include "nsIStandardURL.h" michael@0: #include "nsIURL.h" michael@0: #include "nsMimeTypes.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsINetUtil.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsError.h" michael@0: #include "prlog.h" michael@0: #include "prtime.h" michael@0: #include "prprf.h" michael@0: #include "plstr.h" michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: #define MOZ_GNOMEVFS_SCHEME "moz-gnomevfs" michael@0: #define MOZ_GNOMEVFS_SUPPORTED_PROTOCOLS "network.gnomevfs.supported-protocols" michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: // NSPR_LOG_MODULES=gnomevfs:5 michael@0: #ifdef PR_LOGGING michael@0: static PRLogModuleInfo *sGnomeVFSLog; michael@0: #define LOG(args) PR_LOG(sGnomeVFSLog, PR_LOG_DEBUG, args) michael@0: #else michael@0: #define LOG(args) michael@0: #endif michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: static nsresult michael@0: MapGnomeVFSResult(GnomeVFSResult result) michael@0: { michael@0: switch (result) michael@0: { michael@0: case GNOME_VFS_OK: return NS_OK; michael@0: case GNOME_VFS_ERROR_NOT_FOUND: return NS_ERROR_FILE_NOT_FOUND; michael@0: case GNOME_VFS_ERROR_INTERNAL: return NS_ERROR_UNEXPECTED; michael@0: case GNOME_VFS_ERROR_BAD_PARAMETERS: return NS_ERROR_INVALID_ARG; michael@0: case GNOME_VFS_ERROR_NOT_SUPPORTED: return NS_ERROR_NOT_AVAILABLE; michael@0: case GNOME_VFS_ERROR_CORRUPTED_DATA: return NS_ERROR_FILE_CORRUPTED; michael@0: case GNOME_VFS_ERROR_TOO_BIG: return NS_ERROR_FILE_TOO_BIG; michael@0: case GNOME_VFS_ERROR_NO_SPACE: return NS_ERROR_FILE_NO_DEVICE_SPACE; michael@0: case GNOME_VFS_ERROR_READ_ONLY: michael@0: case GNOME_VFS_ERROR_READ_ONLY_FILE_SYSTEM: return NS_ERROR_FILE_READ_ONLY; michael@0: case GNOME_VFS_ERROR_INVALID_URI: michael@0: case GNOME_VFS_ERROR_INVALID_HOST_NAME: return NS_ERROR_MALFORMED_URI; michael@0: case GNOME_VFS_ERROR_ACCESS_DENIED: michael@0: case GNOME_VFS_ERROR_NOT_PERMITTED: michael@0: case GNOME_VFS_ERROR_LOGIN_FAILED: return NS_ERROR_FILE_ACCESS_DENIED; michael@0: case GNOME_VFS_ERROR_EOF: return NS_BASE_STREAM_CLOSED; michael@0: case GNOME_VFS_ERROR_NOT_A_DIRECTORY: return NS_ERROR_FILE_NOT_DIRECTORY; michael@0: case GNOME_VFS_ERROR_IN_PROGRESS: return NS_ERROR_IN_PROGRESS; michael@0: case GNOME_VFS_ERROR_FILE_EXISTS: return NS_ERROR_FILE_ALREADY_EXISTS; michael@0: case GNOME_VFS_ERROR_IS_DIRECTORY: return NS_ERROR_FILE_IS_DIRECTORY; michael@0: case GNOME_VFS_ERROR_NO_MEMORY: return NS_ERROR_OUT_OF_MEMORY; michael@0: case GNOME_VFS_ERROR_HOST_NOT_FOUND: michael@0: case GNOME_VFS_ERROR_HOST_HAS_NO_ADDRESS: return NS_ERROR_UNKNOWN_HOST; michael@0: case GNOME_VFS_ERROR_CANCELLED: michael@0: case GNOME_VFS_ERROR_INTERRUPTED: return NS_ERROR_ABORT; michael@0: case GNOME_VFS_ERROR_DIRECTORY_NOT_EMPTY: return NS_ERROR_FILE_DIR_NOT_EMPTY; michael@0: case GNOME_VFS_ERROR_NAME_TOO_LONG: return NS_ERROR_FILE_NAME_TOO_LONG; michael@0: case GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE: return NS_ERROR_UNKNOWN_PROTOCOL; michael@0: michael@0: /* No special mapping for these error codes... michael@0: michael@0: case GNOME_VFS_ERROR_GENERIC: michael@0: case GNOME_VFS_ERROR_IO: michael@0: case GNOME_VFS_ERROR_WRONG_FORMAT: michael@0: case GNOME_VFS_ERROR_BAD_FILE: michael@0: case GNOME_VFS_ERROR_NOT_OPEN: michael@0: case GNOME_VFS_ERROR_INVALID_OPEN_MODE: michael@0: case GNOME_VFS_ERROR_TOO_MANY_OPEN_FILES: michael@0: case GNOME_VFS_ERROR_LOOP: michael@0: case GNOME_VFS_ERROR_DIRECTORY_BUSY: michael@0: case GNOME_VFS_ERROR_TOO_MANY_LINKS: michael@0: case GNOME_VFS_ERROR_NOT_SAME_FILE_SYSTEM: michael@0: case GNOME_VFS_ERROR_SERVICE_OBSOLETE: michael@0: case GNOME_VFS_ERROR_PROTOCOL_ERROR: michael@0: case GNOME_VFS_ERROR_NO_MASTER_BROWSER: michael@0: michael@0: */ michael@0: michael@0: // Make GCC happy michael@0: default: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: static void michael@0: ProxiedAuthCallback(gconstpointer in, michael@0: gsize in_size, michael@0: gpointer out, michael@0: gsize out_size, michael@0: gpointer callback_data) michael@0: { michael@0: GnomeVFSModuleCallbackAuthenticationIn *authIn = michael@0: (GnomeVFSModuleCallbackAuthenticationIn *) in; michael@0: GnomeVFSModuleCallbackAuthenticationOut *authOut = michael@0: (GnomeVFSModuleCallbackAuthenticationOut *) out; michael@0: michael@0: LOG(("gnomevfs: ProxiedAuthCallback [uri=%s]\n", authIn->uri)); michael@0: michael@0: // Without a channel, we have no way of getting a prompter. michael@0: nsIChannel *channel = (nsIChannel *) callback_data; michael@0: if (!channel) michael@0: return; michael@0: michael@0: nsCOMPtr prompt; michael@0: NS_QueryNotificationCallbacks(channel, prompt); michael@0: michael@0: // If no auth prompt, then give up. We could failover to using the michael@0: // WindowWatcher service, but that might defeat a consumer's purposeful michael@0: // attempt to disable authentication (for whatever reason). michael@0: if (!prompt) michael@0: return; michael@0: michael@0: // Parse out the host and port... michael@0: nsCOMPtr uri; michael@0: channel->GetURI(getter_AddRefs(uri)); michael@0: if (!uri) michael@0: return; michael@0: michael@0: #ifdef DEBUG michael@0: { michael@0: // michael@0: // Make sure authIn->uri is consistent with the channel's URI. michael@0: // michael@0: // XXX This check is probably not IDN safe, and it might incorrectly michael@0: // fire as a result of escaping differences. It's unclear what michael@0: // kind of transforms GnomeVFS might have applied to the URI spec michael@0: // that we originally gave to it. In spite of the likelihood of michael@0: // false hits, this check is probably still valuable. michael@0: // michael@0: nsAutoCString spec; michael@0: uri->GetSpec(spec); michael@0: int uriLen = strlen(authIn->uri); michael@0: if (!StringHead(spec, uriLen).Equals(nsDependentCString(authIn->uri, uriLen))) michael@0: { michael@0: LOG(("gnomevfs: [spec=%s authIn->uri=%s]\n", spec.get(), authIn->uri)); michael@0: NS_ERROR("URI mismatch"); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: nsAutoCString scheme, hostPort; michael@0: uri->GetScheme(scheme); michael@0: uri->GetHostPort(hostPort); michael@0: michael@0: // It doesn't make sense for either of these strings to be empty. What kind michael@0: // of funky URI is this? michael@0: if (scheme.IsEmpty() || hostPort.IsEmpty()) michael@0: return; michael@0: michael@0: // Construct the single signon key. Altering the value of this key will michael@0: // cause people's remembered passwords to be forgotten. Think carefully michael@0: // before changing the way this key is constructed. michael@0: nsAutoString key, realm; michael@0: michael@0: NS_ConvertUTF8toUTF16 dispHost(scheme); michael@0: dispHost.Append(NS_LITERAL_STRING("://")); michael@0: dispHost.Append(NS_ConvertUTF8toUTF16(hostPort)); michael@0: michael@0: key = dispHost; michael@0: if (authIn->realm) michael@0: { michael@0: // We assume the realm string is ASCII. That might be a bogus assumption, michael@0: // but we have no idea what encoding GnomeVFS is using, so for now we'll michael@0: // limit ourselves to ISO-Latin-1. XXX What is a better solution? michael@0: realm.Append('"'); michael@0: realm.Append(NS_ConvertASCIItoUTF16(authIn->realm)); michael@0: realm.Append('"'); michael@0: key.Append(' '); michael@0: key.Append(realm); michael@0: } michael@0: michael@0: // Construct the message string... michael@0: // michael@0: // We use Necko's string bundle here. This code really should be encapsulated michael@0: // behind some Necko API, after all this code is based closely on the code in michael@0: // nsHttpChannel.cpp. michael@0: michael@0: nsCOMPtr bundleSvc = michael@0: do_GetService(NS_STRINGBUNDLE_CONTRACTID); michael@0: if (!bundleSvc) michael@0: return; michael@0: michael@0: nsCOMPtr bundle; michael@0: bundleSvc->CreateBundle("chrome://global/locale/commonDialogs.properties", michael@0: getter_AddRefs(bundle)); michael@0: if (!bundle) michael@0: return; michael@0: michael@0: nsString message; michael@0: if (!realm.IsEmpty()) michael@0: { michael@0: const char16_t *strings[] = { realm.get(), dispHost.get() }; michael@0: bundle->FormatStringFromName(MOZ_UTF16("EnterUserPasswordForRealm"), michael@0: strings, 2, getter_Copies(message)); michael@0: } michael@0: else michael@0: { michael@0: const char16_t *strings[] = { dispHost.get() }; michael@0: bundle->FormatStringFromName(MOZ_UTF16("EnterUserPasswordFor"), michael@0: strings, 1, getter_Copies(message)); michael@0: } michael@0: if (message.IsEmpty()) michael@0: return; michael@0: michael@0: // Prompt the user... michael@0: nsresult rv; michael@0: bool retval = false; michael@0: char16_t *user = nullptr, *pass = nullptr; michael@0: michael@0: rv = prompt->PromptUsernameAndPassword(nullptr, message.get(), michael@0: key.get(), michael@0: nsIAuthPrompt::SAVE_PASSWORD_PERMANENTLY, michael@0: &user, &pass, &retval); michael@0: if (NS_FAILED(rv)) michael@0: return; michael@0: if (!retval || !user || !pass) michael@0: return; michael@0: michael@0: // XXX We need to convert the UTF-16 username and password from our dialog to michael@0: // strings that GnomeVFS can understand. It's unclear what encoding GnomeVFS michael@0: // expects, so for now we assume 7-bit ASCII. Hopefully, we can get a better michael@0: // solution at some point. michael@0: michael@0: // One copy is never enough... michael@0: authOut->username = g_strdup(NS_LossyConvertUTF16toASCII(user).get()); michael@0: authOut->password = g_strdup(NS_LossyConvertUTF16toASCII(pass).get()); michael@0: michael@0: nsMemory::Free(user); michael@0: nsMemory::Free(pass); michael@0: } michael@0: michael@0: struct nsGnomeVFSAuthCallbackEvent : public nsRunnable michael@0: { michael@0: gconstpointer in; michael@0: gsize in_size; michael@0: gpointer out; michael@0: gsize out_size; michael@0: gpointer callback_data; michael@0: michael@0: NS_IMETHOD Run() { michael@0: ProxiedAuthCallback(in, in_size, out, out_size, callback_data); michael@0: return NS_OK; michael@0: } michael@0: }; michael@0: michael@0: static void michael@0: AuthCallback(gconstpointer in, michael@0: gsize in_size, michael@0: gpointer out, michael@0: gsize out_size, michael@0: gpointer callback_data) michael@0: { michael@0: // Need to proxy this callback over to the main thread. Synchronous dispatch michael@0: // is required in order to provide data to the GnomeVFS callback. michael@0: michael@0: nsRefPtr ev = new nsGnomeVFSAuthCallbackEvent(); michael@0: if (!ev) michael@0: return; // OOM michael@0: michael@0: ev->in = in; michael@0: ev->in_size = in_size; michael@0: ev->out = out; michael@0: ev->out_size = out_size; michael@0: ev->callback_data = callback_data; michael@0: michael@0: NS_DispatchToMainThread(ev, NS_DISPATCH_SYNC); michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: static gint michael@0: FileInfoComparator(gconstpointer a, gconstpointer b) michael@0: { michael@0: const GnomeVFSFileInfo *ia = (const GnomeVFSFileInfo *) a; michael@0: const GnomeVFSFileInfo *ib = (const GnomeVFSFileInfo *) b; michael@0: michael@0: return strcasecmp(ia->name, ib->name); michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: class nsGnomeVFSInputStream MOZ_FINAL : public nsIInputStream michael@0: { michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_NSIINPUTSTREAM michael@0: michael@0: nsGnomeVFSInputStream(const nsCString &uriSpec) michael@0: : mSpec(uriSpec) michael@0: , mChannel(nullptr) michael@0: , mHandle(nullptr) michael@0: , mBytesRemaining(UINT64_MAX) michael@0: , mStatus(NS_OK) michael@0: , mDirList(nullptr) michael@0: , mDirListPtr(nullptr) michael@0: , mDirBufCursor(0) michael@0: , mDirOpen(false) {} michael@0: michael@0: ~nsGnomeVFSInputStream() { Close(); } michael@0: michael@0: void SetChannel(nsIChannel *channel) michael@0: { michael@0: // We need to hold an owning reference to our channel. This is done michael@0: // so we can access the channel's notification callbacks to acquire michael@0: // a reference to a nsIAuthPrompt if we need to handle a GnomeVFS michael@0: // authentication callback. michael@0: // michael@0: // However, the channel can only be accessed on the main thread, so michael@0: // we have to be very careful with ownership. Moreover, it doesn't michael@0: // support threadsafe addref/release, so proxying is the answer. michael@0: // michael@0: // Also, it's important to note that this likely creates a reference michael@0: // cycle since the channel likely owns this stream. This reference michael@0: // cycle is broken in our Close method. michael@0: michael@0: NS_ADDREF(mChannel = channel); michael@0: } michael@0: michael@0: private: michael@0: GnomeVFSResult DoOpen(); michael@0: GnomeVFSResult DoRead(char *aBuf, uint32_t aCount, uint32_t *aCountRead); michael@0: nsresult SetContentTypeOfChannel(const char *contentType); michael@0: michael@0: private: michael@0: nsCString mSpec; michael@0: nsIChannel *mChannel; // manually refcounted michael@0: GnomeVFSHandle *mHandle; michael@0: uint64_t mBytesRemaining; michael@0: nsresult mStatus; michael@0: GList *mDirList; michael@0: GList *mDirListPtr; michael@0: nsCString mDirBuf; michael@0: uint32_t mDirBufCursor; michael@0: bool mDirOpen; michael@0: }; michael@0: michael@0: GnomeVFSResult michael@0: nsGnomeVFSInputStream::DoOpen() michael@0: { michael@0: GnomeVFSResult rv; michael@0: michael@0: NS_ASSERTION(mHandle == nullptr, "already open"); michael@0: michael@0: // Push a callback handler on the stack for this thread, so we can intercept michael@0: // authentication requests from GnomeVFS. We'll use the channel to get a michael@0: // nsIAuthPrompt instance. michael@0: michael@0: gnome_vfs_module_callback_push(GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION, michael@0: AuthCallback, mChannel, nullptr); michael@0: michael@0: // Query the mime type first (this could return nullptr). michael@0: // michael@0: // XXX We need to do this up-front in order to determine how to open the URI. michael@0: // Unfortunately, the error code GNOME_VFS_ERROR_IS_DIRECTORY is not michael@0: // always returned by gnome_vfs_open when we pass it a URI to a directory! michael@0: // Otherwise, we could have used that as a way to failover to opening the michael@0: // URI as a directory. Also, it would have been ideal if michael@0: // gnome_vfs_get_file_info_from_handle were actually implemented by the michael@0: // smb:// module, since that would have allowed us to potentially save a michael@0: // round trip to the server to discover the mime type of the document in michael@0: // the case where gnome_vfs_open would have been used. (Oh well! /me michael@0: // throws hands up in the air and moves on...) michael@0: michael@0: GnomeVFSFileInfo info = {0}; michael@0: rv = gnome_vfs_get_file_info(mSpec.get(), &info, GnomeVFSFileInfoOptions( michael@0: GNOME_VFS_FILE_INFO_DEFAULT | michael@0: GNOME_VFS_FILE_INFO_FOLLOW_LINKS)); michael@0: if (rv == GNOME_VFS_OK) michael@0: { michael@0: if (info.type == GNOME_VFS_FILE_TYPE_DIRECTORY) michael@0: { michael@0: rv = gnome_vfs_directory_list_load(&mDirList, mSpec.get(), michael@0: GNOME_VFS_FILE_INFO_DEFAULT); michael@0: michael@0: LOG(("gnomevfs: gnome_vfs_directory_list_load returned %d (%s) [spec=\"%s\"]\n", michael@0: rv, gnome_vfs_result_to_string(rv), mSpec.get())); michael@0: } michael@0: else michael@0: { michael@0: rv = gnome_vfs_open(&mHandle, mSpec.get(), GNOME_VFS_OPEN_READ); michael@0: michael@0: LOG(("gnomevfs: gnome_vfs_open returned %d (%s) [spec=\"%s\"]\n", michael@0: rv, gnome_vfs_result_to_string(rv), mSpec.get())); michael@0: } michael@0: } michael@0: michael@0: gnome_vfs_module_callback_pop(GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION); michael@0: michael@0: if (rv == GNOME_VFS_OK) michael@0: { michael@0: if (mHandle) michael@0: { michael@0: // Here we set the content type of the channel to the value of the mime michael@0: // type determined by GnomeVFS. However, if GnomeVFS is telling us that michael@0: // the document is binary, we'll ignore that and keep the channel's michael@0: // content type unspecified. That will enable our content type sniffing michael@0: // algorithms. This should provide more consistent mime type handling. michael@0: michael@0: if (info.mime_type && (strcmp(info.mime_type, APPLICATION_OCTET_STREAM) != 0)) michael@0: SetContentTypeOfChannel(info.mime_type); michael@0: michael@0: mBytesRemaining = info.size; michael@0: michael@0: // Update the content length attribute on the channel. We do this michael@0: // synchronously without proxying. This hack is not as bad as it looks! michael@0: if (mBytesRemaining > INT64_MAX) { michael@0: mChannel->SetContentLength(-1); michael@0: } else { michael@0: mChannel->SetContentLength(mBytesRemaining); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: mDirOpen = true; michael@0: michael@0: // Sort mDirList michael@0: mDirList = g_list_sort(mDirList, FileInfoComparator); michael@0: mDirListPtr = mDirList; michael@0: michael@0: // Write base URL (make sure it ends with a '/') michael@0: mDirBuf.Append("300: "); michael@0: mDirBuf.Append(mSpec); michael@0: if (mSpec.get()[mSpec.Length() - 1] != '/') michael@0: mDirBuf.Append('/'); michael@0: mDirBuf.Append('\n'); michael@0: michael@0: // Write column names michael@0: mDirBuf.Append("200: filename content-length last-modified file-type\n"); michael@0: michael@0: // Write charset (assume UTF-8) michael@0: // XXX is this correct? michael@0: mDirBuf.Append("301: UTF-8\n"); michael@0: michael@0: SetContentTypeOfChannel(APPLICATION_HTTP_INDEX_FORMAT); michael@0: } michael@0: } michael@0: michael@0: gnome_vfs_file_info_clear(&info); michael@0: return rv; michael@0: } michael@0: michael@0: GnomeVFSResult michael@0: nsGnomeVFSInputStream::DoRead(char *aBuf, uint32_t aCount, uint32_t *aCountRead) michael@0: { michael@0: GnomeVFSResult rv; michael@0: michael@0: if (mHandle) michael@0: { michael@0: GnomeVFSFileSize bytesRead; michael@0: rv = gnome_vfs_read(mHandle, aBuf, aCount, &bytesRead); michael@0: if (rv == GNOME_VFS_OK) michael@0: { michael@0: // aCount is 32-bit, so aCountRead is under 32-bit value. michael@0: *aCountRead = (uint32_t) bytesRead; michael@0: mBytesRemaining -= *aCountRead; michael@0: } michael@0: } michael@0: else if (mDirOpen) michael@0: { michael@0: rv = GNOME_VFS_OK; michael@0: michael@0: while (aCount && rv != GNOME_VFS_ERROR_EOF) michael@0: { michael@0: // Copy data out of our buffer michael@0: uint32_t bufLen = mDirBuf.Length() - mDirBufCursor; michael@0: if (bufLen) michael@0: { michael@0: uint32_t n = std::min(bufLen, aCount); michael@0: memcpy(aBuf, mDirBuf.get() + mDirBufCursor, n); michael@0: *aCountRead += n; michael@0: aBuf += n; michael@0: aCount -= n; michael@0: mDirBufCursor += n; michael@0: } michael@0: michael@0: if (!mDirListPtr) // Are we at the end of the directory list? michael@0: { michael@0: rv = GNOME_VFS_ERROR_EOF; michael@0: } michael@0: else if (aCount) // Do we need more data? michael@0: { michael@0: GnomeVFSFileInfo *info = (GnomeVFSFileInfo *) mDirListPtr->data; michael@0: michael@0: // Prune '.' and '..' from directory listing. michael@0: if (info->name[0] == '.' && michael@0: (info->name[1] == '\0' || michael@0: (info->name[1] == '.' && info->name[2] == '\0'))) michael@0: { michael@0: mDirListPtr = mDirListPtr->next; michael@0: continue; michael@0: } michael@0: michael@0: mDirBuf.Assign("201: "); michael@0: michael@0: // The "filename" field michael@0: nsCString escName; michael@0: nsCOMPtr nu = do_GetService(NS_NETUTIL_CONTRACTID); michael@0: if (nu) { michael@0: nu->EscapeString(nsDependentCString(info->name), michael@0: nsINetUtil::ESCAPE_URL_PATH, escName); michael@0: michael@0: mDirBuf.Append(escName); michael@0: mDirBuf.Append(' '); michael@0: } michael@0: michael@0: // The "content-length" field michael@0: // XXX truncates size from 64-bit to 32-bit michael@0: mDirBuf.AppendInt(int32_t(info->size)); michael@0: mDirBuf.Append(' '); michael@0: michael@0: // The "last-modified" field michael@0: // michael@0: // NSPR promises: PRTime is compatible with time_t michael@0: // we just need to convert from seconds to microseconds michael@0: PRExplodedTime tm; michael@0: PRTime pt = ((PRTime) info->mtime) * 1000000; michael@0: PR_ExplodeTime(pt, PR_GMTParameters, &tm); michael@0: { michael@0: char buf[64]; michael@0: PR_FormatTimeUSEnglish(buf, sizeof(buf), michael@0: "%a,%%20%d%%20%b%%20%Y%%20%H:%M:%S%%20GMT ", &tm); michael@0: mDirBuf.Append(buf); michael@0: } michael@0: michael@0: // The "file-type" field michael@0: switch (info->type) michael@0: { michael@0: case GNOME_VFS_FILE_TYPE_REGULAR: michael@0: mDirBuf.Append("FILE "); michael@0: break; michael@0: case GNOME_VFS_FILE_TYPE_DIRECTORY: michael@0: mDirBuf.Append("DIRECTORY "); michael@0: break; michael@0: case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK: michael@0: mDirBuf.Append("SYMBOLIC-LINK "); michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: mDirBuf.Append('\n'); michael@0: michael@0: mDirBufCursor = 0; michael@0: mDirListPtr = mDirListPtr->next; michael@0: } michael@0: } michael@0: } michael@0: else michael@0: { michael@0: NS_NOTREACHED("reading from what?"); michael@0: rv = GNOME_VFS_ERROR_GENERIC; michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: // This class is used to implement SetContentTypeOfChannel. michael@0: class nsGnomeVFSSetContentTypeEvent : public nsRunnable michael@0: { michael@0: public: michael@0: nsGnomeVFSSetContentTypeEvent(nsIChannel *channel, const char *contentType) michael@0: : mChannel(channel), mContentType(contentType) michael@0: { michael@0: // stash channel reference in mChannel. no AddRef here! see note michael@0: // in SetContentTypeOfchannel. michael@0: } michael@0: michael@0: NS_IMETHOD Run() michael@0: { michael@0: mChannel->SetContentType(mContentType); michael@0: return NS_OK; michael@0: } michael@0: michael@0: private: michael@0: nsIChannel *mChannel; michael@0: nsCString mContentType; michael@0: }; michael@0: michael@0: nsresult michael@0: nsGnomeVFSInputStream::SetContentTypeOfChannel(const char *contentType) michael@0: { michael@0: // We need to proxy this call over to the main thread. We post an michael@0: // asynchronous event in this case so that we don't delay reading data, and michael@0: // we know that this is safe to do since the channel's reference will be michael@0: // released asynchronously as well. We trust the ordering of the main michael@0: // thread's event queue to protect us against memory corruption. michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr ev = michael@0: new nsGnomeVFSSetContentTypeEvent(mChannel, contentType); michael@0: if (!ev) michael@0: { michael@0: rv = NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: else michael@0: { michael@0: rv = NS_DispatchToMainThread(ev); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsGnomeVFSInputStream, nsIInputStream) michael@0: michael@0: NS_IMETHODIMP michael@0: nsGnomeVFSInputStream::Close() michael@0: { michael@0: if (mHandle) michael@0: { michael@0: gnome_vfs_close(mHandle); michael@0: mHandle = nullptr; michael@0: } michael@0: michael@0: if (mDirList) michael@0: { michael@0: // Destroy the list of GnomeVFSFileInfo objects... michael@0: g_list_foreach(mDirList, (GFunc) gnome_vfs_file_info_unref, nullptr); michael@0: g_list_free(mDirList); michael@0: mDirList = nullptr; michael@0: mDirListPtr = nullptr; michael@0: } michael@0: michael@0: if (mChannel) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: michael@0: nsCOMPtr thread = do_GetMainThread(); michael@0: if (thread) michael@0: rv = NS_ProxyRelease(thread, mChannel); michael@0: michael@0: NS_ASSERTION(thread && NS_SUCCEEDED(rv), "leaking channel reference"); michael@0: mChannel = nullptr; michael@0: } michael@0: michael@0: mSpec.Truncate(); // free memory michael@0: michael@0: // Prevent future reads from re-opening the handle. michael@0: if (NS_SUCCEEDED(mStatus)) michael@0: mStatus = NS_BASE_STREAM_CLOSED; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGnomeVFSInputStream::Available(uint64_t *aResult) michael@0: { michael@0: if (NS_FAILED(mStatus)) michael@0: return mStatus; michael@0: michael@0: *aResult = mBytesRemaining; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGnomeVFSInputStream::Read(char *aBuf, michael@0: uint32_t aCount, michael@0: uint32_t *aCountRead) michael@0: { michael@0: *aCountRead = 0; michael@0: michael@0: if (mStatus == NS_BASE_STREAM_CLOSED) michael@0: return NS_OK; michael@0: if (NS_FAILED(mStatus)) michael@0: return mStatus; michael@0: michael@0: GnomeVFSResult rv = GNOME_VFS_OK; michael@0: michael@0: // If this is our first-time through here, then open the URI. michael@0: if (!mHandle && !mDirOpen) michael@0: rv = DoOpen(); michael@0: michael@0: if (rv == GNOME_VFS_OK) michael@0: rv = DoRead(aBuf, aCount, aCountRead); michael@0: michael@0: if (rv != GNOME_VFS_OK) michael@0: { michael@0: // If we reach here, we hit some kind of error. EOF is not an error. michael@0: mStatus = MapGnomeVFSResult(rv); michael@0: if (mStatus == NS_BASE_STREAM_CLOSED) michael@0: return NS_OK; michael@0: michael@0: LOG(("gnomevfs: result %d [%s] mapped to 0x%x\n", michael@0: rv, gnome_vfs_result_to_string(rv), mStatus)); michael@0: } michael@0: return mStatus; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGnomeVFSInputStream::ReadSegments(nsWriteSegmentFun aWriter, michael@0: void *aClosure, michael@0: uint32_t aCount, michael@0: uint32_t *aResult) michael@0: { michael@0: // There is no way to implement this using GnomeVFS, but fortunately michael@0: // that doesn't matter. Because we are a blocking input stream, Necko michael@0: // isn't going to call our ReadSegments method. michael@0: NS_NOTREACHED("nsGnomeVFSInputStream::ReadSegments"); michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGnomeVFSInputStream::IsNonBlocking(bool *aResult) michael@0: { michael@0: *aResult = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: class nsGnomeVFSProtocolHandler MOZ_FINAL : public nsIProtocolHandler michael@0: , public nsIObserver michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIPROTOCOLHANDLER michael@0: NS_DECL_NSIOBSERVER michael@0: michael@0: nsresult Init(); michael@0: michael@0: private: michael@0: void InitSupportedProtocolsPref(nsIPrefBranch *prefs); michael@0: bool IsSupportedProtocol(const nsCString &spec); michael@0: michael@0: nsCString mSupportedProtocols; michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(nsGnomeVFSProtocolHandler, nsIProtocolHandler, nsIObserver) michael@0: michael@0: nsresult michael@0: nsGnomeVFSProtocolHandler::Init() michael@0: { michael@0: #ifdef PR_LOGGING michael@0: sGnomeVFSLog = PR_NewLogModule("gnomevfs"); michael@0: #endif michael@0: michael@0: if (!gnome_vfs_initialized()) michael@0: { michael@0: if (!gnome_vfs_init()) michael@0: { michael@0: NS_WARNING("gnome_vfs_init failed"); michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: } michael@0: michael@0: nsCOMPtr prefs = do_GetService(NS_PREFSERVICE_CONTRACTID); michael@0: if (prefs) michael@0: { michael@0: InitSupportedProtocolsPref(prefs); michael@0: prefs->AddObserver(MOZ_GNOMEVFS_SUPPORTED_PROTOCOLS, this, false); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsGnomeVFSProtocolHandler::InitSupportedProtocolsPref(nsIPrefBranch *prefs) michael@0: { michael@0: // read preferences michael@0: nsresult rv = prefs->GetCharPref(MOZ_GNOMEVFS_SUPPORTED_PROTOCOLS, michael@0: getter_Copies(mSupportedProtocols)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: mSupportedProtocols.StripWhitespace(); michael@0: ToLowerCase(mSupportedProtocols); michael@0: } michael@0: else michael@0: mSupportedProtocols.Assign("smb:,sftp:"); // use defaults michael@0: michael@0: LOG(("gnomevfs: supported protocols \"%s\"\n", mSupportedProtocols.get())); michael@0: } michael@0: michael@0: bool michael@0: nsGnomeVFSProtocolHandler::IsSupportedProtocol(const nsCString &aSpec) michael@0: { michael@0: const char *specString = aSpec.get(); michael@0: const char *colon = strchr(specString, ':'); michael@0: if (!colon) michael@0: return false; michael@0: michael@0: uint32_t length = colon - specString + 1; michael@0: michael@0: // + ':' michael@0: nsCString scheme(specString, length); michael@0: michael@0: char *found = PL_strcasestr(mSupportedProtocols.get(), scheme.get()); michael@0: if (!found) michael@0: return false; michael@0: michael@0: if (found[length] != ',' && found[length] != '\0') michael@0: return false; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGnomeVFSProtocolHandler::GetScheme(nsACString &aScheme) michael@0: { michael@0: aScheme.Assign(MOZ_GNOMEVFS_SCHEME); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGnomeVFSProtocolHandler::GetDefaultPort(int32_t *aDefaultPort) michael@0: { michael@0: *aDefaultPort = -1; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGnomeVFSProtocolHandler::GetProtocolFlags(uint32_t *aProtocolFlags) michael@0: { michael@0: // Is URI_STD true of all GnomeVFS URI types? michael@0: *aProtocolFlags = URI_STD | URI_DANGEROUS_TO_LOAD; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGnomeVFSProtocolHandler::NewURI(const nsACString &aSpec, michael@0: const char *aOriginCharset, michael@0: nsIURI *aBaseURI, michael@0: nsIURI **aResult) michael@0: { michael@0: const nsCString flatSpec(aSpec); michael@0: LOG(("gnomevfs: NewURI [spec=%s]\n", flatSpec.get())); michael@0: michael@0: if (!aBaseURI) michael@0: { michael@0: // michael@0: // XXX This check is used to limit the gnome-vfs protocols we support. For michael@0: // security reasons, it is best that we limit the protocols we support to michael@0: // those with known characteristics. We might want to lessen this michael@0: // restriction if it proves to be too heavy handed. A black list of michael@0: // protocols we don't want to support might be better. For example, we michael@0: // probably don't want to try to load "start-here:" inside the browser. michael@0: // There are others that fall into this category, which are best handled michael@0: // externally by Nautilus (or another app like it). michael@0: // michael@0: if (!IsSupportedProtocol(flatSpec)) michael@0: return NS_ERROR_UNKNOWN_PROTOCOL; michael@0: michael@0: // Verify that GnomeVFS supports this URI scheme. michael@0: GnomeVFSURI *uri = gnome_vfs_uri_new(flatSpec.get()); michael@0: if (!uri) michael@0: return NS_ERROR_UNKNOWN_PROTOCOL; michael@0: } michael@0: michael@0: // michael@0: // XXX Can we really assume that all gnome-vfs URIs can be parsed using michael@0: // nsStandardURL? We probably really need to implement nsIURI/nsIURL michael@0: // in terms of the gnome_vfs_uri_XXX methods, but at least this works michael@0: // correctly for smb:// URLs ;-) michael@0: // michael@0: // Also, it might not be possible to fully implement nsIURI/nsIURL in michael@0: // terms of GnomeVFSURI since some Necko methods have no GnomeVFS michael@0: // equivalent. michael@0: // michael@0: nsresult rv; michael@0: nsCOMPtr url = michael@0: do_CreateInstance(NS_STANDARDURL_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: rv = url->Init(nsIStandardURL::URLTYPE_STANDARD, -1, flatSpec, michael@0: aOriginCharset, aBaseURI); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = CallQueryInterface(url, aResult); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGnomeVFSProtocolHandler::NewChannel(nsIURI *aURI, nsIChannel **aResult) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aURI); michael@0: nsresult rv; michael@0: michael@0: nsAutoCString spec; michael@0: rv = aURI->GetSpec(spec); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: nsRefPtr stream = new nsGnomeVFSInputStream(spec); michael@0: if (!stream) michael@0: { michael@0: rv = NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: else michael@0: { michael@0: // start out assuming an unknown content-type. we'll set the content-type michael@0: // to something better once we open the URI. michael@0: rv = NS_NewInputStreamChannel(aResult, aURI, stream, michael@0: NS_LITERAL_CSTRING(UNKNOWN_CONTENT_TYPE)); michael@0: if (NS_SUCCEEDED(rv)) michael@0: stream->SetChannel(*aResult); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGnomeVFSProtocolHandler::AllowPort(int32_t aPort, michael@0: const char *aScheme, michael@0: bool *aResult) michael@0: { michael@0: // Don't override anything. michael@0: *aResult = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGnomeVFSProtocolHandler::Observe(nsISupports *aSubject, michael@0: const char *aTopic, michael@0: const char16_t *aData) michael@0: { michael@0: if (strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0) { michael@0: nsCOMPtr prefs = do_QueryInterface(aSubject); michael@0: InitSupportedProtocolsPref(prefs); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: #define NS_GNOMEVFSPROTOCOLHANDLER_CID \ michael@0: { /* 9b6dc177-a2e4-49e1-9c98-0a8384de7f6c */ \ michael@0: 0x9b6dc177, \ michael@0: 0xa2e4, \ michael@0: 0x49e1, \ michael@0: {0x9c, 0x98, 0x0a, 0x83, 0x84, 0xde, 0x7f, 0x6c} \ michael@0: } michael@0: michael@0: NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsGnomeVFSProtocolHandler, Init) michael@0: NS_DEFINE_NAMED_CID(NS_GNOMEVFSPROTOCOLHANDLER_CID); michael@0: michael@0: static const mozilla::Module::CIDEntry kVFSCIDs[] = { michael@0: { &kNS_GNOMEVFSPROTOCOLHANDLER_CID, false, nullptr, nsGnomeVFSProtocolHandlerConstructor }, michael@0: { nullptr } michael@0: }; michael@0: michael@0: static const mozilla::Module::ContractIDEntry kVFSContracts[] = { michael@0: { NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX MOZ_GNOMEVFS_SCHEME, &kNS_GNOMEVFSPROTOCOLHANDLER_CID }, michael@0: { nullptr } michael@0: }; michael@0: michael@0: static const mozilla::Module kVFSModule = { michael@0: mozilla::Module::kVersion, michael@0: kVFSCIDs, michael@0: kVFSContracts michael@0: }; michael@0: michael@0: NSMODULE_DEFN(nsGnomeVFSModule) = &kVFSModule;