gfx/2d/BorrowedContext.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef _MOZILLA_GFX_BORROWED_CONTEXT_H
michael@0 7 #define _MOZILLA_GFX_BORROWED_CONTEXT_H
michael@0 8
michael@0 9 #include "2D.h"
michael@0 10
michael@0 11 struct _cairo;
michael@0 12 typedef struct _cairo cairo_t;
michael@0 13
michael@0 14 namespace mozilla {
michael@0 15
michael@0 16 namespace gfx {
michael@0 17
michael@0 18 /* This is a helper class that let's you borrow a cairo_t from a
michael@0 19 * DrawTargetCairo. This is used for drawing themed widgets.
michael@0 20 *
michael@0 21 * Callers should check the cr member after constructing the object
michael@0 22 * to see if it succeeded. The DrawTarget should not be used while
michael@0 23 * the context is borrowed. */
michael@0 24 class BorrowedCairoContext
michael@0 25 {
michael@0 26 public:
michael@0 27 BorrowedCairoContext()
michael@0 28 : mCairo(nullptr)
michael@0 29 , mDT(nullptr)
michael@0 30 { }
michael@0 31
michael@0 32 BorrowedCairoContext(DrawTarget *aDT)
michael@0 33 : mDT(aDT)
michael@0 34 {
michael@0 35 mCairo = BorrowCairoContextFromDrawTarget(aDT);
michael@0 36 }
michael@0 37
michael@0 38 // We can optionally Init after construction in
michael@0 39 // case we don't know what the DT will be at construction
michael@0 40 // time.
michael@0 41 cairo_t *Init(DrawTarget *aDT)
michael@0 42 {
michael@0 43 MOZ_ASSERT(!mDT, "Can't initialize twice!");
michael@0 44 mDT = aDT;
michael@0 45 return mCairo = BorrowCairoContextFromDrawTarget(aDT);
michael@0 46 }
michael@0 47
michael@0 48 // The caller needs to call Finish if cr is non-null when
michael@0 49 // they are done with the context. This is currently explicit
michael@0 50 // instead of happening implicitly in the destructor to make
michael@0 51 // what's happening in the caller more clear. It also
michael@0 52 // let's you resume using the DrawTarget in the same scope.
michael@0 53 void Finish()
michael@0 54 {
michael@0 55 if (mCairo) {
michael@0 56 ReturnCairoContextToDrawTarget(mDT, mCairo);
michael@0 57 mCairo = nullptr;
michael@0 58 }
michael@0 59 }
michael@0 60
michael@0 61 ~BorrowedCairoContext() {
michael@0 62 MOZ_ASSERT(!mCairo);
michael@0 63 }
michael@0 64
michael@0 65 cairo_t *mCairo;
michael@0 66 private:
michael@0 67 static cairo_t* BorrowCairoContextFromDrawTarget(DrawTarget *aDT);
michael@0 68 static void ReturnCairoContextToDrawTarget(DrawTarget *aDT, cairo_t *aCairo);
michael@0 69 DrawTarget *mDT;
michael@0 70 };
michael@0 71
michael@0 72 #ifdef XP_MACOSX
michael@0 73 /* This is a helper class that let's you borrow a CGContextRef from a
michael@0 74 * DrawTargetCG. This is used for drawing themed widgets.
michael@0 75 *
michael@0 76 * Callers should check the cg member after constructing the object
michael@0 77 * to see if it succeeded. The DrawTarget should not be used while
michael@0 78 * the context is borrowed. */
michael@0 79 class BorrowedCGContext
michael@0 80 {
michael@0 81 public:
michael@0 82 BorrowedCGContext()
michael@0 83 : cg(nullptr)
michael@0 84 , mDT(nullptr)
michael@0 85 { }
michael@0 86
michael@0 87 BorrowedCGContext(DrawTarget *aDT)
michael@0 88 : mDT(aDT)
michael@0 89 {
michael@0 90 cg = BorrowCGContextFromDrawTarget(aDT);
michael@0 91 }
michael@0 92
michael@0 93 // We can optionally Init after construction in
michael@0 94 // case we don't know what the DT will be at construction
michael@0 95 // time.
michael@0 96 CGContextRef Init(DrawTarget *aDT)
michael@0 97 {
michael@0 98 MOZ_ASSERT(!mDT, "Can't initialize twice!");
michael@0 99 mDT = aDT;
michael@0 100 cg = BorrowCGContextFromDrawTarget(aDT);
michael@0 101 return cg;
michael@0 102 }
michael@0 103
michael@0 104 // The caller needs to call Finish if cg is non-null when
michael@0 105 // they are done with the context. This is currently explicit
michael@0 106 // instead of happening implicitly in the destructor to make
michael@0 107 // what's happening in the caller more clear. It also
michael@0 108 // let's you resume using the DrawTarget in the same scope.
michael@0 109 void Finish()
michael@0 110 {
michael@0 111 if (cg) {
michael@0 112 ReturnCGContextToDrawTarget(mDT, cg);
michael@0 113 cg = nullptr;
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 ~BorrowedCGContext() {
michael@0 118 MOZ_ASSERT(!cg);
michael@0 119 }
michael@0 120
michael@0 121 CGContextRef cg;
michael@0 122 private:
michael@0 123 static CGContextRef BorrowCGContextFromDrawTarget(DrawTarget *aDT);
michael@0 124 static void ReturnCGContextToDrawTarget(DrawTarget *aDT, CGContextRef cg);
michael@0 125 DrawTarget *mDT;
michael@0 126 };
michael@0 127 #endif
michael@0 128
michael@0 129 }
michael@0 130 }
michael@0 131
michael@0 132 #endif // _MOZILLA_GFX_BORROWED_CONTEXT_H

mercurial