widget/windows/IEnumFE.cpp

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
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 "IEnumFE.h"
     7 #include "nsAlgorithm.h"
     8 #include <algorithm>
    10 CEnumFormatEtc::CEnumFormatEtc() :
    11   mRefCnt(0),
    12   mCurrentIdx(0)
    13 {
    14 }
    16 // Constructor used by Clone()
    17 CEnumFormatEtc::CEnumFormatEtc(nsTArray<FormatEtc>& aArray) :
    18   mRefCnt(0),
    19   mCurrentIdx(0)
    20 {
    21   // a deep copy, calls FormatEtc's copy constructor on each
    22   mFormatList.AppendElements(aArray);
    23 }
    25 CEnumFormatEtc::~CEnumFormatEtc()
    26 {
    27 }
    29 /* IUnknown impl. */
    31 STDMETHODIMP
    32 CEnumFormatEtc::QueryInterface(REFIID riid, LPVOID *ppv)
    33 {
    34   *ppv = nullptr;
    36   if (IsEqualIID(riid, IID_IUnknown) ||
    37       IsEqualIID(riid, IID_IEnumFORMATETC))
    38       *ppv = (LPVOID)this;
    40   if (*ppv == nullptr)
    41       return E_NOINTERFACE;
    43   // AddRef any interface we'll return.
    44   ((LPUNKNOWN)*ppv)->AddRef();
    45   return S_OK;
    46 }
    48 STDMETHODIMP_(ULONG)
    49 CEnumFormatEtc::AddRef()
    50 {
    51   ++mRefCnt;
    52   NS_LOG_ADDREF(this, mRefCnt, "CEnumFormatEtc",sizeof(*this));
    53   return mRefCnt;
    54 }
    56 STDMETHODIMP_(ULONG)
    57 CEnumFormatEtc::Release()
    58 {
    59   uint32_t refReturn;
    61   refReturn = --mRefCnt;
    62   NS_LOG_RELEASE(this, mRefCnt, "CEnumFormatEtc");
    64   if (mRefCnt == 0)
    65       delete this;
    67   return refReturn;
    68 }
    70 /* IEnumFORMATETC impl. */
    72 STDMETHODIMP
    73 CEnumFormatEtc::Next(ULONG aMaxToFetch, FORMATETC *aResult, ULONG *aNumFetched)
    74 {
    75   // If the method retrieves the number of items requested, the return
    76   // value is S_OK. Otherwise, it is S_FALSE.
    78   if (aNumFetched)
    79       *aNumFetched = 0;
    81   // aNumFetched can be null if aMaxToFetch is 1
    82   if (!aNumFetched && aMaxToFetch > 1)
    83       return S_FALSE;
    85   if (!aResult)
    86       return S_FALSE;
    88   // We're done walking the list
    89   if (mCurrentIdx >= mFormatList.Length())
    90       return S_FALSE;
    92   uint32_t left = mFormatList.Length() - mCurrentIdx;
    94   if (!aMaxToFetch)
    95       return S_FALSE;
    97   uint32_t count = std::min(static_cast<uint32_t>(aMaxToFetch), left);
    99   uint32_t idx = 0;
   100   while (count > 0) {
   101       // Copy out to aResult
   102       mFormatList[mCurrentIdx++].CopyOut(&aResult[idx++]);
   103       count--;
   104   }
   106   if (aNumFetched)
   107       *aNumFetched = idx;
   109   return S_OK;
   110 }
   112 STDMETHODIMP
   113 CEnumFormatEtc::Skip(ULONG aSkipNum)
   114 {
   115   // If the method skips the number of items requested, the return value is S_OK.
   116   // Otherwise, it is S_FALSE.
   118   if ((mCurrentIdx + aSkipNum) >= mFormatList.Length())
   119       return S_FALSE;
   121   mCurrentIdx += aSkipNum;
   123   return S_OK;
   124 }
   126 STDMETHODIMP
   127 CEnumFormatEtc::Reset(void)
   128 {
   129   mCurrentIdx = 0;
   130   return S_OK;
   131 }
   133 STDMETHODIMP
   134 CEnumFormatEtc::Clone(LPENUMFORMATETC *aResult)
   135 {
   136   // Must return a new IEnumFORMATETC interface with the same iterative state.
   138   if (!aResult)
   139       return E_INVALIDARG;
   141   CEnumFormatEtc * pEnumObj = new CEnumFormatEtc(mFormatList);
   143   if (!pEnumObj)
   144       return E_OUTOFMEMORY;
   146   pEnumObj->AddRef();
   147   pEnumObj->SetIndex(mCurrentIdx);
   149   *aResult = pEnumObj;
   151   return S_OK;
   152 }
   154 /* utils */
   156 void
   157 CEnumFormatEtc::AddFormatEtc(LPFORMATETC aFormat)
   158 {
   159   if (!aFormat)
   160       return;
   161   FormatEtc * etc = mFormatList.AppendElement();
   162   // Make a copy of aFormat
   163   if (etc)
   164       etc->CopyIn(aFormat);
   165 }
   167 /* private */
   169 void
   170 CEnumFormatEtc::SetIndex(uint32_t aIdx)
   171 {
   172   mCurrentIdx = aIdx;
   173 }

mercurial