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"
31 static NSString *DebuggerContinueToolbarItem = @"DebuggerContinueToolbarItem";
32 static NSString *DebuggerPauseToolbarItem = @"DebuggerPauseToolbarItem";
33 static NSString *DebuggerStepIntoToolbarItem = @"DebuggerStepIntoToolbarItem";
35 @implementation DebuggerDocument
36 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
41 + (BOOL)isKeyExcludedFromWebScript:(const char *)name
48 - (id)initWithServerName:(NSString *)serverName
50 if ((self = [super init]))
51 [self switchToServerNamed:serverName];
58 [currentServerName release];
63 #pragma mark Stack & Variables
65 - (WebScriptCallFrame *)currentFrame
70 - (NSString *)currentFrameFunctionName
72 return [currentFrame functionName];
75 - (NSArray *)currentFunctionStack
77 NSMutableArray *result = [[NSMutableArray alloc] init];
78 WebScriptCallFrame *frame = currentFrame;
80 if ([frame functionName])
81 [result addObject:[frame functionName]];
82 frame = [frame caller];
84 return [result autorelease];
88 #pragma mark Pause & Step
98 if ([[(NSDistantObject *)server connectionForProxy] isValid])
100 [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
106 if ([[(NSDistantObject *)server connectionForProxy] isValid])
112 if ([[(NSDistantObject *)server connectionForProxy] isValid])
116 - (void)log:(NSString *)msg
122 #pragma mark Interface Actions
124 - (IBAction)pause:(id)sender
126 [[webView windowScriptObject] callWebScriptMethod:@"pause" withArguments:nil];
129 - (IBAction)resume:(id)sender
131 [[webView windowScriptObject] callWebScriptMethod:@"resume" withArguments:nil];
134 - (IBAction)stepInto:(id)sender
136 [[webView windowScriptObject] callWebScriptMethod:@"stepInto" withArguments:nil];
140 #pragma mark Window Controller Overrides
142 - (NSString *)windowNibName
147 - (void)windowDidLoad
149 [super windowDidLoad];
151 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationTerminating:) name:NSApplicationWillTerminateNotification object:nil];
153 NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"debugger" ofType:@"html" inDirectory:nil];
154 [[webView mainFrame] loadRequest:[[[NSURLRequest alloc] initWithURL:[NSURL fileURLWithPath:path]] autorelease]];
156 NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"debugger"];
157 [toolbar setDelegate:self];
158 [toolbar setAllowsUserCustomization:YES];
159 [toolbar setAutosavesConfiguration:YES];
160 [[self window] setToolbar:toolbar];
164 - (void)windowWillClose:(NSNotification *)notification
166 [[webView windowScriptObject] removeWebScriptKey:@"DebuggerDocument"];
168 [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil];
169 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:WebScriptDebugServerQueryReplyNotification object:nil];
170 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:WebScriptDebugServerWillUnloadNotification object:nil];
172 [self switchToServerNamed:nil];
174 [self autorelease]; // DebuggerApplication expects us to release on close
178 #pragma mark Connection Handling
180 - (void)switchToServerNamed:(NSString *)name
183 [[NSNotificationCenter defaultCenter] removeObserver:self name:NSConnectionDidDieNotification object:[(NSDistantObject *)server connectionForProxy]];
184 if ([[(NSDistantObject *)server connectionForProxy] isValid]) {
186 [server removeListener:self];
191 server = ([name length] ? [[NSConnection rootProxyForConnectionWithRegisteredName:name host:nil] retain] : nil);
194 old = currentServerName;
195 currentServerName = [name copy];
200 [(NSDistantObject *)server setProtocolForProxy:@protocol(WebScriptDebugServer)];
201 [server addListener:self];
202 } @catch (NSException *exception) {
203 [currentServerName release];
204 currentServerName = nil;
210 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serverConnectionDidDie:) name:NSConnectionDidDieNotification object:[(NSDistantObject *)server connectionForProxy]];
214 - (void)applicationTerminating:(NSNotification *)notifiction
216 if (server && [[(NSDistantObject *)server connectionForProxy] isValid]) {
217 [self switchToServerNamed:nil];
218 // call the runloop for a while to make sure our removeListener: is sent to the server
219 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
223 - (void)serverConnectionDidDie:(NSNotification *)notifiction
225 [self switchToServerNamed:nil];
229 #pragma mark Toolbar Delegate
231 - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
233 if ([itemIdentifier isEqualToString:DebuggerContinueToolbarItem]) {
234 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
236 [item setLabel:@"Continue"];
237 [item setPaletteLabel:@"Continue"];
239 [item setToolTip:@"Continue script execution"];
240 [item setImage:[NSImage imageNamed:@"continue"]];
242 [item setTarget:self];
243 [item setAction:@selector(resume:)];
245 return [item autorelease];
246 } else if ([itemIdentifier isEqualToString:DebuggerPauseToolbarItem]) {
247 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
249 [item setLabel:@"Pause"];
250 [item setPaletteLabel:@"Pause"];
252 [item setToolTip:@"Pause script execution"];
253 [item setImage:[NSImage imageNamed:@"pause"]];
255 [item setTarget:self];
256 [item setAction:@selector(pause:)];
258 return [item autorelease];
259 } else if ([itemIdentifier isEqualToString:DebuggerStepIntoToolbarItem]) {
260 NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
262 [item setLabel:@"Step Into"];
263 [item setPaletteLabel:@"Step Into"];
265 [item setToolTip:@"Step into function call"];
266 [item setImage:[NSImage imageNamed:@"step"]];
268 [item setTarget:self];
269 [item setAction:@selector(stepInto:)];
271 return [item autorelease];
277 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
279 return [NSArray arrayWithObjects:DebuggerContinueToolbarItem, DebuggerPauseToolbarItem,
280 NSToolbarSeparatorItemIdentifier, DebuggerStepIntoToolbarItem, nil];
283 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
285 return [NSArray arrayWithObjects:DebuggerContinueToolbarItem, DebuggerPauseToolbarItem, DebuggerStepIntoToolbarItem,
286 NSToolbarCustomizeToolbarItemIdentifier, NSToolbarFlexibleSpaceItemIdentifier,
287 NSToolbarSpaceItemIdentifier, NSToolbarSeparatorItemIdentifier, nil];
290 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)interfaceItem
292 if ([interfaceItem action] == @selector(pause:))
293 return ![self isPaused];
294 if ([interfaceItem action] == @selector(resume:))
295 return [self isPaused];
296 if ([interfaceItem action] == @selector(stepInto:))
297 return [self isPaused];
302 #pragma mark WebView Frame Load Delegate
304 - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)windowScriptObject
306 // note: this is the Debuggers's own WebView, not the one being debugged
307 [[sender windowScriptObject] setValue:self forKey:@"DebuggerDocument"];
310 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
312 // note: this is the Debuggers's own WebView, not the one being debugged
317 #pragma mark Debug Listener Callbacks
319 - (void)webView:(WebView *)view didLoadMainResourceForDataSource:(WebDataSource *)dataSource
321 NSString *documentSourceCopy = nil;
322 id <WebDocumentRepresentation> rep = [dataSource representation];
323 if ([rep canProvideDocumentSource])
324 documentSourceCopy = [[rep documentSource] copy];
326 if (!documentSourceCopy)
329 NSString *urlCopy = [[[[dataSource response] URL] absoluteString] copy];
330 NSArray *args = [NSArray arrayWithObjects:(documentSourceCopy ? documentSourceCopy : @""), (urlCopy ? urlCopy : @""), [NSNumber numberWithBool:NO], nil];
331 [[webView windowScriptObject] callWebScriptMethod:@"updateFileSource" withArguments:args];
333 [documentSourceCopy release];
337 - (void)webView:(WebView *)view didParseSource:(NSString *)source baseLineNumber:(unsigned)baseLine fromURL:(NSURL *)url sourceId:(int)sid forWebFrame:(WebFrame *)webFrame
342 NSString *sourceCopy = [source copy];
346 NSString *documentSourceCopy = nil;
347 NSString *urlCopy = [[url absoluteString] copy];
349 WebDataSource *dataSource = [webFrame dataSource];
350 if (!url || [[[dataSource response] URL] isEqual:url]) {
351 id <WebDocumentRepresentation> rep = [dataSource representation];
352 if ([rep canProvideDocumentSource])
353 documentSourceCopy = [[rep documentSource] copy];
355 urlCopy = [[[[dataSource response] URL] absoluteString] copy];
358 NSArray *args = [NSArray arrayWithObjects:sourceCopy, (documentSourceCopy ? documentSourceCopy : @""), (urlCopy ? urlCopy : @""), [NSNumber numberWithInt:sid], [NSNumber numberWithUnsignedInt:baseLine], nil];
359 [[webView windowScriptObject] callWebScriptMethod:@"didParseScript" withArguments:args];
361 [sourceCopy release];
362 [documentSourceCopy release];
366 - (void)webView:(WebView *)view failedToParseSource:(NSString *)source baseLineNumber:(unsigned)baseLine fromURL:(NSURL *)url withError:(NSError *)error forWebFrame:(WebFrame *)webFrame
370 - (void)webView:(WebView *)view didEnterCallFrame:(WebScriptCallFrame *)frame sourceId:(int)sid line:(int)lineno forWebFrame:(WebFrame *)webFrame
375 id old = currentFrame;
376 currentFrame = [frame retain];
379 NSArray *args = [NSArray arrayWithObjects:[NSNumber numberWithInt:sid], [NSNumber numberWithInt:lineno], nil];
380 [[webView windowScriptObject] callWebScriptMethod:@"didEnterCallFrame" withArguments:args];
383 - (void)webView:(WebView *)view willExecuteStatement:(WebScriptCallFrame *)frame sourceId:(int)sid line:(int)lineno forWebFrame:(WebFrame *)webFrame
388 NSArray *args = [NSArray arrayWithObjects:[NSNumber numberWithInt:sid], [NSNumber numberWithInt:lineno], nil];
389 [[webView windowScriptObject] callWebScriptMethod:@"willExecuteStatement" withArguments:args];
392 - (void)webView:(WebView *)view willLeaveCallFrame:(WebScriptCallFrame *)frame sourceId:(int)sid line:(int)lineno forWebFrame:(WebFrame *)webFrame
397 NSArray *args = [NSArray arrayWithObjects:[NSNumber numberWithInt:sid], [NSNumber numberWithInt:lineno], nil];
398 [[webView windowScriptObject] callWebScriptMethod:@"willLeaveCallFrame" withArguments:args];
400 id old = currentFrame;
401 currentFrame = [[frame caller] retain];