2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2002-07-09 06:49:58 +04:00
|
|
|
|
2012-12-15 03:58:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
2001-11-02 04:53:13 +03:00
|
|
|
#include "nsXBLDocumentInfo.h"
|
|
|
|
#include "nsIDocument.h"
|
2003-03-07 02:59:18 +03:00
|
|
|
#include "nsXBLPrototypeBinding.h"
|
2001-11-02 04:53:13 +03:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "nsIScriptContext.h"
|
2013-08-22 10:30:55 +04:00
|
|
|
#include "nsIDOMDocument.h"
|
2001-11-02 04:53:13 +03:00
|
|
|
#include "jsapi.h"
|
2013-07-30 03:45:26 +04:00
|
|
|
#include "jsfriendapi.h"
|
2001-11-02 04:53:13 +03:00
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsIChromeRegistry.h"
|
2003-10-22 10:09:48 +04:00
|
|
|
#include "nsIPrincipal.h"
|
2012-10-25 19:01:07 +04:00
|
|
|
#include "nsJSPrincipals.h"
|
2005-10-19 22:45:14 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsContentUtils.h"
|
2006-06-13 07:07:47 +04:00
|
|
|
#include "nsDOMJSUtils.h"
|
2010-05-14 13:24:41 +04:00
|
|
|
#include "mozilla/Services.h"
|
2010-09-03 10:22:18 +04:00
|
|
|
#include "xpcpublic.h"
|
2011-11-04 00:39:08 +04:00
|
|
|
#include "mozilla/scache/StartupCache.h"
|
|
|
|
#include "mozilla/scache/StartupCacheUtils.h"
|
2012-01-13 16:44:14 +04:00
|
|
|
#include "nsCCUncollectableMarker.h"
|
2012-05-03 08:35:38 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2013-11-20 20:29:03 +04:00
|
|
|
#include "mozilla/dom/URL.h"
|
2011-11-04 00:39:08 +04:00
|
|
|
|
2012-03-28 17:14:33 +04:00
|
|
|
using namespace mozilla;
|
2013-11-20 20:29:03 +04:00
|
|
|
using namespace mozilla::scache;
|
|
|
|
using namespace mozilla::dom;
|
2011-11-04 00:39:08 +04:00
|
|
|
|
|
|
|
static const char kXBLCachePrefix[] = "xblcache";
|
|
|
|
|
2001-11-02 04:53:13 +03:00
|
|
|
/* Implementation file */
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsXBLDocumentInfo)
|
|
|
|
|
2010-11-08 18:02:49 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXBLDocumentInfo)
|
2007-09-28 17:45:01 +04:00
|
|
|
if (tmp->mBindingTable) {
|
2015-07-27 05:29:52 +03:00
|
|
|
for (auto iter = tmp->mBindingTable->ConstIter();
|
|
|
|
!iter.Done(); iter.Next()) {
|
|
|
|
iter.UserData()->Unlink();
|
|
|
|
}
|
2007-09-28 17:45:01 +04:00
|
|
|
}
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument)
|
2007-03-08 14:17:16 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXBLDocumentInfo)
|
2012-01-13 16:44:14 +04:00
|
|
|
if (tmp->mDocument &&
|
|
|
|
nsCCUncollectableMarker::InGeneration(cb, tmp->mDocument->GetMarkedCCGeneration())) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
return NS_SUCCESS_INTERRUPTED_TRAVERSE;
|
|
|
|
}
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument)
|
2007-03-08 14:17:16 +03:00
|
|
|
if (tmp->mBindingTable) {
|
2015-07-27 05:29:52 +03:00
|
|
|
for (auto iter = tmp->mBindingTable->ConstIter();
|
|
|
|
!iter.Done(); iter.Next()) {
|
|
|
|
iter.UserData()->Traverse(cb);
|
|
|
|
}
|
2007-03-08 14:17:16 +03:00
|
|
|
}
|
2007-10-29 16:45:07 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2007-03-08 14:17:16 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2007-10-29 16:45:07 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsXBLDocumentInfo)
|
|
|
|
if (tmp->mBindingTable) {
|
2015-07-27 05:29:52 +03:00
|
|
|
for (auto iter = tmp->mBindingTable->ConstIter();
|
|
|
|
!iter.Done(); iter.Next()) {
|
|
|
|
iter.UserData()->Trace(aCallbacks, aClosure);
|
|
|
|
}
|
2007-10-29 16:45:07 +03:00
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2012-02-11 15:04:28 +04:00
|
|
|
static void
|
2014-12-05 20:38:34 +03:00
|
|
|
UnmarkXBLJSObject(JS::GCCellPtr aPtr, const char* aName, void* aClosure)
|
2012-02-11 15:04:28 +04:00
|
|
|
{
|
2015-07-29 19:31:47 +03:00
|
|
|
JS::ExposeObjectToActiveJS(&aPtr.as<JSObject>());
|
2012-02-11 15:04:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsXBLDocumentInfo::MarkInCCGeneration(uint32_t aGeneration)
|
2012-02-11 15:04:28 +04:00
|
|
|
{
|
|
|
|
if (mDocument) {
|
|
|
|
mDocument->MarkUncollectableForCCGeneration(aGeneration);
|
|
|
|
}
|
2012-03-18 18:17:05 +04:00
|
|
|
// Unmark any JS we hold
|
2012-02-11 15:04:28 +04:00
|
|
|
if (mBindingTable) {
|
2015-11-16 01:52:57 +03:00
|
|
|
for (auto iter = mBindingTable->Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
iter.UserData()->Trace(TraceCallbackFunc(UnmarkXBLJSObject), nullptr);
|
|
|
|
}
|
2012-02-11 15:04:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-25 20:35:27 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXBLDocumentInfo)
|
2007-01-05 01:31:26 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2013-08-09 20:25:14 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2007-01-05 01:31:26 +03:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2011-03-06 14:11:31 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXBLDocumentInfo)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXBLDocumentInfo)
|
2001-11-02 04:53:13 +03:00
|
|
|
|
2003-11-18 00:03:32 +03:00
|
|
|
nsXBLDocumentInfo::nsXBLDocumentInfo(nsIDocument* aDocument)
|
|
|
|
: mDocument(aDocument),
|
2011-10-17 18:59:28 +04:00
|
|
|
mScriptAccess(true),
|
|
|
|
mIsChrome(false),
|
2012-07-30 18:20:58 +04:00
|
|
|
mFirstBinding(nullptr)
|
2001-11-02 04:53:13 +03:00
|
|
|
{
|
2004-01-10 02:54:21 +03:00
|
|
|
nsIURI* uri = aDocument->GetDocumentURI();
|
2005-02-23 00:49:45 +03:00
|
|
|
if (IsChromeURI(uri)) {
|
2001-11-02 04:53:13 +03:00
|
|
|
// Cache whether or not this chrome XBL can execute scripts.
|
2010-05-14 13:24:41 +04:00
|
|
|
nsCOMPtr<nsIXULChromeRegistry> reg =
|
|
|
|
mozilla::services::GetXULChromeRegistryService();
|
2001-11-02 04:53:13 +03:00
|
|
|
if (reg) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool allow = true;
|
2005-02-23 00:49:45 +03:00
|
|
|
reg->AllowScriptsForPackage(uri, &allow);
|
2005-06-03 05:54:50 +04:00
|
|
|
mScriptAccess = allow;
|
2001-11-02 04:53:13 +03:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsChrome = true;
|
2013-12-13 20:54:04 +04:00
|
|
|
} else {
|
|
|
|
// If this binding isn't running with system principal, then it's running
|
|
|
|
// from a remote-XUL whitelisted domain. This is already a not-really-
|
|
|
|
// supported configuration (among other things, we don't use XBL scopes in
|
|
|
|
// that configuration for compatibility reasons). But we should still at
|
|
|
|
// least make an effort to prevent binding code from running if content
|
|
|
|
// script is disabled or if the source domain is blacklisted (since the
|
|
|
|
// source domain for remote XBL must always be the same as the source domain
|
|
|
|
// of the bound content).
|
|
|
|
//
|
|
|
|
// If we just ask the binding document if script is enabled, it will
|
|
|
|
// discover that it has no inner window, and return false. So instead, we
|
|
|
|
// short-circuit the normal compartment-managed script-disabling machinery,
|
|
|
|
// and query the policy for the URI directly.
|
|
|
|
bool allow;
|
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
nsresult rv = ssm->PolicyAllowsScript(uri, &allow);
|
|
|
|
mScriptAccess = NS_SUCCEEDED(rv) && allow;
|
2001-11-02 04:53:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXBLDocumentInfo::~nsXBLDocumentInfo()
|
|
|
|
{
|
2014-02-26 08:32:48 +04:00
|
|
|
mozilla::DropJSObjects(this);
|
2001-11-02 04:53:13 +03:00
|
|
|
}
|
|
|
|
|
2010-07-15 05:55:54 +04:00
|
|
|
nsXBLPrototypeBinding*
|
|
|
|
nsXBLDocumentInfo::GetPrototypeBinding(const nsACString& aRef)
|
2001-11-02 04:53:13 +03:00
|
|
|
{
|
|
|
|
if (!mBindingTable)
|
2013-04-03 05:14:24 +04:00
|
|
|
return nullptr;
|
2001-11-02 04:53:13 +03:00
|
|
|
|
2007-02-24 19:27:51 +03:00
|
|
|
if (aRef.IsEmpty()) {
|
|
|
|
// Return our first binding
|
2010-07-15 05:55:54 +04:00
|
|
|
return mFirstBinding;
|
2007-02-24 19:27:51 +03:00
|
|
|
}
|
|
|
|
|
2014-02-26 08:32:48 +04:00
|
|
|
return mBindingTable->Get(aRef);
|
2003-03-07 02:59:18 +03:00
|
|
|
}
|
|
|
|
|
2010-07-15 05:55:54 +04:00
|
|
|
nsresult
|
2003-03-07 02:59:18 +03:00
|
|
|
nsXBLDocumentInfo::SetPrototypeBinding(const nsACString& aRef, nsXBLPrototypeBinding* aBinding)
|
2001-11-02 04:53:13 +03:00
|
|
|
{
|
2007-10-29 16:45:07 +03:00
|
|
|
if (!mBindingTable) {
|
2014-02-26 08:32:48 +04:00
|
|
|
mBindingTable = new nsClassHashtable<nsCStringHashKey, nsXBLPrototypeBinding>();
|
2013-08-17 00:10:17 +04:00
|
|
|
mozilla::HoldJSObjects(this);
|
2007-10-29 16:45:07 +03:00
|
|
|
}
|
2001-11-02 04:53:13 +03:00
|
|
|
|
2014-02-26 08:32:48 +04:00
|
|
|
NS_ENSURE_STATE(!mBindingTable->Get(aRef));
|
|
|
|
mBindingTable->Put(aRef, aBinding);
|
2001-11-02 04:53:13 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-04 00:39:08 +04:00
|
|
|
void
|
|
|
|
nsXBLDocumentInfo::RemovePrototypeBinding(const nsACString& aRef)
|
|
|
|
{
|
|
|
|
if (mBindingTable) {
|
2014-02-26 08:32:48 +04:00
|
|
|
nsAutoPtr<nsXBLPrototypeBinding> bindingToRemove;
|
|
|
|
mBindingTable->RemoveAndForget(aRef, bindingToRemove);
|
|
|
|
|
|
|
|
// We do not want to destroy the binding, so just forget it.
|
|
|
|
bindingToRemove.forget();
|
2011-11-04 00:39:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsXBLDocumentInfo::ReadPrototypeBindings(nsIURI* aURI, nsXBLDocumentInfo** aDocInfo)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aDocInfo = nullptr;
|
2011-11-04 00:39:08 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec(kXBLCachePrefix);
|
2011-11-04 00:39:08 +04:00
|
|
|
nsresult rv = PathifyURI(aURI, spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
StartupCache* startupCache = StartupCache::GetSingleton();
|
|
|
|
NS_ENSURE_TRUE(startupCache, NS_ERROR_FAILURE);
|
|
|
|
|
2016-02-18 20:26:28 +03:00
|
|
|
UniquePtr<char[]> buf;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len;
|
2016-02-18 20:26:28 +03:00
|
|
|
rv = startupCache->GetBuffer(spec.get(), &buf, &len);
|
2011-11-04 00:39:08 +04:00
|
|
|
// GetBuffer will fail if the binding is not in the cache.
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObjectInputStream> stream;
|
2016-02-18 20:26:28 +03:00
|
|
|
rv = NewObjectInputStreamFromBuffer(Move(buf), len, getter_AddRefs(stream));
|
2011-11-04 00:39:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// The file compatibility.ini stores the build id. This is checked in
|
|
|
|
// nsAppRunner.cpp and will delete the cache if a different build is
|
|
|
|
// present. However, we check that the version matches here to be safe.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t version;
|
2011-11-04 00:39:08 +04:00
|
|
|
rv = stream->Read32(&version);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (version != XBLBinding_Serialize_Version) {
|
|
|
|
// The version that exists is different than expected, likely created with a
|
|
|
|
// different build, so invalidate the cache.
|
|
|
|
startupCache->InvalidateCache();
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
nsContentUtils::GetSecurityManager()->
|
|
|
|
GetSystemPrincipal(getter_AddRefs(principal));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc;
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = NS_NewXBLDocument(getter_AddRefs(domdoc), aURI, nullptr, principal);
|
2011-11-04 00:39:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
|
2012-03-23 00:46:03 +04:00
|
|
|
NS_ASSERTION(doc, "Must have a document!");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsXBLDocumentInfo> docInfo = new nsXBLDocumentInfo(doc);
|
2011-11-04 00:39:08 +04:00
|
|
|
|
|
|
|
while (1) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t flags;
|
2011-11-04 00:39:08 +04:00
|
|
|
nsresult rv = stream->Read8(&flags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (flags == XBLBinding_Serialize_NoMoreBindings)
|
|
|
|
break;
|
|
|
|
|
2014-02-27 22:04:10 +04:00
|
|
|
rv = nsXBLPrototypeBinding::ReadNewBinding(stream, docInfo, doc, flags);
|
2011-11-04 00:39:08 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
docInfo.swap(*aDocInfo);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsXBLDocumentInfo::WritePrototypeBindings()
|
|
|
|
{
|
|
|
|
// Only write out bindings with the system principal
|
|
|
|
if (!nsContentUtils::IsSystemPrincipal(mDocument->NodePrincipal()))
|
|
|
|
return NS_OK;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec(kXBLCachePrefix);
|
2011-11-04 00:39:08 +04:00
|
|
|
nsresult rv = PathifyURI(DocumentURI(), spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
StartupCache* startupCache = StartupCache::GetSingleton();
|
|
|
|
NS_ENSURE_TRUE(startupCache, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObjectOutputStream> stream;
|
|
|
|
nsCOMPtr<nsIStorageStream> storageStream;
|
|
|
|
rv = NewObjectOutputWrappedStorageStream(getter_AddRefs(stream),
|
|
|
|
getter_AddRefs(storageStream),
|
|
|
|
true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stream->Write32(XBLBinding_Serialize_Version);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-02-26 08:32:48 +04:00
|
|
|
if (mBindingTable) {
|
2015-11-16 01:52:57 +03:00
|
|
|
for (auto iter = mBindingTable->Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
iter.UserData()->Write(stream);
|
|
|
|
}
|
2014-02-26 08:32:48 +04:00
|
|
|
}
|
2011-11-04 00:39:08 +04:00
|
|
|
|
|
|
|
// write a end marker at the end
|
|
|
|
rv = stream->Write8(XBLBinding_Serialize_NoMoreBindings);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
stream->Close();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len;
|
2016-02-18 20:04:40 +03:00
|
|
|
UniquePtr<char[]> buf;
|
|
|
|
rv = NewBufferFromStorageStream(storageStream, &buf, &len);
|
2011-11-04 00:39:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-02-18 20:04:40 +03:00
|
|
|
return startupCache->PutBuffer(spec.get(), buf.get(), len);
|
2011-11-04 00:39:08 +04:00
|
|
|
}
|
|
|
|
|
2010-07-15 05:55:54 +04:00
|
|
|
void
|
2007-02-24 19:27:51 +03:00
|
|
|
nsXBLDocumentInfo::SetFirstPrototypeBinding(nsXBLPrototypeBinding* aBinding)
|
|
|
|
{
|
|
|
|
mFirstBinding = aBinding;
|
|
|
|
}
|
|
|
|
|
2010-07-15 05:55:54 +04:00
|
|
|
void
|
2001-12-18 01:51:39 +03:00
|
|
|
nsXBLDocumentInfo::FlushSkinStylesheets()
|
|
|
|
{
|
2014-02-26 08:32:48 +04:00
|
|
|
if (mBindingTable) {
|
2015-11-16 01:52:57 +03:00
|
|
|
for (auto iter = mBindingTable->Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
iter.UserData()->FlushSkinSheets();
|
|
|
|
}
|
2014-02-26 08:32:48 +04:00
|
|
|
}
|
2001-12-18 01:51:39 +03:00
|
|
|
}
|
|
|
|
|
2013-08-09 20:25:12 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
AssertInCompilationScope()
|
|
|
|
{
|
|
|
|
AutoJSContext cx;
|
2014-08-18 21:57:30 +04:00
|
|
|
MOZ_ASSERT(xpc::CompilationScope() == JS::CurrentGlobalOrNull(cx));
|
2013-08-09 20:25:12 +04:00
|
|
|
}
|
|
|
|
#endif
|