ipc/testshell/TestShellParent.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 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "TestShellParent.h"
michael@0 6
michael@0 7 /* This must occur *after* TestShellParent.h to avoid typedefs conflicts. */
michael@0 8 #include "mozilla/ArrayUtils.h"
michael@0 9
michael@0 10 #include "mozilla/dom/ContentParent.h"
michael@0 11
michael@0 12 #include "nsAutoPtr.h"
michael@0 13 #include "nsCxPusher.h"
michael@0 14
michael@0 15 using namespace mozilla;
michael@0 16 using mozilla::ipc::TestShellParent;
michael@0 17 using mozilla::ipc::TestShellCommandParent;
michael@0 18 using mozilla::ipc::PTestShellCommandParent;
michael@0 19 using mozilla::dom::ContentParent;
michael@0 20
michael@0 21 PTestShellCommandParent*
michael@0 22 TestShellParent::AllocPTestShellCommandParent(const nsString& aCommand)
michael@0 23 {
michael@0 24 return new TestShellCommandParent();
michael@0 25 }
michael@0 26
michael@0 27 bool
michael@0 28 TestShellParent::DeallocPTestShellCommandParent(PTestShellCommandParent* aActor)
michael@0 29 {
michael@0 30 delete aActor;
michael@0 31 return true;
michael@0 32 }
michael@0 33
michael@0 34 bool
michael@0 35 TestShellParent::CommandDone(TestShellCommandParent* command,
michael@0 36 const nsString& aResponse)
michael@0 37 {
michael@0 38 // XXX what should happen if the callback fails?
michael@0 39 /*bool ok = */command->RunCallback(aResponse);
michael@0 40 command->ReleaseCallback();
michael@0 41
michael@0 42 return true;
michael@0 43 }
michael@0 44
michael@0 45 bool
michael@0 46 TestShellCommandParent::SetCallback(JSContext* aCx,
michael@0 47 JS::Value aCallback)
michael@0 48 {
michael@0 49 if (!mCallback.Hold(aCx)) {
michael@0 50 return false;
michael@0 51 }
michael@0 52
michael@0 53 mCallback = aCallback;
michael@0 54 mCx = aCx;
michael@0 55
michael@0 56 return true;
michael@0 57 }
michael@0 58
michael@0 59 bool
michael@0 60 TestShellCommandParent::RunCallback(const nsString& aResponse)
michael@0 61 {
michael@0 62 NS_ENSURE_TRUE(!mCallback.get().isNull() && mCx, false);
michael@0 63
michael@0 64 // We're pulling a cx off the heap, so make sure it's stack-top.
michael@0 65 AutoCxPusher pusher(mCx);
michael@0 66 NS_ENSURE_TRUE(mCallback.ToJSObject(), false);
michael@0 67 JSAutoCompartment ac(mCx, mCallback.ToJSObject());
michael@0 68 JS::Rooted<JSObject*> global(mCx, JS::CurrentGlobalOrNull(mCx));
michael@0 69
michael@0 70 JSString* str = JS_NewUCStringCopyN(mCx, aResponse.get(), aResponse.Length());
michael@0 71 NS_ENSURE_TRUE(str, false);
michael@0 72
michael@0 73 JS::Rooted<JS::Value> strVal(mCx, JS::StringValue(str));
michael@0 74
michael@0 75 JS::Rooted<JS::Value> rval(mCx);
michael@0 76 JS::Rooted<JS::Value> callback(mCx, mCallback);
michael@0 77 bool ok = JS_CallFunctionValue(mCx, global, callback, strVal, &rval);
michael@0 78 NS_ENSURE_TRUE(ok, false);
michael@0 79
michael@0 80 return true;
michael@0 81 }
michael@0 82
michael@0 83 void
michael@0 84 TestShellCommandParent::ReleaseCallback()
michael@0 85 {
michael@0 86 mCallback.Release();
michael@0 87 }
michael@0 88
michael@0 89 bool
michael@0 90 TestShellCommandParent::ExecuteCallback(const nsString& aResponse)
michael@0 91 {
michael@0 92 return static_cast<TestShellParent*>(Manager())->CommandDone(
michael@0 93 this, aResponse);
michael@0 94 }
michael@0 95
michael@0 96 void
michael@0 97 TestShellCommandParent::ActorDestroy(ActorDestroyReason why)
michael@0 98 {
michael@0 99 if (why == AbnormalShutdown) {
michael@0 100 ExecuteCallback(EmptyString());
michael@0 101 }
michael@0 102 }

mercurial