| OLD | NEW |
| (Empty) | |
| 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. |
| 4 #if !defined(NDEBUG) |
| 5 #import "ios/web/web_state/ui/crw_debug_web_view.h" |
| 6 |
| 7 #include "base/mac/scoped_nsobject.h" |
| 8 |
| 9 // These categories define private API in iOS, in reality part of WebKit. |
| 10 @interface NSObject (CRWDebugWebView_WebScriptCallFrame_methods) |
| 11 - (id)functionName; |
| 12 - (id)caller; |
| 13 - (id)exception; |
| 14 @end |
| 15 |
| 16 @interface NSObject (CRWDebugWebView_WebView_methods) |
| 17 - (void)setScriptDebugDelegate: |
| 18 (id<CRWDebugWebView_WebViewScriptDelegate>)delegate; |
| 19 @end |
| 20 |
| 21 @interface NSObject (CRWDebugWebView_WebScriptObject_methods) |
| 22 - (id)callWebScriptMethod:(NSString*)name withArguments:(NSArray*)args; |
| 23 @end |
| 24 |
| 25 #pragma mark - |
| 26 |
| 27 // The debug implementation of the webscript delegate. |
| 28 @interface CRWDebugWebDelegate : |
| 29 NSObject<CRWDebugWebView_WebViewScriptDelegate> { |
| 30 base::scoped_nsobject<NSMutableDictionary> _sidsToURLs; |
| 31 } |
| 32 @end |
| 33 |
| 34 @implementation CRWDebugWebDelegate |
| 35 |
| 36 - (id)init { |
| 37 if ((self = [super init])) { |
| 38 _sidsToURLs.reset([[NSMutableDictionary alloc] init]); |
| 39 } |
| 40 return self; |
| 41 } |
| 42 |
| 43 - (void)webView:(WebView*)webView addMessageToConsole:(NSDictionary*)dict { |
| 44 NSLog(@"JS: %@", dict); |
| 45 } |
| 46 |
| 47 - (void)webView:(WebView*)webView |
| 48 didParseSource:(NSString*)source |
| 49 baseLineNumber:(NSUInteger)lineNumber |
| 50 fromURL:(NSURL*)url |
| 51 sourceId:(int)sid |
| 52 forWebFrame:(WebFrame*)webFrame { |
| 53 if (url && sid) |
| 54 [_sidsToURLs setObject:url forKey:[NSNumber numberWithInt:sid]]; |
| 55 } |
| 56 |
| 57 - (void)webView:(WebView*)webView |
| 58 failedToParseSource:(NSString*)source |
| 59 baseLineNumber:(unsigned)lineNumber |
| 60 fromURL:(NSURL*)url |
| 61 withError:(NSError*)error |
| 62 forWebFrame:(WebFrame*)webFrame { |
| 63 NSLog(@"JS Failed to parse: url=%@ line=%d error=%@\nsource=%@", |
| 64 url, lineNumber, error, source); |
| 65 } |
| 66 |
| 67 - (void)webView:(WebView*)webView |
| 68 exceptionWasRaised:(WebScriptCallFrame*)frame |
| 69 sourceId:(int)sid |
| 70 line:(int)lineno |
| 71 forWebFrame:(WebFrame*)webFrame { |
| 72 NSURL* url = [_sidsToURLs objectForKey:[NSNumber numberWithInt:sid]]; |
| 73 id representation = [frame exception]; |
| 74 if ([representation isKindOfClass:NSClassFromString(@"WebScriptObject")]) { |
| 75 representation = |
| 76 [representation callWebScriptMethod:@"toString" withArguments:nil]; |
| 77 } |
| 78 base::scoped_nsobject<NSMutableArray> message([[NSMutableArray alloc] init]); |
| 79 |
| 80 [message addObject: |
| 81 [NSString stringWithFormat:@"JS Exception: sid=%d url=%@ exception=%@", |
| 82 sid, url, representation]]; |
| 83 |
| 84 if ([frame respondsToSelector:@selector(caller)]) { |
| 85 while (frame) { |
| 86 frame = [frame caller]; |
| 87 [message addObject:[NSString stringWithFormat:@"line=%d function=%@", |
| 88 lineno, [frame functionName]]]; |
| 89 } |
| 90 } |
| 91 |
| 92 NSLog(@"%@", [message componentsJoinedByString:@"\n\t"]); |
| 93 } |
| 94 |
| 95 @end |
| 96 |
| 97 #pragma mark - |
| 98 |
| 99 @implementation CRWDebugWebView { |
| 100 base::scoped_nsobject<CRWDebugWebDelegate> _webDelegate; |
| 101 } |
| 102 |
| 103 - (void)webView:(id)sender |
| 104 didClearWindowObject:(id)windowObject |
| 105 forFrame:(WebFrame*)frame { |
| 106 if (!_webDelegate) |
| 107 _webDelegate.reset([[CRWDebugWebDelegate alloc] init]); |
| 108 [sender setScriptDebugDelegate:_webDelegate]; |
| 109 } |
| 110 |
| 111 @end |
| 112 |
| 113 #endif // !defined(NDEBUG) |
| OLD | NEW |