editor/txmgr/src/nsTransactionList.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.

     1 /* -*- Mode: C++; tab-width: 2; 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 #include "mozilla/mozalloc.h"
     7 #include "nsCOMPtr.h"
     8 #include "nsDebug.h"
     9 #include "nsError.h"
    10 #include "nsID.h"
    11 #include "nsISupportsUtils.h"
    12 #include "nsITransactionManager.h"
    13 #include "nsTransactionItem.h"
    14 #include "nsTransactionList.h"
    15 #include "nsTransactionStack.h"
    16 #include "nscore.h"
    18 NS_IMPL_ISUPPORTS(nsTransactionList, nsITransactionList)
    20 nsTransactionList::nsTransactionList(nsITransactionManager *aTxnMgr, nsTransactionStack *aTxnStack)
    21   : mTxnStack(aTxnStack)
    22   , mTxnItem(0)
    23 {
    24   if (aTxnMgr)
    25     mTxnMgr = do_GetWeakReference(aTxnMgr);
    26 }
    28 nsTransactionList::nsTransactionList(nsITransactionManager *aTxnMgr, nsTransactionItem *aTxnItem)
    29   : mTxnStack(0)
    30   , mTxnItem(aTxnItem)
    31 {
    32   if (aTxnMgr)
    33     mTxnMgr = do_GetWeakReference(aTxnMgr);
    34 }
    36 nsTransactionList::~nsTransactionList()
    37 {
    38   mTxnStack = 0;
    39   mTxnItem  = 0;
    40 }
    42 /* readonly attribute long numItems; */
    43 NS_IMETHODIMP nsTransactionList::GetNumItems(int32_t *aNumItems)
    44 {
    45   NS_ENSURE_TRUE(aNumItems, NS_ERROR_NULL_POINTER);
    47   *aNumItems = 0;
    49   nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
    51   NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
    53   nsresult result = NS_OK;
    55   if (mTxnStack)
    56     *aNumItems = mTxnStack->GetSize();
    57   else if (mTxnItem)
    58     result = mTxnItem->GetNumberOfChildren(aNumItems);
    60   return result;
    61 }
    63 /* boolean itemIsBatch (in long aIndex); */
    64 NS_IMETHODIMP nsTransactionList::ItemIsBatch(int32_t aIndex, bool *aIsBatch)
    65 {
    66   NS_ENSURE_TRUE(aIsBatch, NS_ERROR_NULL_POINTER);
    68   *aIsBatch = false;
    70   nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
    72   NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
    74   nsRefPtr<nsTransactionItem> item;
    76   nsresult result = NS_OK;
    78   if (mTxnStack)
    79     item = mTxnStack->GetItem(aIndex);
    80   else if (mTxnItem)
    81     result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
    83   NS_ENSURE_SUCCESS(result, result);
    85   NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
    87   return item->GetIsBatch(aIsBatch);
    88 }
    90 /* void getData (in long aIndex,
    91                  [optional] out unsigned long aLength,
    92                  [array, size_is (aLength), retval]
    93                  out nsISupports aData); */
    94 NS_IMETHODIMP nsTransactionList::GetData(int32_t aIndex,
    95                                          uint32_t *aLength,
    96                                          nsISupports ***aData)
    97 {
    98   nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
   100   NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
   102   nsRefPtr<nsTransactionItem> item;
   104   if (mTxnStack) {
   105     item = mTxnStack->GetItem(aIndex);
   106   } else if (mTxnItem) {
   107     nsresult result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
   108     NS_ENSURE_SUCCESS(result, result);
   109   }
   111   nsCOMArray<nsISupports>& data = item->GetData();
   113   nsISupports** ret = static_cast<nsISupports**>(NS_Alloc(data.Count() *
   114     sizeof(nsISupports*)));
   116   for (int32_t i = 0; i < data.Count(); i++) {
   117     NS_ADDREF(ret[i] = data[i]);
   118   }
   120   *aLength = data.Count();
   121   *aData = ret;
   123   return NS_OK;
   124 }
   126 /* nsITransaction getItem (in long aIndex); */
   127 NS_IMETHODIMP nsTransactionList::GetItem(int32_t aIndex, nsITransaction **aItem)
   128 {
   129   NS_ENSURE_TRUE(aItem, NS_ERROR_NULL_POINTER);
   131   *aItem = 0;
   133   nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
   135   NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
   137   nsRefPtr<nsTransactionItem> item;
   139   nsresult result = NS_OK;
   141   if (mTxnStack)
   142     item = mTxnStack->GetItem(aIndex);
   143   else if (mTxnItem)
   144     result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
   146   NS_ENSURE_SUCCESS(result, result);
   148   NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
   150   *aItem = item->GetTransaction().take();
   152   return NS_OK;
   153 }
   155 /* long getNumChildrenForItem (in long aIndex); */
   156 NS_IMETHODIMP nsTransactionList::GetNumChildrenForItem(int32_t aIndex, int32_t *aNumChildren)
   157 {
   158   NS_ENSURE_TRUE(aNumChildren, NS_ERROR_NULL_POINTER);
   160   *aNumChildren = 0;
   162   nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
   164   NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
   166   nsRefPtr<nsTransactionItem> item;
   168   nsresult result = NS_OK;
   170   if (mTxnStack)
   171     item = mTxnStack->GetItem(aIndex);
   172   else if (mTxnItem)
   173     result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
   175   NS_ENSURE_SUCCESS(result, result);
   177   NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
   179   return item->GetNumberOfChildren(aNumChildren);
   180 }
   182 /* nsITransactionList getChildListForItem (in long aIndex); */
   183 NS_IMETHODIMP nsTransactionList::GetChildListForItem(int32_t aIndex, nsITransactionList **aTxnList)
   184 {
   185   NS_ENSURE_TRUE(aTxnList, NS_ERROR_NULL_POINTER);
   187   *aTxnList = 0;
   189   nsCOMPtr<nsITransactionManager> txMgr = do_QueryReferent(mTxnMgr);
   191   NS_ENSURE_TRUE(txMgr, NS_ERROR_FAILURE);
   193   nsRefPtr<nsTransactionItem> item;
   195   nsresult result = NS_OK;
   197   if (mTxnStack)
   198     item = mTxnStack->GetItem(aIndex);
   199   else if (mTxnItem)
   200     result = mTxnItem->GetChild(aIndex, getter_AddRefs(item));
   202   NS_ENSURE_SUCCESS(result, result);
   204   NS_ENSURE_TRUE(item, NS_ERROR_FAILURE);
   206   *aTxnList = (nsITransactionList *)new nsTransactionList(txMgr, item);
   208   NS_ENSURE_TRUE(*aTxnList, NS_ERROR_OUT_OF_MEMORY);
   210   NS_ADDREF(*aTxnList);
   212   return NS_OK;
   213 }

mercurial