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