gfx/src/nsScriptableRegion.cpp

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: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsScriptableRegion.h"
michael@0 8 #include <stdint.h> // for uint32_t
michael@0 9 #include <sys/types.h> // for int32_t
michael@0 10 #include "js/RootingAPI.h" // for Rooted
michael@0 11 #include "js/Value.h" // for INT_TO_JSVAL, etc
michael@0 12 #include "jsapi.h" // for JS_DefineElement, etc
michael@0 13 #include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2
michael@0 14 #include "nsError.h" // for NS_OK, NS_ERROR_FAILURE, etc
michael@0 15 #include "nsID.h"
michael@0 16 #include "nsRect.h" // for nsIntRect
michael@0 17 #include "nscore.h" // for NS_IMETHODIMP
michael@0 18
michael@0 19 class JSObject;
michael@0 20 struct JSContext;
michael@0 21
michael@0 22 nsScriptableRegion::nsScriptableRegion()
michael@0 23 {
michael@0 24 }
michael@0 25
michael@0 26 NS_IMPL_ISUPPORTS(nsScriptableRegion, nsIScriptableRegion)
michael@0 27
michael@0 28 NS_IMETHODIMP nsScriptableRegion::Init()
michael@0 29 {
michael@0 30 return NS_OK;
michael@0 31 }
michael@0 32
michael@0 33 NS_IMETHODIMP nsScriptableRegion::SetToRegion(nsIScriptableRegion *aRegion)
michael@0 34 {
michael@0 35 aRegion->GetRegion(&mRegion);
michael@0 36 return NS_OK;
michael@0 37 }
michael@0 38
michael@0 39 NS_IMETHODIMP nsScriptableRegion::SetToRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight)
michael@0 40 {
michael@0 41 mRegion = nsIntRect(aX, aY, aWidth, aHeight);
michael@0 42 return NS_OK;
michael@0 43 }
michael@0 44
michael@0 45 NS_IMETHODIMP nsScriptableRegion::IntersectRegion(nsIScriptableRegion *aRegion)
michael@0 46 {
michael@0 47 nsIntRegion region;
michael@0 48 aRegion->GetRegion(&region);
michael@0 49 mRegion.And(mRegion, region);
michael@0 50 return NS_OK;
michael@0 51 }
michael@0 52
michael@0 53 NS_IMETHODIMP nsScriptableRegion::IntersectRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight)
michael@0 54 {
michael@0 55 mRegion.And(mRegion, nsIntRect(aX, aY, aWidth, aHeight));
michael@0 56 return NS_OK;
michael@0 57 }
michael@0 58
michael@0 59 NS_IMETHODIMP nsScriptableRegion::UnionRegion(nsIScriptableRegion *aRegion)
michael@0 60 {
michael@0 61 nsIntRegion region;
michael@0 62 aRegion->GetRegion(&region);
michael@0 63 mRegion.Or(mRegion, region);
michael@0 64 return NS_OK;
michael@0 65 }
michael@0 66
michael@0 67 NS_IMETHODIMP nsScriptableRegion::UnionRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight)
michael@0 68 {
michael@0 69 mRegion.Or(mRegion, nsIntRect(aX, aY, aWidth, aHeight));
michael@0 70 return NS_OK;
michael@0 71 }
michael@0 72
michael@0 73 NS_IMETHODIMP nsScriptableRegion::SubtractRegion(nsIScriptableRegion *aRegion)
michael@0 74 {
michael@0 75 nsIntRegion region;
michael@0 76 aRegion->GetRegion(&region);
michael@0 77 mRegion.Sub(mRegion, region);
michael@0 78 return NS_OK;
michael@0 79 }
michael@0 80
michael@0 81 NS_IMETHODIMP nsScriptableRegion::SubtractRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight)
michael@0 82 {
michael@0 83 mRegion.Sub(mRegion, nsIntRect(aX, aY, aWidth, aHeight));
michael@0 84 return NS_OK;
michael@0 85 }
michael@0 86
michael@0 87 NS_IMETHODIMP nsScriptableRegion::IsEmpty(bool *isEmpty)
michael@0 88 {
michael@0 89 *isEmpty = mRegion.IsEmpty();
michael@0 90 return NS_OK;
michael@0 91 }
michael@0 92
michael@0 93 NS_IMETHODIMP nsScriptableRegion::IsEqualRegion(nsIScriptableRegion *aRegion, bool *isEqual)
michael@0 94 {
michael@0 95 nsIntRegion region;
michael@0 96 aRegion->GetRegion(&region);
michael@0 97 *isEqual = mRegion.IsEqual(region);
michael@0 98 return NS_OK;
michael@0 99 }
michael@0 100
michael@0 101 NS_IMETHODIMP nsScriptableRegion::GetBoundingBox(int32_t *aX, int32_t *aY, int32_t *aWidth, int32_t *aHeight)
michael@0 102 {
michael@0 103 nsIntRect boundRect = mRegion.GetBounds();
michael@0 104 *aX = boundRect.x;
michael@0 105 *aY = boundRect.y;
michael@0 106 *aWidth = boundRect.width;
michael@0 107 *aHeight = boundRect.height;
michael@0 108 return NS_OK;
michael@0 109 }
michael@0 110
michael@0 111 NS_IMETHODIMP nsScriptableRegion::Offset(int32_t aXOffset, int32_t aYOffset)
michael@0 112 {
michael@0 113 mRegion.MoveBy(aXOffset, aYOffset);
michael@0 114 return NS_OK;
michael@0 115 }
michael@0 116
michael@0 117 NS_IMETHODIMP nsScriptableRegion::ContainsRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight, bool *containsRect)
michael@0 118 {
michael@0 119 *containsRect = mRegion.Contains(nsIntRect(aX, aY, aWidth, aHeight));
michael@0 120 return NS_OK;
michael@0 121 }
michael@0 122
michael@0 123
michael@0 124 NS_IMETHODIMP nsScriptableRegion::GetRegion(nsIntRegion* outRgn)
michael@0 125 {
michael@0 126 *outRgn = mRegion;
michael@0 127 return NS_OK;
michael@0 128 }
michael@0 129
michael@0 130 NS_IMETHODIMP nsScriptableRegion::GetRects(JSContext* aCx, JS::MutableHandle<JS::Value> aRects)
michael@0 131 {
michael@0 132 uint32_t numRects = mRegion.GetNumRects();
michael@0 133
michael@0 134 if (!numRects) {
michael@0 135 aRects.setNull();
michael@0 136 return NS_OK;
michael@0 137 }
michael@0 138
michael@0 139 JS::Rooted<JSObject*> destArray(aCx, JS_NewArrayObject(aCx, numRects * 4));
michael@0 140 if (!destArray) {
michael@0 141 return NS_ERROR_OUT_OF_MEMORY;
michael@0 142 }
michael@0 143
michael@0 144 aRects.setObject(*destArray);
michael@0 145
michael@0 146 uint32_t n = 0;
michael@0 147 nsIntRegionRectIterator iter(mRegion);
michael@0 148 const nsIntRect *rect;
michael@0 149
michael@0 150 while ((rect = iter.Next())) {
michael@0 151 if (!JS_DefineElement(aCx, destArray, n, INT_TO_JSVAL(rect->x), nullptr, nullptr, JSPROP_ENUMERATE) ||
michael@0 152 !JS_DefineElement(aCx, destArray, n + 1, INT_TO_JSVAL(rect->y), nullptr, nullptr, JSPROP_ENUMERATE) ||
michael@0 153 !JS_DefineElement(aCx, destArray, n + 2, INT_TO_JSVAL(rect->width), nullptr, nullptr, JSPROP_ENUMERATE) ||
michael@0 154 !JS_DefineElement(aCx, destArray, n + 3, INT_TO_JSVAL(rect->height), nullptr, nullptr, JSPROP_ENUMERATE)) {
michael@0 155 return NS_ERROR_FAILURE;
michael@0 156 }
michael@0 157 n += 4;
michael@0 158 }
michael@0 159
michael@0 160 return NS_OK;
michael@0 161 }

mercurial