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.

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

mercurial