Add methods to the user/agent sheet service to unregister sheets and check

whether a sheet is registered.  Bug 286518, patch by Ted Mielczarek (luser)
<ted.mielczarek@gmail.com>, r+sr=bzbarsky
This commit is contained in:
bzbarsky%mit.edu 2005-03-23 02:34:30 +00:00
Родитель 48177b8d52
Коммит 77a64a3976
3 изменённых файлов: 71 добавлений и 16 удалений

Просмотреть файл

@ -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);
};

Просмотреть файл

@ -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<nsIStyleSheet> &sheets,
nsIURI *sheetURI)
{
for (PRInt32 i = sheets.Count() - 1; i >= 0; i-- ) {
PRBool bEqual;
nsCOMPtr<nsIURI> 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<nsICSSLoader> loader = do_CreateInstance(kCSSLoaderCID);
nsCOMPtr<nsICSSStyleSheet> 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;
}

Просмотреть файл

@ -63,8 +63,8 @@ class nsStyleSheetService : public nsIStyleSheetService
NS_HIDDEN_(nsresult) Init();
nsCOMArray<nsIStyleSheet>* AgentStyleSheets() { return &mAgentSheets; }
nsCOMArray<nsIStyleSheet>* UserStyleSheets() { return &mUserSheets; }
nsCOMArray<nsIStyleSheet>* AgentStyleSheets() { return &mSheets[AGENT_SHEET]; }
nsCOMArray<nsIStyleSheet>* UserStyleSheets() { return &mSheets[USER_SHEET]; }
static nsStyleSheetService *gInstance;
@ -75,8 +75,10 @@ class nsStyleSheetService : public nsIStyleSheetService
nsISimpleEnumerator *aEnumerator,
PRUint32 aSheetType);
nsCOMArray<nsIStyleSheet> mAgentSheets;
nsCOMArray<nsIStyleSheet> mUserSheets;
NS_HIDDEN_(PRInt32) FindSheetByURI(const nsCOMArray<nsIStyleSheet> &sheets,
nsIURI *sheetURI);
nsCOMArray<nsIStyleSheet> mSheets[2];
};
#endif