styleSet->EndUpdate();
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);    }  }  // Now handle the user sheets.  nsCOMPtr<nsIDocShellTreeItem> docShell(do_QueryInterface(mContainer));  PRInt32 shellType;  docShell->GetItemType(&shellType);  nsICSSStyleSheet* sheet = nsnull;  if (shellType == nsIDocShellTreeItem::typeChrome) {    sheet = nsLayoutStylesheetCache::UserChromeSheet();  }  else {    sheet = nsLayoutStylesheetCache::UserContentSheet();  }  if (sheet)    styleSet->PrependStyleSheet(nsStyleSet::eUserSheet, sheet);  // Append chrome sheets (scrollbars + forms).  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 = str;        char *token;        while ( (token = nsCRT::strtok(newStr, ", ", &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);      }    }  }  if (!shouldOverride) {    sheet = nsLayoutStylesheetCache::ScrollbarsSheet();    if (sheet) {      styleSet->AppendStyleSheet(nsStyleSet::eAgentSheet, sheet);    }  }  sheet = nsLayoutStylesheetCache::FormsSheet();  if (sheet) {    styleSet->AppendStyleSheet(nsStyleSet::eAgentSheet, sheet);  }  if (mUAStyleSheet) {    styleSet->AppendStyleSheet(nsStyleSet::eAgentSheet, mUAStyleSheet);  }  styleSet->EndUpdate();  *aStyleSet = styleSet;  return NS_OK;}