1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * The Mozilla Foundation <http://www.mozilla.org/>.
19 : * Portions created by the Initial Developer are Copyright (C) 2009-2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Dave Camp <dcamp@mozilla.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsJSInspector.h"
40 : #include "nsIXPConnect.h"
41 : #include "nsIJSContextStack.h"
42 : #include "nsThreadUtils.h"
43 : #include "jsapi.h"
44 : #include "jsgc.h"
45 : #include "jsfriendapi.h"
46 : #include "jsdbgapi.h"
47 : #include "mozilla/ModuleUtils.h"
48 : #include "nsServiceManagerUtils.h"
49 : #include "nsMemory.h"
50 :
51 : #define JSINSPECTOR_CONTRACTID \
52 : "@mozilla.org/jsinspector;1"
53 :
54 : #define JSINSPECTOR_CID \
55 : { 0xec5aa99c, 0x7abb, 0x4142, { 0xac, 0x5f, 0xaa, 0xb2, 0x41, 0x9e, 0x38, 0xe2 } }
56 :
57 : namespace mozilla {
58 : namespace jsinspector {
59 :
60 92 : NS_GENERIC_FACTORY_CONSTRUCTOR(nsJSInspector)
61 :
62 1064 : NS_IMPL_ISUPPORTS1(nsJSInspector, nsIJSInspector)
63 :
64 46 : nsJSInspector::nsJSInspector() : mNestedLoopLevel(0)
65 : {
66 46 : }
67 :
68 46 : nsJSInspector::~nsJSInspector()
69 : {
70 46 : }
71 :
72 : NS_IMETHODIMP
73 102 : nsJSInspector::EnterNestedEventLoop(PRUint32 *out)
74 : {
75 : nsresult rv;
76 : nsCOMPtr<nsIJSContextStack> stack =
77 204 : do_GetService("@mozilla.org/js/xpc/ContextStack;1", &rv);
78 102 : NS_ENSURE_SUCCESS(rv, rv);
79 :
80 102 : PRUint32 nestLevel = ++mNestedLoopLevel;
81 102 : if (NS_SUCCEEDED(stack->Push(nsnull))) {
82 1379 : while (NS_SUCCEEDED(rv) && mNestedLoopLevel >= nestLevel) {
83 1175 : if (!NS_ProcessNextEvent())
84 0 : rv = NS_ERROR_UNEXPECTED;
85 : }
86 :
87 : JSContext *cx;
88 102 : stack->Pop(&cx);
89 102 : NS_ASSERTION(cx == nsnull, "JSContextStack mismatch");
90 : } else {
91 0 : rv = NS_ERROR_FAILURE;
92 : }
93 :
94 102 : NS_ASSERTION(mNestedLoopLevel <= nestLevel,
95 : "nested event didn't unwind properly");
96 :
97 102 : if (mNestedLoopLevel == nestLevel)
98 0 : --mNestedLoopLevel;
99 :
100 102 : *out = mNestedLoopLevel;
101 102 : return rv;
102 : }
103 :
104 : NS_IMETHODIMP
105 102 : nsJSInspector::ExitNestedEventLoop(PRUint32 *out)
106 : {
107 102 : if (mNestedLoopLevel > 0) {
108 102 : --mNestedLoopLevel;
109 : } else {
110 0 : return NS_ERROR_FAILURE;
111 : }
112 :
113 102 : *out = mNestedLoopLevel;
114 :
115 102 : return NS_OK;
116 : }
117 :
118 : NS_IMETHODIMP
119 7 : nsJSInspector::GetEventLoopNestLevel(PRUint32 *out)
120 : {
121 7 : *out = mNestedLoopLevel;
122 7 : return NS_OK;
123 : }
124 :
125 : }
126 : }
127 :
128 : NS_DEFINE_NAMED_CID(JSINSPECTOR_CID);
129 :
130 : static const mozilla::Module::CIDEntry kJSInspectorCIDs[] = {
131 : { &kJSINSPECTOR_CID, false, NULL, mozilla::jsinspector::nsJSInspectorConstructor },
132 : { NULL }
133 : };
134 :
135 : static const mozilla::Module::ContractIDEntry kJSInspectorContracts[] = {
136 : { JSINSPECTOR_CONTRACTID, &kJSINSPECTOR_CID },
137 : { NULL }
138 : };
139 :
140 : static const mozilla::Module kJSInspectorModule = {
141 : mozilla::Module::kVersion,
142 : kJSInspectorCIDs,
143 : kJSInspectorContracts
144 : };
145 :
146 : NSMODULE_DEFN(jsinspector) = &kJSInspectorModule;
|