diff --git a/layout/base/nsIStyleSheetService.idl b/layout/base/nsIStyleSheetService.idl index 637dba68816..dc14e2a0fde 100644 --- a/layout/base/nsIStyleSheetService.idl +++ b/layout/base/nsIStyleSheetService.idl @@ -45,15 +45,28 @@ interface nsIURI; * built-in list of user or agent style sheets. */ -[scriptable, uuid(c0f35b2d-de8f-4dab-9538-ff079bda74dc)] +[scriptable, uuid(41d979dc-ea03-4235-86ff-1e3c090c5630)] interface nsIStyleSheetService : nsISupports { const unsigned long AGENT_SHEET = 0; const unsigned long USER_SHEET = 1; /** - * Synchrnously loads a style sheet from |sheetURI| and adds it to the list + * Synchronously loads a style sheet from |sheetURI| and adds it to the list * of user or agent style sheets. */ void loadAndRegisterSheet(in nsIURI sheetURI, in unsigned long type); + + /** + * Returns true if a style sheet at |sheetURI| has previously been + * added to the list of style sheets specified by |type|. + */ + boolean sheetRegistered(in nsIURI sheetURI, in unsigned long type); + + /** + * Remove the style sheet at |sheetURI| from the list of style + * sheets specified by |type|. All documents loaded after + * this call will no longer use the style sheet. + */ + void unregisterSheet(in nsIURI sheetURI, in unsigned long type); }; diff --git a/layout/base/nsStyleSheetService.cpp b/layout/base/nsStyleSheetService.cpp index 8e0c1fb80c3..257d2481e57 100644 --- a/layout/base/nsStyleSheetService.cpp +++ b/layout/base/nsStyleSheetService.cpp @@ -37,8 +37,10 @@ * ***** END LICENSE BLOCK ***** */ #include "nsStyleSheetService.h" +#include "nsIStyleSheet.h" #include "nsICSSLoader.h" #include "nsICSSStyleSheet.h" +#include "nsIURI.h" #include "nsContentCID.h" #include "nsCOMPtr.h" #include "nsIServiceManager.h" @@ -52,6 +54,7 @@ nsStyleSheetService *nsStyleSheetService::gInstance = nsnull; nsStyleSheetService::nsStyleSheetService() { + NS_ASSERTION(0 == AGENT_SHEET && 1 == USER_SHEET, "Invalid value for USER_SHEET or AGENT_SHEET"); NS_ASSERTION(!gInstance, "Someone is using CreateInstance instead of GetService"); gInstance = this; } @@ -95,6 +98,24 @@ nsStyleSheetService::RegisterFromEnumerator(nsICategoryManager *aManager, } } +PRInt32 +nsStyleSheetService::FindSheetByURI(const nsCOMArray &sheets, + nsIURI *sheetURI) +{ + for (PRInt32 i = sheets.Count() - 1; i >= 0; i-- ) { + PRBool bEqual; + nsCOMPtr uri; + if (NS_SUCCEEDED(sheets[i]->GetSheetURI(getter_AddRefs(uri))) + && uri + && NS_SUCCEEDED(uri->Equals(sheetURI, &bEqual)) + && bEqual) { + return i; + } + } + + return -1; +} + nsresult nsStyleSheetService::Init() { @@ -120,21 +141,40 @@ NS_IMETHODIMP nsStyleSheetService::LoadAndRegisterSheet(nsIURI *aSheetURI, PRUint32 aSheetType) { + NS_ENSURE_ARG(aSheetType == AGENT_SHEET || aSheetType == USER_SHEET); + nsCOMPtr loader = do_CreateInstance(kCSSLoaderCID); nsCOMPtr sheet; nsresult rv = loader->LoadAgentSheet(aSheetURI, getter_AddRefs(sheet)); NS_ENSURE_SUCCESS(rv, rv); - switch (aSheetType) { - case AGENT_SHEET: - mAgentSheets.AppendObject(sheet); - break; - case USER_SHEET: - mUserSheets.AppendObject(sheet); - break; - default: - NS_ERROR("Unknown sheet type"); - } + mSheets[aSheetType].AppendObject(sheet); + + return NS_OK; +} + +NS_IMETHODIMP +nsStyleSheetService::SheetRegistered(nsIURI *sheetURI, + PRUint32 aSheetType, PRBool *_retval) +{ + NS_ENSURE_ARG(aSheetType == AGENT_SHEET || aSheetType == USER_SHEET); + NS_ENSURE_ARG_POINTER(sheetURI); + NS_PRECONDITION(_retval, "Null out param"); + + *_retval = (FindSheetByURI(mSheets[aSheetType], sheetURI) >= 0); + + return NS_OK; +} + +NS_IMETHODIMP +nsStyleSheetService::UnregisterSheet(nsIURI *sheetURI, PRUint32 aSheetType) +{ + NS_ENSURE_ARG(aSheetType == AGENT_SHEET || aSheetType == USER_SHEET); + NS_ENSURE_ARG_POINTER(sheetURI); + + PRInt32 foundIndex = FindSheetByURI(mSheets[aSheetType], sheetURI); + NS_ENSURE_TRUE(foundIndex >= 0, NS_ERROR_INVALID_ARG); + mSheets[aSheetType].RemoveObjectAt(foundIndex); return NS_OK; } diff --git a/layout/base/nsStyleSheetService.h b/layout/base/nsStyleSheetService.h index 6d8122bc2c6..16b896c592f 100644 --- a/layout/base/nsStyleSheetService.h +++ b/layout/base/nsStyleSheetService.h @@ -63,8 +63,8 @@ class nsStyleSheetService : public nsIStyleSheetService NS_HIDDEN_(nsresult) Init(); - nsCOMArray* AgentStyleSheets() { return &mAgentSheets; } - nsCOMArray* UserStyleSheets() { return &mUserSheets; } + nsCOMArray* AgentStyleSheets() { return &mSheets[AGENT_SHEET]; } + nsCOMArray* UserStyleSheets() { return &mSheets[USER_SHEET]; } static nsStyleSheetService *gInstance; @@ -75,8 +75,10 @@ class nsStyleSheetService : public nsIStyleSheetService nsISimpleEnumerator *aEnumerator, PRUint32 aSheetType); - nsCOMArray mAgentSheets; - nsCOMArray mUserSheets; + NS_HIDDEN_(PRInt32) FindSheetByURI(const nsCOMArray &sheets, + nsIURI *sheetURI); + + nsCOMArray mSheets[2]; }; #endif