1 // Copyright 2005 Ben Hutchings <ben@decadent.org.uk>.
2 // See the file "COPYING" for licence details.
4 #include "style_sheets.hpp"
6 #include <nsContentCID.h>
7 #if MOZ_VERSION_MAJOR > 1 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
8 # include <nsIStyleSheetService.h>
9 # include <nsServiceManagerUtils.h>
11 # include <nsICSSLoader.h>
12 # include <nsICSSStyleSheet.h>
13 # include <nsIPresShell.h>
14 # include <nsIServiceManagerUtils.h>
17 #include <nsNetUtil.h>
19 #include "xpcom_support.hpp"
21 using xpcom_support::check;
23 #if MOZ_VERSION_MAJOR > 1 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
25 // We just have to load and register a style-sheet as a user
26 // style-sheet. There is no need to do anything for each page.
28 agent_style_sheet_holder init_agent_style_sheet(const char * uri)
30 nsCOMPtr<nsIURI> style_sheet_uri;
31 check(NS_NewURI(getter_AddRefs(style_sheet_uri), nsCString(uri)));
33 nsCOMPtr<nsIStyleSheetService> style_sheet_service;
34 static const nsCID style_sheet_service_cid = {
35 // NS_STYLESHEETSERVICE_CID copied from
36 // layout/base/nsStyleSheetService.cpp
37 0xfcca6f83, 0x9f7d, 0x44e4,
38 {0xa7, 0x4b, 0xb5, 0x94, 0x33, 0xe6, 0xc8, 0xc3}
40 check(CallGetService<nsIStyleSheetService>(
41 style_sheet_service_cid, getter_AddRefs(style_sheet_service)));
42 check(style_sheet_service->LoadAndRegisterSheet(
43 style_sheet_uri, nsIStyleSheetService::USER_SHEET));
45 return agent_style_sheet_holder();
48 #else // Mozilla version < 1.8
50 already_AddRefed<nsIStyleSheet> init_agent_style_sheet(const char * uri)
52 nsCOMPtr<nsICSSLoader> css_loader;
53 static const nsCID css_loader_cid = NS_CSS_LOADER_CID;
54 check(CallGetService<nsICSSLoader>(css_loader_cid,
55 getter_AddRefs(css_loader)));
57 nsCOMPtr<nsIURI> style_sheet_uri;
58 check(NS_NewURI(getter_AddRefs(style_sheet_uri), nsCString(uri)));
60 nsICSSStyleSheet * style_sheet;
61 check(css_loader->LoadAgentSheet(style_sheet_uri, &style_sheet));
65 // Apply a style-sheet to a given presentation shell as the top-priority
66 // agent style-sheet and disable the preferences-derived style rules.
67 void apply_agent_style_sheet(nsIStyleSheet * style_sheet,
68 nsIPresShell * pres_shell)
70 nsCOMArray<nsIStyleSheet> style_sheets;
71 check(pres_shell->GetAgentStyleSheets(style_sheets));
72 check(style_sheets.InsertObjectAt(style_sheet, 0));
73 check(pres_shell->SetAgentStyleSheets(style_sheets));
75 check(pres_shell->EnablePrefStyleRules(false));
78 check(pres_shell->ReconstructStyleData());
79 check(pres_shell->FlushPendingNotifications(true));
82 #endif // Mozilla version >=/< 1.8