Bug 1011738 - Theme support for b2g/gaia, Part 2 : stylesheet reloading r=bz,seth

This commit is contained in:
Fabrice Desré 2014-08-28 17:20:27 -07:00
Родитель baeeaa2c70
Коммит 3acf50131b
6 изменённых файлов: 86 добавлений и 9 удалений

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

@ -12,8 +12,8 @@ class nsICSSLoaderObserver;
class nsIURI;
#define NS_ISTYLESHEETLINKINGELEMENT_IID \
{ 0xd753c84a, 0x17fd, 0x4d5f, \
{ 0xb2, 0xe9, 0x63, 0x52, 0x8c, 0x87, 0x99, 0x7a } }
{ 0xe5855604, 0x8a9a, 0x4181, \
{ 0xbe, 0x41, 0xdd, 0xf7, 0x08, 0x70, 0x3f, 0xbe } }
namespace mozilla {
class CSSStyleSheet;
@ -60,10 +60,13 @@ public:
* failure code will be returned.
* @param [out] whether the sheet is an alternate sheet. This value is only
* meaningful if aWillNotify is true.
* @param aForceUpdate whether we wand to force the update, flushing the
* cached version if any.
*/
NS_IMETHOD UpdateStyleSheet(nsICSSLoaderObserver* aObserver,
bool *aWillNotify,
bool *aIsAlternate) = 0;
bool *aIsAlternate,
bool aForceUpdate = false) = 0;
/**
* Tells this element whether to update the stylesheet when the

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

@ -8873,7 +8873,7 @@ nsDocument::OnPageShow(bool aPersisted,
"chrome-page-shown" :
"content-page-shown",
nullptr);
os->AddObserver(this, "app-theme-changed", /* ownsWeak */ false);
DispatchPageTransition(target, NS_LITERAL_STRING("pageshow"), aPersisted);
}
@ -8947,6 +8947,8 @@ nsDocument::OnPageHide(bool aPersisted,
"chrome-page-hidden" :
"content-page-hidden",
nullptr);
os->RemoveObserver(this, "app-theme-changed");
}
DispatchPageTransition(target, NS_LITERAL_STRING("pagehide"), aPersisted);
@ -11656,10 +11658,57 @@ nsDocument::Observe(nsISupports *aSubject,
NS_DispatchToMainThread(r);
}
}
} else if (strcmp("app-theme-changed", aTopic) == 0) {
if (!nsContentUtils::IsSystemPrincipal(NodePrincipal()) &&
!IsUnstyledDocument()) {
// We don't want to style the chrome window, only app ones.
OnAppThemeChanged();
}
}
return NS_OK;
}
void
nsDocument::OnAppThemeChanged()
{
// Bail out if there is no theme support set up properly.
auto themeOrigin = Preferences::GetString("b2g.theme.origin");
if (!themeOrigin || !Preferences::GetBool("dom.mozApps.themable")) {
return;
}
for (int32_t i = 0; i < GetNumberOfStyleSheets(); i++) {
nsRefPtr<CSSStyleSheet> sheet = do_QueryObject(GetStyleSheetAt(i));
if (!sheet) {
continue;
}
nsINode* owningNode = sheet->GetOwnerNode();
if (!owningNode) {
continue;
}
// Get a DOM stylesheet link to check the href against the theme origin.
nsIURI* sheetURI = sheet->GetOriginalURI();
if (!sheetURI) {
continue;
}
nsAutoString sheetOrigin;
nsContentUtils::GetUTFOrigin(sheetURI, sheetOrigin);
if (!sheetOrigin.Equals(themeOrigin)) {
continue;
}
// Finally getting a Stylesheet link.
nsCOMPtr<nsIStyleSheetLinkingElement> link = do_QueryInterface(owningNode);
if (!link) {
continue;
}
bool willNotify;
bool isAlternate;
link->UpdateStyleSheet(nullptr, &willNotify, &isAlternate, true);
}
}
void
nsDocument::RequestPointerLock(Element* aElement)
{

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

@ -937,6 +937,12 @@ public:
virtual nsViewportInfo GetViewportInfo(const mozilla::ScreenIntSize& aDisplaySize) MOZ_OVERRIDE;
/**
* Called when an app-theme-changed observer notification is
* received by this document.
*/
void OnAppThemeChanged();
private:
void AddOnDemandBuiltInUASheet(mozilla::CSSStyleSheet* aSheet);
nsRadioGroupStruct* GetRadioGroupInternal(const nsAString& aName) const;

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

@ -201,10 +201,21 @@ uint32_t nsStyleLinkElement::ParseLinkTypes(const nsAString& aTypes, nsIPrincipa
NS_IMETHODIMP
nsStyleLinkElement::UpdateStyleSheet(nsICSSLoaderObserver* aObserver,
bool* aWillNotify,
bool* aIsAlternate)
bool* aIsAlternate,
bool aForceReload)
{
if (aForceReload) {
// We remove this stylesheet from the cache to load a new version.
nsCOMPtr<nsIContent> thisContent;
CallQueryInterface(this, getter_AddRefs(thisContent));
nsIDocument* doc = thisContent->GetCrossShadowCurrentDoc();
if (doc && doc->CSSLoader()->GetEnabled() &&
mStyleSheet && mStyleSheet->GetOriginalURI()) {
doc->CSSLoader()->ObsoleteSheet(mStyleSheet->GetOriginalURI());
}
}
return DoUpdateStyleSheet(nullptr, nullptr, aObserver, aWillNotify,
aIsAlternate, false);
aIsAlternate, aForceReload);
}
nsresult

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

@ -45,7 +45,8 @@ public:
NS_IMETHOD InitStyleLinkElement(bool aDontLoadStyle) MOZ_OVERRIDE;
NS_IMETHOD UpdateStyleSheet(nsICSSLoaderObserver* aObserver,
bool* aWillNotify,
bool* aIsAlternate) MOZ_OVERRIDE;
bool* aIsAlternate,
bool aForceReload) MOZ_OVERRIDE;
NS_IMETHOD SetEnableUpdates(bool aEnableUpdates) MOZ_OVERRIDE;
NS_IMETHOD GetCharset(nsAString& aCharset) MOZ_OVERRIDE;

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

@ -858,7 +858,8 @@ NS_IMPL_ISUPPORTS(imgCacheObserver, nsIObserver)
NS_IMETHODIMP
imgCacheObserver::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aSomeData)
{
if (strcmp(aTopic, "memory-pressure") == 0) {
if (strcmp(aTopic, "memory-pressure") == 0 ||
strcmp(aTopic, "app-theme-changed") == 0) {
DiscardTracker::DiscardAll();
}
return NS_OK;
@ -1015,8 +1016,10 @@ void imgLoader::GlobalInit()
NS_ADDREF(gCacheObserver);
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
if (os)
if (os) {
os->AddObserver(gCacheObserver, "memory-pressure", false);
os->AddObserver(gCacheObserver, "app-theme-changed", false);
}
int32_t timeweight;
nsresult rv = Preferences::GetInt("image.cache.timeweight", &timeweight);
@ -1045,6 +1048,7 @@ nsresult imgLoader::InitCache()
return NS_ERROR_FAILURE;
os->AddObserver(this, "memory-pressure", false);
os->AddObserver(this, "app-theme-changed", false);
os->AddObserver(this, "chrome-flush-skin-caches", false);
os->AddObserver(this, "chrome-flush-caches", false);
os->AddObserver(this, "last-pb-context-exited", false);
@ -1085,6 +1089,9 @@ imgLoader::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aD
} else if (strcmp(aTopic, "memory-pressure") == 0) {
MinimizeCaches();
} else if (strcmp(aTopic, "app-theme-changed") == 0) {
ClearImageCache();
MinimizeCaches();
} else if (strcmp(aTopic, "chrome-flush-skin-caches") == 0 ||
strcmp(aTopic, "chrome-flush-caches") == 0) {
MinimizeCaches();