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: #include "nsCOMPtr.h" michael@0: #include "nsIInputStream.h" michael@0: #include "nsIStringStream.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsIJARURI.h" michael@0: #include "nsIResProtocolHandler.h" michael@0: #include "nsIChromeRegistry.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "StartupCacheUtils.h" michael@0: #include "mozilla/scache/StartupCache.h" michael@0: #include "mozilla/Omnijar.h" michael@0: michael@0: namespace mozilla { michael@0: namespace scache { michael@0: michael@0: NS_EXPORT nsresult michael@0: NewObjectInputStreamFromBuffer(char* buffer, uint32_t len, michael@0: nsIObjectInputStream** stream) michael@0: { michael@0: nsCOMPtr stringStream michael@0: = do_CreateInstance("@mozilla.org/io/string-input-stream;1"); michael@0: nsCOMPtr objectInput michael@0: = do_CreateInstance("@mozilla.org/binaryinputstream;1"); michael@0: michael@0: stringStream->AdoptData(buffer, len); michael@0: objectInput->SetInputStream(stringStream); michael@0: michael@0: objectInput.forget(stream); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_EXPORT nsresult michael@0: NewObjectOutputWrappedStorageStream(nsIObjectOutputStream **wrapperStream, michael@0: nsIStorageStream** stream, michael@0: bool wantDebugStream) michael@0: { michael@0: nsCOMPtr storageStream; michael@0: michael@0: nsresult rv = NS_NewStorageStream(256, UINT32_MAX, getter_AddRefs(storageStream)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr objectOutput michael@0: = do_CreateInstance("@mozilla.org/binaryoutputstream;1"); michael@0: nsCOMPtr outputStream michael@0: = do_QueryInterface(storageStream); michael@0: michael@0: objectOutput->SetOutputStream(outputStream); michael@0: michael@0: #ifdef DEBUG michael@0: if (wantDebugStream) { michael@0: // Wrap in debug stream to detect unsupported writes of michael@0: // multiply-referenced non-singleton objects michael@0: StartupCache* sc = StartupCache::GetSingleton(); michael@0: NS_ENSURE_TRUE(sc, NS_ERROR_UNEXPECTED); michael@0: nsCOMPtr debugStream; michael@0: sc->GetDebugObjectOutputStream(objectOutput, getter_AddRefs(debugStream)); michael@0: debugStream.forget(wrapperStream); michael@0: } else { michael@0: objectOutput.forget(wrapperStream); michael@0: } michael@0: #else michael@0: objectOutput.forget(wrapperStream); michael@0: #endif michael@0: michael@0: storageStream.forget(stream); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_EXPORT nsresult michael@0: NewBufferFromStorageStream(nsIStorageStream *storageStream, michael@0: char** buffer, uint32_t* len) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr inputStream; michael@0: rv = storageStream->NewInputStream(0, getter_AddRefs(inputStream)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: uint64_t avail64; michael@0: rv = inputStream->Available(&avail64); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: NS_ENSURE_TRUE(avail64 <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG); michael@0: michael@0: uint32_t avail = (uint32_t)avail64; michael@0: nsAutoArrayPtr temp (new char[avail]); michael@0: uint32_t read; michael@0: rv = inputStream->Read(temp, avail, &read); michael@0: if (NS_SUCCEEDED(rv) && avail != read) michael@0: rv = NS_ERROR_UNEXPECTED; michael@0: michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: michael@0: *len = avail; michael@0: *buffer = temp.forget(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: static const char baseName[2][5] = { "gre/", "app/" }; michael@0: michael@0: static inline bool michael@0: canonicalizeBase(nsAutoCString &spec, michael@0: nsACString &out) michael@0: { michael@0: nsAutoCString greBase, appBase; michael@0: nsresult rv = mozilla::Omnijar::GetURIString(mozilla::Omnijar::GRE, greBase); michael@0: if (NS_FAILED(rv) || !greBase.Length()) michael@0: return false; michael@0: michael@0: rv = mozilla::Omnijar::GetURIString(mozilla::Omnijar::APP, appBase); michael@0: if (NS_FAILED(rv)) michael@0: return false; michael@0: michael@0: bool underGre = !greBase.Compare(spec.get(), false, greBase.Length()); michael@0: bool underApp = appBase.Length() && michael@0: !appBase.Compare(spec.get(), false, appBase.Length()); michael@0: michael@0: if (!underGre && !underApp) michael@0: return false; michael@0: michael@0: /** michael@0: * At this point, if both underGre and underApp are true, it can be one michael@0: * of the two following cases: michael@0: * - the GRE directory points to a subdirectory of the APP directory, michael@0: * meaning spec points under GRE. michael@0: * - the APP directory points to a subdirectory of the GRE directory, michael@0: * meaning spec points under APP. michael@0: * Checking the GRE and APP path length is enough to know in which case michael@0: * we are. michael@0: */ michael@0: if (underGre && underApp && greBase.Length() < appBase.Length()) michael@0: underGre = false; michael@0: michael@0: out.Append("/resource/"); michael@0: out.Append(baseName[underGre ? mozilla::Omnijar::GRE : mozilla::Omnijar::APP]); michael@0: out.Append(Substring(spec, underGre ? greBase.Length() : appBase.Length())); michael@0: return true; michael@0: } michael@0: michael@0: /** michael@0: * PathifyURI transforms uris into useful zip paths michael@0: * to make it easier to manipulate startup cache entries michael@0: * using standard zip tools. michael@0: * Transformations applied: michael@0: * * resource:// URIs are resolved to their corresponding file/jar URI to michael@0: * canonicalize resources URIs other than gre and app. michael@0: * * Paths under GRE or APP directory have their base path replaced with michael@0: * resource/gre or resource/app to avoid depending on install location. michael@0: * * jar:file:///path/to/file.jar!/sub/path urls are replaced with michael@0: * /path/to/file.jar/sub/path michael@0: * michael@0: * The result is appended to the string passed in. Adding a prefix before michael@0: * calling is recommended to avoid colliding with other cache users. michael@0: * michael@0: * For example, in the js loader (string is prefixed with jsloader by caller): michael@0: * resource://gre/modules/XPCOMUtils.jsm or michael@0: * file://$GRE_DIR/modules/XPCOMUtils.jsm or michael@0: * jar:file://$GRE_DIR/omni.jar!/modules/XPCOMUtils.jsm becomes michael@0: * jsloader/resource/gre/modules/XPCOMUtils.jsm michael@0: * file://$PROFILE_DIR/extensions/{uuid}/components/component.js becomes michael@0: * jsloader/$PROFILE_DIR/extensions/%7Buuid%7D/components/component.js michael@0: * jar:file://$PROFILE_DIR/extensions/some.xpi!/components/component.js becomes michael@0: * jsloader/$PROFILE_DIR/extensions/some.xpi/components/component.js michael@0: */ michael@0: NS_EXPORT nsresult michael@0: PathifyURI(nsIURI *in, nsACString &out) michael@0: { michael@0: bool equals; michael@0: nsresult rv; michael@0: nsCOMPtr uri = in; michael@0: nsAutoCString spec; michael@0: michael@0: // Resolve resource:// URIs. At the end of this if/else block, we michael@0: // have both spec and uri variables identifying the same URI. michael@0: if (NS_SUCCEEDED(in->SchemeIs("resource", &equals)) && equals) { michael@0: nsCOMPtr ioService = do_GetIOService(&rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr ph; michael@0: rv = ioService->GetProtocolHandler("resource", getter_AddRefs(ph)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr irph(do_QueryInterface(ph, &rv)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = irph->ResolveURI(in, spec); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = ioService->NewURI(spec, nullptr, nullptr, getter_AddRefs(uri)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } else { michael@0: if (NS_SUCCEEDED(in->SchemeIs("chrome", &equals)) && equals) { michael@0: nsCOMPtr chromeReg = michael@0: mozilla::services::GetChromeRegistryService(); michael@0: if (!chromeReg) michael@0: return NS_ERROR_UNEXPECTED; michael@0: michael@0: rv = chromeReg->ConvertChromeURL(in, getter_AddRefs(uri)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: rv = uri->GetSpec(spec); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: if (!canonicalizeBase(spec, out)) { michael@0: if (NS_SUCCEEDED(uri->SchemeIs("file", &equals)) && equals) { michael@0: nsCOMPtr baseFileURL; michael@0: baseFileURL = do_QueryInterface(uri, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsAutoCString path; michael@0: rv = baseFileURL->GetPath(path); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: out.Append(path); michael@0: } else if (NS_SUCCEEDED(uri->SchemeIs("jar", &equals)) && equals) { michael@0: nsCOMPtr jarURI = do_QueryInterface(uri, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr jarFileURI; michael@0: rv = jarURI->GetJARFile(getter_AddRefs(jarFileURI)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = PathifyURI(jarFileURI, out); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsAutoCString path; michael@0: rv = jarURI->GetJAREntry(path); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: out.Append("/"); michael@0: out.Append(path); michael@0: } else { // Very unlikely michael@0: nsAutoCString spec; michael@0: rv = uri->GetSpec(spec); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: out.Append("/"); michael@0: out.Append(spec); michael@0: } michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: } michael@0: }