ipc/testshell/TestShellParent.cpp

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

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

mercurial