Bug 1164766 - Clean up error checking in CanvasRenderingContext2D. r=mats

This commit is contained in:
Markus Stange 2015-07-14 16:59:12 -04:00
parent 3b5af1496d
commit 60020d51a1
3 changed files with 62 additions and 25 deletions

View File

@ -1014,7 +1014,7 @@ CanvasRenderingContext2D::ParseColor(const nsAString& aString,
*aColor = value.GetColorValue();
} else {
// otherwise resolve it
nsIPresShell* presShell = GetPresShell();
nsCOMPtr<nsIPresShell> presShell = GetPresShell();
nsRefPtr<nsStyleContext> parentContext;
if (mCanvasElement && mCanvasElement->IsInDoc()) {
// Inherit from the canvas element.
@ -2151,9 +2151,15 @@ GetFontParentStyleContext(Element* aElement, nsIPresShell* presShell,
ErrorResult& error)
{
if (aElement && aElement->IsInDoc()) {
// inherit from the canvas element
return nsComputedDOMStyle::GetStyleContextForElement(aElement, nullptr,
presShell);
// Inherit from the canvas element.
nsRefPtr<nsStyleContext> result =
nsComputedDOMStyle::GetStyleContextForElement(aElement, nullptr,
presShell);
if (!result) {
error.Throw(NS_ERROR_FAILURE);
return nullptr;
}
return result.forget();
}
// otherwise inherit from default (10px sans-serif)
@ -2168,7 +2174,14 @@ GetFontParentStyleContext(Element* aElement, nsIPresShell* presShell,
nsTArray<nsCOMPtr<nsIStyleRule>> parentRules;
parentRules.AppendElement(parentRule);
return presShell->StyleSet()->ResolveStyleForRules(nullptr, parentRules);
nsRefPtr<nsStyleContext> result =
presShell->StyleSet()->ResolveStyleForRules(nullptr, parentRules);
if (!result) {
error.Throw(NS_ERROR_FAILURE);
return nullptr;
}
return result.forget();
}
static bool
@ -2222,6 +2235,12 @@ GetFontStyleContext(Element* aElement, const nsAString& aFont,
return nullptr;
}
MOZ_RELEASE_ASSERT(parentContext,
"GetFontParentStyleContext should have returned an error if it couldn't get a parent context.");
MOZ_ASSERT(!presShell->IsDestroying(),
"GetFontParentStyleContext should have returned an error if the presshell is being destroyed.");
nsTArray<nsCOMPtr<nsIStyleRule>> rules;
rules.AppendElement(rule);
// add a rule to prevent text zoom from affecting the style
@ -2299,7 +2318,7 @@ CanvasRenderingContext2D::ParseFilter(const nsAString& aString,
return false;
}
nsIPresShell* presShell = GetPresShell();
nsCOMPtr<nsIPresShell> presShell = GetPresShell();
if (!presShell) {
error.Throw(NS_ERROR_FAILURE);
return false;
@ -3034,6 +3053,13 @@ CanvasRenderingContext2D::TransformWillUpdate()
void
CanvasRenderingContext2D::SetFont(const nsAString& font,
ErrorResult& error)
{
SetFontInternal(font, error);
}
bool
CanvasRenderingContext2D::SetFontInternal(const nsAString& font,
ErrorResult& error)
{
/*
* If font is defined with relative units (e.g. ems) and the parent
@ -3046,20 +3072,20 @@ CanvasRenderingContext2D::SetFont(const nsAString& font,
if (!mCanvasElement && !mDocShell) {
NS_WARNING("Canvas element must be non-null or a docshell must be provided");
error.Throw(NS_ERROR_FAILURE);
return;
return false;
}
nsIPresShell* presShell = GetPresShell();
nsCOMPtr<nsIPresShell> presShell = GetPresShell();
if (!presShell) {
error.Throw(NS_ERROR_FAILURE);
return;
return false;
}
nsString usedFont;
nsRefPtr<nsStyleContext> sc =
GetFontStyleContext(mCanvasElement, font, presShell, usedFont, error);
if (!sc) {
return;
return false;
}
const nsStyleFont* fontStyle = sc->StyleFont();
@ -3099,6 +3125,8 @@ CanvasRenderingContext2D::SetFont(const nsAString& font,
CurrentState().fontFont.size = fontStyle->mSize;
CurrentState().fontLanguage = fontStyle->mLanguage;
CurrentState().fontExplicitLanguage = fontStyle->mExplicitLanguage;
return true;
}
void
@ -3693,7 +3721,12 @@ CanvasRenderingContext2D::DrawOrMeasureText(const nsAString& aRawText,
}
gfxFontGroup* currentFontStyle = GetCurrentFontStyle();
NS_ASSERTION(currentFontStyle, "font group is null");
if (!currentFontStyle) {
return NS_ERROR_FAILURE;
}
MOZ_ASSERT(!presShell->IsDestroying(),
"GetCurrentFontStyle() should have returned null if the presshell is being destroyed");
// ensure user font set is up to date
currentFontStyle->
@ -3891,8 +3924,9 @@ gfxFontGroup *CanvasRenderingContext2D::GetCurrentFontStyle()
ErrorResult err;
NS_NAMED_LITERAL_STRING(kDefaultFontStyle, "10px sans-serif");
static float kDefaultFontSize = 10.0;
SetFont(kDefaultFontStyle, err);
if (err.Failed()) {
nsCOMPtr<nsIPresShell> presShell = GetPresShell();
bool fontUpdated = SetFontInternal(kDefaultFontStyle, err);
if (err.Failed() || !fontUpdated) {
gfxFontStyle style;
style.size = kDefaultFontSize;
CurrentState().fontGroup =
@ -3901,9 +3935,7 @@ gfxFontGroup *CanvasRenderingContext2D::GetCurrentFontStyle()
nullptr);
if (CurrentState().fontGroup) {
CurrentState().font = kDefaultFontStyle;
nsIPresShell* presShell = GetPresShell();
if (presShell) {
if (presShell && !presShell->IsDestroying()) {
CurrentState().fontGroup->SetTextPerfMetrics(
presShell->GetPresContext()->GetTextPerfMetrics());
}
@ -3911,7 +3943,6 @@ gfxFontGroup *CanvasRenderingContext2D::GetCurrentFontStyle()
NS_ERROR("Default canvas font is invalid");
}
}
}
return CurrentState().fontGroup;

View File

@ -597,6 +597,10 @@ protected:
nsTArray<nsStyleFilter>& aFilterChain,
ErrorResult& error);
// Returns whether the font was successfully updated.
bool SetFontInternal(const nsAString& font, mozilla::ErrorResult& error);
/**
* Creates the error target, if it doesn't exist
*/
@ -631,7 +635,7 @@ protected:
*/
RenderingMode EnsureTarget(RenderingMode aRenderMode = RenderingMode::DefaultBackendMode);
/*
/**
* Disposes an old target and prepares to lazily create a new target.
*/
void ClearTarget();
@ -867,10 +871,10 @@ protected:
protected:
gfxFontGroup *GetCurrentFontStyle();
/*
* Implementation of the fillText, strokeText, and measure functions with
* the operation abstracted to a flag.
*/
/**
* Implementation of the fillText, strokeText, and measure functions with
* the operation abstracted to a flag.
*/
nsresult DrawOrMeasureText(const nsAString& text,
float x,
float y,

View File

@ -878,19 +878,21 @@ HTMLCanvasElement::UpdateContext(JSContext* aCx, JS::Handle<JS::Value> aNewConte
nsIntSize sz = GetWidthHeight();
nsresult rv = mCurrentContext->SetIsOpaque(HasAttr(kNameSpaceID_None, nsGkAtoms::moz_opaque));
nsCOMPtr<nsICanvasRenderingContextInternal> currentContext = mCurrentContext;
nsresult rv = currentContext->SetIsOpaque(HasAttr(kNameSpaceID_None, nsGkAtoms::moz_opaque));
if (NS_FAILED(rv)) {
mCurrentContext = nullptr;
return rv;
}
rv = mCurrentContext->SetContextOptions(aCx, aNewContextOptions);
rv = currentContext->SetContextOptions(aCx, aNewContextOptions);
if (NS_FAILED(rv)) {
mCurrentContext = nullptr;
return rv;
}
rv = mCurrentContext->SetDimensions(sz.width, sz.height);
rv = currentContext->SetDimensions(sz.width, sz.height);
if (NS_FAILED(rv)) {
mCurrentContext = nullptr;
return rv;