1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "ios/web/web_state/ui/crw_debug_web_view.h"
7 #include "base/mac/scoped_nsobject.h"
9 // These categories define private API in iOS, in reality part of WebKit.
10 @interface NSObject (CRWDebugWebView_WebScriptCallFrame_methods)
16 @interface NSObject (CRWDebugWebView_WebView_methods)
17 - (void)setScriptDebugDelegate:
18 (id<CRWDebugWebView_WebViewScriptDelegate>)delegate;
21 @interface NSObject (CRWDebugWebView_WebScriptObject_methods)
22 - (id)callWebScriptMethod:(NSString*)name withArguments:(NSArray*)args;
27 // The debug implementation of the webscript delegate.
28 @interface CRWDebugWebDelegate :
29 NSObject<CRWDebugWebView_WebViewScriptDelegate> {
30 base::scoped_nsobject<NSMutableDictionary> _sidsToURLs;
34 @implementation CRWDebugWebDelegate
37 if ((self = [super init])) {
38 _sidsToURLs.reset([[NSMutableDictionary alloc] init]);
43 - (void)webView:(WebView*)webView addMessageToConsole:(NSDictionary*)dict {
44 NSLog(@"JS: %@", dict);
47 - (void)webView:(WebView*)webView
48 didParseSource:(NSString*)source
49 baseLineNumber:(NSUInteger)lineNumber
52 forWebFrame:(WebFrame*)webFrame {
54 [_sidsToURLs setObject:url forKey:[NSNumber numberWithInt:sid]];
57 - (void)webView:(WebView*)webView
58 failedToParseSource:(NSString*)source
59 baseLineNumber:(unsigned)lineNumber
61 withError:(NSError*)error
62 forWebFrame:(WebFrame*)webFrame {
63 NSLog(@"JS Failed to parse: url=%@ line=%d error=%@\nsource=%@",
64 url, lineNumber, error, source);
67 - (void)webView:(WebView*)webView
68 exceptionWasRaised:(WebScriptCallFrame*)frame
71 forWebFrame:(WebFrame*)webFrame {
72 NSURL* url = [_sidsToURLs objectForKey:[NSNumber numberWithInt:sid]];
73 id representation = [frame exception];
74 if ([representation isKindOfClass:NSClassFromString(@"WebScriptObject")]) {
76 [representation callWebScriptMethod:@"toString" withArguments:nil];
78 base::scoped_nsobject<NSMutableArray> message([[NSMutableArray alloc] init]);
81 [NSString stringWithFormat:@"JS Exception: sid=%d url=%@ exception=%@",
82 sid, url, representation]];
84 if ([frame respondsToSelector:@selector(caller)]) {
86 frame = [frame caller];
87 [message addObject:[NSString stringWithFormat:@"line=%d function=%@",
88 lineno, [frame functionName]]];
92 NSLog(@"%@", [message componentsJoinedByString:@"\n\t"]);
99 @implementation CRWDebugWebView {
100 base::scoped_nsobject<CRWDebugWebDelegate> _webDelegate;
103 - (void)webView:(id)sender
104 didClearWindowObject:(id)windowObject
105 forFrame:(WebFrame*)frame {
107 _webDelegate.reset([[CRWDebugWebDelegate alloc] init]);
108 [sender setScriptDebugDelegate:_webDelegate];
113 #endif // !defined(NDEBUG)