nsCOMPtr<nsIDocShellTreeItem> docShell(do_QueryInterface(mContainer)); PRInt32 shellType; docShell->GetItemType(&shellType); PRBool isChrome = (shellType == nsIDocShellTreeItem::typeChrome); chromeRegistry->GetUserSheets(isChrome, getter_AddRefs(sheets)); if (sheets) { nsCOMPtr<nsICSSStyleSheet> sheet; PRUint32 count; sheets->Count(&count);   for (PRUint32 i=0; i<count; i++) { sheets->GetElementAt(i, getter_AddRefs(sheet)); styleSet->PrependStyleSheet(nsStyleSet::eUserSheet, sheet);
PRBool shouldOverride = PR_FALSE; nsCOMPtr<nsIDocShell> ds(do_QueryInterface(mContainer)); nsCOMPtr<nsIChromeEventHandler> chromeHandler; nsCOMPtr<nsICSSLoader> cssLoader( do_GetService(kCSSLoaderCID) ); nsCOMPtr<nsIURI> uri; nsCOMPtr<nsICSSStyleSheet> csssheet;  ds->GetChromeEventHandler(getter_AddRefs(chromeHandler)); if (chromeHandler) { nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(chromeHandler)); nsCOMPtr<nsIContent> content(do_QueryInterface(elt)); if (elt && content) { nsCOMPtr<nsIURI> baseURI = content->GetBaseURI();  nsAutoString sheets; elt->GetAttribute(NS_LITERAL_STRING("usechromesheets"), sheets); if (!sheets.IsEmpty() && baseURI) { char *str = ToNewCString(sheets); char *newStr; char *token = str; while ( (token = nsCRT::strtok(token, ", ", &newStr)) ) { NS_NewURI(getter_AddRefs(uri), nsDependentCString(token), nsnull, baseURI); if (!uri) continue;  cssLoader->LoadAgentSheet(uri, getter_AddRefs(csssheet)); if (!sheet) continue;  styleSet->AppendStyleSheet(nsStyleSet::eAgentSheet, csssheet); shouldOverride = PR_TRUE; } nsMemory::Free(str);
DocumentViewerImpl::CreateStyleSet(nsIDocument* aDocument,                                   nsStyleSet** aStyleSet){  // this should eventually get expanded to allow for creating  // different sets for different media  if (!mUAStyleSheet) {    NS_WARNING("unable to load UA style sheet");  }  nsStyleSet *styleSet = new nsStyleSet();  if (!styleSet) {    return NS_ERROR_OUT_OF_MEMORY;  }  PRInt32 index = aDocument->GetNumberOfStyleSheets(PR_TRUE);  styleSet->BeginUpdate();  while (0 < index--) {    nsIStyleSheet *sheet = aDocument->GetStyleSheetAt(index, PR_TRUE);    /*     * GetStyleSheetAt will return all style sheets in the document but     * we're only interested in the ones that are enabled.     */    PRBool styleApplicable;    sheet->GetApplicable(styleApplicable);    if (styleApplicable) {      styleSet->AddDocStyleSheet(sheet, aDocument);    }  }  nsCOMPtr<nsIChromeRegistry> chromeRegistry =    do_GetService("@mozilla.org/chrome/chrome-registry;1");  if (chromeRegistry) {    nsCOMPtr<nsISupportsArray> sheets;    // Now handle the user sheets.    nsCOMPtr<nsIDocShellTreeItem> docShell(do_QueryInterface(mContainer));    PRInt32 shellType;    docShell->GetItemType(&shellType);    PRBool isChrome = (shellType == nsIDocShellTreeItem::typeChrome);    chromeRegistry->GetUserSheets(isChrome, getter_AddRefs(sheets));    if (sheets) {      nsCOMPtr<nsICSSStyleSheet> sheet;      PRUint32 count;      sheets->Count(&count);      // Insert the user sheets at the front of the user sheet list      // so that they are most significant user sheets.      for (PRUint32 i=0; i<count; i++) {        sheets->GetElementAt(i, getter_AddRefs(sheet));        styleSet->PrependStyleSheet(nsStyleSet::eUserSheet, sheet);      }    }    // Append chrome sheets (scrollbars + forms).    nsCOMPtr<nsIDocShell> ds(do_QueryInterface(mContainer));    chromeRegistry->GetAgentSheets(ds, getter_AddRefs(sheets));    if (sheets) {      nsCOMPtr<nsICSSStyleSheet> sheet;      PRUint32 count;      sheets->Count(&count);      for (PRUint32 i=0; i<count; i++) {        sheets->GetElementAt(i, getter_AddRefs(sheet));        styleSet->AppendStyleSheet(nsStyleSet::eAgentSheet, sheet);      }    }  }  if (mUAStyleSheet) {    styleSet->AppendStyleSheet(nsStyleSet::eAgentSheet, mUAStyleSheet);  }  styleSet->EndUpdate();  *aStyleSet = styleSet;  return NS_OK;}