2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #import "DebuggerDocument.h"
30 #import "DebuggerApplication.h"
31 #import <Carbon/Carbon.h>
33 static NSString *DebuggerConsoleToolbarItem = @"DebuggerConsoleToolbarItem";
34 static NSString *DebuggerContinueToolbarItem = @"DebuggerContinueToolbarItem";
35 static NSString *DebuggerPauseToolbarItem = @"DebuggerPauseToolbarItem";
36 static NSString *DebuggerStepIntoToolbarItem = @"DebuggerStepIntoToolbarItem";
37 static NSString *DebuggerStepOverToolbarItem = @"DebuggerStepOverToolbarItem";
38 static NSString *DebuggerStepOutToolbarItem = @"DebuggerStepOutToolbarItem";
40 @interface WebScriptObject (WebScriptObjectPrivate)
41 - (unsigned int)count;
44 @implementation DebuggerDocument
45 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
50 + (BOOL)isKeyExcludedFromWebScript:(const char *)name
57 - (id)initWithServerName:(NSString *)serverName
59 if ((self = [super init]))
60 [self switchToServerNamed:serverName];
67 [currentServerName release];
72 #pragma mark Stack & Variables
74 - (WebScriptCallFrame *)currentFrame
79 - (NSString *)currentFrameFunctionName
81 return [currentFrame functionName];
84 - (NSArray *)currentFunctionStack
86 NSMutableArray *result = [[NSMutableArray alloc] init];
87 WebScriptCallFrame *frame = currentFrame;
89 if ([frame functionName])
90 [result addObject:[frame functionName]];
91 else if ([frame caller])
92 [result addObject:@"(anonymous function)"];
94 [result addObject:@"(global scope)"];
95 frame = [frame caller];
97 return [result autorelease];
100 - (id)evaluateScript:(NSString *)script inCallFrame:(int)frame
102 WebScriptCallFrame *cframe = currentFrame;
103 for (unsigned count = 0; count < frame; count++)
104 cframe = [cframe caller];
108 id result = [cframe evaluateWebScript:script];
109 if ([result isKindOfClass:NSClassFromString(@"WebScriptObject")])
110 return [result callWebScriptMethod:@"toString" withArguments:nil];
114 - (NSArray *)webScriptAttributeKeysForScriptObject:(WebScriptObject *)object
116 [object evaluateWebScript:@"this.__drosera_introspection = function () { var result = new Array(); for (var x in this) { result.push(x); } return result; }"];
118 NSMutableArray *result = [[NSMutableArray alloc] init];
119 WebScriptObject *variables = [object callWebScriptMethod:@"__drosera_introspection" withArguments:nil];
120 unsigned length = [variables count];
121 for (unsigned i = 0; i < length; i++) {
122 NSString *key = [variables webScriptValueAtIndex:i];
123 if (![key isEqualToString:@"__drosera_introspection"])
124 [result addObject:key];
127 [object removeWebScriptKey:@"__drosera_introspection"];
129 [result sortUsingSelector:@selector(compare:)];
130 return [result autorelease];
133 - (NSArray *)localScopeVariableNamesForCallFrame:(int)frame
135 WebScriptCallFrame *cframe = currentFrame;
136 for (unsigned count = 0; count < frame; count++)
137 cframe = [cframe caller];
139 if (![[cframe scopeChain] count])
142 WebScriptObject *scope = [[cframe scopeChain] objectAtIndex:0]; // local is always first
143 return [self webScriptAttributeKeysForScriptObject:scope];
146 - (NSString *)valueForScopeVariableNamed:(NSString *)key inCallFrame:(int)frame
148 WebScriptCallFrame *cframe = currentFrame;
149 for (unsigned count = 0; count < frame; count++)
150 cframe = [cframe caller];
152 if (![[cframe scopeChain] count])
155 unsigned scopeCount = [[cframe scopeChain] count];
156 for (unsigned i = 0; i < scopeCount; i++) {
157 WebScriptObject *scope = [[cframe scopeChain] objectAtIndex:i];
158 id value = [scope valueForKey:key];
159 if ([value isKindOfClass:NSClassFromString(@"WebScriptObject")])
160 return [value callWebScriptMethod:@"toString" withArguments:nil];
161 if (value && ![value isKindOfClass:[NSString class]])
162 return [NSString stringWithFormat:@"%@", value];
171 #pragma mark System Information
173 - (int)doubleClickMilliseconds
175 // GetDblTime() returns values in 1/60ths of a second
176 return ((double)GetDblTime() / 60.0) * 1000;
180 #pragma mark File Loading
182 - (NSString *)breakpointEditorHTML
184 return [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"breakpointEditor" ofType:@"html"]];
188 #pragma mark Pause & Step
198 if ([[(NSDistantObject *)server connectionForProxy] isValid])
200 [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
206 if ([[(NSDistantObject *)server connectionForProxy] isValid])
212 if ([[(NSDistantObject *)server connectionForProxy] isValid])
216 - (void)log:(NSString *)msg
222 #pragma mark Interface Actions
224 - (IBAction)pause:(id)sender
226 [[webView windowScriptObject] callWebScriptMethod:@"pause" withArguments:nil];
229 - (IBAction)resume:(id)sender
231 [[webView windowScriptObject] callWebScriptMethod:@"resume" withArguments:nil];
234 - (IBAction)stepInto:(id)sender
236 [[webView windowScriptObject] callWebScriptMethod:@"stepInto" withArguments:nil];
239 - (IBAction)stepOver:(id)sender
241 [[webView windowScriptObject] callWebScriptMethod:@"stepOver" withArguments:nil];
244 - (IBAction)stepOut:(id)sender
246 [[webView windowScriptObject] callWebScriptMethod:@"stepOut" withArguments:nil];
249 - (IBAction)showConsole:(id)sender
251 [[webView windowScriptObject] callWebScriptMethod:@"showConsoleWindow" withArguments:nil];
255 #pragma mark Window Controller Overrides
257 - (NSString *)windowNibName
262 - (void)windowDidLoad
264 [super windowDidLoad];
266 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationTerminating:) name:NSApplicationWillTerminateNotification object:nil];
268 NSString *path = [[NSBundle mainBundle] pathForResource:@"debugger" ofType:@"html" inDirectory:nil];
269 [[webView mainFrame] loadRequest:[[[NSURLRequest alloc] initWithURL:[NSURL fileURLWithPath:path]] autorelease]];
271 NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"debugger"];
272 [toolbar setDelegate:self];
273 [toolbar setAllowsUserCustomization:YES];
274 [toolbar setAutosavesConfiguration:YES];
275 [[self window] setToolbar:toolbar];
279 - (void)windowWillClose:(NSNotification *)notification
281 [[webView windowScriptObject] removeWebScriptKey:@"DebuggerDocument"];
283 [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil];
285 [self switchToServerNamed:nil];
287 [self autorelease]; // DebuggerApplication expects us to release on close
291 #pragma mark Connection Handling
293 - (void)switchToServerNamed:(NSString *)name
296 [[NSNotificationCenter defaultCenter] removeObserver:self name:NSConnectionDidDieNotification object:[(NSDistantObject *)server connectionForProxy]];
297 if ([[(NSDistantObject *)server connectionForProxy] isValid]) {
298 [server removeListener:self];
304 server = ([name length] ? [[NSConnection rootProxyForConnectionWithRegisteredName:name host:nil] retain] : nil);
307 old = currentServerName;
308 currentServerName = [name copy];
313 [(NSDistantObject *)server setProtocolForProxy:@protocol(WebScriptDebugServer)];
314 [server addListener:self];
315 } @catch (NSException *exception) {
316 [currentServerName release];
317 currentServerName = nil;
323 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serverConnectionDidDie:) name:NSConnectionDidDieNotification object:[(NSDistantObject *)server connectionForProxy]];
327 - (void)applicationTerminating:(NSNotification *)notifiction
329 if (server && [[(NSDistantObject *)server connectionForProxy] isValid]) {
330 [self switchToServerNamed:nil];
331 // call the runloop for a while to make sure our removeListener: is sent to the server
332 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
336 - (void)serverConnectionDidDie:(NSNotification *)notifiction
338 [self switchToServerNamed:nil];
342 #pragma mark Toolbar Delegate
344 - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
346 if ([itemIdentifier isEqualToString:DebuggerContinueToolbarItem]) {
347 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
349 [item setLabel:@"Continue"];
350 [item setPaletteLabel:@"Continue"];
352 [item setToolTip:@"Continue script execution"];
353 [item setImage:[NSImage imageNamed:@"continue"]];
355 [item setTarget:self];
356 [item setAction:@selector(resume:)];
358 return [item autorelease];
359 } else if ([itemIdentifier isEqualToString:DebuggerConsoleToolbarItem]) {
360 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
362 [item setLabel:@"Console"];
363 [item setPaletteLabel:@"Console"];
365 [item setToolTip:@"Console"];
366 [item setImage:[NSImage imageNamed:@"console"]];
368 [item setTarget:self];
369 [item setAction:@selector(showConsole:)];
371 return [item autorelease];
372 } else if ([itemIdentifier isEqualToString:DebuggerPauseToolbarItem]) {
373 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
375 [item setLabel:@"Pause"];
376 [item setPaletteLabel:@"Pause"];
378 [item setToolTip:@"Pause script execution"];
379 [item setImage:[NSImage imageNamed:@"pause"]];
381 [item setTarget:self];
382 [item setAction:@selector(pause:)];
384 return [item autorelease];
385 } else if ([itemIdentifier isEqualToString:DebuggerStepIntoToolbarItem]) {
386 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
388 [item setLabel:@"Step Into"];
389 [item setPaletteLabel:@"Step Into"];
391 [item setToolTip:@"Step into function call"];
392 [item setImage:[NSImage imageNamed:@"step"]];
394 [item setTarget:self];
395 [item setAction:@selector(stepInto:)];
397 return [item autorelease];
398 } else if ([itemIdentifier isEqualToString:DebuggerStepOverToolbarItem]) {
399 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
401 [item setLabel:@"Step Over"];
402 [item setPaletteLabel:@"Step Over"];
404 [item setToolTip:@"Step over function call"];
405 [item setImage:[NSImage imageNamed:@"stepOver"]];
407 [item setTarget:self];
408 [item setAction:@selector(stepOver:)];
410 return [item autorelease];
411 } else if ([itemIdentifier isEqualToString:DebuggerStepOutToolbarItem]) {
412 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
414 [item setLabel:@"Step Out"];
415 [item setPaletteLabel:@"Step Over"];
417 [item setToolTip:@"Step out of current function"];
418 [item setImage:[NSImage imageNamed:@"stepOut"]];
420 [item setTarget:self];
421 [item setAction:@selector(stepOut:)];
423 return [item autorelease];
429 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
431 return [NSArray arrayWithObjects:DebuggerContinueToolbarItem, DebuggerPauseToolbarItem,
432 NSToolbarSeparatorItemIdentifier, DebuggerStepIntoToolbarItem, DebuggerStepOutToolbarItem,
433 DebuggerStepOverToolbarItem, NSToolbarFlexibleSpaceItemIdentifier, DebuggerConsoleToolbarItem, nil];
436 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
438 return [NSArray arrayWithObjects:DebuggerConsoleToolbarItem, DebuggerContinueToolbarItem, DebuggerPauseToolbarItem,
439 DebuggerStepIntoToolbarItem, DebuggerStepOutToolbarItem, DebuggerStepOverToolbarItem, NSToolbarCustomizeToolbarItemIdentifier,
440 NSToolbarFlexibleSpaceItemIdentifier, NSToolbarSpaceItemIdentifier, NSToolbarSeparatorItemIdentifier, nil];
443 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)interfaceItem
445 SEL action = [interfaceItem action];
446 if (action == @selector(pause:))
447 return ![self isPaused];
448 if (action == @selector(resume:) ||
449 action == @selector(stepOver:) ||
450 action == @selector(stepOut:) ||
451 action == @selector(stepInto:))
452 return [self isPaused];
457 #pragma mark WebView UI Delegate
459 - (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
461 WebView *newWebView = [[WebView alloc] initWithFrame:NSZeroRect frameName:nil groupName:nil];
462 [newWebView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
463 [newWebView setUIDelegate:self];
464 [newWebView setPolicyDelegate:self];
465 [newWebView setFrameLoadDelegate:self];
467 [[newWebView mainFrame] loadRequest:request];
469 NSWindow *window = [[NSWindow alloc] initWithContentRect:NSZeroRect styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask | NSUnifiedTitleAndToolbarWindowMask) backing:NSBackingStoreBuffered defer:NO screen:[[webView window] screen]];
470 [window setReleasedWhenClosed:YES];
471 [newWebView setFrame:[[window contentView] frame]];
472 [[window contentView] addSubview:newWebView];
473 [newWebView release];
478 - (void)webViewShow:(WebView *)sender
480 [[sender window] makeKeyAndOrderFront:sender];
483 - (BOOL)webViewAreToolbarsVisible:(WebView *)sender
485 return [[[sender window] toolbar] isVisible];
488 - (void)webView:(WebView *)sender setToolbarsVisible:(BOOL)visible
490 [[[sender window] toolbar] setVisible:visible];
493 - (void)webView:(WebView *)sender setResizable:(BOOL)resizable
495 [[sender window] setShowsResizeIndicator:resizable];
496 [[[sender window] standardWindowButton:NSWindowZoomButton] setEnabled:resizable];
499 - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
501 NSRange range = [message rangeOfString:@"\t"];
502 NSString *title = @"Alert";
503 if (range.location != NSNotFound) {
504 title = [message substringToIndex:range.location];
505 message = [message substringFromIndex:(range.location + range.length)];
508 NSBeginInformationalAlertSheet(title, nil, nil, nil, [sender window], nil, NULL, NULL, NULL, message);
511 - (void)scriptConfirmSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(int *)contextInfo
513 *contextInfo = returnCode;
516 - (BOOL)webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
518 NSRange range = [message rangeOfString:@"\t"];
519 NSString *title = @"Alert";
520 if (range.location != NSNotFound) {
521 title = [message substringToIndex:range.location];
522 message = [message substringFromIndex:(range.location + range.length)];
525 int result = NSNotFound;
526 NSBeginInformationalAlertSheet(title, nil, @"Cancel", nil, [sender window], self, @selector(scriptConfirmSheetDidEnd:returnCode:contextInfo:), NULL, &result, message);
528 while (result == NSNotFound) {
529 NSEvent *nextEvent = [[NSApplication sharedApplication] nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:YES];
530 [[NSApplication sharedApplication] sendEvent:nextEvent];
537 #pragma mark WebView Frame Load Delegate
539 - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)windowScriptObject
541 // note: this is the Debuggers's own WebView, not the one being debugged
542 if ([[sender window] isEqual:[self window]])
543 [[sender windowScriptObject] setValue:self forKey:@"DebuggerDocument"];
546 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
548 // note: this is the Debuggers's own WebView, not the one being debugged
549 if ([[sender window] isEqual:[self window]])
553 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
555 // note: this is the Debuggers's own WebViews, not the one being debugged
556 if ([frame isEqual:[sender mainFrame]]) {
557 NSDictionary *info = [[(DebuggerApplication *)[[NSApplication sharedApplication] delegate] knownServers] objectForKey:currentServerName];
558 NSString *processName = [info objectForKey:WebScriptDebugServerProcessNameKey];
559 if (info && [processName length]) {
560 NSMutableString *newTitle = [[NSMutableString alloc] initWithString:processName];
561 [newTitle appendString:@" - "];
562 [newTitle appendString:title];
563 [[sender window] setTitle:newTitle];
566 [[sender window] setTitle:title];
571 #pragma mark Debug Listener Callbacks
573 - (void)webView:(WebView *)view didLoadMainResourceForDataSource:(WebDataSource *)dataSource
575 NSString *documentSourceCopy = nil;
576 id <WebDocumentRepresentation> rep = [dataSource representation];
577 if ([rep canProvideDocumentSource])
578 documentSourceCopy = [[rep documentSource] copy];
580 if (!documentSourceCopy)
583 NSString *urlCopy = [[[[dataSource response] URL] absoluteString] copy];
584 NSArray *args = [[NSArray alloc] initWithObjects:(documentSourceCopy ? documentSourceCopy : @""), (urlCopy ? urlCopy : @""), [NSNumber numberWithBool:NO], nil];
585 [[webView windowScriptObject] callWebScriptMethod:@"updateFileSource" withArguments:args];
588 [documentSourceCopy release];
592 - (void)webView:(WebView *)view didParseSource:(NSString *)source baseLineNumber:(unsigned)baseLine fromURL:(NSURL *)url sourceId:(int)sid forWebFrame:(WebFrame *)webFrame
597 NSString *sourceCopy = [source copy];
601 NSString *documentSourceCopy = nil;
602 NSString *urlCopy = [[url absoluteString] copy];
604 WebDataSource *dataSource = [webFrame dataSource];
605 if (!url || [[[dataSource response] URL] isEqual:url]) {
606 id <WebDocumentRepresentation> rep = [dataSource representation];
607 if ([rep canProvideDocumentSource])
608 documentSourceCopy = [[rep documentSource] copy];
610 urlCopy = [[[[dataSource response] URL] absoluteString] copy];
613 NSArray *args = [[NSArray alloc] initWithObjects:sourceCopy, (documentSourceCopy ? documentSourceCopy : @""), (urlCopy ? urlCopy : @""), [NSNumber numberWithInt:sid], [NSNumber numberWithUnsignedInt:baseLine], nil];
614 [[webView windowScriptObject] callWebScriptMethod:@"didParseScript" withArguments:args];
617 [sourceCopy release];
618 [documentSourceCopy release];
622 - (void)webView:(WebView *)view failedToParseSource:(NSString *)source baseLineNumber:(unsigned)baseLine fromURL:(NSURL *)url withError:(NSError *)error forWebFrame:(WebFrame *)webFrame
626 - (void)webView:(WebView *)view didEnterCallFrame:(WebScriptCallFrame *)frame sourceId:(int)sid line:(int)lineno forWebFrame:(WebFrame *)webFrame
631 id old = currentFrame;
632 currentFrame = [frame retain];
635 NSArray *args = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:sid], [NSNumber numberWithInt:lineno], nil];
636 [[webView windowScriptObject] callWebScriptMethod:@"didEnterCallFrame" withArguments:args];
640 - (void)webView:(WebView *)view willExecuteStatement:(WebScriptCallFrame *)frame sourceId:(int)sid line:(int)lineno forWebFrame:(WebFrame *)webFrame
645 NSArray *args = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:sid], [NSNumber numberWithInt:lineno], nil];
646 [[webView windowScriptObject] callWebScriptMethod:@"willExecuteStatement" withArguments:args];
650 - (void)webView:(WebView *)view willLeaveCallFrame:(WebScriptCallFrame *)frame sourceId:(int)sid line:(int)lineno forWebFrame:(WebFrame *)webFrame
655 NSArray *args = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:sid], [NSNumber numberWithInt:lineno], nil];
656 [[webView windowScriptObject] callWebScriptMethod:@"willLeaveCallFrame" withArguments:args];
659 id old = currentFrame;
660 currentFrame = [[frame caller] retain];
664 - (void)webView:(WebView *)view exceptionWasRaised:(WebScriptCallFrame *)frame sourceId:(int)sid line:(int)lineno forWebFrame:(WebFrame *)webFrame
669 NSArray *args = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:sid], [NSNumber numberWithInt:lineno], nil];
670 [[webView windowScriptObject] callWebScriptMethod:@"exceptionWasRaised" withArguments:args];