1 /* 2 * Copyright (C) 2007 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 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. 16 * 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. 27 */ 28 29 #import "WebInspector.h" 30 31 #import "WebFrameInternal.h" 32 #import "WebInspectorPrivate.h" 33 34 #include <WebCore/Document.h> 35 #include <WebCore/Frame.h> 36 #include <WebCore/InspectorController.h> 37 #include <WebCore/Page.h> 38 39 using namespace WebCore; 40 41 @implementation WebInspector 42 - (id)initWithWebView:(WebView *)webView 43 { 44 if (!(self = [super init])) 45 return nil; 46 _webView = webView; // not retained to prevent a cycle 47 return self; 48 } 49 50 - (void)webViewClosed 51 { 52 _webView = nil; 53 } 54 55 - (void)show:(id)sender 56 { 57 if (Page* page = core(_webView)) 58 page->inspectorController()->show(); 59 } 60 61 - (void)showConsole:(id)sender 62 { 63 if (Page* page = core(_webView)) 64 page->inspectorController()->showPanel(InspectorController::ConsolePanel); 65 } 66 67 - (void)showTimeline:(id)sender 68 { 69 // Not used anymore. Remove when a release of Safari non-longer calls this. 70 } 71 72 - (BOOL)isDebuggingJavaScript 73 { 74 if (Page* page = core(_webView)) 75 return page->inspectorController()->debuggerEnabled(); 76 return NO; 77 } 78 79 - (void)toggleDebuggingJavaScript:(id)sender 80 { 81 if ([self isDebuggingJavaScript]) 82 [self stopDebuggingJavaScript:sender]; 83 else 84 [self startDebuggingJavaScript:sender]; 85 } 86 87 - (void)startDebuggingJavaScript:(id)sender 88 { 89 Page* page = core(_webView); 90 if (!page) 91 return; 92 page->inspectorController()->showPanel(InspectorController::ScriptsPanel); 93 page->inspectorController()->enableDebugger(); 94 } 95 96 - (void)stopDebuggingJavaScript:(id)sender 97 { 98 if (Page* page = core(_webView)) 99 page->inspectorController()->disableDebugger(); 100 } 101 102 - (BOOL)isProfilingJavaScript 103 { 104 if (Page* page = core(_webView)) 105 return page->inspectorController()->isRecordingUserInitiatedProfile(); 106 return NO; 107 } 108 109 - (void)toggleProfilingJavaScript:(id)sender 110 { 111 if ([self isProfilingJavaScript]) 112 [self stopProfilingJavaScript:sender]; 113 else 114 [self startProfilingJavaScript:sender]; 115 } 116 117 - (void)startProfilingJavaScript:(id)sender 118 { 119 if (Page* page = core(_webView)) 120 page->inspectorController()->startUserInitiatedProfiling(); 121 } 122 123 - (void)stopProfilingJavaScript:(id)sender 124 { 125 Page* page = core(_webView); 126 if (!page) 127 return; 128 page->inspectorController()->stopUserInitiatedProfiling(); 129 page->inspectorController()->showPanel(InspectorController::ProfilesPanel); 130 } 131 132 - (BOOL)isJavaScriptProfilingEnabled 133 { 134 if (Page* page = core(_webView)) 135 return page->inspectorController()->profilerEnabled(); 136 return NO; 137 } 138 139 - (void)setJavaScriptProfilingEnabled:(BOOL)enabled 140 { 141 Page* page = core(_webView); 142 if (!page) 143 return; 144 145 if (enabled) 146 page->inspectorController()->enableProfiler(); 147 else 148 page->inspectorController()->disableProfiler(); 149 } 150 151 - (BOOL)isTimelineProfilingEnabled 152 { 153 if (Page* page = core(_webView)) 154 return page->inspectorController()->timelineAgent() ? YES : NO; 155 return NO; 156 } 157 158 - (void)setTimelineProfilingEnabled:(BOOL)enabled 159 { 160 Page* page = core(_webView); 161 if (!page) 162 return; 163 164 if (enabled) 165 page->inspectorController()->startTimelineProfiler(); 166 else 167 page->inspectorController()->stopTimelineProfiler(); 168 } 169 170 - (void)close:(id)sender 171 { 172 if (Page* page = core(_webView)) 173 page->inspectorController()->close(); 174 } 175 176 - (void)attach:(id)sender 177 { 178 if (Page* page = core(_webView)) 179 page->inspectorController()->attachWindow(); 180 } 181 182 - (void)detach:(id)sender 183 { 184 if (Page* page = core(_webView)) 185 page->inspectorController()->detachWindow(); 186 } 187 188 - (void)evaluateInFrontend:(id)sender callId:(long)callId script:(NSString *)script 189 { 190 if (Page* page = core(_webView)) 191 page->inspectorController()->evaluateForTestInFrontend(callId, script); 192 } 193 @end 194 195 @implementation WebInspector (Obsolete) 196 + (WebInspector *)webInspector 197 { 198 // Safari 3.0 calls this method 199 static BOOL logged = NO; 200 if (!logged) { 201 NSLog(@"+[WebInspector webInspector]: this method is obsolete."); 202 logged = YES; 203 } 204 205 return [[[WebInspector alloc] init] autorelease]; 206 } 207 208 - (void)setWebFrame:(WebFrame *)frame 209 { 210 // Safari 3.0 calls this method 211 static BOOL logged = NO; 212 if (!logged) { 213 NSLog(@"-[WebInspector setWebFrame:]: this method is obsolete."); 214 logged = YES; 215 } 216 217 _webView = [frame webView]; 218 } 219 220 - (NSWindow *)window 221 { 222 // Shiira calls this internal method, return nil since we can't easily return the window 223 static BOOL logged = NO; 224 if (!logged) { 225 NSLog(@"-[WebInspector window]: this method is obsolete and now returns nil."); 226 logged = YES; 227 } 228 229 return nil; 230 } 231 232 - (void)showWindow:(id)sender 233 { 234 // Safari 3.0 calls this method 235 static BOOL logged = NO; 236 if (!logged) { 237 NSLog(@"-[WebInspector showWindow:]: this method is obsolete."); 238 logged = YES; 239 } 240 241 [self show:sender]; 242 } 243 @end 244