1/*
2 * Copyright (C) 2012 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/**
32 * @constructor
33 * @extends {WebInspector.Panel}
34 * @param {string} id
35 * @param {string} pageURL
36 */
37WebInspector.ExtensionPanel = function(id, pageURL)
38{
39    WebInspector.Panel.call(this, id);
40    this.setHideOnDetach();
41    this._statusBarItems = [];
42    var extensionView = new WebInspector.ExtensionView(id, pageURL, "extension panel");
43    extensionView.show(this.element);
44    this.setDefaultFocusedElement(extensionView.defaultFocusedElement());
45}
46
47WebInspector.ExtensionPanel.prototype = {
48    defaultFocusedElement: function()
49    {
50        return WebInspector.View.prototype.defaultFocusedElement.call(this);
51    },
52
53    get statusBarItems()
54    {
55        return this._statusBarItems;
56    },
57
58    /**
59     * @param {Element} element
60     */
61    addStatusBarItem: function(element)
62    {
63        this._statusBarItems.push(element);
64    },
65
66    searchCanceled: function(startingNewSearch)
67    {
68        WebInspector.extensionServer.notifySearchAction(this.name, WebInspector.extensionAPI.panels.SearchAction.CancelSearch);
69        WebInspector.Panel.prototype.searchCanceled.apply(this, arguments);
70    },
71
72    /**
73     * @param {string} query
74     * @param {boolean} shouldJump
75     */
76    performSearch: function(query, shouldJump)
77    {
78        WebInspector.extensionServer.notifySearchAction(this.name, WebInspector.extensionAPI.panels.SearchAction.PerformSearch, query);
79    },
80
81    jumpToNextSearchResult: function()
82    {
83        WebInspector.extensionServer.notifySearchAction(this.name, WebInspector.extensionAPI.panels.SearchAction.NextSearchResult);
84        WebInspector.Panel.prototype.jumpToNextSearchResult.call(this);
85    },
86
87    jumpToPreviousSearchResult: function()
88    {
89        WebInspector.extensionServer.notifySearchAction(this.name, WebInspector.extensionAPI.panels.SearchAction.PreviousSearchResult);
90        WebInspector.Panel.prototype.jumpToPreviousSearchResult.call(this);
91    },
92
93    __proto__: WebInspector.Panel.prototype
94}
95
96/**
97 * @constructor
98 * @param {string} id
99 * @param {string} iconURL
100 * @param {string=} tooltip
101 * @param {boolean=} disabled
102 */
103WebInspector.ExtensionButton = function(id, iconURL, tooltip, disabled)
104{
105    this._id = id;
106    this.element = document.createElement("button");
107    this.element.className = "status-bar-item extension";
108    this.element.addEventListener("click", this._onClicked.bind(this), false);
109    this.update(iconURL, tooltip, disabled);
110}
111
112WebInspector.ExtensionButton.prototype = {
113    /**
114     * @param {string} iconURL
115     * @param {string=} tooltip
116     * @param {boolean=} disabled
117     */
118    update: function(iconURL, tooltip, disabled)
119    {
120        if (typeof iconURL === "string")
121            this.element.style.backgroundImage = "url(" + iconURL + ")";
122        if (typeof tooltip === "string")
123            this.element.title = tooltip;
124        if (typeof disabled === "boolean")
125            this.element.disabled = disabled;
126    },
127
128    _onClicked: function()
129    {
130        WebInspector.extensionServer.notifyButtonClicked(this._id);
131    }
132}
133
134/**
135 * @constructor
136 * @extends {WebInspector.SidebarPane}
137 * @param {string} title
138 * @param {string} id
139 */
140WebInspector.ExtensionSidebarPane = function(title, id)
141{
142    WebInspector.SidebarPane.call(this, title);
143    this.setHideOnDetach();
144    this._id = id;
145}
146
147WebInspector.ExtensionSidebarPane.prototype = {
148    /**
149     * @param {Object} object
150     * @param {string} title
151     * @param {function(?string=)} callback
152     */
153    setObject: function(object, title, callback)
154    {
155        this._createObjectPropertiesView();
156        this._setObject(WebInspector.RemoteObject.fromLocalObject(object), title, callback);
157    },
158
159    /**
160     * @param {string} expression
161     * @param {string} title
162     * @param {function(?string=)} callback
163     */
164    setExpression: function(expression, title, evaluateOptions, securityOrigin, callback)
165    {
166        this._createObjectPropertiesView();
167        return WebInspector.extensionServer.evaluate(expression, true, false, evaluateOptions, securityOrigin, this._onEvaluate.bind(this, title, callback));
168    },
169
170    /**
171     * @param {string} url
172     */
173    setPage: function(url)
174    {
175        if (this._objectPropertiesView) {
176            this._objectPropertiesView.detach();
177            delete this._objectPropertiesView;
178        }
179        if (this._extensionView)
180            this._extensionView.detach(true);
181
182        this._extensionView = new WebInspector.ExtensionView(this._id, url, "extension fill");
183        this._extensionView.show(this.bodyElement);
184
185        if (!this.bodyElement.style.height)
186            this.setHeight("150px");
187    },
188
189    /**
190     * @param {string} height
191     */
192    setHeight: function(height)
193    {
194        this.bodyElement.style.height = height;
195    },
196
197    /**
198     * @param {string} title
199     * @param {function(?string=)} callback
200     * @param {?Protocol.Error} error
201     * @param {RuntimeAgent.RemoteObject} result
202     * @param {boolean=} wasThrown
203     */
204    _onEvaluate: function(title, callback, error, result, wasThrown)
205    {
206        if (error)
207            callback(error.toString());
208        else
209            this._setObject(WebInspector.RemoteObject.fromPayload(result), title, callback);
210    },
211
212    _createObjectPropertiesView: function()
213    {
214        if (this._objectPropertiesView)
215            return;
216        if (this._extensionView) {
217            this._extensionView.detach(true);
218            delete this._extensionView;
219        }
220        this._objectPropertiesView = new WebInspector.ExtensionNotifierView(this._id);
221        this._objectPropertiesView.show(this.bodyElement);
222    },
223
224    /**
225     * @param {WebInspector.RemoteObject} object
226     * @param {string} title
227     * @param {function(?string=)} callback
228     */
229    _setObject: function(object, title, callback)
230    {
231        // This may only happen if setPage() was called while we were evaluating the expression.
232        if (!this._objectPropertiesView) {
233            callback("operation cancelled");
234            return;
235        }
236        this._objectPropertiesView.element.removeChildren();
237        var section = new WebInspector.ObjectPropertiesSection(object, title);
238        if (!title)
239            section.headerElement.addStyleClass("hidden");
240        section.expanded = true;
241        section.editable = false;
242        this._objectPropertiesView.element.appendChild(section.element);
243        callback();
244    },
245
246    __proto__: WebInspector.SidebarPane.prototype
247}
248