1999-05-18 23:15:10 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2004-05-13 18:34:18 +00:00
|
|
|
* vim: ft=cpp tw=78 sw=2 et ts=2
|
1999-05-18 23:15:10 +00:00
|
|
|
*
|
2012-05-21 11:12:37 +00: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-12-03 05:48:14 +00:00
|
|
|
*
|
2010-03-02 20:59:32 +00:00
|
|
|
* This Original Code has been modified by IBM Corporation.
|
|
|
|
* Modifications made by IBM described herein are Copyright (c)
|
|
|
|
* International Business Machines Corporation, 2000. Modifications
|
|
|
|
* to Mozilla code or documentation identified per MPL Section 3.3
|
2000-05-02 23:07:11 +00:00
|
|
|
*
|
|
|
|
* Date Modified by Description of modification
|
|
|
|
* 04/20/2000 IBM Corp. OS/2 VisualAge build.
|
1999-05-18 23:15:10 +00:00
|
|
|
*/
|
2006-03-25 05:47:31 +00:00
|
|
|
|
|
|
|
/* loading of CSS style sheets using the network APIs */
|
|
|
|
|
2013-12-09 02:52:54 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-06-23 12:03:39 +00:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2011-10-11 05:50:08 +00:00
|
|
|
|
2010-06-28 22:49:35 +00:00
|
|
|
#include "mozilla/css/Loader.h"
|
2010-03-02 20:59:32 +00:00
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsIUnicharStreamLoader.h"
|
2011-05-19 01:22:59 +00:00
|
|
|
#include "nsSyncLoadService.h"
|
2010-03-02 20:59:32 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
1999-05-26 23:52:37 +00:00
|
|
|
#include "nsIContent.h"
|
2011-05-26 08:18:43 +00:00
|
|
|
#include "nsIDocument.h"
|
1999-05-18 23:15:10 +00:00
|
|
|
#include "nsIDOMNode.h"
|
2011-05-26 08:18:43 +00:00
|
|
|
#include "nsIDOMDocument.h"
|
2005-04-14 15:27:16 +00:00
|
|
|
#include "nsIURI.h"
|
1999-11-30 04:50:42 +00:00
|
|
|
#include "nsNetUtil.h"
|
2003-10-30 03:01:25 +00:00
|
|
|
#include "nsContentUtils.h"
|
2000-05-26 23:28:40 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2003-04-04 23:57:06 +00:00
|
|
|
#include "nsContentPolicyUtils.h"
|
2001-10-25 00:34:20 +00:00
|
|
|
#include "nsIHttpChannel.h"
|
2012-12-04 23:06:29 +00:00
|
|
|
#include "nsIHttpChannelInternal.h"
|
2002-07-02 03:11:57 +00:00
|
|
|
#include "nsIScriptError.h"
|
2002-11-06 22:29:17 +00:00
|
|
|
#include "nsMimeTypes.h"
|
2010-05-11 20:41:47 +00:00
|
|
|
#include "nsCSSStyleSheet.h"
|
2005-04-14 15:27:16 +00:00
|
|
|
#include "nsIStyleSheetLinkingElement.h"
|
|
|
|
#include "nsICSSLoaderObserver.h"
|
2010-03-02 20:59:32 +00:00
|
|
|
#include "nsCSSParser.h"
|
2011-03-17 17:41:52 +00:00
|
|
|
#include "mozilla/css/ImportRule.h"
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2006-12-26 17:47:52 +00:00
|
|
|
#include "nsGkAtoms.h"
|
2011-09-26 21:27:13 +00:00
|
|
|
#include "nsIThreadInternal.h"
|
2012-08-28 17:10:08 +00:00
|
|
|
#include "nsCrossSiteListenerProxy.h"
|
2013-10-25 21:56:56 +00:00
|
|
|
#include "nsINetworkSeer.h"
|
2013-12-02 10:26:12 +00:00
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2013-11-20 16:29:03 +00:00
|
|
|
#include "mozilla/dom/URL.h"
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2003-04-11 00:56:27 +00:00
|
|
|
#ifdef MOZ_XUL
|
2007-03-12 05:53:33 +00:00
|
|
|
#include "nsXULPrototypeCache.h"
|
2000-07-28 09:36:51 +00:00
|
|
|
#endif
|
|
|
|
|
2005-03-30 00:36:57 +00:00
|
|
|
#include "nsIMediaList.h"
|
2001-08-04 21:19:53 +00:00
|
|
|
#include "nsIDOMStyleSheet.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2002-12-03 05:48:14 +00:00
|
|
|
|
2010-04-23 22:05:32 +00:00
|
|
|
#include "nsIChannelPolicy.h"
|
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2012-10-08 02:39:09 +00:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2010-04-23 22:05:32 +00:00
|
|
|
|
2012-11-15 08:47:30 +00:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
|
|
|
using mozilla::dom::EncodingUtils;
|
2010-05-19 23:22:19 +00:00
|
|
|
|
2013-01-08 23:25:47 +00:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2010-03-02 20:59:32 +00:00
|
|
|
/**
|
|
|
|
* OVERALL ARCHITECTURE
|
|
|
|
*
|
|
|
|
* The CSS Loader gets requests to load various sorts of style sheets:
|
|
|
|
* inline style from <style> elements, linked style, @import-ed child
|
|
|
|
* sheets, non-document sheets. The loader handles the following tasks:
|
|
|
|
*
|
|
|
|
* 1) Checking whether the load is allowed: CheckLoadAllowed()
|
|
|
|
* 2) Creation of the actual style sheet objects: CreateSheet()
|
|
|
|
* 3) setting of the right media, title, enabled state, etc on the
|
|
|
|
* sheet: PrepareSheet()
|
|
|
|
* 4) Insertion of the sheet in the proper cascade order:
|
|
|
|
* InsertSheetInDoc() and InsertChildSheet()
|
|
|
|
* 5) Load of the sheet: LoadSheet()
|
|
|
|
* 6) Parsing of the sheet: ParseSheet()
|
|
|
|
* 7) Cleanup: SheetComplete()
|
|
|
|
*
|
|
|
|
* The detailed documentation for these functions is found with the
|
|
|
|
* function implementations.
|
|
|
|
*
|
|
|
|
* The following helper object is used:
|
|
|
|
* SheetLoadData -- a small class that is used to store all the
|
|
|
|
* information needed for the loading of a sheet;
|
|
|
|
* this class handles listening for the stream
|
|
|
|
* loader completion and also handles charset
|
|
|
|
* determination.
|
|
|
|
*/
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace css {
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
/*********************************************
|
|
|
|
* Data needed to properly load a stylesheet *
|
|
|
|
*********************************************/
|
|
|
|
|
|
|
|
class SheetLoadData : public nsIRunnable,
|
2011-09-26 21:27:13 +00:00
|
|
|
public nsIUnicharStreamLoaderObserver,
|
|
|
|
public nsIThreadObserver
|
2010-03-02 20:59:32 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~SheetLoadData(void);
|
|
|
|
// Data for loading a sheet linked from a document
|
2010-03-02 21:00:53 +00:00
|
|
|
SheetLoadData(Loader* aLoader,
|
|
|
|
const nsSubstring& aTitle,
|
|
|
|
nsIURI* aURI,
|
2010-05-11 20:41:47 +00:00
|
|
|
nsCSSStyleSheet* aSheet,
|
2010-03-02 21:00:53 +00:00
|
|
|
nsIStyleSheetLinkingElement* aOwningElement,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aIsAlternate,
|
2010-03-02 21:00:53 +00:00
|
|
|
nsICSSLoaderObserver* aObserver,
|
|
|
|
nsIPrincipal* aLoaderPrincipal);
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
// Data for loading a sheet linked from an @import rule
|
2010-03-02 21:00:53 +00:00
|
|
|
SheetLoadData(Loader* aLoader,
|
|
|
|
nsIURI* aURI,
|
2010-05-11 20:41:47 +00:00
|
|
|
nsCSSStyleSheet* aSheet,
|
2010-03-02 21:00:53 +00:00
|
|
|
SheetLoadData* aParentData,
|
|
|
|
nsICSSLoaderObserver* aObserver,
|
|
|
|
nsIPrincipal* aLoaderPrincipal);
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
// Data for loading a non-document sheet
|
2010-03-02 21:00:53 +00:00
|
|
|
SheetLoadData(Loader* aLoader,
|
|
|
|
nsIURI* aURI,
|
2010-05-11 20:41:47 +00:00
|
|
|
nsCSSStyleSheet* aSheet,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aSyncLoad,
|
|
|
|
bool aAllowUnsafeRules,
|
|
|
|
bool aUseSystemPrincipal,
|
2010-03-02 21:00:53 +00:00
|
|
|
const nsCString& aCharset,
|
|
|
|
nsICSSLoaderObserver* aObserver,
|
|
|
|
nsIPrincipal* aLoaderPrincipal);
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
already_AddRefed<nsIURI> GetReferrerURI();
|
|
|
|
|
2011-09-26 21:27:13 +00:00
|
|
|
void ScheduleLoadEventIfNeeded(nsresult aStatus);
|
|
|
|
|
2010-03-02 20:59:32 +00:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIRUNNABLE
|
2011-09-26 21:27:13 +00:00
|
|
|
NS_DECL_NSITHREADOBSERVER
|
2010-03-02 20:59:32 +00:00
|
|
|
NS_DECL_NSIUNICHARSTREAMLOADEROBSERVER
|
|
|
|
|
|
|
|
// Hold a ref to the CSSLoader so we can call back to it to let it
|
|
|
|
// know the load finished
|
2010-03-02 21:00:53 +00:00
|
|
|
Loader* mLoader; // strong ref
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
// Title needed to pull datas out of the pending datas table when
|
|
|
|
// the preferred title is changed
|
|
|
|
nsString mTitle;
|
|
|
|
|
|
|
|
// Charset we decided to use for the sheet
|
|
|
|
nsCString mCharset;
|
|
|
|
|
|
|
|
// URI we're loading. Null for inline sheets
|
|
|
|
nsCOMPtr<nsIURI> mURI;
|
|
|
|
|
|
|
|
// Should be 1 for non-inline sheets.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mLineNumber;
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
// The sheet we're loading data for
|
2010-05-11 20:41:47 +00:00
|
|
|
nsRefPtr<nsCSSStyleSheet> mSheet;
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
// Linked list of datas for the same URI as us
|
|
|
|
SheetLoadData* mNext; // strong ref
|
|
|
|
|
|
|
|
// Load data for the sheet that @import-ed us if we were @import-ed
|
|
|
|
// during the parse
|
|
|
|
SheetLoadData* mParentData; // strong ref
|
|
|
|
|
|
|
|
// Number of sheets we @import-ed that are still loading
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mPendingChildren;
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
// mSyncLoad is true when the load needs to be synchronous -- right
|
|
|
|
// now only for LoadSheetSync and children of sync loads.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mSyncLoad : 1;
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
// mIsNonDocumentSheet is true if the load was triggered by LoadSheetSync or
|
|
|
|
// LoadSheet or an @import from such a sheet. Non-document sheet loads can
|
|
|
|
// proceed even if we have no document.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mIsNonDocumentSheet : 1;
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
// mIsLoading is true from the moment we are placed in the loader's
|
|
|
|
// "loading datas" table (right after the async channel is opened)
|
|
|
|
// to the moment we are removed from said table (due to the load
|
|
|
|
// completing or being cancelled).
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mIsLoading : 1;
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
// mIsCancelled is set to true when a sheet load is stopped by
|
2010-04-03 11:36:19 +00:00
|
|
|
// Stop() or StopLoadingSheet() (which was removed in Bug 556446).
|
2011-05-06 15:45:36 +00:00
|
|
|
// SheetLoadData::OnStreamComplete() checks this to avoid parsing
|
2010-04-03 11:36:19 +00:00
|
|
|
// sheets that have been cancelled and such.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mIsCancelled : 1;
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
// mMustNotify is true if the load data is being loaded async and
|
|
|
|
// the original function call that started the load has returned.
|
2011-09-26 21:27:00 +00:00
|
|
|
// This applies only to observer notifications; load/error events
|
|
|
|
// are fired for any SheetLoadData that has a non-null
|
|
|
|
// mOwningElement.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mMustNotify : 1;
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
// mWasAlternate is true if the sheet was an alternate when the load data was
|
|
|
|
// created.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mWasAlternate : 1;
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
// mAllowUnsafeRules is true if we should allow unsafe rules to be parsed
|
|
|
|
// in the loaded sheet.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mAllowUnsafeRules : 1;
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
// mUseSystemPrincipal is true if the system principal should be used for
|
|
|
|
// this sheet, no matter what the channel principal is. Only true for sync
|
|
|
|
// loads.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mUseSystemPrincipal : 1;
|
2010-03-02 20:59:32 +00:00
|
|
|
|
2011-09-26 21:26:54 +00:00
|
|
|
// If true, this SheetLoadData is being used as a way to handle
|
|
|
|
// async observer notification for an already-complete sheet.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mSheetAlreadyComplete : 1;
|
2011-09-26 21:26:54 +00:00
|
|
|
|
2010-03-02 20:59:32 +00:00
|
|
|
// This is the element that imported the sheet. Needed to get the
|
2011-09-26 21:27:00 +00:00
|
|
|
// charset set on it and to fire load/error events.
|
2010-03-02 20:59:32 +00:00
|
|
|
nsCOMPtr<nsIStyleSheetLinkingElement> mOwningElement;
|
|
|
|
|
|
|
|
// The observer that wishes to be notified of load completion
|
|
|
|
nsCOMPtr<nsICSSLoaderObserver> mObserver;
|
|
|
|
|
|
|
|
// The principal that identifies who started loading us.
|
2010-03-02 21:00:53 +00:00
|
|
|
nsCOMPtr<nsIPrincipal> mLoaderPrincipal;
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
// The charset to use if the transport and sheet don't indicate one.
|
|
|
|
// May be empty. Must be empty if mOwningElement is non-null.
|
2010-03-02 21:00:53 +00:00
|
|
|
nsCString mCharsetHint;
|
2011-09-26 21:27:13 +00:00
|
|
|
|
|
|
|
// The status our load ended up with; this determines whether we
|
|
|
|
// should fire error events or load events. This gets initialized
|
|
|
|
// by ScheduleLoadEventIfNeeded, and is only used after that has
|
|
|
|
// been called.
|
|
|
|
nsresult mStatus;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void FireLoadEvent(nsIThreadInternal* aThread);
|
2010-03-02 20:59:32 +00:00
|
|
|
};
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
#ifdef MOZ_LOGGING
|
|
|
|
// #define FORCE_PR_LOG /* Allow logging in the release build */
|
|
|
|
#endif /* MOZ_LOGGING */
|
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2012-10-29 23:32:10 +00:00
|
|
|
static PRLogModuleInfo *
|
|
|
|
GetLoaderLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("nsCSSLoader");
|
|
|
|
return sLog;
|
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
#endif /* PR_LOGGING */
|
|
|
|
|
2012-10-29 23:32:10 +00:00
|
|
|
#define LOG_FORCE(args) PR_LOG(GetLoaderLog(), PR_LOG_ALWAYS, args)
|
|
|
|
#define LOG_ERROR(args) PR_LOG(GetLoaderLog(), PR_LOG_ERROR, args)
|
|
|
|
#define LOG_WARN(args) PR_LOG(GetLoaderLog(), PR_LOG_WARNING, args)
|
|
|
|
#define LOG_DEBUG(args) PR_LOG(GetLoaderLog(), PR_LOG_DEBUG, args)
|
2002-12-03 05:48:14 +00:00
|
|
|
#define LOG(args) LOG_DEBUG(args)
|
|
|
|
|
2012-10-29 23:32:10 +00:00
|
|
|
#define LOG_FORCE_ENABLED() PR_LOG_TEST(GetLoaderLog(), PR_LOG_ALWAYS)
|
|
|
|
#define LOG_ERROR_ENABLED() PR_LOG_TEST(GetLoaderLog(), PR_LOG_ERROR)
|
|
|
|
#define LOG_WARN_ENABLED() PR_LOG_TEST(GetLoaderLog(), PR_LOG_WARNING)
|
|
|
|
#define LOG_DEBUG_ENABLED() PR_LOG_TEST(GetLoaderLog(), PR_LOG_DEBUG)
|
2002-12-03 05:48:14 +00:00
|
|
|
#define LOG_ENABLED() LOG_DEBUG_ENABLED()
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
#define LOG_URI(format, uri) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
NS_ASSERTION(uri, "Logging null uri"); \
|
|
|
|
if (LOG_ENABLED()) { \
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString _logURISpec; \
|
2002-12-03 05:48:14 +00:00
|
|
|
uri->GetSpec(_logURISpec); \
|
|
|
|
LOG((format, _logURISpec.get())); \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
#else // PR_LOGGING
|
|
|
|
#define LOG_URI(format, uri)
|
|
|
|
#endif // PR_LOGGING
|
2001-08-04 21:19:53 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
// And some convenience strings...
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
static const char* const gStateStrings[] = {
|
|
|
|
"eSheetStateUnknown",
|
|
|
|
"eSheetNeedsParser",
|
|
|
|
"eSheetPending",
|
|
|
|
"eSheetLoading",
|
|
|
|
"eSheetComplete"
|
1999-05-18 23:15:10 +00:00
|
|
|
};
|
2002-12-03 05:48:14 +00:00
|
|
|
#endif
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
/********************************
|
|
|
|
* SheetLoadData implementation *
|
|
|
|
********************************/
|
2011-09-26 21:27:13 +00:00
|
|
|
NS_IMPL_ISUPPORTS3(SheetLoadData, nsIUnicharStreamLoaderObserver, nsIRunnable,
|
|
|
|
nsIThreadObserver)
|
2002-12-03 05:48:14 +00:00
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
SheetLoadData::SheetLoadData(Loader* aLoader,
|
2005-03-30 00:36:57 +00:00
|
|
|
const nsSubstring& aTitle,
|
2002-12-03 05:48:14 +00:00
|
|
|
nsIURI* aURI,
|
2010-05-11 20:41:47 +00:00
|
|
|
nsCSSStyleSheet* aSheet,
|
2002-12-03 05:48:14 +00:00
|
|
|
nsIStyleSheetLinkingElement* aOwningElement,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aIsAlternate,
|
2007-05-01 19:26:15 +00:00
|
|
|
nsICSSLoaderObserver* aObserver,
|
|
|
|
nsIPrincipal* aLoaderPrincipal)
|
1999-05-18 23:15:10 +00:00
|
|
|
: mLoader(aLoader),
|
|
|
|
mTitle(aTitle),
|
2002-12-03 05:48:14 +00:00
|
|
|
mURI(aURI),
|
2004-09-09 17:32:35 +00:00
|
|
|
mLineNumber(1),
|
2002-12-03 05:48:14 +00:00
|
|
|
mSheet(aSheet),
|
2012-07-30 14:20:58 +00:00
|
|
|
mNext(nullptr),
|
|
|
|
mParentData(nullptr),
|
1999-05-18 23:15:10 +00:00
|
|
|
mPendingChildren(0),
|
2011-10-17 14:59:28 +00:00
|
|
|
mSyncLoad(false),
|
|
|
|
mIsNonDocumentSheet(false),
|
|
|
|
mIsLoading(false),
|
|
|
|
mIsCancelled(false),
|
|
|
|
mMustNotify(false),
|
2005-09-12 18:41:15 +00:00
|
|
|
mWasAlternate(aIsAlternate),
|
2011-10-17 14:59:28 +00:00
|
|
|
mAllowUnsafeRules(false),
|
|
|
|
mUseSystemPrincipal(false),
|
|
|
|
mSheetAlreadyComplete(false),
|
2002-12-03 05:48:14 +00:00
|
|
|
mOwningElement(aOwningElement),
|
2007-05-01 19:26:15 +00:00
|
|
|
mObserver(aObserver),
|
|
|
|
mLoaderPrincipal(aLoaderPrincipal)
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_PRECONDITION(mLoader, "Must have a loader!");
|
1999-05-18 23:15:10 +00:00
|
|
|
NS_ADDREF(mLoader);
|
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
SheetLoadData::SheetLoadData(Loader* aLoader,
|
2002-12-03 05:48:14 +00:00
|
|
|
nsIURI* aURI,
|
2010-05-11 20:41:47 +00:00
|
|
|
nsCSSStyleSheet* aSheet,
|
2002-12-03 05:48:14 +00:00
|
|
|
SheetLoadData* aParentData,
|
2007-05-01 19:26:15 +00:00
|
|
|
nsICSSLoaderObserver* aObserver,
|
|
|
|
nsIPrincipal* aLoaderPrincipal)
|
1999-05-18 23:15:10 +00:00
|
|
|
: mLoader(aLoader),
|
2002-12-03 05:48:14 +00:00
|
|
|
mURI(aURI),
|
2004-09-09 17:32:35 +00:00
|
|
|
mLineNumber(1),
|
2002-12-03 05:48:14 +00:00
|
|
|
mSheet(aSheet),
|
2012-07-30 14:20:58 +00:00
|
|
|
mNext(nullptr),
|
2002-12-03 05:48:14 +00:00
|
|
|
mParentData(aParentData),
|
1999-05-18 23:15:10 +00:00
|
|
|
mPendingChildren(0),
|
2011-10-17 14:59:28 +00:00
|
|
|
mSyncLoad(false),
|
|
|
|
mIsNonDocumentSheet(false),
|
|
|
|
mIsLoading(false),
|
|
|
|
mIsCancelled(false),
|
|
|
|
mMustNotify(false),
|
|
|
|
mWasAlternate(false),
|
|
|
|
mAllowUnsafeRules(false),
|
|
|
|
mUseSystemPrincipal(false),
|
|
|
|
mSheetAlreadyComplete(false),
|
2012-07-30 14:20:58 +00:00
|
|
|
mOwningElement(nullptr),
|
2007-05-01 19:26:15 +00:00
|
|
|
mObserver(aObserver),
|
|
|
|
mLoaderPrincipal(aLoaderPrincipal)
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_PRECONDITION(mLoader, "Must have a loader!");
|
1999-05-18 23:15:10 +00:00
|
|
|
NS_ADDREF(mLoader);
|
2002-12-03 05:48:14 +00:00
|
|
|
if (mParentData) {
|
|
|
|
NS_ADDREF(mParentData);
|
|
|
|
mSyncLoad = mParentData->mSyncLoad;
|
2005-09-12 18:41:15 +00:00
|
|
|
mIsNonDocumentSheet = mParentData->mIsNonDocumentSheet;
|
2006-06-15 03:20:19 +00:00
|
|
|
mAllowUnsafeRules = mParentData->mAllowUnsafeRules;
|
2009-03-17 01:38:36 +00:00
|
|
|
mUseSystemPrincipal = mParentData->mUseSystemPrincipal;
|
2002-12-03 05:48:14 +00:00
|
|
|
++(mParentData->mPendingChildren);
|
|
|
|
}
|
2009-03-17 01:38:36 +00:00
|
|
|
|
|
|
|
NS_POSTCONDITION(!mUseSystemPrincipal || mSyncLoad,
|
|
|
|
"Shouldn't use system principal for async loads");
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
SheetLoadData::SheetLoadData(Loader* aLoader,
|
2002-12-03 05:48:14 +00:00
|
|
|
nsIURI* aURI,
|
2010-05-11 20:41:47 +00:00
|
|
|
nsCSSStyleSheet* aSheet,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aSyncLoad,
|
|
|
|
bool aAllowUnsafeRules,
|
|
|
|
bool aUseSystemPrincipal,
|
2009-04-10 14:29:08 +00:00
|
|
|
const nsCString& aCharset,
|
2007-07-11 19:44:27 +00:00
|
|
|
nsICSSLoaderObserver* aObserver,
|
|
|
|
nsIPrincipal* aLoaderPrincipal)
|
1999-05-26 23:52:37 +00:00
|
|
|
: mLoader(aLoader),
|
2002-12-03 05:48:14 +00:00
|
|
|
mURI(aURI),
|
2004-09-09 17:32:35 +00:00
|
|
|
mLineNumber(1),
|
2002-12-03 05:48:14 +00:00
|
|
|
mSheet(aSheet),
|
2012-07-30 14:20:58 +00:00
|
|
|
mNext(nullptr),
|
|
|
|
mParentData(nullptr),
|
1999-05-26 23:52:37 +00:00
|
|
|
mPendingChildren(0),
|
2002-12-11 00:28:47 +00:00
|
|
|
mSyncLoad(aSyncLoad),
|
2011-10-17 14:59:28 +00:00
|
|
|
mIsNonDocumentSheet(true),
|
|
|
|
mIsLoading(false),
|
|
|
|
mIsCancelled(false),
|
|
|
|
mMustNotify(false),
|
|
|
|
mWasAlternate(false),
|
2006-06-15 03:20:19 +00:00
|
|
|
mAllowUnsafeRules(aAllowUnsafeRules),
|
2009-03-17 01:38:36 +00:00
|
|
|
mUseSystemPrincipal(aUseSystemPrincipal),
|
2011-10-17 14:59:28 +00:00
|
|
|
mSheetAlreadyComplete(false),
|
2012-07-30 14:20:58 +00:00
|
|
|
mOwningElement(nullptr),
|
2007-05-01 19:26:15 +00:00
|
|
|
mObserver(aObserver),
|
2009-04-10 14:29:08 +00:00
|
|
|
mLoaderPrincipal(aLoaderPrincipal),
|
|
|
|
mCharsetHint(aCharset)
|
1999-05-26 23:52:37 +00:00
|
|
|
{
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_PRECONDITION(mLoader, "Must have a loader!");
|
1999-05-26 23:52:37 +00:00
|
|
|
NS_ADDREF(mLoader);
|
2009-03-17 01:38:36 +00:00
|
|
|
|
|
|
|
NS_POSTCONDITION(!mUseSystemPrincipal || mSyncLoad,
|
|
|
|
"Shouldn't use system principal for async loads");
|
1999-05-26 23:52:37 +00:00
|
|
|
}
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
SheetLoadData::~SheetLoadData()
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
|
|
|
NS_RELEASE(mLoader);
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_IF_RELEASE(mParentData);
|
2000-05-17 18:29:30 +00:00
|
|
|
NS_IF_RELEASE(mNext);
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SheetLoadData::Run()
|
|
|
|
{
|
|
|
|
mLoader->HandleLoadEvent(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-26 21:27:13 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SheetLoadData::OnDispatchedEvent(nsIThreadInternal* aThread)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SheetLoadData::OnProcessNextEvent(nsIThreadInternal* aThread,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aMayWait,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aRecursionDepth)
|
2011-09-26 21:27:13 +00:00
|
|
|
{
|
|
|
|
// We want to fire our load even before or after event processing,
|
|
|
|
// whichever comes first.
|
|
|
|
FireLoadEvent(aThread);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SheetLoadData::AfterProcessNextEvent(nsIThreadInternal* aThread,
|
2013-10-23 12:01:20 +00:00
|
|
|
uint32_t aRecursionDepth,
|
|
|
|
bool aEventWasProcessed)
|
2011-09-26 21:27:13 +00:00
|
|
|
{
|
|
|
|
// We want to fire our load even before or after event processing,
|
|
|
|
// whichever comes first.
|
|
|
|
FireLoadEvent(aThread);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SheetLoadData::FireLoadEvent(nsIThreadInternal* aThread)
|
|
|
|
{
|
|
|
|
|
|
|
|
// First remove ourselves as a thread observer. But we need to keep
|
|
|
|
// ourselves alive while doing that!
|
|
|
|
nsRefPtr<SheetLoadData> kungFuDeathGrip(this);
|
|
|
|
aThread->RemoveObserver(this);
|
|
|
|
|
|
|
|
// Now fire the event
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(mOwningElement);
|
|
|
|
NS_ASSERTION(node, "How did that happen???");
|
|
|
|
|
2011-10-18 10:53:36 +00:00
|
|
|
nsContentUtils::DispatchTrustedEvent(node->OwnerDoc(),
|
2011-09-26 21:27:13 +00:00
|
|
|
node,
|
|
|
|
NS_SUCCEEDED(mStatus) ?
|
|
|
|
NS_LITERAL_STRING("load") :
|
|
|
|
NS_LITERAL_STRING("error"),
|
2011-10-17 14:59:28 +00:00
|
|
|
false, false);
|
2011-09-26 21:27:13 +00:00
|
|
|
|
|
|
|
// And unblock onload
|
|
|
|
if (mLoader->mDocument) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mLoader->mDocument->UnblockOnload(true);
|
2011-09-26 21:27:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SheetLoadData::ScheduleLoadEventIfNeeded(nsresult aStatus)
|
|
|
|
{
|
|
|
|
if (!mOwningElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mStatus = aStatus;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> thread = do_GetMainThread();
|
|
|
|
nsCOMPtr<nsIThreadInternal> internalThread = do_QueryInterface(thread);
|
|
|
|
if (NS_SUCCEEDED(internalThread->AddObserver(this))) {
|
|
|
|
// Make sure to block onload here
|
|
|
|
if (mLoader->mDocument) {
|
|
|
|
mLoader->mDocument->BlockOnload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
/*************************
|
|
|
|
* Loader Implementation *
|
|
|
|
*************************/
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
Loader::Loader(void)
|
2012-07-30 14:20:58 +00:00
|
|
|
: mDocument(nullptr)
|
2010-03-02 21:00:53 +00:00
|
|
|
, mDatasToNotifyOn(0)
|
|
|
|
, mCompatMode(eCompatibility_FullStandards)
|
2011-10-17 14:59:28 +00:00
|
|
|
, mEnabled(true)
|
2010-03-02 21:00:53 +00:00
|
|
|
#ifdef DEBUG
|
2011-10-17 14:59:28 +00:00
|
|
|
, mSyncCallback(false)
|
2010-03-02 21:00:53 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Loader::Loader(nsIDocument* aDocument)
|
|
|
|
: mDocument(aDocument)
|
|
|
|
, mDatasToNotifyOn(0)
|
|
|
|
, mCompatMode(eCompatibility_FullStandards)
|
2011-10-17 14:59:28 +00:00
|
|
|
, mEnabled(true)
|
2010-03-02 21:00:53 +00:00
|
|
|
#ifdef DEBUG
|
2011-10-17 14:59:28 +00:00
|
|
|
, mSyncCallback(false)
|
2010-03-02 21:00:53 +00:00
|
|
|
#endif
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
// We can just use the preferred set, since there are no sheets in the
|
|
|
|
// document yet (if there are, how did they get there? _we_ load the sheets!)
|
|
|
|
// and hence the selected set makes no sense at this time.
|
2011-05-26 08:18:43 +00:00
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(mDocument);
|
2010-03-02 21:00:53 +00:00
|
|
|
if (domDoc) {
|
|
|
|
domDoc->GetPreferredStyleSheetSet(mPreferredSheet);
|
|
|
|
}
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
Loader::~Loader()
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
2013-09-02 08:41:57 +00:00
|
|
|
NS_ASSERTION(!mSheets || mSheets->mLoadingDatas.Count() == 0,
|
2002-12-03 05:48:14 +00:00
|
|
|
"How did we get destroyed when there are loading data?");
|
2013-09-02 08:41:57 +00:00
|
|
|
NS_ASSERTION(!mSheets || mSheets->mPendingDatas.Count() == 0,
|
2002-12-03 05:48:14 +00:00
|
|
|
"How did we get destroyed when there are pending data?");
|
2005-09-12 18:41:15 +00:00
|
|
|
// Note: no real need to revoke our stylesheet loaded events -- they
|
|
|
|
// hold strong references to us, so if we're going away that means
|
|
|
|
// they're all done.
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
void
|
|
|
|
Loader::DropDocumentReference(void)
|
1999-05-26 23:52:37 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
mDocument = nullptr;
|
2002-12-03 05:48:14 +00:00
|
|
|
// Flush out pending datas just so we don't leak by accident. These
|
|
|
|
// loads should short-circuit through the mDocument check in
|
|
|
|
// LoadSheet and just end up in SheetComplete immediately
|
2013-09-02 08:41:57 +00:00
|
|
|
if (mSheets) {
|
2007-05-31 01:42:48 +00:00
|
|
|
StartAlternateLoads();
|
|
|
|
}
|
2010-03-02 20:59:32 +00:00
|
|
|
}
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashOperator
|
2012-08-28 17:10:08 +00:00
|
|
|
CollectNonAlternates(URIPrincipalAndCORSModeHashKey *aKey,
|
2007-05-31 01:42:48 +00:00
|
|
|
SheetLoadData* &aData,
|
|
|
|
void* aClosure)
|
2002-12-03 05:48:14 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aData, "Must have a data");
|
2007-05-31 01:42:48 +00:00
|
|
|
NS_PRECONDITION(aClosure, "Must have an array");
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2005-09-12 18:41:15 +00:00
|
|
|
// Note that we don't want to affect what the selected style set is,
|
2011-10-17 14:59:28 +00:00
|
|
|
// so use true for aHasAlternateRel.
|
|
|
|
if (aData->mLoader->IsAlternate(aData->mTitle, true)) {
|
2003-07-27 15:20:11 +00:00
|
|
|
return PL_DHASH_NEXT;
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
static_cast<Loader::LoadDataArray*>(aClosure)->AppendElement(aData);
|
2003-07-27 15:20:11 +00:00
|
|
|
return PL_DHASH_REMOVE;
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
nsresult
|
|
|
|
Loader::SetPreferredSheet(const nsAString& aTitle)
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
2007-01-11 19:32:31 +00:00
|
|
|
#ifdef DEBUG
|
2011-05-26 08:18:43 +00:00
|
|
|
nsCOMPtr<nsIDOMDocument> doc = do_QueryInterface(mDocument);
|
2007-01-11 19:32:31 +00:00
|
|
|
if (doc) {
|
|
|
|
nsAutoString currentPreferred;
|
|
|
|
doc->GetLastStyleSheetSet(currentPreferred);
|
|
|
|
if (DOMStringIsNull(currentPreferred)) {
|
|
|
|
doc->GetPreferredStyleSheetSet(currentPreferred);
|
|
|
|
}
|
|
|
|
NS_ASSERTION(currentPreferred.Equals(aTitle),
|
2011-05-06 15:45:36 +00:00
|
|
|
"Unexpected argument to SetPreferredSheet");
|
2007-01-11 19:32:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
2011-05-06 15:45:36 +00:00
|
|
|
|
1999-05-18 23:15:10 +00:00
|
|
|
mPreferredSheet = aTitle;
|
|
|
|
|
|
|
|
// start any pending alternates that aren't alternates anymore
|
2013-09-02 08:41:57 +00:00
|
|
|
if (mSheets) {
|
|
|
|
LoadDataArray arr(mSheets->mPendingDatas.Count());
|
|
|
|
mSheets->mPendingDatas.Enumerate(CollectNonAlternates, &arr);
|
2007-05-31 01:42:48 +00:00
|
|
|
|
|
|
|
mDatasToNotifyOn += arr.Length();
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < arr.Length(); ++i) {
|
2007-05-31 01:42:48 +00:00
|
|
|
--mDatasToNotifyOn;
|
|
|
|
LoadSheet(arr[i], eSheetNeedsParser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-05-18 23:15:10 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-11-12 03:36:20 +00:00
|
|
|
static const char kCharsetSym[] = "@charset \"";
|
2002-08-27 23:47:25 +00:00
|
|
|
|
2012-11-15 08:47:30 +00:00
|
|
|
static bool GetCharsetFromData(const char* aStyleSheetData,
|
|
|
|
uint32_t aDataLength,
|
|
|
|
nsACString& aCharset)
|
2002-08-27 23:47:25 +00:00
|
|
|
{
|
|
|
|
aCharset.Truncate();
|
|
|
|
if (aDataLength <= sizeof(kCharsetSym) - 1)
|
2012-11-15 08:47:30 +00:00
|
|
|
return false;
|
2002-08-27 23:47:25 +00:00
|
|
|
|
2012-11-15 08:47:30 +00:00
|
|
|
if (strncmp(aStyleSheetData,
|
|
|
|
kCharsetSym,
|
|
|
|
sizeof(kCharsetSym) - 1)) {
|
|
|
|
return false;
|
2002-08-27 23:47:25 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 08:47:30 +00:00
|
|
|
for (uint32_t i = sizeof(kCharsetSym) - 1; i < aDataLength; ++i) {
|
|
|
|
char c = aStyleSheetData[i];
|
|
|
|
if (c == '"') {
|
|
|
|
++i;
|
|
|
|
if (i < aDataLength && aStyleSheetData[i] == ';') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// fail
|
2002-08-27 23:47:25 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-15 08:47:30 +00:00
|
|
|
aCharset.Append(c);
|
2002-08-27 23:47:25 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 08:47:30 +00:00
|
|
|
// Did not see end quote or semicolon
|
|
|
|
aCharset.Truncate();
|
|
|
|
return false;
|
2002-08-27 23:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
SheetLoadData::OnDetermineCharset(nsIUnicharStreamLoader* aLoader,
|
|
|
|
nsISupports* aContext,
|
2011-05-19 01:22:59 +00:00
|
|
|
nsACString const& aSegment,
|
2002-08-27 23:47:25 +00:00
|
|
|
nsACString& aCharset)
|
|
|
|
{
|
2009-04-10 14:29:08 +00:00
|
|
|
NS_PRECONDITION(!mOwningElement || mCharsetHint.IsEmpty(),
|
|
|
|
"Can't have element _and_ charset hint");
|
|
|
|
|
2004-01-13 19:01:26 +00:00
|
|
|
LOG_URI("SheetLoadData::OnDetermineCharset for '%s'", mURI);
|
2012-11-15 08:47:30 +00:00
|
|
|
|
|
|
|
// The precedence is (per CSS3 Syntax 2012-11-08 ED):
|
|
|
|
// BOM
|
|
|
|
// Channel
|
|
|
|
// @charset rule
|
|
|
|
// charset attribute on the referrer
|
|
|
|
// encoding of the referrer
|
|
|
|
// UTF-8
|
2002-08-27 23:47:25 +00:00
|
|
|
|
2003-06-23 05:04:43 +00:00
|
|
|
aCharset.Truncate();
|
|
|
|
|
2012-11-15 08:47:30 +00:00
|
|
|
if (nsContentUtils::CheckForBOM((const unsigned char*)aSegment.BeginReading(),
|
|
|
|
aSegment.Length(),
|
|
|
|
aCharset)) {
|
2013-04-16 12:19:29 +00:00
|
|
|
// aCharset is now either "UTF-16BE", "UTF-16BE" or "UTF-8"
|
|
|
|
// which will swallow the BOM.
|
2012-11-15 08:47:30 +00:00
|
|
|
mCharset.Assign(aCharset);
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
LOG((" Setting from BOM to: %s", PromiseFlatCString(aCharset).get()));
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
2002-08-27 23:47:25 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 08:47:30 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsAutoCString specified;
|
|
|
|
aLoader->GetChannel(getter_AddRefs(channel));
|
|
|
|
if (channel) {
|
|
|
|
channel->GetContentCharset(specified);
|
|
|
|
if (EncodingUtils::FindEncodingForLabel(specified, aCharset)) {
|
|
|
|
mCharset.Assign(aCharset);
|
2004-01-13 19:01:26 +00:00
|
|
|
#ifdef PR_LOGGING
|
2012-11-15 08:47:30 +00:00
|
|
|
LOG((" Setting from HTTP to: %s", PromiseFlatCString(aCharset).get()));
|
2003-06-23 05:04:43 +00:00
|
|
|
#endif
|
2012-11-15 08:47:30 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2002-08-27 23:47:25 +00:00
|
|
|
|
2012-11-15 08:47:30 +00:00
|
|
|
if (GetCharsetFromData(aSegment.BeginReading(),
|
|
|
|
aSegment.Length(),
|
|
|
|
specified)) {
|
|
|
|
if (EncodingUtils::FindEncodingForLabel(specified, aCharset)) {
|
|
|
|
// FindEncodingForLabel currently never returns UTF-16LE but will
|
|
|
|
// probably change to never return UTF-16 instead, so check both here
|
|
|
|
// to avoid relying on the exact behavior.
|
|
|
|
if (aCharset.EqualsLiteral("UTF-16") ||
|
|
|
|
aCharset.EqualsLiteral("UTF-16BE") ||
|
|
|
|
aCharset.EqualsLiteral("UTF-16LE")) {
|
|
|
|
// Be consistent with HTML <meta> handling in face of impossibility.
|
|
|
|
// When the @charset rule itself evidently was not UTF-16-encoded,
|
|
|
|
// it saying UTF-16 has to be a lie.
|
|
|
|
aCharset.AssignLiteral("UTF-8");
|
|
|
|
}
|
|
|
|
mCharset.Assign(aCharset);
|
2004-01-13 19:01:26 +00:00
|
|
|
#ifdef PR_LOGGING
|
2012-11-15 08:47:30 +00:00
|
|
|
LOG((" Setting from @charset rule to: %s",
|
|
|
|
PromiseFlatCString(aCharset).get()));
|
2003-06-23 05:04:43 +00:00
|
|
|
#endif
|
2012-11-15 08:47:30 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-08-27 23:47:25 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 08:47:30 +00:00
|
|
|
// Now try the charset on the <link> or processing instruction
|
|
|
|
// that loaded us
|
|
|
|
if (mOwningElement) {
|
|
|
|
nsAutoString specified16;
|
|
|
|
mOwningElement->GetCharset(specified16);
|
|
|
|
if (EncodingUtils::FindEncodingForLabel(specified16, aCharset)) {
|
|
|
|
mCharset.Assign(aCharset);
|
2004-01-13 19:01:26 +00:00
|
|
|
#ifdef PR_LOGGING
|
2012-11-15 08:47:30 +00:00
|
|
|
LOG((" Setting from charset attribute to: %s",
|
|
|
|
PromiseFlatCString(aCharset).get()));
|
2003-06-23 05:04:43 +00:00
|
|
|
#endif
|
2012-11-15 08:47:30 +00:00
|
|
|
return NS_OK;
|
2002-08-27 23:47:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-15 08:47:30 +00:00
|
|
|
// In the preload case, the value of the charset attribute on <link> comes
|
|
|
|
// in via mCharsetHint instead.
|
|
|
|
if (EncodingUtils::FindEncodingForLabel(mCharsetHint, aCharset)) {
|
|
|
|
mCharset.Assign(aCharset);
|
2004-01-13 19:01:26 +00:00
|
|
|
#ifdef PR_LOGGING
|
2012-11-15 08:47:30 +00:00
|
|
|
LOG((" Setting from charset attribute (preload case) to: %s",
|
|
|
|
PromiseFlatCString(aCharset).get()));
|
2004-01-13 19:01:26 +00:00
|
|
|
#endif
|
2012-11-15 08:47:30 +00:00
|
|
|
return NS_OK;
|
2004-01-13 19:01:26 +00:00
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2012-11-15 08:47:30 +00:00
|
|
|
// Try charset from the parent stylesheet.
|
|
|
|
if (mParentData) {
|
|
|
|
aCharset = mParentData->mCharset;
|
|
|
|
if (!aCharset.IsEmpty()) {
|
|
|
|
mCharset.Assign(aCharset);
|
2004-01-13 19:01:26 +00:00
|
|
|
#ifdef PR_LOGGING
|
2012-11-15 08:47:30 +00:00
|
|
|
LOG((" Setting from parent sheet to: %s",
|
|
|
|
PromiseFlatCString(aCharset).get()));
|
2002-08-27 23:47:25 +00:00
|
|
|
#endif
|
2012-11-15 08:47:30 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
}
|
2002-08-27 23:47:25 +00:00
|
|
|
|
2012-11-15 08:47:30 +00:00
|
|
|
if (mLoader->mDocument) {
|
|
|
|
// no useful data on charset. Try the document charset.
|
|
|
|
aCharset = mLoader->mDocument->GetDocumentCharacterSet();
|
|
|
|
MOZ_ASSERT(!aCharset.IsEmpty());
|
|
|
|
mCharset.Assign(aCharset);
|
2004-01-13 19:01:26 +00:00
|
|
|
#ifdef PR_LOGGING
|
2012-11-15 08:47:30 +00:00
|
|
|
LOG((" Setting from document to: %s", PromiseFlatCString(aCharset).get()));
|
2004-01-13 19:01:26 +00:00
|
|
|
#endif
|
2012-11-15 08:47:30 +00:00
|
|
|
return NS_OK;
|
2002-08-27 23:47:25 +00:00
|
|
|
}
|
2004-01-13 19:01:26 +00:00
|
|
|
|
2012-11-15 08:47:30 +00:00
|
|
|
aCharset.AssignLiteral("UTF-8");
|
2004-01-13 19:01:26 +00:00
|
|
|
mCharset = aCharset;
|
2012-11-15 08:47:30 +00:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
LOG((" Setting from default to: %s", PromiseFlatCString(aCharset).get()));
|
|
|
|
#endif
|
2002-08-27 23:47:25 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-08-18 02:32:25 +00:00
|
|
|
already_AddRefed<nsIURI>
|
|
|
|
SheetLoadData::GetReferrerURI()
|
|
|
|
{
|
2010-05-12 20:18:47 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2004-08-18 02:32:25 +00:00
|
|
|
if (mParentData)
|
2010-05-12 20:18:47 +00:00
|
|
|
uri = mParentData->mSheet->GetSheetURI();
|
2004-08-18 02:32:25 +00:00
|
|
|
if (!uri && mLoader->mDocument)
|
2010-05-12 20:18:47 +00:00
|
|
|
uri = mLoader->mDocument->GetDocumentURI();
|
|
|
|
return uri.forget();
|
2004-08-18 02:32:25 +00:00
|
|
|
}
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
/*
|
|
|
|
* Here we need to check that the load did not give us an http error
|
|
|
|
* page and check the mimetype on the channel to make sure we're not
|
|
|
|
* loading non-text/css data in standards mode.
|
|
|
|
*/
|
1999-11-02 06:49:44 +00:00
|
|
|
NS_IMETHODIMP
|
2002-08-27 23:47:25 +00:00
|
|
|
SheetLoadData::OnStreamComplete(nsIUnicharStreamLoader* aLoader,
|
2001-10-25 00:34:20 +00:00
|
|
|
nsISupports* aContext,
|
2000-02-02 23:04:47 +00:00
|
|
|
nsresult aStatus,
|
2011-05-19 01:22:59 +00:00
|
|
|
const nsAString& aBuffer)
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
2002-12-03 05:48:14 +00:00
|
|
|
LOG(("SheetLoadData::OnStreamComplete"));
|
|
|
|
NS_ASSERTION(!mLoader->mSyncCallback, "Synchronous callback from necko");
|
|
|
|
|
|
|
|
if (mIsCancelled) {
|
|
|
|
// Just return. Don't call SheetComplete -- it's already been
|
|
|
|
// called and calling it again will lead to an extra NS_RELEASE on
|
|
|
|
// this data and a likely crash.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2005-09-12 18:41:15 +00:00
|
|
|
if (!mLoader->mDocument && !mIsNonDocumentSheet) {
|
2002-12-03 05:48:14 +00:00
|
|
|
// Sorry, we don't care about this load anymore
|
2005-09-12 18:41:15 +00:00
|
|
|
LOG_WARN((" No document and not non-document sheet; dropping load"));
|
|
|
|
mLoader->SheetComplete(this, NS_BINDING_ABORTED);
|
2002-12-03 05:48:14 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2007-05-01 19:26:15 +00:00
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
LOG_WARN((" Load failed: status 0x%x", aStatus));
|
|
|
|
mLoader->SheetComplete(this, aStatus);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-08-27 23:47:25 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsresult result = aLoader->GetChannel(getter_AddRefs(channel));
|
2007-05-01 19:26:15 +00:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
LOG_WARN((" No channel from loader"));
|
|
|
|
mLoader->SheetComplete(this, result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2007-10-23 21:56:41 +00:00
|
|
|
nsCOMPtr<nsIURI> originalURI;
|
|
|
|
channel->GetOriginalURI(getter_AddRefs(originalURI));
|
|
|
|
|
2007-05-01 19:26:15 +00:00
|
|
|
// If the channel's original URI is "chrome:", we want that, since
|
|
|
|
// the observer code in nsXULPrototypeCache depends on chrome stylesheets
|
|
|
|
// having a chrome URI. (Whether or not chrome stylesheets come through
|
|
|
|
// this codepath seems nondeterministic.)
|
|
|
|
// Otherwise we want the potentially-HTTP-redirected URI.
|
2007-10-23 21:56:41 +00:00
|
|
|
nsCOMPtr<nsIURI> channelURI;
|
|
|
|
NS_GetFinalChannelURI(channel, getter_AddRefs(channelURI));
|
2007-05-01 19:26:15 +00:00
|
|
|
|
2007-10-23 21:56:41 +00:00
|
|
|
if (!channelURI || !originalURI) {
|
2007-05-01 19:26:15 +00:00
|
|
|
NS_ERROR("Someone just violated the nsIRequest contract");
|
|
|
|
LOG_WARN((" Channel without a URI. Bad!"));
|
|
|
|
mLoader->SheetComplete(this, NS_ERROR_UNEXPECTED);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
result = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
if (secMan) { // Could be null if we already shut down
|
2011-05-19 01:22:59 +00:00
|
|
|
if (mUseSystemPrincipal) {
|
|
|
|
result = secMan->GetSystemPrincipal(getter_AddRefs(principal));
|
|
|
|
} else {
|
|
|
|
result = secMan->GetChannelPrincipal(channel, getter_AddRefs(principal));
|
|
|
|
}
|
2007-05-01 19:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
LOG_WARN((" Couldn't get principal"));
|
|
|
|
mLoader->SheetComplete(this, result);
|
|
|
|
return NS_OK;
|
2003-07-18 00:35:49 +00:00
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2007-05-01 19:26:15 +00:00
|
|
|
mSheet->SetPrincipal(principal);
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-06-18 02:46:11 +00:00
|
|
|
// If it's an HTTP channel, we want to make sure this is not an
|
|
|
|
// error document we got.
|
2002-08-27 23:47:25 +00:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
|
2002-06-18 02:46:11 +00:00
|
|
|
if (httpChannel) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool requestSucceeded;
|
2002-07-01 23:12:13 +00:00
|
|
|
result = httpChannel->GetRequestSucceeded(&requestSucceeded);
|
|
|
|
if (NS_SUCCEEDED(result) && !requestSucceeded) {
|
2002-12-03 05:48:14 +00:00
|
|
|
LOG((" Load returned an error page"));
|
2005-09-12 18:41:15 +00:00
|
|
|
mLoader->SheetComplete(this, NS_ERROR_NOT_AVAILABLE);
|
2002-12-03 05:48:14 +00:00
|
|
|
return NS_OK;
|
2002-06-18 02:46:11 +00:00
|
|
|
}
|
|
|
|
}
|
2002-08-27 23:47:25 +00:00
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString contentType;
|
2009-12-23 18:01:51 +00:00
|
|
|
if (channel) {
|
|
|
|
channel->GetContentType(contentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// In standards mode, a style sheet must have one of these MIME
|
|
|
|
// types to be processed at all. In quirks mode, we accept any
|
|
|
|
// MIME type, but only if the style sheet is same-origin with the
|
|
|
|
// requesting document or parent sheet. See bug 524223.
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool validType = contentType.EqualsLiteral("text/css") ||
|
2009-12-23 18:01:51 +00:00
|
|
|
contentType.EqualsLiteral(UNKNOWN_CONTENT_TYPE) ||
|
|
|
|
contentType.IsEmpty();
|
|
|
|
|
|
|
|
if (!validType) {
|
|
|
|
const char *errorMessage;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t errorFlag;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool sameOrigin = true;
|
2009-12-23 18:01:51 +00:00
|
|
|
|
|
|
|
if (mLoaderPrincipal) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool subsumed;
|
2009-12-23 18:01:51 +00:00
|
|
|
result = mLoaderPrincipal->Subsumes(principal, &subsumed);
|
|
|
|
if (NS_FAILED(result) || !subsumed) {
|
2011-10-17 14:59:28 +00:00
|
|
|
sameOrigin = false;
|
2009-12-23 18:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sameOrigin && mLoader->mCompatMode == eCompatibility_NavQuirks) {
|
|
|
|
errorMessage = "MimeNotCssWarn";
|
|
|
|
errorFlag = nsIScriptError::warningFlag;
|
|
|
|
} else {
|
|
|
|
errorMessage = "MimeNotCss";
|
|
|
|
errorFlag = nsIScriptError::errorFlag;
|
|
|
|
}
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString spec;
|
2009-12-23 18:01:51 +00:00
|
|
|
channelURI->GetSpec(spec);
|
|
|
|
|
|
|
|
const nsAFlatString& specUTF16 = NS_ConvertUTF8toUTF16(spec);
|
|
|
|
const nsAFlatString& ctypeUTF16 = NS_ConvertASCIItoUTF16(contentType);
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t *strings[] = { specUTF16.get(), ctypeUTF16.get() };
|
2009-12-23 18:01:51 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> referrer = GetReferrerURI();
|
2011-12-15 14:47:03 +00:00
|
|
|
nsContentUtils::ReportToConsole(errorFlag,
|
2013-08-21 19:28:26 +00:00
|
|
|
NS_LITERAL_CSTRING("CSS Loader"),
|
|
|
|
mLoader->mDocument,
|
2011-12-15 14:47:03 +00:00
|
|
|
nsContentUtils::eCSS_PROPERTIES,
|
2009-12-23 18:01:51 +00:00
|
|
|
errorMessage,
|
2011-10-11 05:50:08 +00:00
|
|
|
strings, ArrayLength(strings),
|
2011-12-15 14:47:03 +00:00
|
|
|
referrer);
|
2009-12-23 18:01:51 +00:00
|
|
|
|
|
|
|
if (errorFlag == nsIScriptError::errorFlag) {
|
|
|
|
LOG_WARN((" Ignoring sheet with improper MIME type %s",
|
|
|
|
contentType.get()));
|
|
|
|
mLoader->SheetComplete(this, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2005-09-12 18:41:15 +00:00
|
|
|
|
2007-10-23 21:56:41 +00:00
|
|
|
// Enough to set the URIs on mSheet, since any sibling datas we have share
|
2007-05-01 19:26:15 +00:00
|
|
|
// the same mInner as mSheet and will thus get the same URI.
|
2007-10-23 21:56:41 +00:00
|
|
|
mSheet->SetURIs(channelURI, originalURI, channelURI);
|
2009-12-23 18:01:51 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool completed;
|
2011-05-19 01:22:59 +00:00
|
|
|
result = mLoader->ParseSheet(aBuffer, this, completed);
|
|
|
|
NS_ASSERTION(completed || !mSyncLoad, "sync load did not complete");
|
|
|
|
return result;
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
|
|
|
Loader::IsAlternate(const nsAString& aTitle, bool aHasAlternateRel)
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
2005-09-12 18:41:15 +00:00
|
|
|
// A sheet is alternate if it has a nonempty title that doesn't match the
|
|
|
|
// currently selected style set. But if there _is_ no currently selected
|
|
|
|
// style set, the sheet wasn't marked as an alternate explicitly, and aTitle
|
|
|
|
// is nonempty, we should select the style set corresponding to aTitle, since
|
|
|
|
// that's a preferred sheet.
|
|
|
|
if (aTitle.IsEmpty()) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2005-09-12 18:41:15 +00:00
|
|
|
if (!aHasAlternateRel && mDocument && mPreferredSheet.IsEmpty()) {
|
|
|
|
// There's no preferred set yet, and we now have a sheet with a title.
|
|
|
|
// Make that be the preferred set.
|
2006-12-26 17:47:52 +00:00
|
|
|
mDocument->SetHeaderData(nsGkAtoms::headerDefaultStyle, aTitle);
|
2005-09-12 18:41:15 +00:00
|
|
|
// We're definitely not an alternate
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2005-09-12 18:41:15 +00:00
|
|
|
}
|
|
|
|
|
2007-01-11 19:32:31 +00:00
|
|
|
return !aTitle.Equals(mPreferredSheet);
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
|
|
|
|
2013-07-15 21:28:33 +00:00
|
|
|
/* static */ PLDHashOperator
|
|
|
|
Loader::RemoveEntriesWithURI(URIPrincipalAndCORSModeHashKey* aKey,
|
|
|
|
nsRefPtr<nsCSSStyleSheet> &aSheet,
|
|
|
|
void* aUserData)
|
|
|
|
{
|
|
|
|
nsIURI* obsoleteURI = static_cast<nsIURI*>(aUserData);
|
|
|
|
nsIURI* sheetURI = aKey->GetURI();
|
|
|
|
bool areEqual;
|
|
|
|
nsresult rv = sheetURI->Equals(obsoleteURI, &areEqual);
|
|
|
|
if (NS_SUCCEEDED(rv) && areEqual) {
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Loader::ObsoleteSheet(nsIURI* aURI)
|
|
|
|
{
|
2013-09-02 08:41:57 +00:00
|
|
|
if (!mSheets) {
|
2013-07-15 21:28:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (!aURI) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets->mCompleteSheets.Enumerate(RemoveEntriesWithURI, aURI);
|
2013-07-15 21:28:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-04-04 23:57:06 +00:00
|
|
|
/**
|
|
|
|
* CheckLoadAllowed will return success if the load is allowed,
|
2011-05-06 15:45:36 +00:00
|
|
|
* failure otherwise.
|
2003-04-04 23:57:06 +00:00
|
|
|
*
|
2007-05-01 19:26:15 +00:00
|
|
|
* @param aSourcePrincipal the principal of the node or document or parent
|
|
|
|
* sheet loading the sheet
|
2003-04-04 23:57:06 +00:00
|
|
|
* @param aTargetURI the uri of the sheet to be loaded
|
2004-07-27 17:15:53 +00:00
|
|
|
* @param aContext the node owning the sheet. This is the element or document
|
|
|
|
* owning the stylesheet (possibly indirectly, for child sheets)
|
2003-04-04 23:57:06 +00:00
|
|
|
*/
|
|
|
|
nsresult
|
2010-03-02 21:00:53 +00:00
|
|
|
Loader::CheckLoadAllowed(nsIPrincipal* aSourcePrincipal,
|
|
|
|
nsIURI* aTargetURI,
|
|
|
|
nsISupports* aContext)
|
2003-04-04 23:57:06 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::CheckLoadAllowed"));
|
2007-07-11 19:44:27 +00:00
|
|
|
|
|
|
|
nsresult rv;
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2007-07-11 19:44:27 +00:00
|
|
|
if (aSourcePrincipal) {
|
|
|
|
// Check with the security manager
|
|
|
|
nsIScriptSecurityManager *secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
rv =
|
|
|
|
secMan->CheckLoadURIWithPrincipal(aSourcePrincipal, aTargetURI,
|
|
|
|
nsIScriptSecurityManager::ALLOW_CHROME);
|
|
|
|
if (NS_FAILED(rv)) { // failure is normal here; don't warn
|
|
|
|
return rv;
|
|
|
|
}
|
2003-04-04 23:57:06 +00:00
|
|
|
|
2007-08-08 01:16:09 +00:00
|
|
|
LOG((" Passed security check"));
|
2003-04-04 23:57:06 +00:00
|
|
|
|
2007-07-11 19:44:27 +00:00
|
|
|
// Check with content policy
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int16_t shouldLoad = nsIContentPolicy::ACCEPT;
|
2007-07-11 19:44:27 +00:00
|
|
|
rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_STYLESHEET,
|
|
|
|
aTargetURI,
|
2007-08-08 01:16:09 +00:00
|
|
|
aSourcePrincipal,
|
2007-07-11 19:44:27 +00:00
|
|
|
aContext,
|
|
|
|
NS_LITERAL_CSTRING("text/css"),
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr, //extra param
|
2007-07-11 19:44:27 +00:00
|
|
|
&shouldLoad,
|
2007-08-08 01:16:09 +00:00
|
|
|
nsContentUtils::GetContentPolicy(),
|
|
|
|
nsContentUtils::GetSecurityManager());
|
2007-07-11 19:44:27 +00:00
|
|
|
|
|
|
|
if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) {
|
|
|
|
LOG((" Load blocked by content policy"));
|
|
|
|
return NS_ERROR_CONTENT_BLOCKED;
|
|
|
|
}
|
2003-04-04 23:57:06 +00:00
|
|
|
}
|
|
|
|
|
2007-07-11 19:44:27 +00:00
|
|
|
return NS_OK;
|
2003-04-04 23:57:06 +00:00
|
|
|
}
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
/**
|
2010-05-11 20:41:47 +00:00
|
|
|
* CreateSheet() creates an nsCSSStyleSheet object for the given URI,
|
2002-12-03 05:48:14 +00:00
|
|
|
* if any. If there is no URI given, we just create a new style sheet
|
|
|
|
* object. Otherwise, we check for an existing style sheet object for
|
|
|
|
* that uri in various caches and clone it if we find it. Cloned
|
|
|
|
* sheets will have the title/media/enabled state of the sheet they
|
|
|
|
* are clones off; make sure to call PrepareSheet() on the result of
|
|
|
|
* CreateSheet().
|
|
|
|
*/
|
1999-05-18 23:15:10 +00:00
|
|
|
nsresult
|
2010-03-02 21:00:53 +00:00
|
|
|
Loader::CreateSheet(nsIURI* aURI,
|
|
|
|
nsIContent* aLinkingContent,
|
|
|
|
nsIPrincipal* aLoaderPrincipal,
|
2012-08-28 17:10:08 +00:00
|
|
|
CORSMode aCORSMode,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aSyncLoad,
|
2011-11-08 04:41:17 +00:00
|
|
|
bool aHasAlternateRel,
|
|
|
|
const nsAString& aTitle,
|
2010-03-02 21:00:53 +00:00
|
|
|
StyleSheetState& aSheetState,
|
2011-11-08 04:41:17 +00:00
|
|
|
bool *aIsAlternate,
|
2010-05-11 20:41:47 +00:00
|
|
|
nsCSSStyleSheet** aSheet)
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::CreateSheet"));
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_PRECONDITION(aSheet, "Null out param!");
|
2003-07-27 15:20:11 +00:00
|
|
|
|
2013-09-02 08:41:57 +00:00
|
|
|
if (!mSheets) {
|
|
|
|
mSheets = new Sheets();
|
2012-05-18 17:30:49 +00:00
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
*aSheet = nullptr;
|
2002-12-03 05:48:14 +00:00
|
|
|
aSheetState = eSheetStateUnknown;
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2011-11-08 04:41:17 +00:00
|
|
|
// Check the alternate state before doing anything else, because it
|
|
|
|
// can mess with our hashtables.
|
|
|
|
*aIsAlternate = IsAlternate(aTitle, aHasAlternateRel);
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
if (aURI) {
|
|
|
|
aSheetState = eSheetComplete;
|
2010-05-11 20:41:47 +00:00
|
|
|
nsRefPtr<nsCSSStyleSheet> sheet;
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2002-12-11 00:28:47 +00:00
|
|
|
// First, the XUL cache
|
2003-04-11 00:56:27 +00:00
|
|
|
#ifdef MOZ_XUL
|
2002-12-11 00:28:47 +00:00
|
|
|
if (IsChromeURI(aURI)) {
|
2007-03-12 05:53:33 +00:00
|
|
|
nsXULPrototypeCache* cache = nsXULPrototypeCache::GetInstance();
|
2002-12-03 05:48:14 +00:00
|
|
|
if (cache) {
|
2007-03-12 05:53:33 +00:00
|
|
|
if (cache->IsEnabled()) {
|
|
|
|
sheet = cache->GetStyleSheet(aURI);
|
2002-12-11 00:28:47 +00:00
|
|
|
LOG((" From XUL cache: %p", sheet.get()));
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-24 18:59:14 +00:00
|
|
|
bool fromCompleteSheets = false;
|
2002-12-03 05:48:14 +00:00
|
|
|
if (!sheet) {
|
2008-10-20 20:33:03 +00:00
|
|
|
// Then our per-document complete sheets.
|
2012-08-28 17:10:08 +00:00
|
|
|
URIPrincipalAndCORSModeHashKey key(aURI, aLoaderPrincipal, aCORSMode);
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets->mCompleteSheets.Get(&key, getter_AddRefs(sheet));
|
2002-12-11 00:28:47 +00:00
|
|
|
LOG((" From completed: %p", sheet.get()));
|
2013-04-24 18:59:14 +00:00
|
|
|
|
|
|
|
fromCompleteSheets = !!sheet;
|
2008-10-20 20:33:03 +00:00
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2008-10-20 20:33:03 +00:00
|
|
|
if (sheet) {
|
|
|
|
// This sheet came from the XUL cache or our per-document hashtable; it
|
|
|
|
// better be a complete sheet.
|
2010-05-14 19:05:14 +00:00
|
|
|
NS_ASSERTION(sheet->IsComplete(),
|
2008-10-20 20:33:03 +00:00
|
|
|
"Sheet thinks it's not complete while we think it is");
|
2010-05-14 19:05:14 +00:00
|
|
|
|
2008-10-20 20:33:03 +00:00
|
|
|
// Make sure it hasn't been modified; if it has, we can't use it
|
2010-05-11 20:41:47 +00:00
|
|
|
if (sheet->IsModified()) {
|
2008-10-20 20:33:03 +00:00
|
|
|
LOG((" Not cloning completed sheet %p because it's been modified",
|
|
|
|
sheet.get()));
|
2012-07-30 14:20:58 +00:00
|
|
|
sheet = nullptr;
|
2013-05-23 13:20:52 +00:00
|
|
|
fromCompleteSheets = false;
|
2008-10-20 20:33:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then loading sheets
|
|
|
|
if (!sheet && !aSyncLoad) {
|
|
|
|
aSheetState = eSheetLoading;
|
2012-07-30 14:20:58 +00:00
|
|
|
SheetLoadData* loadData = nullptr;
|
2012-08-28 17:10:08 +00:00
|
|
|
URIPrincipalAndCORSModeHashKey key(aURI, aLoaderPrincipal, aCORSMode);
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets->mLoadingDatas.Get(&key, &loadData);
|
2008-10-20 20:33:03 +00:00
|
|
|
if (loadData) {
|
|
|
|
sheet = loadData->mSheet;
|
|
|
|
LOG((" From loading: %p", sheet.get()));
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2011-09-29 06:19:26 +00:00
|
|
|
bool debugEqual;
|
2008-10-20 20:33:03 +00:00
|
|
|
NS_ASSERTION((!aLoaderPrincipal && !loadData->mLoaderPrincipal) ||
|
|
|
|
(aLoaderPrincipal && loadData->mLoaderPrincipal &&
|
|
|
|
NS_SUCCEEDED(aLoaderPrincipal->
|
|
|
|
Equals(loadData->mLoaderPrincipal,
|
|
|
|
&debugEqual)) && debugEqual),
|
|
|
|
"Principals should be the same");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then alternate sheets
|
|
|
|
if (!sheet) {
|
|
|
|
aSheetState = eSheetPending;
|
2013-02-18 06:56:32 +00:00
|
|
|
loadData = nullptr;
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets->mPendingDatas.Get(&key, &loadData);
|
2002-12-11 00:28:47 +00:00
|
|
|
if (loadData) {
|
|
|
|
sheet = loadData->mSheet;
|
2008-10-20 20:33:03 +00:00
|
|
|
LOG((" From pending: %p", sheet.get()));
|
2007-05-01 19:26:15 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2011-09-29 06:19:26 +00:00
|
|
|
bool debugEqual;
|
2007-05-01 19:26:15 +00:00
|
|
|
NS_ASSERTION((!aLoaderPrincipal && !loadData->mLoaderPrincipal) ||
|
|
|
|
(aLoaderPrincipal && loadData->mLoaderPrincipal &&
|
|
|
|
NS_SUCCEEDED(aLoaderPrincipal->
|
2008-10-20 20:33:03 +00:00
|
|
|
Equals(loadData->mLoaderPrincipal,
|
|
|
|
&debugEqual)) && debugEqual),
|
2007-05-01 19:26:15 +00:00
|
|
|
"Principals should be the same");
|
|
|
|
#endif
|
2002-12-11 00:28:47 +00:00
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sheet) {
|
2008-10-20 20:33:03 +00:00
|
|
|
// The sheet we have now should be either incomplete or unmodified
|
2010-05-14 19:05:14 +00:00
|
|
|
NS_ASSERTION(!sheet->IsModified() || !sheet->IsComplete(),
|
2008-10-20 20:33:03 +00:00
|
|
|
"Unexpected modified complete sheet");
|
2010-05-14 19:05:14 +00:00
|
|
|
NS_ASSERTION(sheet->IsComplete() || aSheetState != eSheetComplete,
|
2008-10-20 20:33:03 +00:00
|
|
|
"Sheet thinks it's not complete while we think it is");
|
2010-05-14 19:05:14 +00:00
|
|
|
|
2014-03-15 19:00:15 +00:00
|
|
|
*aSheet = sheet->Clone(nullptr, nullptr, nullptr, nullptr).take();
|
2013-04-24 18:59:14 +00:00
|
|
|
if (*aSheet && fromCompleteSheets &&
|
|
|
|
!sheet->GetOwnerNode() && !sheet->GetParentSheet()) {
|
|
|
|
// The sheet we're cloning isn't actually referenced by
|
|
|
|
// anyone. Replace it in the cache, so that if our CSSOM is
|
|
|
|
// later modified we don't end up with two copies of our inner
|
|
|
|
// hanging around.
|
|
|
|
URIPrincipalAndCORSModeHashKey key(aURI, aLoaderPrincipal, aCORSMode);
|
2013-05-23 13:20:52 +00:00
|
|
|
NS_ASSERTION((*aSheet)->IsComplete(),
|
|
|
|
"Should only be caching complete sheets");
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets->mCompleteSheets.Put(&key, *aSheet);
|
2013-04-24 18:59:14 +00:00
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
if (!*aSheet) {
|
|
|
|
aSheetState = eSheetNeedsParser;
|
2007-10-23 21:56:41 +00:00
|
|
|
nsIURI *sheetURI;
|
|
|
|
nsCOMPtr<nsIURI> baseURI;
|
|
|
|
nsIURI* originalURI;
|
2004-09-10 06:45:59 +00:00
|
|
|
if (!aURI) {
|
2003-10-06 13:24:55 +00:00
|
|
|
// Inline style. Use the document's base URL so that @import in
|
|
|
|
// the inline sheet picks up the right base.
|
|
|
|
NS_ASSERTION(aLinkingContent, "Inline stylesheet without linking content?");
|
2004-09-10 06:45:59 +00:00
|
|
|
baseURI = aLinkingContent->GetBaseURI();
|
|
|
|
sheetURI = aLinkingContent->GetDocument()->GetDocumentURI();
|
2012-07-30 14:20:58 +00:00
|
|
|
originalURI = nullptr;
|
2007-10-23 21:56:41 +00:00
|
|
|
} else {
|
|
|
|
baseURI = aURI;
|
|
|
|
sheetURI = aURI;
|
|
|
|
originalURI = aURI;
|
2003-10-06 13:24:55 +00:00
|
|
|
}
|
|
|
|
|
2012-08-28 17:10:08 +00:00
|
|
|
nsRefPtr<nsCSSStyleSheet> sheet = new nsCSSStyleSheet(aCORSMode);
|
2012-08-04 07:44:01 +00:00
|
|
|
sheet->SetURIs(sheetURI, originalURI, baseURI);
|
|
|
|
sheet.forget(aSheet);
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(*aSheet, "We should have a sheet by now!");
|
|
|
|
NS_ASSERTION(aSheetState != eSheetStateUnknown, "Have to set a state!");
|
|
|
|
LOG((" State: %s", gStateStrings[aSheetState]));
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
return NS_OK;
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
/**
|
|
|
|
* PrepareSheet() handles setting the media and title on the sheet, as
|
2005-09-12 18:41:15 +00:00
|
|
|
* well as setting the enabled state based on the title and whether
|
|
|
|
* the sheet had "alternate" in its rel.
|
2002-12-03 05:48:14 +00:00
|
|
|
*/
|
2013-11-11 07:57:53 +00:00
|
|
|
void
|
2010-05-11 20:41:47 +00:00
|
|
|
Loader::PrepareSheet(nsCSSStyleSheet* aSheet,
|
2010-03-02 21:00:53 +00:00
|
|
|
const nsSubstring& aTitle,
|
|
|
|
const nsSubstring& aMediaString,
|
|
|
|
nsMediaList* aMediaList,
|
2013-01-08 23:25:47 +00:00
|
|
|
Element* aScopeElement,
|
2011-11-08 04:41:17 +00:00
|
|
|
bool isAlternate)
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_PRECONDITION(aSheet, "Must have a sheet!");
|
|
|
|
|
2010-05-31 23:50:10 +00:00
|
|
|
nsRefPtr<nsMediaList> mediaList(aMediaList);
|
2005-03-30 00:36:57 +00:00
|
|
|
|
|
|
|
if (!aMediaString.IsEmpty()) {
|
|
|
|
NS_ASSERTION(!aMediaList,
|
|
|
|
"must not provide both aMediaString and aMediaList");
|
|
|
|
mediaList = new nsMediaList();
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
nsCSSParser mediumParser(this);
|
|
|
|
|
2005-03-30 00:36:57 +00:00
|
|
|
// We have aMediaString only when linked from link elements, style
|
2011-10-17 14:59:28 +00:00
|
|
|
// elements, or PIs, so pass true.
|
2013-11-11 07:57:34 +00:00
|
|
|
mediumParser.ParseMediaList(aMediaString, nullptr, 0, mediaList, true);
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
2005-03-30 00:36:57 +00:00
|
|
|
|
2010-05-11 20:41:47 +00:00
|
|
|
aSheet->SetMedia(mediaList);
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
aSheet->SetTitle(aTitle);
|
2011-11-08 04:41:17 +00:00
|
|
|
aSheet->SetEnabled(! isAlternate);
|
2013-01-08 23:25:47 +00:00
|
|
|
aSheet->SetScopeElement(aScopeElement);
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
/**
|
|
|
|
* InsertSheetInDoc handles ordering of sheets in the document. Here
|
|
|
|
* we have two types of sheets -- those with linking elements and
|
|
|
|
* those without. The latter are loaded by Link: headers.
|
|
|
|
* The following constraints are observed:
|
|
|
|
* 1) Any sheet with a linking element comes after all sheets without
|
|
|
|
* linking elements
|
|
|
|
* 2) Sheets without linking elements are inserted in the order in
|
|
|
|
* which the inserting requests come in, since all of these are
|
|
|
|
* inserted during header data processing in the content sink
|
|
|
|
* 3) Sheets with linking elements are ordered based on document order
|
2002-12-04 05:18:39 +00:00
|
|
|
* as determined by CompareDocumentPosition.
|
2002-12-03 05:48:14 +00:00
|
|
|
*/
|
|
|
|
nsresult
|
2010-05-11 20:41:47 +00:00
|
|
|
Loader::InsertSheetInDoc(nsCSSStyleSheet* aSheet,
|
2010-03-02 21:00:53 +00:00
|
|
|
nsIContent* aLinkingContent,
|
|
|
|
nsIDocument* aDocument)
|
2002-12-03 05:48:14 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::InsertSheetInDoc"));
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_PRECONDITION(aSheet, "Nothing to insert");
|
|
|
|
NS_PRECONDITION(aDocument, "Must have a document to insert into");
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
// XXX Need to cancel pending sheet loads for this element, if any
|
2001-10-31 19:03:46 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t sheetCount = aDocument->GetNumberOfStyleSheets();
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Start the walk at the _end_ of the list, since in the typical
|
|
|
|
* case we'll just want to append anyway. We want to break out of
|
|
|
|
* the loop when insertionPoint points to just before the index we
|
|
|
|
* want to insert at. In other words, when we leave the loop
|
|
|
|
* insertionPoint is the index of the stylesheet that immediately
|
|
|
|
* precedes the one we're inserting.
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t insertionPoint;
|
2002-12-03 05:48:14 +00:00
|
|
|
for (insertionPoint = sheetCount - 1; insertionPoint >= 0; --insertionPoint) {
|
2004-07-28 07:08:41 +00:00
|
|
|
nsIStyleSheet *curSheet = aDocument->GetStyleSheetAt(insertionPoint);
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_ASSERTION(curSheet, "There must be a sheet here!");
|
|
|
|
nsCOMPtr<nsIDOMStyleSheet> domSheet = do_QueryInterface(curSheet);
|
|
|
|
NS_ASSERTION(domSheet, "All the \"normal\" sheets implement nsIDOMStyleSheet");
|
|
|
|
nsCOMPtr<nsIDOMNode> sheetOwner;
|
|
|
|
domSheet->GetOwnerNode(getter_AddRefs(sheetOwner));
|
2006-05-16 15:50:47 +00:00
|
|
|
if (sheetOwner && !aLinkingContent) {
|
2002-12-03 05:48:14 +00:00
|
|
|
// Keep moving; all sheets with a sheetOwner come after all
|
2011-05-06 15:45:36 +00:00
|
|
|
// sheets without a linkingNode
|
2002-12-03 05:48:14 +00:00
|
|
|
continue;
|
2001-10-31 19:03:46 +00:00
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
if (!sheetOwner) {
|
|
|
|
// Aha! The current sheet has no sheet owner, so we want to
|
|
|
|
// insert after it no matter whether we have a linkingNode
|
|
|
|
break;
|
2001-10-31 19:03:46 +00:00
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
|
2006-05-16 15:50:47 +00:00
|
|
|
nsCOMPtr<nsINode> sheetOwnerNode = do_QueryInterface(sheetOwner);
|
|
|
|
NS_ASSERTION(aLinkingContent != sheetOwnerNode,
|
|
|
|
"Why do we still have our old sheet?");
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
// Have to compare
|
2006-05-16 15:52:45 +00:00
|
|
|
if (nsContentUtils::PositionIsBefore(sheetOwnerNode, aLinkingContent)) {
|
2002-12-03 05:48:14 +00:00
|
|
|
// The current sheet comes before us, and it better be the first
|
|
|
|
// such, because now we break
|
|
|
|
break;
|
2001-10-31 19:03:46 +00:00
|
|
|
}
|
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
++insertionPoint; // adjust the index to the spot we want to insert in
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
// XXX <meta> elements do not implement nsIStyleSheetLinkingElement;
|
|
|
|
// need to fix this for them to be ordered correctly.
|
|
|
|
nsCOMPtr<nsIStyleSheetLinkingElement>
|
2003-07-03 02:45:34 +00:00
|
|
|
linkingElement = do_QueryInterface(aLinkingContent);
|
2002-12-03 05:48:14 +00:00
|
|
|
if (linkingElement) {
|
|
|
|
linkingElement->SetStyleSheet(aSheet); // This sets the ownerNode on the sheet
|
|
|
|
}
|
|
|
|
|
2003-10-02 21:29:40 +00:00
|
|
|
aDocument->BeginUpdate(UPDATE_STYLE);
|
2002-12-03 05:48:14 +00:00
|
|
|
aDocument->InsertStyleSheetAt(aSheet, insertionPoint);
|
2003-10-02 21:29:40 +00:00
|
|
|
aDocument->EndUpdate(UPDATE_STYLE);
|
2002-12-03 05:48:14 +00:00
|
|
|
LOG((" Inserting into document at position %d", insertionPoint));
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
/**
|
|
|
|
* InsertChildSheet handles ordering of @import-ed sheet in their
|
|
|
|
* parent sheets. Here we want to just insert based on order of the
|
|
|
|
* @import rules that imported the sheets. In theory we can't just
|
|
|
|
* append to the end because the CSSOM can insert @import rules. In
|
|
|
|
* practice, we get the call to load the child sheet before the CSSOM
|
|
|
|
* has finished inserting the @import rule, so we have no idea where
|
|
|
|
* to put it anyway. So just append for now.
|
|
|
|
*/
|
1999-05-18 23:15:10 +00:00
|
|
|
nsresult
|
2010-05-11 20:41:47 +00:00
|
|
|
Loader::InsertChildSheet(nsCSSStyleSheet* aSheet,
|
|
|
|
nsCSSStyleSheet* aParentSheet,
|
2011-03-17 17:41:52 +00:00
|
|
|
ImportRule* aParentRule)
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::InsertChildSheet"));
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_PRECONDITION(aSheet, "Nothing to insert");
|
|
|
|
NS_PRECONDITION(aParentSheet, "Need a parent to insert into");
|
|
|
|
NS_PRECONDITION(aParentSheet, "How did we get imported?");
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2002-02-25 22:26:26 +00:00
|
|
|
// child sheets should always start out enabled, even if they got
|
|
|
|
// cloned off of top-level sheets which were disabled
|
2011-10-17 14:59:28 +00:00
|
|
|
aSheet->SetEnabled(true);
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
aParentSheet->AppendStyleSheet(aSheet);
|
|
|
|
aParentRule->SetSheet(aSheet); // This sets the ownerRule on the sheet
|
|
|
|
|
|
|
|
LOG((" Inserting into parent sheet"));
|
|
|
|
// LOG((" Inserting into parent sheet at position %d", insertionPoint));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LoadSheet handles the actual load of a sheet. If the load is
|
|
|
|
* supposed to be synchronous it just opens a channel synchronously
|
|
|
|
* using the given uri, wraps the resulting stream in a converter
|
|
|
|
* stream and calls ParseSheet. Otherwise it tries to look for an
|
|
|
|
* existing load for this URI and piggyback on it. Failing all that,
|
|
|
|
* a new load is kicked off asynchronously.
|
|
|
|
*/
|
|
|
|
nsresult
|
2010-03-02 21:00:53 +00:00
|
|
|
Loader::LoadSheet(SheetLoadData* aLoadData, StyleSheetState aSheetState)
|
2002-12-03 05:48:14 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::LoadSheet"));
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_PRECONDITION(aLoadData, "Need a load data");
|
|
|
|
NS_PRECONDITION(aLoadData->mURI, "Need a URI to load");
|
|
|
|
NS_PRECONDITION(aLoadData->mSheet, "Need a sheet to load into");
|
|
|
|
NS_PRECONDITION(aSheetState != eSheetComplete, "Why bother?");
|
2009-03-17 01:38:36 +00:00
|
|
|
NS_PRECONDITION(!aLoadData->mUseSystemPrincipal || aLoadData->mSyncLoad,
|
|
|
|
"Shouldn't use system principal for async loads");
|
2013-09-02 08:41:57 +00:00
|
|
|
NS_ASSERTION(mSheets, "mLoadingDatas should be initialized by now.");
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
LOG_URI(" Load from: '%s'", aLoadData->mURI);
|
2011-05-06 15:45:36 +00:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2002-12-03 05:48:14 +00:00
|
|
|
|
2005-09-12 18:41:15 +00:00
|
|
|
if (!mDocument && !aLoadData->mIsNonDocumentSheet) {
|
2002-12-03 05:48:14 +00:00
|
|
|
// No point starting the load; just release all the data and such.
|
2005-09-12 18:41:15 +00:00
|
|
|
LOG_WARN((" No document and not non-document sheet; pre-dropping load"));
|
|
|
|
SheetComplete(aLoadData, NS_BINDING_ABORTED);
|
|
|
|
return NS_BINDING_ABORTED;
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aLoadData->mSyncLoad) {
|
|
|
|
LOG((" Synchronous load"));
|
2005-09-12 18:41:15 +00:00
|
|
|
NS_ASSERTION(!aLoadData->mObserver, "Observer for a sync load?");
|
2002-12-11 00:28:47 +00:00
|
|
|
NS_ASSERTION(aSheetState == eSheetNeedsParser,
|
|
|
|
"Sync loads can't reuse existing async loads");
|
2003-07-18 00:35:49 +00:00
|
|
|
|
2011-05-19 01:22:59 +00:00
|
|
|
// Create a nsIUnicharStreamLoader instance to which we will feed
|
|
|
|
// the data from the sync load. Do this before creating the
|
|
|
|
// channel to make error recovery simpler.
|
|
|
|
nsCOMPtr<nsIUnicharStreamLoader> streamLoader;
|
|
|
|
rv = NS_NewUnicharStreamLoader(getter_AddRefs(streamLoader), aLoadData);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG_ERROR((" Failed to create stream loader for sync load"));
|
|
|
|
SheetComplete(aLoadData, rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-10-25 21:56:56 +00:00
|
|
|
if (mDocument) {
|
|
|
|
mozilla::net::SeerLearn(aLoadData->mURI, mDocument->GetDocumentURI(),
|
|
|
|
nsINetworkSeer::LEARN_LOAD_SUBRESOURCE,
|
|
|
|
mDocument);
|
|
|
|
}
|
|
|
|
|
2002-12-11 00:28:47 +00:00
|
|
|
// Just load it
|
2002-12-03 05:48:14 +00:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2007-05-01 19:26:15 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = NS_OpenURI(getter_AddRefs(stream), aLoadData->mURI, nullptr,
|
|
|
|
nullptr, nullptr, nsIRequest::LOAD_NORMAL,
|
2007-05-01 19:26:15 +00:00
|
|
|
getter_AddRefs(channel));
|
2002-12-03 05:48:14 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG_ERROR((" Failed to open URI synchronously"));
|
2005-09-12 18:41:15 +00:00
|
|
|
SheetComplete(aLoadData, rv);
|
2002-12-03 05:48:14 +00:00
|
|
|
return rv;
|
2001-10-31 19:03:46 +00:00
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
|
2007-05-01 19:26:15 +00:00
|
|
|
NS_ASSERTION(channel, "NS_OpenURI lied?");
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2011-05-19 01:22:59 +00:00
|
|
|
// Force UA sheets to be UTF-8.
|
|
|
|
// XXX this is only necessary because the default in
|
|
|
|
// SheetLoadData::OnDetermineCharset is wrong (bug 521039).
|
|
|
|
channel->SetContentCharset(NS_LITERAL_CSTRING("UTF-8"));
|
|
|
|
|
|
|
|
// Manually feed the streamloader the contents of the stream we
|
|
|
|
// got from NS_OpenURI. This will call back into OnStreamComplete
|
|
|
|
// and thence to ParseSheet. Regardless of whether this fails,
|
|
|
|
// SheetComplete has been called.
|
|
|
|
return nsSyncLoadService::PushSyncStreamToListener(stream,
|
|
|
|
streamLoader,
|
|
|
|
channel);
|
2001-10-31 19:03:46 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
SheetLoadData* existingData = nullptr;
|
2002-12-03 05:48:14 +00:00
|
|
|
|
2012-08-28 17:10:08 +00:00
|
|
|
URIPrincipalAndCORSModeHashKey key(aLoadData->mURI,
|
|
|
|
aLoadData->mLoaderPrincipal,
|
|
|
|
aLoadData->mSheet->GetCORSMode());
|
2002-12-03 05:48:14 +00:00
|
|
|
if (aSheetState == eSheetLoading) {
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets->mLoadingDatas.Get(&key, &existingData);
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_ASSERTION(existingData, "CreateSheet lied about the state");
|
|
|
|
}
|
|
|
|
else if (aSheetState == eSheetPending){
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets->mPendingDatas.Get(&key, &existingData);
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_ASSERTION(existingData, "CreateSheet lied about the state");
|
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
if (existingData) {
|
|
|
|
LOG((" Glomming on to existing load"));
|
|
|
|
SheetLoadData* data = existingData;
|
|
|
|
while (data->mNext) {
|
|
|
|
data = data->mNext;
|
2001-10-31 19:03:46 +00:00
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
data->mNext = aLoadData; // transfer ownership
|
2005-09-12 18:41:15 +00:00
|
|
|
if (aSheetState == eSheetPending && !aLoadData->mWasAlternate) {
|
2002-12-03 05:48:14 +00:00
|
|
|
// Kick the load off; someone cares about it right away
|
2003-07-27 15:20:11 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
#ifdef DEBUG
|
2003-07-27 15:20:11 +00:00
|
|
|
SheetLoadData* removedData;
|
2013-09-02 08:41:57 +00:00
|
|
|
NS_ASSERTION(mSheets->mPendingDatas.Get(&key, &removedData) &&
|
2003-07-27 15:20:11 +00:00
|
|
|
removedData == existingData,
|
|
|
|
"Bad pending table.");
|
2002-12-03 05:48:14 +00:00
|
|
|
#endif
|
2003-07-27 15:20:11 +00:00
|
|
|
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets->mPendingDatas.Remove(&key);
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
LOG((" Forcing load of pending data"));
|
2005-09-12 18:41:15 +00:00
|
|
|
return LoadSheet(existingData, eSheetNeedsParser);
|
2001-10-31 19:03:46 +00:00
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
// All done here; once the load completes we'll be marked complete
|
|
|
|
// automatically
|
2001-10-31 19:03:46 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2011-10-17 14:59:28 +00:00
|
|
|
mSyncCallback = true;
|
2002-12-03 05:48:14 +00:00
|
|
|
#endif
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
2010-04-23 22:05:32 +00:00
|
|
|
// Content Security Policy information to pass into channel
|
|
|
|
nsCOMPtr<nsIChannelPolicy> channelPolicy;
|
2002-12-03 05:48:14 +00:00
|
|
|
if (mDocument) {
|
2003-10-22 06:09:48 +00:00
|
|
|
loadGroup = mDocument->GetDocumentLoadGroup();
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_ASSERTION(loadGroup,
|
|
|
|
"No loadgroup for stylesheet; onload will fire early");
|
2010-04-23 22:05:32 +00:00
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
rv = mDocument->NodePrincipal()->GetCsp(getter_AddRefs(csp));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (csp) {
|
|
|
|
channelPolicy = do_CreateInstance("@mozilla.org/nschannelpolicy;1");
|
|
|
|
channelPolicy->SetContentSecurityPolicy(csp);
|
|
|
|
channelPolicy->SetLoadType(nsIContentPolicy::TYPE_STYLESHEET);
|
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
2012-07-30 14:20:58 +00:00
|
|
|
aLoadData->mURI, nullptr, loadGroup, nullptr,
|
2010-05-21 21:03:02 +00:00
|
|
|
nsIChannel::LOAD_NORMAL | nsIChannel::LOAD_CLASSIFY_URI,
|
|
|
|
channelPolicy);
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2008-07-11 21:13:01 +00:00
|
|
|
#ifdef DEBUG
|
2011-10-17 14:59:28 +00:00
|
|
|
mSyncCallback = false;
|
2008-07-11 21:13:01 +00:00
|
|
|
#endif
|
2002-12-03 05:48:14 +00:00
|
|
|
LOG_ERROR((" Failed to create channel"));
|
2005-09-12 18:41:15 +00:00
|
|
|
SheetComplete(aLoadData, rv);
|
2002-12-03 05:48:14 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-12-04 23:06:29 +00:00
|
|
|
nsCOMPtr<nsIHttpChannelInternal>
|
|
|
|
internalHttpChannel(do_QueryInterface(channel));
|
|
|
|
if (internalHttpChannel)
|
|
|
|
internalHttpChannel->SetLoadAsBlocking(!aLoadData->mWasAlternate);
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
|
|
|
|
if (httpChannel) {
|
|
|
|
// send a minimal Accept header for text/css
|
|
|
|
httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
|
|
|
|
NS_LITERAL_CSTRING("text/css,*/*;q=0.1"),
|
2011-10-17 14:59:28 +00:00
|
|
|
false);
|
2004-08-18 02:32:25 +00:00
|
|
|
nsCOMPtr<nsIURI> referrerURI = aLoadData->GetReferrerURI();
|
|
|
|
if (referrerURI)
|
|
|
|
httpChannel->SetReferrer(referrerURI);
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
|
|
|
|
2003-06-23 21:00:46 +00:00
|
|
|
// Now tell the channel we expect text/css data back.... We do
|
|
|
|
// this before opening it, so it's only treated as a hint.
|
|
|
|
channel->SetContentType(NS_LITERAL_CSTRING("text/css"));
|
|
|
|
|
2007-05-01 19:26:15 +00:00
|
|
|
if (aLoadData->mLoaderPrincipal) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool inherit;
|
2007-05-01 19:26:15 +00:00
|
|
|
rv = NS_URIChainHasFlags(aLoadData->mURI,
|
|
|
|
nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT,
|
|
|
|
&inherit);
|
2008-03-22 16:50:47 +00:00
|
|
|
if ((NS_SUCCEEDED(rv) && inherit) ||
|
|
|
|
(nsContentUtils::URIIsLocalFile(aLoadData->mURI) &&
|
|
|
|
NS_SUCCEEDED(aLoadData->mLoaderPrincipal->
|
2012-08-20 18:34:33 +00:00
|
|
|
CheckMayLoad(aLoadData->mURI, false, false)))) {
|
2007-05-01 19:26:15 +00:00
|
|
|
channel->SetOwner(aLoadData->mLoaderPrincipal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
// We don't have to hold on to the stream loader. The ownership
|
|
|
|
// model is: Necko owns the stream loader, which owns the load data,
|
|
|
|
// which owns us
|
|
|
|
nsCOMPtr<nsIUnicharStreamLoader> streamLoader;
|
2008-03-22 16:50:47 +00:00
|
|
|
rv = NS_NewUnicharStreamLoader(getter_AddRefs(streamLoader), aLoadData);
|
2012-08-28 17:10:08 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
mSyncCallback = false;
|
|
|
|
#endif
|
|
|
|
LOG_ERROR((" Failed to create stream loader"));
|
|
|
|
SheetComplete(aLoadData, rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamListener> channelListener;
|
|
|
|
CORSMode ourCORSMode = aLoadData->mSheet->GetCORSMode();
|
|
|
|
if (ourCORSMode != CORS_NONE) {
|
|
|
|
bool withCredentials = (ourCORSMode == CORS_USE_CREDENTIALS);
|
|
|
|
LOG((" Doing CORS-enabled load; credentials %d", withCredentials));
|
2012-09-19 02:16:23 +00:00
|
|
|
nsRefPtr<nsCORSListenerProxy> corsListener =
|
2012-08-28 17:10:08 +00:00
|
|
|
new nsCORSListenerProxy(streamLoader, aLoadData->mLoaderPrincipal,
|
2012-09-19 02:16:23 +00:00
|
|
|
withCredentials);
|
|
|
|
rv = corsListener->Init(channel);
|
2012-08-28 17:10:08 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
mSyncCallback = false;
|
|
|
|
#endif
|
|
|
|
LOG_ERROR((" Initial CORS check failed"));
|
|
|
|
SheetComplete(aLoadData, rv);
|
|
|
|
return rv;
|
|
|
|
}
|
2012-09-19 02:16:23 +00:00
|
|
|
channelListener = corsListener;
|
2012-08-28 17:10:08 +00:00
|
|
|
} else {
|
|
|
|
channelListener = streamLoader;
|
|
|
|
}
|
2006-10-24 21:47:40 +00:00
|
|
|
|
2013-10-25 21:56:56 +00:00
|
|
|
if (mDocument) {
|
|
|
|
mozilla::net::SeerLearn(aLoadData->mURI, mDocument->GetDocumentURI(),
|
|
|
|
nsINetworkSeer::LEARN_LOAD_SUBRESOURCE, mDocument);
|
|
|
|
}
|
|
|
|
|
2012-08-28 17:10:08 +00:00
|
|
|
rv = channel->AsyncOpen(channelListener, nullptr);
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2011-10-17 14:59:28 +00:00
|
|
|
mSyncCallback = false;
|
2002-12-03 05:48:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG_ERROR((" Failed to create stream loader"));
|
2005-09-12 18:41:15 +00:00
|
|
|
SheetComplete(aLoadData, rv);
|
2002-12-03 05:48:14 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets->mLoadingDatas.Put(&key, aLoadData);
|
2011-10-17 14:59:28 +00:00
|
|
|
aLoadData->mIsLoading = true;
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
return NS_OK;
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
/**
|
|
|
|
* ParseSheet handles parsing the data stream. The main idea here is
|
|
|
|
* to push the current load data onto the parse stack before letting
|
|
|
|
* the CSS parser at the data stream. That lets us handle @import
|
|
|
|
* correctly.
|
|
|
|
*/
|
1999-05-18 23:15:10 +00:00
|
|
|
nsresult
|
2011-05-19 01:22:59 +00:00
|
|
|
Loader::ParseSheet(const nsAString& aInput,
|
2010-03-02 21:00:53 +00:00
|
|
|
SheetLoadData* aLoadData,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool& aCompleted)
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::ParseSheet"));
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_PRECONDITION(aLoadData, "Must have load data");
|
|
|
|
NS_PRECONDITION(aLoadData->mSheet, "Must have sheet to parse into");
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
aCompleted = false;
|
2003-07-18 00:35:49 +00:00
|
|
|
|
2010-03-02 20:59:32 +00:00
|
|
|
nsCSSParser parser(this, aLoadData->mSheet);
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
// Push our load data on the stack so any kids can pick it up
|
|
|
|
mParsingDatas.AppendElement(aLoadData);
|
2010-05-18 04:00:40 +00:00
|
|
|
nsIURI* sheetURI = aLoadData->mSheet->GetSheetURI();
|
|
|
|
nsIURI* baseURI = aLoadData->mSheet->GetBaseURI();
|
2011-05-19 01:22:59 +00:00
|
|
|
nsresult rv = parser.ParseSheet(aInput, sheetURI, baseURI,
|
2011-05-19 01:22:59 +00:00
|
|
|
aLoadData->mSheet->Principal(),
|
|
|
|
aLoadData->mLineNumber,
|
|
|
|
aLoadData->mAllowUnsafeRules);
|
2009-02-03 14:42:18 +00:00
|
|
|
mParsingDatas.RemoveElementAt(mParsingDatas.Length() - 1);
|
2010-03-02 20:59:32 +00:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG_ERROR((" Low-level error in parser!"));
|
|
|
|
SheetComplete(aLoadData, rv);
|
|
|
|
return rv;
|
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
|
2002-12-11 00:28:47 +00:00
|
|
|
NS_ASSERTION(aLoadData->mPendingChildren == 0 || !aLoadData->mSyncLoad,
|
|
|
|
"Sync load has leftover pending children!");
|
2010-03-02 20:59:32 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
if (aLoadData->mPendingChildren == 0) {
|
|
|
|
LOG((" No pending kids from parse"));
|
2011-10-17 14:59:28 +00:00
|
|
|
aCompleted = true;
|
2005-09-12 18:41:15 +00:00
|
|
|
SheetComplete(aLoadData, NS_OK);
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
|
|
|
// Otherwise, the children are holding strong refs to the data and
|
|
|
|
// will call SheetComplete() on it when they complete.
|
2010-03-02 20:59:32 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SheetComplete is the do-it-all cleanup function. It removes the
|
|
|
|
* load data from the "loading" hashtable, adds the sheet to the
|
|
|
|
* "completed" hashtable, massages the XUL cache, handles siblings of
|
2005-09-12 18:41:15 +00:00
|
|
|
* the load data (other loads for the same URI), handles unblocking
|
2002-12-03 05:48:14 +00:00
|
|
|
* blocked parent loads as needed, and most importantly calls
|
|
|
|
* NS_RELEASE on the load data to destroy the whole mess.
|
|
|
|
*/
|
|
|
|
void
|
2010-03-02 21:00:53 +00:00
|
|
|
Loader::SheetComplete(SheetLoadData* aLoadData, nsresult aStatus)
|
2002-12-03 05:48:14 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::SheetComplete"));
|
2007-04-20 22:59:18 +00:00
|
|
|
|
|
|
|
// 8 is probably big enough for all our common cases. It's not likely that
|
|
|
|
// imports will nest more than 8 deep, and multiple sheets with the same URI
|
|
|
|
// are rare.
|
|
|
|
nsAutoTArray<nsRefPtr<SheetLoadData>, 8> datasToNotify;
|
|
|
|
DoSheetComplete(aLoadData, aStatus, datasToNotify);
|
|
|
|
|
|
|
|
// Now it's safe to go ahead and notify observers
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count = datasToNotify.Length();
|
2007-05-31 01:42:48 +00:00
|
|
|
mDatasToNotifyOn += count;
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2007-05-31 01:42:48 +00:00
|
|
|
--mDatasToNotifyOn;
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2007-04-20 22:59:18 +00:00
|
|
|
SheetLoadData* data = datasToNotify[i];
|
2007-05-31 01:42:48 +00:00
|
|
|
NS_ASSERTION(data && data->mMustNotify, "How did this data get here?");
|
|
|
|
if (data->mObserver) {
|
2009-03-30 15:25:35 +00:00
|
|
|
LOG((" Notifying observer 0x%x for data 0x%x. wasAlternate: %d",
|
2007-05-31 01:42:48 +00:00
|
|
|
data->mObserver.get(), data, data->mWasAlternate));
|
|
|
|
data->mObserver->StyleSheetLoaded(data->mSheet, data->mWasAlternate,
|
|
|
|
aStatus);
|
|
|
|
}
|
|
|
|
|
2007-12-22 00:56:28 +00:00
|
|
|
nsTObserverArray<nsCOMPtr<nsICSSLoaderObserver> >::ForwardIterator iter(mObservers);
|
2007-05-31 01:42:48 +00:00
|
|
|
nsCOMPtr<nsICSSLoaderObserver> obs;
|
2007-12-20 07:30:04 +00:00
|
|
|
while (iter.HasMore()) {
|
|
|
|
obs = iter.GetNext();
|
2007-05-31 01:42:48 +00:00
|
|
|
LOG((" Notifying global observer 0x%x for data 0x%s. wasAlternate: %d",
|
|
|
|
obs.get(), data, data->mWasAlternate));
|
|
|
|
obs->StyleSheetLoaded(data->mSheet, data->mWasAlternate, aStatus);
|
|
|
|
}
|
2007-04-20 22:59:18 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 08:41:57 +00:00
|
|
|
if (mSheets->mLoadingDatas.Count() == 0 && mSheets->mPendingDatas.Count() > 0) {
|
2007-04-20 22:59:18 +00:00
|
|
|
LOG((" No more loading sheets; starting alternates"));
|
2007-05-31 01:42:48 +00:00
|
|
|
StartAlternateLoads();
|
2007-04-20 22:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-03-02 21:00:53 +00:00
|
|
|
Loader::DoSheetComplete(SheetLoadData* aLoadData, nsresult aStatus,
|
|
|
|
LoadDataArray& aDatasToNotify)
|
2007-04-20 22:59:18 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::DoSheetComplete"));
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_PRECONDITION(aLoadData, "Must have a load data!");
|
|
|
|
NS_PRECONDITION(aLoadData->mSheet, "Must have a sheet");
|
2013-09-02 08:41:57 +00:00
|
|
|
NS_ASSERTION(mSheets, "mLoadingDatas should be initialized by now.");
|
2002-12-03 05:48:14 +00:00
|
|
|
|
2005-09-12 18:41:15 +00:00
|
|
|
LOG(("Load completed, status: 0x%x", aStatus));
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
// Twiddle the hashtables
|
|
|
|
if (aLoadData->mURI) {
|
|
|
|
LOG_URI(" Finished loading: '%s'", aLoadData->mURI);
|
|
|
|
// Remove the data from the list of loading datas
|
|
|
|
if (aLoadData->mIsLoading) {
|
2012-08-28 17:10:08 +00:00
|
|
|
URIPrincipalAndCORSModeHashKey key(aLoadData->mURI,
|
|
|
|
aLoadData->mLoaderPrincipal,
|
|
|
|
aLoadData->mSheet->GetCORSMode());
|
2003-07-27 15:46:52 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
SheetLoadData *loadingData;
|
2013-09-02 08:41:57 +00:00
|
|
|
NS_ASSERTION(mSheets->mLoadingDatas.Get(&key, &loadingData) &&
|
2003-07-27 15:46:52 +00:00
|
|
|
loadingData == aLoadData,
|
|
|
|
"Bad loading table");
|
|
|
|
#endif
|
|
|
|
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets->mLoadingDatas.Remove(&key);
|
2011-10-17 14:59:28 +00:00
|
|
|
aLoadData->mIsLoading = false;
|
1999-09-30 01:39:57 +00:00
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
// Go through and deal with the whole linked list.
|
|
|
|
SheetLoadData* data = aLoadData;
|
|
|
|
while (data) {
|
2011-09-26 21:26:54 +00:00
|
|
|
if (!data->mSheetAlreadyComplete) {
|
2011-10-03 19:11:59 +00:00
|
|
|
// If mSheetAlreadyComplete, then the sheet could well be modified between
|
|
|
|
// when we posted the async call to SheetComplete and now, since the sheet
|
|
|
|
// was page-accessible during that whole time.
|
|
|
|
NS_ABORT_IF_FALSE(!data->mSheet->IsModified(),
|
|
|
|
"should not get marked modified during parsing");
|
2011-09-26 21:26:54 +00:00
|
|
|
data->mSheet->SetComplete();
|
2011-09-26 21:27:13 +00:00
|
|
|
data->ScheduleLoadEventIfNeeded(aStatus);
|
2011-09-26 21:26:54 +00:00
|
|
|
}
|
2007-05-31 01:42:48 +00:00
|
|
|
if (data->mMustNotify && (data->mObserver || !mObservers.IsEmpty())) {
|
2007-04-20 22:59:18 +00:00
|
|
|
// Don't notify here so we don't trigger script. Remember the
|
|
|
|
// info we need to notify, then do it later when it's safe.
|
|
|
|
aDatasToNotify.AppendElement(data);
|
2005-09-12 18:41:15 +00:00
|
|
|
|
2007-04-20 22:59:18 +00:00
|
|
|
// On append failure, just press on. We'll fail to notify the observer,
|
|
|
|
// but not much we can do about that....
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
2002-08-27 23:47:25 +00:00
|
|
|
|
2003-01-07 00:37:19 +00:00
|
|
|
NS_ASSERTION(!data->mParentData ||
|
|
|
|
data->mParentData->mPendingChildren != 0,
|
|
|
|
"Broken pending child count on our parent");
|
|
|
|
|
2003-01-07 01:13:07 +00:00
|
|
|
// If we have a parent, our parent is no longer being parsed, and
|
|
|
|
// we are the last pending child, then our load completion
|
|
|
|
// completes the parent too. Note that the parent _can_ still be
|
|
|
|
// being parsed (eg if the child (us) failed to open the channel
|
|
|
|
// or some such).
|
2002-12-03 05:48:14 +00:00
|
|
|
if (data->mParentData &&
|
|
|
|
--(data->mParentData->mPendingChildren) == 0 &&
|
2009-02-03 14:42:18 +00:00
|
|
|
!mParsingDatas.Contains(data->mParentData)) {
|
2007-04-20 22:59:18 +00:00
|
|
|
DoSheetComplete(data->mParentData, aStatus, aDatasToNotify);
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
data = data->mNext;
|
|
|
|
}
|
2002-08-27 23:47:25 +00:00
|
|
|
|
2011-09-26 21:27:13 +00:00
|
|
|
// Now that it's marked complete, put the sheet in our cache.
|
|
|
|
// If we ever start doing this for failure aStatus, we'll need to
|
|
|
|
// adjust the PostLoadEvent code that thinks anything already
|
|
|
|
// complete must have loaded succesfully.
|
2005-09-12 18:41:15 +00:00
|
|
|
if (NS_SUCCEEDED(aStatus) && aLoadData->mURI) {
|
2013-04-24 18:59:14 +00:00
|
|
|
// Pick our sheet to cache carefully. Ideally, we want to cache
|
|
|
|
// one of the sheets that will be kept alive by a document or
|
|
|
|
// parent sheet anyway, so that if someone then accesses it via
|
|
|
|
// CSSOM we won't have extra clones of the inner lying around.
|
|
|
|
data = aLoadData;
|
|
|
|
nsCSSStyleSheet* sheet = aLoadData->mSheet;
|
|
|
|
while (data) {
|
|
|
|
if (data->mSheet->GetParentSheet() || data->mSheet->GetOwnerNode()) {
|
|
|
|
sheet = data->mSheet;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data = data->mNext;
|
|
|
|
}
|
2003-04-11 00:56:27 +00:00
|
|
|
#ifdef MOZ_XUL
|
2003-01-17 04:20:03 +00:00
|
|
|
if (IsChromeURI(aLoadData->mURI)) {
|
2007-03-12 05:53:33 +00:00
|
|
|
nsXULPrototypeCache* cache = nsXULPrototypeCache::GetInstance();
|
|
|
|
if (cache && cache->IsEnabled()) {
|
|
|
|
if (!cache->GetStyleSheet(aLoadData->mURI)) {
|
|
|
|
LOG((" Putting sheet in XUL prototype cache"));
|
2013-05-23 13:20:52 +00:00
|
|
|
NS_ASSERTION(sheet->IsComplete(),
|
|
|
|
"Should only be caching complete sheets");
|
2013-04-24 18:59:14 +00:00
|
|
|
cache->PutStyleSheet(sheet);
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-01-17 04:20:03 +00:00
|
|
|
else {
|
2002-12-03 05:48:14 +00:00
|
|
|
#endif
|
2012-08-28 17:10:08 +00:00
|
|
|
URIPrincipalAndCORSModeHashKey key(aLoadData->mURI,
|
|
|
|
aLoadData->mLoaderPrincipal,
|
|
|
|
aLoadData->mSheet->GetCORSMode());
|
2013-05-23 13:20:52 +00:00
|
|
|
NS_ASSERTION(sheet->IsComplete(),
|
|
|
|
"Should only be caching complete sheets");
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets->mCompleteSheets.Put(&key, sheet);
|
2003-04-11 00:56:27 +00:00
|
|
|
#ifdef MOZ_XUL
|
2003-01-17 04:20:03 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
NS_RELEASE(aLoadData); // this will release parents and siblings and all that
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
nsresult
|
|
|
|
Loader::LoadInlineStyle(nsIContent* aElement,
|
2011-05-19 01:22:59 +00:00
|
|
|
const nsAString& aBuffer,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aLineNumber,
|
2010-03-08 09:34:55 +00:00
|
|
|
const nsAString& aTitle,
|
|
|
|
const nsAString& aMedia,
|
2013-01-08 23:25:47 +00:00
|
|
|
Element* aScopeElement,
|
2010-03-02 21:00:53 +00:00
|
|
|
nsICSSLoaderObserver* aObserver,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool* aCompleted,
|
|
|
|
bool* aIsAlternate)
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::LoadInlineStyle"));
|
2009-02-03 14:42:18 +00:00
|
|
|
NS_ASSERTION(mParsingDatas.Length() == 0, "We're in the middle of a parse?");
|
2002-12-03 05:48:14 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
*aCompleted = true;
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
if (!mEnabled) {
|
|
|
|
LOG_WARN((" Not enabled"));
|
2002-10-11 00:38:29 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
2002-10-11 00:38:29 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_ENSURE_TRUE(mDocument, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStyleSheetLinkingElement> owningElement(do_QueryInterface(aElement));
|
|
|
|
NS_ASSERTION(owningElement, "Element is not a style linking element!");
|
2007-05-01 19:26:15 +00:00
|
|
|
|
|
|
|
// Since we're not planning to load a URI, no need to hand a principal to the
|
2012-08-28 17:10:08 +00:00
|
|
|
// load data or to CreateSheet(). Also, OK to use CORS_NONE for the CORS
|
|
|
|
// mode.
|
2002-12-03 05:48:14 +00:00
|
|
|
StyleSheetState state;
|
2010-05-11 20:41:47 +00:00
|
|
|
nsRefPtr<nsCSSStyleSheet> sheet;
|
2012-08-28 17:10:08 +00:00
|
|
|
nsresult rv = CreateSheet(nullptr, aElement, nullptr, CORS_NONE, false, false,
|
2011-11-08 04:41:17 +00:00
|
|
|
aTitle, state, aIsAlternate, getter_AddRefs(sheet));
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ASSERTION(state == eSheetNeedsParser,
|
|
|
|
"Inline sheets should not be cached");
|
|
|
|
|
2005-09-12 18:41:15 +00:00
|
|
|
LOG((" Sheet is alternate: %d", *aIsAlternate));
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2013-11-11 07:57:53 +00:00
|
|
|
PrepareSheet(sheet, aTitle, aMedia, nullptr, aScopeElement, *aIsAlternate);
|
2011-11-08 04:41:17 +00:00
|
|
|
|
2013-12-02 10:26:12 +00:00
|
|
|
if (aElement->HasFlag(NODE_IS_IN_SHADOW_TREE)) {
|
|
|
|
ShadowRoot* containingShadow = aElement->GetContainingShadow();
|
|
|
|
MOZ_ASSERT(containingShadow);
|
|
|
|
containingShadow->InsertSheet(sheet, aElement);
|
|
|
|
} else {
|
|
|
|
rv = InsertSheetInDoc(sheet, aElement, mDocument);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
SheetLoadData* data = new SheetLoadData(this, aTitle, nullptr, sheet,
|
2007-04-20 22:59:18 +00:00
|
|
|
owningElement, *aIsAlternate,
|
2012-07-30 14:20:58 +00:00
|
|
|
aObserver, nullptr);
|
2002-12-03 05:48:14 +00:00
|
|
|
|
2007-05-01 19:26:15 +00:00
|
|
|
// We never actually load this, so just set its principal directly
|
|
|
|
sheet->SetPrincipal(aElement->NodePrincipal());
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_ADDREF(data);
|
2004-09-09 17:32:35 +00:00
|
|
|
data->mLineNumber = aLineNumber;
|
2002-12-03 05:48:14 +00:00
|
|
|
// Parse completion releases the load data
|
2011-05-19 01:22:59 +00:00
|
|
|
rv = ParseSheet(aBuffer, data, *aCompleted);
|
2005-09-12 18:41:15 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// If aCompleted is true, |data| may well be deleted by now.
|
|
|
|
if (!*aCompleted) {
|
2011-10-17 14:59:28 +00:00
|
|
|
data->mMustNotify = true;
|
2005-09-12 18:41:15 +00:00
|
|
|
}
|
|
|
|
return rv;
|
2011-05-06 15:45:36 +00:00
|
|
|
}
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
nsresult
|
|
|
|
Loader::LoadStyleLink(nsIContent* aElement,
|
2011-05-06 15:45:36 +00:00
|
|
|
nsIURI* aURL,
|
2010-03-08 09:34:55 +00:00
|
|
|
const nsAString& aTitle,
|
|
|
|
const nsAString& aMedia,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aHasAlternateRel,
|
2012-08-28 17:10:07 +00:00
|
|
|
CORSMode aCORSMode,
|
2010-03-02 21:00:53 +00:00
|
|
|
nsICSSLoaderObserver* aObserver,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool* aIsAlternate)
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::LoadStyleLink"));
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_PRECONDITION(aURL, "Must have URL to load");
|
2009-02-03 14:42:18 +00:00
|
|
|
NS_ASSERTION(mParsingDatas.Length() == 0, "We're in the middle of a parse?");
|
2002-10-11 00:38:29 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
LOG_URI(" Link uri: '%s'", aURL);
|
2005-09-12 18:41:15 +00:00
|
|
|
LOG((" Link title: '%s'", NS_ConvertUTF16toUTF8(aTitle).get()));
|
|
|
|
LOG((" Link media: '%s'", NS_ConvertUTF16toUTF8(aMedia).get()));
|
|
|
|
LOG((" Link alternate rel: %d", aHasAlternateRel));
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
if (!mEnabled) {
|
|
|
|
LOG_WARN((" Not enabled"));
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
1999-05-26 23:52:37 +00:00
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_ENSURE_TRUE(mDocument, NS_ERROR_NOT_INITIALIZED);
|
2000-05-27 05:22:39 +00:00
|
|
|
|
2007-05-01 19:26:15 +00:00
|
|
|
nsIPrincipal* principal =
|
|
|
|
aElement ? aElement->NodePrincipal() : mDocument->NodePrincipal();
|
|
|
|
|
2004-07-27 17:15:53 +00:00
|
|
|
nsISupports* context = aElement;
|
|
|
|
if (!context) {
|
|
|
|
context = mDocument;
|
2004-05-13 18:34:18 +00:00
|
|
|
}
|
2007-08-08 01:16:09 +00:00
|
|
|
nsresult rv = CheckLoadAllowed(principal, aURL, context);
|
2001-11-17 02:36:05 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-05-26 23:52:37 +00:00
|
|
|
|
2003-04-04 23:57:06 +00:00
|
|
|
LOG((" Passed load check"));
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
StyleSheetState state;
|
2010-05-11 20:41:47 +00:00
|
|
|
nsRefPtr<nsCSSStyleSheet> sheet;
|
2012-08-28 17:10:08 +00:00
|
|
|
rv = CreateSheet(aURL, aElement, principal, aCORSMode, false,
|
|
|
|
aHasAlternateRel, aTitle, state, aIsAlternate,
|
|
|
|
getter_AddRefs(sheet));
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2005-09-12 18:41:15 +00:00
|
|
|
|
|
|
|
LOG((" Sheet is alternate: %d", *aIsAlternate));
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2013-11-11 07:57:53 +00:00
|
|
|
PrepareSheet(sheet, aTitle, aMedia, nullptr, nullptr, *aIsAlternate);
|
2011-11-08 04:41:17 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
rv = InsertSheetInDoc(sheet, aElement, mDocument);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2011-09-26 21:27:00 +00:00
|
|
|
nsCOMPtr<nsIStyleSheetLinkingElement> owningElement(do_QueryInterface(aElement));
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
if (state == eSheetComplete) {
|
2005-09-12 18:41:15 +00:00
|
|
|
LOG((" Sheet already complete: 0x%p",
|
2007-07-08 07:08:04 +00:00
|
|
|
static_cast<void*>(sheet.get())));
|
2011-09-26 21:27:00 +00:00
|
|
|
if (aObserver || !mObservers.IsEmpty() || owningElement) {
|
|
|
|
rv = PostLoadEvent(aURL, sheet, aObserver, *aIsAlternate,
|
|
|
|
owningElement);
|
2007-04-20 22:59:18 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
2000-07-28 09:36:51 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
// Now we need to actually load it
|
2007-04-20 22:59:18 +00:00
|
|
|
SheetLoadData* data = new SheetLoadData(this, aTitle, aURL, sheet,
|
|
|
|
owningElement, *aIsAlternate,
|
2007-05-01 19:26:15 +00:00
|
|
|
aObserver, principal);
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_ADDREF(data);
|
|
|
|
|
|
|
|
// If we have to parse and it's an alternate non-inline, defer it
|
2013-09-02 08:41:57 +00:00
|
|
|
if (aURL && state == eSheetNeedsParser && mSheets->mLoadingDatas.Count() != 0 &&
|
2005-09-12 18:41:15 +00:00
|
|
|
*aIsAlternate) {
|
2002-12-03 05:48:14 +00:00
|
|
|
LOG((" Deferring alternate sheet load"));
|
2012-08-28 17:10:08 +00:00
|
|
|
URIPrincipalAndCORSModeHashKey key(data->mURI, data->mLoaderPrincipal,
|
|
|
|
data->mSheet->GetCORSMode());
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets->mPendingDatas.Put(&key, data);
|
2005-09-12 18:41:15 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
data->mMustNotify = true;
|
2002-12-03 05:48:14 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load completion will free the data
|
2005-09-12 18:41:15 +00:00
|
|
|
rv = LoadSheet(data, state);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
data->mMustNotify = true;
|
2005-09-12 18:41:15 +00:00
|
|
|
return rv;
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool
|
2011-08-08 02:23:51 +00:00
|
|
|
HaveAncestorDataWithURI(SheetLoadData *aData, nsIURI *aURI)
|
|
|
|
{
|
|
|
|
if (!aData->mURI) {
|
|
|
|
// Inline style; this won't have any ancestors
|
|
|
|
NS_ABORT_IF_FALSE(!aData->mParentData,
|
|
|
|
"How does inline style have a parent?");
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2011-08-08 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool equal;
|
2011-08-08 02:23:51 +00:00
|
|
|
if (NS_FAILED(aData->mURI->Equals(aURI, &equal)) || equal) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2011-08-08 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Datas down the mNext chain have the same URI as aData, so we
|
|
|
|
// don't have to compare to them. But they might have different
|
|
|
|
// parents, and we have to check all of those.
|
|
|
|
while (aData) {
|
|
|
|
if (aData->mParentData &&
|
|
|
|
HaveAncestorDataWithURI(aData->mParentData, aURI)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2011-08-08 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aData = aData->mNext;
|
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2011-08-08 02:23:51 +00:00
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
nsresult
|
2010-05-11 20:41:47 +00:00
|
|
|
Loader::LoadChildSheet(nsCSSStyleSheet* aParentSheet,
|
2011-05-06 15:45:36 +00:00
|
|
|
nsIURI* aURL,
|
2010-03-02 21:00:53 +00:00
|
|
|
nsMediaList* aMedia,
|
2011-03-17 17:41:52 +00:00
|
|
|
ImportRule* aParentRule)
|
1999-05-18 23:15:10 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::LoadChildSheet"));
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_PRECONDITION(aURL, "Must have a URI to load");
|
2007-05-01 19:26:15 +00:00
|
|
|
NS_PRECONDITION(aParentSheet, "Must have a parent sheet");
|
2002-10-11 00:38:29 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
if (!mEnabled) {
|
|
|
|
LOG_WARN((" Not enabled"));
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
LOG_URI(" Child uri: '%s'", aURL);
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2004-05-13 18:34:18 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> owningNode;
|
|
|
|
|
|
|
|
// check for an owning document: if none, don't bother walking up the parent
|
|
|
|
// sheets
|
2010-05-18 04:00:40 +00:00
|
|
|
if (aParentSheet->GetOwningDocument()) {
|
2010-05-11 20:41:47 +00:00
|
|
|
nsCOMPtr<nsIDOMStyleSheet> nextParentSheet(aParentSheet);
|
2004-05-13 18:34:18 +00:00
|
|
|
NS_ENSURE_TRUE(nextParentSheet, NS_ERROR_FAILURE); //Not a stylesheet!?
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMStyleSheet> topSheet;
|
|
|
|
//traverse our way to the top-most sheet
|
|
|
|
do {
|
|
|
|
topSheet.swap(nextParentSheet);
|
|
|
|
topSheet->GetParentStyleSheet(getter_AddRefs(nextParentSheet));
|
|
|
|
} while (nextParentSheet);
|
|
|
|
|
|
|
|
topSheet->GetOwnerNode(getter_AddRefs(owningNode));
|
|
|
|
}
|
|
|
|
|
2004-07-27 17:15:53 +00:00
|
|
|
nsISupports* context = owningNode;
|
|
|
|
if (!context) {
|
|
|
|
context = mDocument;
|
2004-05-13 18:34:18 +00:00
|
|
|
}
|
2007-05-01 19:26:15 +00:00
|
|
|
|
|
|
|
nsIPrincipal* principal = aParentSheet->Principal();
|
2010-05-12 20:18:47 +00:00
|
|
|
nsresult rv = CheckLoadAllowed(principal, aURL, context);
|
2002-12-03 05:48:14 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2003-04-04 23:57:06 +00:00
|
|
|
LOG((" Passed load check"));
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
SheetLoadData* parentData = nullptr;
|
2002-12-03 05:48:14 +00:00
|
|
|
nsCOMPtr<nsICSSLoaderObserver> observer;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t count = mParsingDatas.Length();
|
2002-12-03 05:48:14 +00:00
|
|
|
if (count > 0) {
|
|
|
|
LOG((" Have a parent load"));
|
2009-02-03 14:42:18 +00:00
|
|
|
parentData = mParsingDatas.ElementAt(count - 1);
|
2002-12-03 05:48:14 +00:00
|
|
|
// Check for cycles
|
2011-08-08 02:23:51 +00:00
|
|
|
if (HaveAncestorDataWithURI(parentData, aURL)) {
|
|
|
|
// Houston, we have a loop, blow off this child and pretend this never
|
|
|
|
// happened
|
|
|
|
LOG_ERROR((" @import cycle detected, dropping load"));
|
|
|
|
return NS_OK;
|
2000-07-28 09:36:51 +00:00
|
|
|
}
|
|
|
|
|
2003-07-09 01:48:20 +00:00
|
|
|
NS_ASSERTION(parentData->mSheet == aParentSheet,
|
2002-12-03 05:48:14 +00:00
|
|
|
"Unexpected call to LoadChildSheet");
|
|
|
|
} else {
|
|
|
|
LOG((" No parent load; must be CSSOM"));
|
|
|
|
// No parent load data, so the sheet will need to be notified when
|
|
|
|
// we finish, if it can be, if we do the load asynchronously.
|
2010-05-11 20:41:47 +00:00
|
|
|
observer = aParentSheet;
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
// Now that we know it's safe to load this (passes security check and not a
|
2012-08-28 17:10:08 +00:00
|
|
|
// loop) do so.
|
2010-05-11 20:41:47 +00:00
|
|
|
nsRefPtr<nsCSSStyleSheet> sheet;
|
2011-11-08 04:41:17 +00:00
|
|
|
bool isAlternate;
|
2002-12-03 05:48:14 +00:00
|
|
|
StyleSheetState state;
|
2011-11-08 04:41:17 +00:00
|
|
|
const nsSubstring& empty = EmptyString();
|
2012-08-28 17:10:08 +00:00
|
|
|
// For now, use CORS_NONE for child sheets
|
|
|
|
rv = CreateSheet(aURL, nullptr, principal, CORS_NONE,
|
2011-10-17 14:59:28 +00:00
|
|
|
parentData ? parentData->mSyncLoad : false,
|
2011-11-08 04:41:17 +00:00
|
|
|
false, empty, state, &isAlternate, getter_AddRefs(sheet));
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-11-11 07:57:53 +00:00
|
|
|
PrepareSheet(sheet, empty, empty, aMedia, nullptr, isAlternate);
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
rv = InsertChildSheet(sheet, aParentSheet, aParentRule);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
if (state == eSheetComplete) {
|
|
|
|
LOG((" Sheet already complete"));
|
|
|
|
// We're completely done. No need to notify, even, since the
|
|
|
|
// @import rule addition/modification will trigger the right style
|
|
|
|
// changes automatically.
|
|
|
|
return NS_OK;
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
SheetLoadData* data = new SheetLoadData(this, aURL, sheet, parentData,
|
2007-05-01 19:26:15 +00:00
|
|
|
observer, principal);
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
NS_ADDREF(data);
|
2011-09-29 06:19:26 +00:00
|
|
|
bool syncLoad = data->mSyncLoad;
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
// Load completion will release the data
|
2005-09-12 18:41:15 +00:00
|
|
|
rv = LoadSheet(data, state);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2005-09-16 15:38:04 +00:00
|
|
|
// If syncLoad is true, |data| will be deleted by now.
|
|
|
|
if (!syncLoad) {
|
2011-10-17 14:59:28 +00:00
|
|
|
data->mMustNotify = true;
|
2005-09-16 15:38:04 +00:00
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
return rv;
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
Loader::LoadSheetSync(nsIURI* aURL, bool aAllowUnsafeRules,
|
|
|
|
bool aUseSystemPrincipal,
|
2010-05-11 20:41:47 +00:00
|
|
|
nsCSSStyleSheet** aSheet)
|
2002-12-03 05:48:14 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::LoadSheetSync"));
|
2009-03-17 01:38:36 +00:00
|
|
|
return InternalLoadNonDocumentSheet(aURL, aAllowUnsafeRules,
|
2012-07-30 14:20:58 +00:00
|
|
|
aUseSystemPrincipal, nullptr,
|
|
|
|
EmptyCString(), aSheet, nullptr);
|
1999-05-18 23:15:10 +00:00
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
nsresult
|
|
|
|
Loader::LoadSheet(nsIURI* aURL,
|
|
|
|
nsIPrincipal* aOriginPrincipal,
|
|
|
|
const nsCString& aCharset,
|
|
|
|
nsICSSLoaderObserver* aObserver,
|
2010-05-11 20:41:47 +00:00
|
|
|
nsCSSStyleSheet** aSheet)
|
2006-11-11 22:27:00 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::LoadSheet(aURL, aObserver, aSheet) api call"));
|
2006-11-11 22:27:00 +00:00
|
|
|
NS_PRECONDITION(aSheet, "aSheet is null");
|
2011-10-17 14:59:28 +00:00
|
|
|
return InternalLoadNonDocumentSheet(aURL, false, false,
|
2009-04-10 14:29:08 +00:00
|
|
|
aOriginPrincipal, aCharset,
|
2007-08-08 01:16:09 +00:00
|
|
|
aSheet, aObserver);
|
2006-11-11 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
nsresult
|
|
|
|
Loader::LoadSheet(nsIURI* aURL,
|
|
|
|
nsIPrincipal* aOriginPrincipal,
|
|
|
|
const nsCString& aCharset,
|
2012-08-28 17:10:08 +00:00
|
|
|
nsICSSLoaderObserver* aObserver,
|
|
|
|
CORSMode aCORSMode)
|
1999-05-26 23:52:37 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::LoadSheet(aURL, aObserver) api call"));
|
2011-10-17 14:59:28 +00:00
|
|
|
return InternalLoadNonDocumentSheet(aURL, false, false,
|
2009-04-10 14:29:08 +00:00
|
|
|
aOriginPrincipal, aCharset,
|
2012-08-28 17:10:08 +00:00
|
|
|
nullptr, aObserver, aCORSMode);
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-05-06 15:45:36 +00:00
|
|
|
Loader::InternalLoadNonDocumentSheet(nsIURI* aURL,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aAllowUnsafeRules,
|
|
|
|
bool aUseSystemPrincipal,
|
2010-03-02 21:00:53 +00:00
|
|
|
nsIPrincipal* aOriginPrincipal,
|
|
|
|
const nsCString& aCharset,
|
2010-05-11 20:41:47 +00:00
|
|
|
nsCSSStyleSheet** aSheet,
|
2012-08-28 17:10:08 +00:00
|
|
|
nsICSSLoaderObserver* aObserver,
|
|
|
|
CORSMode aCORSMode)
|
2002-12-03 05:48:14 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aURL, "Must have a URI to load");
|
2006-11-17 22:45:52 +00:00
|
|
|
NS_PRECONDITION(aSheet || aObserver, "Sheet and observer can't both be null");
|
2009-03-17 01:38:36 +00:00
|
|
|
NS_PRECONDITION(!aUseSystemPrincipal || !aObserver,
|
|
|
|
"Shouldn't load system-principal sheets async");
|
2009-02-03 14:42:18 +00:00
|
|
|
NS_ASSERTION(mParsingDatas.Length() == 0, "We're in the middle of a parse?");
|
2002-12-03 05:48:14 +00:00
|
|
|
|
2005-09-12 18:41:15 +00:00
|
|
|
LOG_URI(" Non-document sheet uri: '%s'", aURL);
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2005-09-12 18:41:15 +00:00
|
|
|
if (aSheet) {
|
2012-07-30 14:20:58 +00:00
|
|
|
*aSheet = nullptr;
|
2005-09-12 18:41:15 +00:00
|
|
|
}
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
if (!mEnabled) {
|
|
|
|
LOG_WARN((" Not enabled"));
|
2002-10-11 00:38:29 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
|
|
|
|
2007-08-08 01:16:09 +00:00
|
|
|
nsresult rv = CheckLoadAllowed(aOriginPrincipal, aURL, mDocument);
|
2007-07-11 19:44:27 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
StyleSheetState state;
|
2011-11-08 04:41:17 +00:00
|
|
|
bool isAlternate;
|
2010-05-11 20:41:47 +00:00
|
|
|
nsRefPtr<nsCSSStyleSheet> sheet;
|
2012-07-30 14:20:58 +00:00
|
|
|
bool syncLoad = (aObserver == nullptr);
|
2011-11-08 04:41:17 +00:00
|
|
|
const nsSubstring& empty = EmptyString();
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2012-08-28 17:10:08 +00:00
|
|
|
rv = CreateSheet(aURL, nullptr, aOriginPrincipal, aCORSMode, syncLoad, false,
|
|
|
|
empty, state, &isAlternate, getter_AddRefs(sheet));
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-11-11 07:57:53 +00:00
|
|
|
PrepareSheet(sheet, empty, empty, nullptr, nullptr, isAlternate);
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
if (state == eSheetComplete) {
|
|
|
|
LOG((" Sheet already complete"));
|
2011-09-26 21:26:54 +00:00
|
|
|
if (aObserver || !mObservers.IsEmpty()) {
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = PostLoadEvent(aURL, sheet, aObserver, false, nullptr);
|
2006-11-11 22:27:00 +00:00
|
|
|
}
|
2002-12-03 05:48:14 +00:00
|
|
|
if (aSheet) {
|
2005-09-12 18:41:15 +00:00
|
|
|
sheet.swap(*aSheet);
|
1999-05-26 23:52:37 +00:00
|
|
|
}
|
2005-09-12 18:41:15 +00:00
|
|
|
return rv;
|
1999-05-26 23:52:37 +00:00
|
|
|
}
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2006-06-15 03:20:19 +00:00
|
|
|
SheetLoadData* data =
|
2007-07-11 19:44:27 +00:00
|
|
|
new SheetLoadData(this, aURL, sheet, syncLoad, aAllowUnsafeRules,
|
2009-04-10 14:29:08 +00:00
|
|
|
aUseSystemPrincipal, aCharset, aObserver,
|
|
|
|
aOriginPrincipal);
|
2002-12-03 05:48:14 +00:00
|
|
|
|
|
|
|
NS_ADDREF(data);
|
|
|
|
rv = LoadSheet(data, state);
|
2005-09-12 18:41:15 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-12-03 05:48:14 +00:00
|
|
|
|
2005-09-12 18:41:15 +00:00
|
|
|
if (aSheet) {
|
|
|
|
sheet.swap(*aSheet);
|
2006-11-11 22:27:00 +00:00
|
|
|
}
|
|
|
|
if (aObserver) {
|
2011-10-17 14:59:28 +00:00
|
|
|
data->mMustNotify = true;
|
2002-12-03 05:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
1999-05-18 23:15:10 +00:00
|
|
|
|
2005-09-12 18:41:15 +00:00
|
|
|
nsresult
|
2010-03-02 21:00:53 +00:00
|
|
|
Loader::PostLoadEvent(nsIURI* aURI,
|
2010-05-11 20:41:47 +00:00
|
|
|
nsCSSStyleSheet* aSheet,
|
2010-03-02 21:00:53 +00:00
|
|
|
nsICSSLoaderObserver* aObserver,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aWasAlternate,
|
2011-09-26 21:27:00 +00:00
|
|
|
nsIStyleSheetLinkingElement* aElement)
|
2005-09-12 18:41:15 +00:00
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
LOG(("css::Loader::PostLoadEvent"));
|
2005-09-12 18:41:15 +00:00
|
|
|
NS_PRECONDITION(aSheet, "Must have sheet");
|
2011-09-26 21:27:00 +00:00
|
|
|
NS_PRECONDITION(aObserver || !mObservers.IsEmpty() || aElement,
|
|
|
|
"Must have observer or element");
|
2005-09-12 18:41:15 +00:00
|
|
|
|
2007-02-05 18:09:26 +00:00
|
|
|
nsRefPtr<SheetLoadData> evt =
|
2005-09-12 18:41:15 +00:00
|
|
|
new SheetLoadData(this, EmptyString(), // title doesn't matter here
|
|
|
|
aURI,
|
|
|
|
aSheet,
|
2011-09-26 21:27:00 +00:00
|
|
|
aElement,
|
2005-09-12 18:41:15 +00:00
|
|
|
aWasAlternate,
|
2007-05-01 19:26:15 +00:00
|
|
|
aObserver,
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr);
|
2005-09-12 18:41:15 +00:00
|
|
|
NS_ENSURE_TRUE(evt, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
if (!mPostedEvents.AppendElement(evt)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2005-09-12 18:41:15 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
nsresult rv = NS_DispatchToCurrentThread(evt);
|
2005-09-12 18:41:15 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_WARNING("failed to dispatch stylesheet load event");
|
2007-02-05 18:09:26 +00:00
|
|
|
mPostedEvents.RemoveElement(evt);
|
2005-09-12 18:41:15 +00:00
|
|
|
} else {
|
2007-02-05 18:09:26 +00:00
|
|
|
// We'll unblock onload when we handle the event.
|
|
|
|
if (mDocument) {
|
|
|
|
mDocument->BlockOnload();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to notify the observer for this data.
|
2011-10-17 14:59:28 +00:00
|
|
|
evt->mMustNotify = true;
|
|
|
|
evt->mSheetAlreadyComplete = true;
|
2011-09-26 21:27:13 +00:00
|
|
|
|
|
|
|
// If we get to this code, aSheet loaded correctly at some point, so
|
|
|
|
// we can just use NS_OK for the status. Note that we do this here
|
|
|
|
// and not from inside our SheetComplete so that we don't end up
|
|
|
|
// running the load event async.
|
|
|
|
evt->ScheduleLoadEventIfNeeded(NS_OK);
|
2005-09-12 18:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-03-02 21:00:53 +00:00
|
|
|
Loader::HandleLoadEvent(SheetLoadData* aEvent)
|
2005-09-12 18:41:15 +00:00
|
|
|
{
|
|
|
|
// XXXbz can't assert this yet.... May not have an observer because
|
|
|
|
// we're unblocking the parser
|
|
|
|
// NS_ASSERTION(aEvent->mObserver, "Must have observer");
|
|
|
|
NS_ASSERTION(aEvent->mSheet, "Must have sheet");
|
2011-09-26 21:26:05 +00:00
|
|
|
|
|
|
|
// Very important: this needs to come before the SheetComplete call
|
|
|
|
// below, so that HasPendingLoads() will test true as needed under
|
|
|
|
// notifications we send from that SheetComplete call.
|
|
|
|
mPostedEvents.RemoveElement(aEvent);
|
|
|
|
|
2005-09-12 18:41:15 +00:00
|
|
|
if (!aEvent->mIsCancelled) {
|
2007-02-05 18:09:26 +00:00
|
|
|
// SheetComplete will call Release(), so give it a reference to do
|
|
|
|
// that with.
|
2005-09-12 18:41:15 +00:00
|
|
|
NS_ADDREF(aEvent);
|
|
|
|
SheetComplete(aEvent, NS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDocument) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mDocument->UnblockOnload(true);
|
2005-09-12 18:41:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashOperator
|
2012-08-28 17:10:08 +00:00
|
|
|
StopLoadingSheetCallback(URIPrincipalAndCORSModeHashKey* aKey,
|
2007-05-01 19:26:15 +00:00
|
|
|
SheetLoadData*& aData,
|
|
|
|
void* aClosure)
|
2001-05-19 02:59:15 +00:00
|
|
|
{
|
2002-12-03 05:48:14 +00:00
|
|
|
NS_PRECONDITION(aData, "Must have a data!");
|
|
|
|
NS_PRECONDITION(aClosure, "Must have a loader");
|
2001-05-19 02:59:15 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
aData->mIsLoading = false; // we will handle the removal right here
|
|
|
|
aData->mIsCancelled = true;
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
static_cast<Loader::LoadDataArray*>(aClosure)->AppendElement(aData);
|
2001-05-19 02:59:15 +00:00
|
|
|
|
2003-07-27 15:20:11 +00:00
|
|
|
return PL_DHASH_REMOVE;
|
2001-05-19 02:59:15 +00:00
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
nsresult
|
|
|
|
Loader::Stop()
|
2001-05-19 02:59:15 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t pendingCount =
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets ? mSheets->mPendingDatas.Count() : 0;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t loadingCount =
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets ? mSheets->mLoadingDatas.Count() : 0;
|
2007-05-31 01:42:48 +00:00
|
|
|
LoadDataArray arr(pendingCount + loadingCount + mPostedEvents.Length());
|
2011-05-06 15:45:36 +00:00
|
|
|
|
2007-05-31 01:42:48 +00:00
|
|
|
if (pendingCount) {
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets->mPendingDatas.Enumerate(StopLoadingSheetCallback, &arr);
|
2001-05-19 02:59:15 +00:00
|
|
|
}
|
2007-05-31 01:42:48 +00:00
|
|
|
if (loadingCount) {
|
2013-09-02 08:41:57 +00:00
|
|
|
mSheets->mLoadingDatas.Enumerate(StopLoadingSheetCallback, &arr);
|
2007-05-01 19:26:15 +00:00
|
|
|
}
|
2007-05-31 01:42:48 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t i;
|
2007-05-31 01:42:48 +00:00
|
|
|
for (i = 0; i < mPostedEvents.Length(); ++i) {
|
2007-02-05 18:09:26 +00:00
|
|
|
SheetLoadData* data = mPostedEvents[i];
|
2011-10-17 14:59:28 +00:00
|
|
|
data->mIsCancelled = true;
|
2007-05-31 01:42:48 +00:00
|
|
|
if (arr.AppendElement(data)) {
|
|
|
|
// SheetComplete() calls Release(), so give this an extra ref.
|
|
|
|
NS_ADDREF(data);
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("We preallocated this memory... shouldn't really fail, "
|
|
|
|
"except we never check that preallocation succeeds.");
|
|
|
|
}
|
|
|
|
#endif
|
2007-02-05 18:09:26 +00:00
|
|
|
}
|
2005-09-12 18:41:15 +00:00
|
|
|
mPostedEvents.Clear();
|
2007-05-31 01:42:48 +00:00
|
|
|
|
|
|
|
mDatasToNotifyOn += arr.Length();
|
|
|
|
for (i = 0; i < arr.Length(); ++i) {
|
|
|
|
--mDatasToNotifyOn;
|
|
|
|
SheetComplete(arr[i], NS_BINDING_ABORTED);
|
|
|
|
}
|
2001-05-19 02:59:15 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2010-03-02 21:00:53 +00:00
|
|
|
Loader::HasPendingLoads()
|
2007-05-31 01:42:48 +00:00
|
|
|
{
|
|
|
|
return
|
2013-09-02 08:41:57 +00:00
|
|
|
(mSheets && mSheets->mLoadingDatas.Count() != 0) ||
|
|
|
|
(mSheets && mSheets->mPendingDatas.Count() != 0) ||
|
2007-05-31 01:42:48 +00:00
|
|
|
mPostedEvents.Length() != 0 ||
|
|
|
|
mDatasToNotifyOn != 0;
|
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
nsresult
|
|
|
|
Loader::AddObserver(nsICSSLoaderObserver* aObserver)
|
2007-05-31 01:42:48 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aObserver, "Must have observer");
|
2007-12-20 07:30:04 +00:00
|
|
|
if (mObservers.AppendElementUnlessExists(aObserver)) {
|
2007-05-31 01:42:48 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
void
|
|
|
|
Loader::RemoveObserver(nsICSSLoaderObserver* aObserver)
|
2007-05-31 01:42:48 +00:00
|
|
|
{
|
2007-12-22 00:56:28 +00:00
|
|
|
mObservers.RemoveElement(aObserver);
|
2007-05-31 01:42:48 +00:00
|
|
|
}
|
|
|
|
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashOperator
|
2012-08-28 17:10:08 +00:00
|
|
|
CollectLoadDatas(URIPrincipalAndCORSModeHashKey *aKey,
|
2007-05-31 01:42:48 +00:00
|
|
|
SheetLoadData* &aData,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
2010-03-02 21:00:53 +00:00
|
|
|
static_cast<Loader::LoadDataArray*>(aClosure)->AppendElement(aData);
|
2007-05-31 01:42:48 +00:00
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-03-02 21:00:53 +00:00
|
|
|
Loader::StartAlternateLoads()
|
2007-05-31 01:42:48 +00:00
|
|
|
{
|
2013-09-02 08:41:57 +00:00
|
|
|
NS_PRECONDITION(mSheets, "Don't call me!");
|
|
|
|
LoadDataArray arr(mSheets->mPendingDatas.Count());
|
|
|
|
mSheets->mPendingDatas.Enumerate(CollectLoadDatas, &arr);
|
2007-05-31 01:42:48 +00:00
|
|
|
|
|
|
|
mDatasToNotifyOn += arr.Length();
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < arr.Length(); ++i) {
|
2007-05-31 01:42:48 +00:00
|
|
|
--mDatasToNotifyOn;
|
|
|
|
LoadSheet(arr[i], eSheetNeedsParser);
|
|
|
|
}
|
|
|
|
}
|
2010-03-02 21:00:53 +00:00
|
|
|
|
2012-10-08 02:39:09 +00:00
|
|
|
static PLDHashOperator
|
|
|
|
TraverseSheet(URIPrincipalAndCORSModeHashKey*,
|
|
|
|
nsCSSStyleSheet* aSheet,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
nsCycleCollectionTraversalCallback* cb =
|
|
|
|
static_cast<nsCycleCollectionTraversalCallback*>(aClosure);
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*cb, "Sheet cache nsCSSLoader");
|
|
|
|
cb->NoteXPCOMChild(NS_ISUPPORTS_CAST(nsIStyleSheet*, aSheet));
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loader::TraverseCachedSheets(nsCycleCollectionTraversalCallback& cb)
|
|
|
|
{
|
2013-09-02 08:41:57 +00:00
|
|
|
if (mSheets) {
|
|
|
|
mSheets->mCompleteSheets.EnumerateRead(TraverseSheet, &cb);
|
2012-10-08 02:39:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Loader::UnlinkCachedSheets()
|
|
|
|
{
|
2013-09-02 08:41:57 +00:00
|
|
|
if (mSheets) {
|
|
|
|
mSheets->mCompleteSheets.Clear();
|
2012-10-08 02:39:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-17 21:01:56 +00:00
|
|
|
struct SheetMemoryCounter {
|
|
|
|
size_t size;
|
2013-06-23 12:03:39 +00:00
|
|
|
mozilla::MallocSizeOf mallocSizeOf;
|
2012-10-17 21:01:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
CountSheetMemory(URIPrincipalAndCORSModeHashKey* /* unused */,
|
|
|
|
const nsRefPtr<nsCSSStyleSheet>& aSheet,
|
2013-06-23 12:03:39 +00:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf,
|
2012-10-17 21:01:56 +00:00
|
|
|
void* /* unused */)
|
|
|
|
{
|
|
|
|
// If aSheet has a parent, then its parent will report it so we don't
|
|
|
|
// have to worry about it here.
|
|
|
|
// Likewise, if aSheet has an owning node, then the document that
|
|
|
|
// node is in will report it.
|
2013-03-17 14:42:59 +00:00
|
|
|
if (aSheet->GetOwnerNode() || aSheet->GetParentSheet()) {
|
2012-10-17 21:01:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return aSheet->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2013-06-23 12:03:39 +00:00
|
|
|
Loader::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
2012-10-17 21:01:56 +00:00
|
|
|
{
|
|
|
|
size_t s = aMallocSizeOf(this);
|
|
|
|
|
2013-09-02 08:41:57 +00:00
|
|
|
if (mSheets) {
|
|
|
|
s += mSheets->mCompleteSheets.SizeOfExcludingThis(CountSheetMemory, aMallocSizeOf);
|
|
|
|
}
|
2012-10-17 21:01:56 +00:00
|
|
|
s += mObservers.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
|
|
|
|
// Measurement of the following members may be added later if DMD finds it is
|
|
|
|
// worthwhile:
|
|
|
|
// - mLoadingDatas: transient, and should be small
|
|
|
|
// - mPendingDatas: transient, and should be small
|
|
|
|
// - mParsingDatas: transient, and should be small
|
|
|
|
// - mPostedEvents: transient, and should be small
|
|
|
|
//
|
|
|
|
// The following members aren't measured:
|
|
|
|
// - mDocument, because it's a weak backpointer
|
|
|
|
// - mPreferredSheet, because it can be a shared string
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
} // namespace css
|
|
|
|
} // namespace mozilla
|