1 /* See license.txt for terms of usage */
  2 
  3 // ********************************************************************************************* //
  4 // Module
  5 
  6 define([
  7     "firebug/lib/object",
  8     "firebug/firebug",
  9     "firebug/lib/tool",
 10     "firebug/js/debugger",  // TODO firefox/jsdebugger
 11     "arch/compilationunit"
 12 ],
 13 function initializeJavaScriptTool(Obj, Firebug, Tool, JSDebugger, CompilationUnit) {
 14 
 15 // ********************************************************************************************* //
 16 // Implement JavaScript tool for Firefox inProcess
 17 
 18 var JavaScriptTool = Obj.extend(Firebug.Module,
 19 {
 20     dispatchName: "JavaScriptTool",
 21 });
 22 
 23 /**
 24  * A Turn is an call stack for an active being-handled event, similar to a thread.
 25  * Currently it only makes sense, when we have stopped the server.
 26  * Currently only one or zero Turn objects can exist ("single-threaded").
 27  */
 28 JavaScriptTool.Turn =
 29 {
 30 }
 31 
 32 JavaScriptTool.breakOnNext = function(context, enable)
 33 {
 34     if (enable)
 35         JSDebugger.suspend(context);
 36     else
 37         JSDebugger.unSuspend(context);
 38 }
 39 
 40 JavaScriptTool.setBreakpoint = function(context, url, lineNumber)
 41 {
 42     // TODO we should be sending URLs over, not compilation units
 43     var compilationUnit = context.getCompilationUnit(url);
 44     JSDebugger.setBreakpoint(compilationUnit, lineNumber);
 45 };
 46 
 47 JavaScriptTool.clearBreakpoint = function(context, url, lineNumber)
 48 {
 49     // This is more correct, but bypasses Debugger
 50     JSDebugger.fbs.clearBreakpoint(url, lineNumber);
 51 };
 52 
 53 JavaScriptTool.enableBreakpoint = function(context, url, lineNumber)
 54 {
 55     JSDebugger.fbs.enableBreakpoint(url, lineNumber);
 56 };
 57 
 58 JavaScriptTool.disableBreakpoint = function(context, url, lineNumber)
 59 {
 60     JSDebugger.fbs.disableBreakpoint(url, lineNumber);
 61 };
 62 
 63 JavaScriptTool.isBreakpointDisabled = function(context, url, lineNumber)
 64 {
 65     return JSDebugger.fbs.isBreakpointDisabled(url, lineNumber);
 66 };
 67 
 68 JavaScriptTool.getBreakpointCondition = function(context, url, lineNumber)
 69 {
 70     return JSDebugger.fbs.getBreakpointCondition(url, lineNumber);
 71 };
 72 
 73 // ********************************************************************************************* //
 74 // These functions should be on the stack instead.
 75 
 76 JavaScriptTool.rerun = function(context)
 77 {
 78     JSDebugger.rerun(context);
 79 };
 80 
 81 JavaScriptTool.resumeJavaScript = function(context)
 82 {
 83   JSDebugger.resume(context);
 84 };
 85 
 86 JavaScriptTool.stepOver = function(context)
 87 {
 88     JSDebugger.stepOver(context);
 89 };
 90 
 91 JavaScriptTool.stepInto = function(context)
 92 {
 93     JSDebugger.stepInto(context);
 94 };
 95 
 96 JavaScriptTool.stepOut = function(context)
 97 {
 98     JSDebugger.stepOut(context);
 99 };
100 
101 JavaScriptTool.runUntil = function(compilationUnit, lineNumber)
102 {
103     JSDebugger.runUntil(compilationUnit.getBrowserContext(), compilationUnit,
104         lineNumber, JSDebugger);
105 };
106 
107 /**
108  * Browser connection
109  */
110 JavaScriptTool.onConnect = function(connection)
111 {
112     if (!Firebug.connection.getTool("script"))
113     {
114         // this is the script tool
115         JavaScriptTool.asTool = new Tool("script"),
116         connection.registerTool(JavaScriptTool.asTool);
117     }
118     else
119     {
120         if (FBTrace.DBG_ERRORS)
121             FBTrace.sysout("JavaScriptTool onConnect ERROR script tool already registered");
122     }
123 };
124 
125 JavaScriptTool.onDisconnect = function(connection)
126 {
127     if (JavaScriptTool.asTool)
128         connection.unregisterTool(JavaScriptTool.asTool);
129 };
130 
131 /**
132  * Command the backend to enable JS
133  */
134 JavaScriptTool.setActivation = function(enable)
135 {
136     if (FBTrace.DBG_CONSOLE || FBTrace.DBG_ACTIVATION)
137         FBTrace.sysout("ScriptPanel.onActivationChanged; " + enable);
138 
139     if (enable)
140         JSDebugger.addObserver(this);
141     else
142         JSDebugger.removeObserver(this);
143 }
144 
145 /**
146  * A previously enabled tool becomes active and sends us an event.
147  */
148 JavaScriptTool.onActivateTool = function(toolname, active)
149 {
150     if (FBTrace.DBG_ACTIVATION)
151     {
152         FBTrace.sysout("JavaScriptTool.onActivateTool "+toolname+" = "+active+" asTool "+
153             JavaScriptTool.asTool);
154     }
155 
156     if (toolname === "script")
157     {
158         Firebug.ScriptPanel.prototype.onJavaScriptDebugging(active);
159         Firebug.connection.eachContext(function refresh(context)
160         {
161             context.invalidatePanels('script');
162         });
163         JavaScriptTool.asTool.setActive(active);
164     }
165 },
166 
167 /**
168  * @param context context of the newest frame, where the breakpoint hit
169  * @param frame newest StackFrame (crossbrowser) eg where the break point hit
170  */
171 JavaScriptTool.onStartDebugging = function(context, frame)
172 {
173     Firebug.selectContext(context);
174     var panel = Firebug.chrome.selectPanel("script");
175     if (!panel)
176     {
177         // Bail out if there is no UI
178         JavaScriptTool.resumeJavaScript(context);
179         return;
180     }
181 
182     if (FBTrace.DBG_STACK)
183         FBTrace.sysout("javascripttool currentFrame ", frame);
184 
185     JavaScriptTool.Turn.currentFrame = frame;
186     panel.onStartDebugging(frame);
187 }
188 
189 JavaScriptTool.onStopDebugging = function(context)
190 {
191     var panel = context.getPanel("script", true);
192     // Then we are looking at the Script panel.
193     if (panel && panel === Firebug.chrome.getSelectedPanel())
194     {
195         // unhighlight and remove toolbar-status line
196         panel.showNoStackFrame();
197     }
198 
199     if (panel)
200         panel.onStopDebugging();
201 
202     delete JavaScriptTool.Turn.currentFrame;
203 }
204 
205 JavaScriptTool.onCompilationUnit = function(context, url, kind)
206 {
207      var compilationUnit = new CompilationUnit(url, context);
208 
209      compilationUnit.kind = kind;
210 
211      context.compilationUnits[url] = compilationUnit;
212 
213      if (FBTrace.DBG_COMPILATION_UNITS)
214      {
215          FBTrace.sysout("JavaScriptTool.onCompilationUnit "+url+" added to "+context.getName(),
216              compilationUnit);
217      }
218 }
219 
220 JavaScriptTool.initialize = function()
221 {
222     if (FBTrace.DBG_INITIALIZE)
223         FBTrace.sysout("JavaScriptTool initialize");
224 
225     // This is how we get events.
226     Firebug.connection.addListener(JavaScriptTool);
227 }
228 
229 JavaScriptTool.shutdown = function()
230 {
231     // This is how we get events.
232     Firebug.connection.removeListener(JavaScriptTool);
233 }
234 
235 // ********************************************************************************************* //
236 // Registration
237 
238 Firebug.registerModule(JavaScriptTool);
239 
240 return JavaScriptTool;
241 
242 // ********************************************************************************************* //
243 });