xpcom/glue/nsProxyRelease.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 /* vim:set ts=4 sw=4 sts=4 et cin: */
     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 "nsProxyRelease.h"
     7 #include "nsThreadUtils.h"
     8 #include "nsAutoPtr.h"
    10 class nsProxyReleaseEvent : public nsRunnable
    11 {
    12 public:
    13     nsProxyReleaseEvent(nsISupports *doomed)
    14         : mDoomed(doomed) {
    15     }
    17     NS_IMETHOD Run()
    18     {
    19         mDoomed->Release();
    20         return NS_OK;
    21     }
    23 private:
    24     nsISupports *mDoomed;
    25 };
    27 nsresult
    28 NS_ProxyRelease(nsIEventTarget *target, nsISupports *doomed,
    29                 bool alwaysProxy)
    30 {
    31     nsresult rv;
    33     if (!doomed) {
    34         // nothing to do
    35         return NS_OK;
    36     }
    38     if (!target) {
    39         NS_RELEASE(doomed);
    40         return NS_OK;
    41     }
    43     if (!alwaysProxy) {
    44         bool onCurrentThread = false;
    45         rv = target->IsOnCurrentThread(&onCurrentThread);
    46         if (NS_SUCCEEDED(rv) && onCurrentThread) {
    47             NS_RELEASE(doomed);
    48             return NS_OK;
    49         }
    50     }
    52     nsRefPtr<nsIRunnable> ev = new nsProxyReleaseEvent(doomed);
    53     if (!ev) {
    54         // we do not release doomed here since it may cause a delete on the
    55         // wrong thread.  better to leak than crash. 
    56         return NS_ERROR_OUT_OF_MEMORY;
    57     }
    59     rv = target->Dispatch(ev, NS_DISPATCH_NORMAL);
    60     if (NS_FAILED(rv)) {
    61         NS_WARNING("failed to post proxy release event");
    62         // again, it is better to leak the doomed object than risk crashing as
    63         // a result of deleting it on the wrong thread.
    64     }
    65     return rv;
    66 }

mercurial