Web Inspector: Embeddable Web Inspector
[WebKit-https.git] / Source / WebCore / inspector / front-end / InspectorFrontendAPI.js
1 /*
2  * Copyright (C) 2011 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 InspectorFrontendAPI = {
32     _pendingCommands: [],
33
34     isDebuggingEnabled: function()
35     {
36         return WebInspector.debuggerModel.debuggerEnabled();
37     },
38
39     setDebuggingEnabled: function(enabled)
40     {
41         if (enabled) {
42             WebInspector.debuggerModel.enableDebugger();
43             WebInspector.showPanel("scripts");
44         } else
45             WebInspector.debuggerModel.disableDebugger();
46     },
47
48     isTimelineProfilingEnabled: function()
49     {
50         return WebInspector.panels.timeline && WebInspector.panels.timeline.timelineProfilingEnabled;
51     },
52
53     setTimelineProfilingEnabled: function(enabled)
54     {
55         WebInspector.showPanel("timeline").setTimelineProfilingEnabled(enabled);
56     },
57
58     isProfilingJavaScript: function()
59     {
60         return WebInspector.panels.profiles && WebInspector.CPUProfileType.instance && WebInspector.CPUProfileType.instance.isRecordingProfile();
61     },
62
63     startProfilingJavaScript: function()
64     {
65         WebInspector.showPanel("profiles").enableProfiler();
66         if (WebInspector.CPUProfileType.instance)
67             WebInspector.CPUProfileType.instance.startRecordingProfile();
68     },
69
70     stopProfilingJavaScript: function()
71     {
72         WebInspector.showPanel("profiles");
73         if (WebInspector.CPUProfileType.instance)
74             WebInspector.CPUProfileType.instance.stopRecordingProfile();
75     },
76
77     setAttachedWindow: function(attached)
78     {
79         WebInspector.attached = attached;
80     },
81
82     showConsole: function()
83     {
84         WebInspector.showPanel("console");
85     },
86
87     showMainResourceForFrame: function(frameId)
88     {
89         // FIXME: Implement this to show the source code for the main resource of a given frame.
90     },
91
92     showResources: function()
93     {
94         WebInspector.showPanel("resources");
95     },
96
97     setDockingUnavailable: function(unavailable)
98     {
99         WebInspector.setDockingUnavailable(unavailable);
100     },
101
102     enterInspectElementMode: function()
103     {
104         WebInspector.panel("elements").toggleSearchingForNode();
105     },
106
107     savedURL: function(url)
108     {
109         WebInspector.fileManager.savedURL(url);
110     },
111
112     appendedToURL: function(url)
113     {
114         WebInspector.fileManager.appendedToURL(url);
115     },
116
117     setToolbarColors: function(backgroundColor, color)
118     {
119         WebInspector.setToolbarColors(backgroundColor, color);
120     },
121
122     evaluateForTest: function(callId, script)
123     {
124         WebInspector.evaluateForTestInFrontend(callId, script);
125     },
126
127     dispatch: function(signature)
128     {
129         if (InspectorFrontendAPI._isLoaded) {
130             var methodName = signature.shift();
131             return InspectorFrontendAPI[methodName].apply(InspectorFrontendAPI, signature);
132         }
133         InspectorFrontendAPI._pendingCommands.push(signature);
134     },
135
136     dispatchQueryParameters: function()
137     {
138         if ("dispatch" in WebInspector.queryParamsObject)
139             InspectorFrontendAPI.dispatch(JSON.parse(window.decodeURI(WebInspector.queryParamsObject["dispatch"])));
140     },
141
142     /**
143      * @param {string} url
144      */
145     loadTimelineFromURL: function(url) 
146     {
147         WebInspector.showPanel("timeline").loadFromURL(url);
148     },
149
150     loadCompleted: function()
151     {
152         InspectorFrontendAPI._isLoaded = true;
153         for (var i = 0; i < InspectorFrontendAPI._pendingCommands.length; ++i)
154             InspectorFrontendAPI.dispatch(InspectorFrontendAPI._pendingCommands[i]);
155         InspectorFrontendAPI._pendingCommands = [];
156         if (window.opener)
157             window.opener.postMessage(["loadCompleted"], "*");
158     }
159 }
160
161 if (window.opener) {
162     function onMessageFromOpener(event)
163     {
164         if (event.source === window.opener)
165             InspectorFrontendAPI.dispatch(event.data);
166     }
167     window.addEventListener("message", onMessageFromOpener, true);
168 }