michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsJSInspector.h" michael@0: #include "nsIXPConnect.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "jsfriendapi.h" michael@0: #include "js/OldDebugAPI.h" michael@0: #include "mozilla/HoldDropJSObjects.h" michael@0: #include "mozilla/ModuleUtils.h" michael@0: #include "mozilla/dom/ScriptSettings.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsMemory.h" michael@0: #include "nsArray.h" michael@0: #include "nsTArray.h" michael@0: michael@0: #define JSINSPECTOR_CONTRACTID \ michael@0: "@mozilla.org/jsinspector;1" michael@0: michael@0: #define JSINSPECTOR_CID \ michael@0: { 0xec5aa99c, 0x7abb, 0x4142, { 0xac, 0x5f, 0xaa, 0xb2, 0x41, 0x9e, 0x38, 0xe2 } } michael@0: michael@0: namespace mozilla { michael@0: namespace jsinspector { michael@0: michael@0: NS_GENERIC_FACTORY_CONSTRUCTOR(nsJSInspector) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsJSInspector) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_ENTRY(nsIJSInspector) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CLASS(nsJSInspector) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(nsJSInspector) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(nsJSInspector) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsJSInspector) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsJSInspector) michael@0: tmp->mRequestors.Clear(); michael@0: tmp->mLastRequestor = JS::NullValue(); michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsJSInspector) michael@0: for (uint32_t i = 0; i < tmp->mRequestors.Length(); ++i) { michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mRequestors[i]) michael@0: } michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mLastRequestor) michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_END michael@0: michael@0: nsJSInspector::nsJSInspector() : mNestedLoopLevel(0), mRequestors(1), mLastRequestor(JSVAL_NULL) michael@0: { michael@0: } michael@0: michael@0: nsJSInspector::~nsJSInspector() michael@0: { michael@0: MOZ_ASSERT(mRequestors.Length() == 0); michael@0: MOZ_ASSERT(mLastRequestor.isNull()); michael@0: mozilla::DropJSObjects(this); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsJSInspector::EnterNestedEventLoop(JS::Handle requestor, uint32_t *out) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: michael@0: mLastRequestor = requestor; michael@0: mRequestors.AppendElement(requestor); michael@0: mozilla::HoldJSObjects(this); michael@0: michael@0: mozilla::dom::AutoNoJSAPI nojsapi; michael@0: michael@0: uint32_t nestLevel = ++mNestedLoopLevel; michael@0: while (NS_SUCCEEDED(rv) && mNestedLoopLevel >= nestLevel) { michael@0: if (!NS_ProcessNextEvent()) michael@0: rv = NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: NS_ASSERTION(mNestedLoopLevel <= nestLevel, michael@0: "nested event didn't unwind properly"); michael@0: michael@0: if (mNestedLoopLevel == nestLevel) { michael@0: mLastRequestor = mRequestors.ElementAt(--mNestedLoopLevel); michael@0: } michael@0: michael@0: *out = mNestedLoopLevel; michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsJSInspector::ExitNestedEventLoop(uint32_t *out) michael@0: { michael@0: if (mNestedLoopLevel > 0) { michael@0: mRequestors.RemoveElementAt(--mNestedLoopLevel); michael@0: if (mNestedLoopLevel > 0) michael@0: mLastRequestor = mRequestors.ElementAt(mNestedLoopLevel - 1); michael@0: else michael@0: mLastRequestor = JSVAL_NULL; michael@0: } else { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: *out = mNestedLoopLevel; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsJSInspector::GetEventLoopNestLevel(uint32_t *out) michael@0: { michael@0: *out = mNestedLoopLevel; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsJSInspector::GetLastNestRequestor(JS::MutableHandle out) michael@0: { michael@0: out.set(mLastRequestor); michael@0: return NS_OK; michael@0: } michael@0: michael@0: } michael@0: } michael@0: michael@0: NS_DEFINE_NAMED_CID(JSINSPECTOR_CID); michael@0: michael@0: static const mozilla::Module::CIDEntry kJSInspectorCIDs[] = { michael@0: { &kJSINSPECTOR_CID, false, nullptr, mozilla::jsinspector::nsJSInspectorConstructor }, michael@0: { nullptr } michael@0: }; michael@0: michael@0: static const mozilla::Module::ContractIDEntry kJSInspectorContracts[] = { michael@0: { JSINSPECTOR_CONTRACTID, &kJSINSPECTOR_CID }, michael@0: { nullptr } michael@0: }; michael@0: michael@0: static const mozilla::Module kJSInspectorModule = { michael@0: mozilla::Module::kVersion, michael@0: kJSInspectorCIDs, michael@0: kJSInspectorContracts michael@0: }; michael@0: michael@0: NSMODULE_DEFN(jsinspector) = &kJSInspectorModule;