2010-11-22 Ilya Tikhonovsky <loislo@chromium.org>
[WebKit-https.git] / WebCore / inspector / front-end / WorkersSidebarPane.js
1 /*
2  * Copyright (C) 2010 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 WebInspector.WorkersSidebarPane = function()
32 {
33     WebInspector.SidebarPane.call(this, WebInspector.UIString("Workers"));
34     
35     this._workers = {};
36
37     this._enableWorkersCheckbox = new WebInspector.Checkbox(
38         WebInspector.UIString("Debug"),
39         "sidebar-pane-subtitle",
40         WebInspector.UIString("Allow debugging workers. Enabling this option will replace native workers with the iframe-based JavaScript implementation"));
41     this.titleElement.insertBefore(this._enableWorkersCheckbox.element, this.titleElement.firstChild);
42
43     this._enableWorkersCheckbox.addEventListener(this._onTriggerInstrument.bind(this));
44     this._enableWorkersCheckbox.checked = false;
45
46     this._listElement = document.createElement("ol");
47     this._listElement.className = "workers-list";
48
49     this.bodyElement.appendChild(this._listElement);
50     this._treeOutline = new TreeOutline(this._listElement);
51 }
52
53 WebInspector.WorkersSidebarPane.prototype = {
54     addWorker: function(id, url, isShared)
55     {
56         if (id in this._workers) 
57             return;
58         var worker = new WebInspector.Worker(id, url, isShared);
59         this._workers[id] = worker;
60
61         var title = WebInspector.linkifyURL(url, WebInspector.displayNameForURL(url), "worker-item", true, url);
62         var treeElement = new TreeElement(null, worker, false);
63         treeElement.titleHTML = title;
64         this._treeOutline.appendChild(treeElement);
65     },
66
67     removeWorker: function(id)
68     {
69         if (id in this._workers) {
70             this._treeOutline.removeChild(this._treeOutline.findTreeElement(this._workers[id]));
71             delete this._workers[id];
72         }
73     },
74
75     setInstrumentation: function(enabled)
76     {
77         InspectorBackend.removeAllScriptsToEvaluateOnLoad();
78         if (enabled)
79             InspectorBackend.addScriptToEvaluateOnLoad("(" + InjectedFakeWorker + ")");
80     },
81
82     reset: function()
83     {
84         this.setInstrumentation(this._enableWorkersCheckbox.checked);
85         this._treeOutline.removeChildren();
86         this._workers = {};
87     },
88
89     _onTriggerInstrument: function(event)
90     {
91         this.setInstrumentation(this._enableWorkersCheckbox.checked);
92     }
93 };
94
95 WebInspector.WorkersSidebarPane.prototype.__proto__ = WebInspector.SidebarPane.prototype;
96
97 WebInspector.Worker = function(id, url, shared)
98 {
99     this.id = id;
100     this.url = url;
101     this.shared = shared;
102 }
103
104 WebInspector.didCreateWorker = function()
105 {
106     var workersPane = WebInspector.panels.scripts.sidebarPanes.workers;
107     workersPane.addWorker.apply(workersPane, arguments);
108 }
109
110 WebInspector.didDestroyWorker = function()
111 {
112     var workersPane = WebInspector.panels.scripts.sidebarPanes.workers;
113     workersPane.removeWorker.apply(workersPane, arguments);
114 }