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 WebScriptObject *enumerateAttributes = [object evaluateWebScript:@"(function () { var result = new Array(); for (var x in this) { result.push(x); } return result; })"];
118 NSMutableArray *result = [[NSMutableArray alloc] init];
119 WebScriptObject *variables = [enumerateAttributes callWebScriptMethod:@"call" withArguments:[NSArray arrayWithObject:object]];
120 unsigned length = [variables count];
121 for (unsigned i = 0; i < length; i++) {
122 NSString *key = [variables webScriptValueAtIndex:i];
123 [result addObject:key];
126 [result sortUsingSelector:@selector(compare:)];
127 return [result autorelease];
130 - (NSArray *)localScopeVariableNamesForCallFrame:(int)frame
132 WebScriptCallFrame *cframe = currentFrame;
133 for (unsigned count = 0; count < frame; count++)
134 cframe = [cframe caller];
136 if (![[cframe scopeChain] count])
139 WebScriptObject *scope = [[cframe scopeChain] objectAtIndex:0]; // local is always first
140 return [self webScriptAttributeKeysForScriptObject:scope];
143 - (NSString *)valueForScopeVariableNamed:(NSString *)key inCallFrame:(int)frame
145 WebScriptCallFrame *cframe = currentFrame;
146 for (unsigned count = 0; count < frame; count++)
147 cframe = [cframe caller];
149 if (![[cframe scopeChain] count])
152 unsigned scopeCount = [[cframe scopeChain] count];
153 for (unsigned i = 0; i < scopeCount; i++) {
154 WebScriptObject *scope = [[cframe scopeChain] objectAtIndex:i];
155 id value = [scope valueForKey:key];
156 if ([value isKindOfClass:NSClassFromString(@"WebScriptObject")])
157 return [value callWebScriptMethod:@"toString" withArguments:nil];
158 if (value && ![value isKindOfClass:[NSString class]])
159 return [NSString stringWithFormat:@"%@", value];
168 #pragma mark System Information
170 - (int)doubleClickMilliseconds
172 // GetDblTime() returns values in 1/60ths of a second
173 return ((double)GetDblTime() / 60.0) * 1000;
177 #pragma mark File Loading
179 - (NSString *)breakpointEditorHTML
181 return [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"breakpointEditor" ofType:@"html"]];
185 #pragma mark Pause & Step
195 if ([[(NSDistantObject *)server connectionForProxy] isValid])
197 [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
203 if ([[(NSDistantObject *)server connectionForProxy] isValid])
209 if ([[(NSDistantObject *)server connectionForProxy] isValid])
213 - (void)log:(NSString *)msg
219 #pragma mark Interface Actions
221 - (IBAction)pause:(id)sender
223 [[webView windowScriptObject] callWebScriptMethod:@"pause" withArguments:nil];
226 - (IBAction)resume:(id)sender
228 [[webView windowScriptObject] callWebScriptMethod:@"resume" withArguments:nil];
231 - (IBAction)stepInto:(id)sender
233 [[webView windowScriptObject] callWebScriptMethod:@"stepInto" withArguments:nil];
236 - (IBAction)stepOver:(id)sender
238 [[webView windowScriptObject] callWebScriptMethod:@"stepOver" withArguments:nil];
241 - (IBAction)stepOut:(id)sender
243 [[webView windowScriptObject] callWebScriptMethod:@"stepOut" withArguments:nil];
246 - (IBAction)showConsole:(id)sender
248 [[webView windowScriptObject] callWebScriptMethod:@"showConsoleWindow" withArguments:nil];
252 #pragma mark Window Controller Overrides
254 - (NSString *)windowNibName
259 - (void)windowDidLoad
261 [super windowDidLoad];
263 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationTerminating:) name:NSApplicationWillTerminateNotification object:nil];
265 NSString *path = [[NSBundle mainBundle] pathForResource:@"debugger" ofType:@"html" inDirectory:nil];
266 [[webView mainFrame] loadRequest:[[[NSURLRequest alloc] initWithURL:[NSURL fileURLWithPath:path]] autorelease]];
268 NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"debugger"];
269 [toolbar setDelegate:self];
270 [toolbar setAllowsUserCustomization:YES];
271 [toolbar setAutosavesConfiguration:YES];
272 [[self window] setToolbar:toolbar];
276 - (void)windowWillClose:(NSNotification *)notification
278 [[webView windowScriptObject] removeWebScriptKey:@"DebuggerDocument"];
280 [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil];
282 [self switchToServerNamed:nil];
284 [self autorelease]; // DebuggerApplication expects us to release on close
288 #pragma mark Connection Handling
290 - (void)switchToServerNamed:(NSString *)name
293 [[NSNotificationCenter defaultCenter] removeObserver:self name:NSConnectionDidDieNotification object:[(NSDistantObject *)server connectionForProxy]];
294 if ([[(NSDistantObject *)server connectionForProxy] isValid]) {
295 [server removeListener:self];
301 server = ([name length] ? [[NSConnection rootProxyForConnectionWithRegisteredName:name host:nil] retain] : nil);
304 old = currentServerName;
305 currentServerName = [name copy];
310 [(NSDistantObject *)server setProtocolForProxy:@protocol(WebScriptDebugServer)];
311 [server addListener:self];
312 } @catch (NSException *exception) {
313 [currentServerName release];
314 currentServerName = nil;
320 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serverConnectionDidDie:) name:NSConnectionDidDieNotification object:[(NSDistantObject *)server connectionForProxy]];
324 - (void)applicationTerminating:(NSNotification *)notifiction
326 if (server && [[(NSDistantObject *)server connectionForProxy] isValid]) {
327 [self switchToServerNamed:nil];
328 // call the runloop for a while to make sure our removeListener: is sent to the server
329 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
333 - (void)serverConnectionDidDie:(NSNotification *)notifiction
335 [self switchToServerNamed:nil];
339 #pragma mark Toolbar Delegate
341 - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
343 if ([itemIdentifier isEqualToString:DebuggerContinueToolbarItem]) {
344 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
346 [item setLabel:@"Continue"];
347 [item setPaletteLabel:@"Continue"];
349 [item setToolTip:@"Continue script execution"];
350 [item setImage:[NSImage imageNamed:@"continue"]];
352 [item setTarget:self];
353 [item setAction:@selector(resume:)];
355 return [item autorelease];
356 } else if ([itemIdentifier isEqualToString:DebuggerConsoleToolbarItem]) {
357 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
359 [item setLabel:@"Console"];
360 [item setPaletteLabel:@"Console"];
362 [item setToolTip:@"Console"];
363 [item setImage:[NSImage imageNamed:@"console"]];
365 [item setTarget:self];
366 [item setAction:@selector(showConsole:)];
368 return [item autorelease];
369 } else if ([itemIdentifier isEqualToString:DebuggerPauseToolbarItem]) {
370 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
372 [item setLabel:@"Pause"];
373 [item setPaletteLabel:@"Pause"];
375 [item setToolTip:@"Pause script execution"];
376 [item setImage:[NSImage imageNamed:@"pause"]];
378 [item setTarget:self];
379 [item setAction:@selector(pause:)];
381 return [item autorelease];
382 } else if ([itemIdentifier isEqualToString:DebuggerStepIntoToolbarItem]) {
383 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
385 [item setLabel:@"Step Into"];
386 [item setPaletteLabel:@"Step Into"];
388 [item setToolTip:@"Step into function call"];
389 [item setImage:[NSImage imageNamed:@"step"]];
391 [item setTarget:self];
392 [item setAction:@selector(stepInto:)];
394 return [item autorelease];
395 } else if ([itemIdentifier isEqualToString:DebuggerStepOverToolbarItem]) {
396 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
398 [item setLabel:@"Step Over"];
399 [item setPaletteLabel:@"Step Over"];
401 [item setToolTip:@"Step over function call"];
402 [item setImage:[NSImage imageNamed:@"stepOver"]];
404 [item setTarget:self];
405 [item setAction:@selector(stepOver:)];
407 return [item autorelease];
408 } else if ([itemIdentifier isEqualToString:DebuggerStepOutToolbarItem]) {
409 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
411 [item setLabel:@"Step Out"];
412 [item setPaletteLabel:@"Step Over"];
414 [item setToolTip:@"Step out of current function"];
415 [item setImage:[NSImage imageNamed:@"stepOut"]];
417 [item setTarget:self];
418 [item setAction:@selector(stepOut:)];
420 return [item autorelease];
426 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
428 return [NSArray arrayWithObjects:DebuggerContinueToolbarItem, DebuggerPauseToolbarItem,
429 NSToolbarSeparatorItemIdentifier, DebuggerStepIntoToolbarItem, DebuggerStepOutToolbarItem,
430 DebuggerStepOverToolbarItem, NSToolbarFlexibleSpaceItemIdentifier, DebuggerConsoleToolbarItem, nil];
433 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
435 return [NSArray arrayWithObjects:DebuggerConsoleToolbarItem, DebuggerContinueToolbarItem, DebuggerPauseToolbarItem,
436 DebuggerStepIntoToolbarItem, DebuggerStepOutToolbarItem, DebuggerStepOverToolbarItem, NSToolbarCustomizeToolbarItemIdentifier,
437 NSToolbarFlexibleSpaceItemIdentifier, NSToolbarSpaceItemIdentifier, NSToolbarSeparatorItemIdentifier, nil];
440 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)interfaceItem
442 SEL action = [interfaceItem action];
443 if (action == @selector(pause:))
444 return ![self isPaused];
445 if (action == @selector(resume:) ||
446 action == @selector(stepOver:) ||
447 action == @selector(stepOut:) ||
448 action == @selector(stepInto:))
449 return [self isPaused];
454 #pragma mark WebView UI Delegate
456 - (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
458 WebView *newWebView = [[WebView alloc] initWithFrame:NSZeroRect frameName:nil groupName:nil];
459 [newWebView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
460 [newWebView setUIDelegate:self];
461 [newWebView setPolicyDelegate:self];
462 [newWebView setFrameLoadDelegate:self];
464 [[newWebView mainFrame] loadRequest:request];
466 NSWindow *window = [[NSWindow alloc] initWithContentRect:NSZeroRect styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask | NSUnifiedTitleAndToolbarWindowMask) backing:NSBackingStoreBuffered defer:NO screen:[[webView window] screen]];
467 [window setReleasedWhenClosed:YES];
468 [newWebView setFrame:[[window contentView] frame]];
469 [[window contentView] addSubview:newWebView];
470 [newWebView release];
475 - (void)webViewShow:(WebView *)sender
477 [[sender window] makeKeyAndOrderFront:sender];
480 - (BOOL)webViewAreToolbarsVisible:(WebView *)sender
482 return [[[sender window] toolbar] isVisible];
485 - (void)webView:(WebView *)sender setToolbarsVisible:(BOOL)visible
487 [[[sender window] toolbar] setVisible:visible];
490 - (void)webView:(WebView *)sender setResizable:(BOOL)resizable
492 [[sender window] setShowsResizeIndicator:resizable];
493 [[[sender window] standardWindowButton:NSWindowZoomButton] setEnabled:resizable];
496 - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
498 NSRange range = [message rangeOfString:@"\t"];
499 NSString *title = @"Alert";
500 if (range.location != NSNotFound) {
501 title = [message substringToIndex:range.location];
502 message = [message substringFromIndex:(range.location + range.length)];
505 NSBeginInformationalAlertSheet(title, nil, nil, nil, [sender window], nil, NULL, NULL, NULL, message);
508 - (void)scriptConfirmSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(int *)contextInfo
510 *contextInfo = returnCode;
513 - (BOOL)webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
515 NSRange range = [message rangeOfString:@"\t"];
516 NSString *title = @"Alert";
517 if (range.location != NSNotFound) {
518 title = [message substringToIndex:range.location];
519 message = [message substringFromIndex:(range.location + range.length)];
522 int result = NSNotFound;
523 NSBeginInformationalAlertSheet(title, nil, @"Cancel", nil, [sender window], self, @selector(scriptConfirmSheetDidEnd:returnCode:contextInfo:), NULL, &result, message);
525 while (result == NSNotFound) {
526 NSEvent *nextEvent = [[NSApplication sharedApplication] nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:YES];
527 [[NSApplication sharedApplication] sendEvent:nextEvent];
534 #pragma mark WebView Frame Load Delegate
536 - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)windowScriptObject
538 // note: this is the Debuggers's own WebView, not the one being debugged
539 if ([[sender window] isEqual:[self window]])
540 [[sender windowScriptObject] setValue:self forKey:@"DebuggerDocument"];
543 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
545 // note: this is the Debuggers's own WebView, not the one being debugged
546 if ([[sender window] isEqual:[self window]])
550 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
552 // note: this is the Debuggers's own WebViews, not the one being debugged
553 if ([frame isEqual:[sender mainFrame]]) {
554 NSDictionary *info = [[(DebuggerApplication *)[[NSApplication sharedApplication] delegate] knownServers] objectForKey:currentServerName];
555 NSString *processName = [info objectForKey:WebScriptDebugServerProcessNameKey];
556 if (info && [processName length]) {
557 NSMutableString *newTitle = [[NSMutableString alloc] initWithString:processName];
558 [newTitle appendString:@" - "];
559 [newTitle appendString:title];
560 [[sender window] setTitle:newTitle];
563 [[sender window] setTitle:title];
568 #pragma mark Debug Listener Callbacks
570 - (void)webView:(WebView *)view didLoadMainResourceForDataSource:(WebDataSource *)dataSource
572 NSString *documentSourceCopy = nil;
573 id <WebDocumentRepresentation> rep = [dataSource representation];
574 if ([rep canProvideDocumentSource])
575 documentSourceCopy = [[rep documentSource] copy];
577 if (!documentSourceCopy)
580 NSString *urlCopy = [[[[dataSource response] URL] absoluteString] copy];
581 NSArray *args = [[NSArray alloc] initWithObjects:(documentSourceCopy ? documentSourceCopy : @""), (urlCopy ? urlCopy : @""), [NSNumber numberWithBool:NO], nil];
582 [[webView windowScriptObject] callWebScriptMethod:@"updateFileSource" withArguments:args];
585 [documentSourceCopy release];
589 - (void)webView:(WebView *)view didParseSource:(NSString *)source baseLineNumber:(unsigned)baseLine fromURL:(NSURL *)url sourceId:(int)sid forWebFrame:(WebFrame *)webFrame
594 NSString *sourceCopy = [source copy];
598 NSString *documentSourceCopy = nil;
599 NSString *urlCopy = [[url absoluteString] copy];
601 WebDataSource *dataSource = [webFrame dataSource];
602 if (!url || [[[dataSource response] URL] isEqual:url]) {
603 id <WebDocumentRepresentation> rep = [dataSource representation];
604 if ([rep canProvideDocumentSource])
605 documentSourceCopy = [[rep documentSource] copy];
607 urlCopy = [[[[dataSource response] URL] absoluteString] copy];
610 NSArray *args = [[NSArray alloc] initWithObjects:sourceCopy, (documentSourceCopy ? documentSourceCopy : @""), (urlCopy ? urlCopy : @""), [NSNumber numberWithInt:sid], [NSNumber numberWithUnsignedInt:baseLine], nil];
611 [[webView windowScriptObject] callWebScriptMethod:@"didParseScript" withArguments:args];
614 [sourceCopy release];
615 [documentSourceCopy release];
619 - (void)webView:(WebView *)view failedToParseSource:(NSString *)source baseLineNumber:(unsigned)baseLine fromURL:(NSURL *)url withError:(NSError *)error forWebFrame:(WebFrame *)webFrame
623 - (void)webView:(WebView *)view didEnterCallFrame:(WebScriptCallFrame *)frame sourceId:(int)sid line:(int)lineno forWebFrame:(WebFrame *)webFrame
628 id old = currentFrame;
629 currentFrame = [frame retain];
632 NSArray *args = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:sid], [NSNumber numberWithInt:lineno], nil];
633 [[webView windowScriptObject] callWebScriptMethod:@"didEnterCallFrame" withArguments:args];
637 - (void)webView:(WebView *)view willExecuteStatement:(WebScriptCallFrame *)frame sourceId:(int)sid line:(int)lineno forWebFrame:(WebFrame *)webFrame
642 NSArray *args = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:sid], [NSNumber numberWithInt:lineno], nil];
643 [[webView windowScriptObject] callWebScriptMethod:@"willExecuteStatement" withArguments:args];
647 - (void)webView:(WebView *)view willLeaveCallFrame:(WebScriptCallFrame *)frame sourceId:(int)sid line:(int)lineno forWebFrame:(WebFrame *)webFrame
652 NSArray *args = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:sid], [NSNumber numberWithInt:lineno], nil];
653 [[webView windowScriptObject] callWebScriptMethod:@"willLeaveCallFrame" withArguments:args];
656 id old = currentFrame;
657 currentFrame = [[frame caller] retain];
661 - (void)webView:(WebView *)view exceptionWasRaised:(WebScriptCallFrame *)frame sourceId:(int)sid line:(int)lineno forWebFrame:(WebFrame *)webFrame
666 NSArray *args = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:sid], [NSNumber numberWithInt:lineno], nil];
667 [[webView windowScriptObject] callWebScriptMethod:@"exceptionWasRaised" withArguments:args];