Web Inspector: Type Profiler and Code Coverage Profiler should work in Workers
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Protocol / Target.js
1 /*
2  * Copyright (C) 2016 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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WebInspector.Target = class Target extends WebInspector.Object
27 {
28     constructor(identifier, name, type, connection)
29     {
30         super();
31
32         this._identifier = identifier;
33         this._name = name;
34         this._type = type;
35         this._connection = connection;
36         this._executionContext = null;
37         this._mainResource = null;
38         this._resourceCollection = new WebInspector.ResourceCollection;
39         this._extraScriptCollection = new WebInspector.Collection(WebInspector.Collection.TypeVerifier.Script);
40
41         this._connection.target = this;
42
43         this.initialize();
44     }
45
46     // Agents
47
48     get RuntimeAgent() { return this._connection._agents.Runtime; }
49     get ConsoleAgent() { return this._connection._agents.Console; }
50     get DebuggerAgent() { return this._connection._agents.Debugger; }
51
52     // Public
53
54     get identifier() { return this._identifier; }
55     get name() { return this._name; }
56     get type() { return this._type; }
57     get connection() { return this._connection; }
58     get executionContext() { return this._executionContext; }
59
60     get resourceCollection() { return this._resourceCollection; }
61     get extraScriptCollection() { return this._extraScriptCollection; }
62
63     get mainResource() { return this._mainResource; }
64     set mainResource(resource) { this._mainResource = resource; }
65
66     addResource(resource)
67     {
68         this._resourceCollection.add(resource);
69
70         this.dispatchEventToListeners(WebInspector.Target.Event.ResourceAdded, {resource});
71     }
72
73     adoptResource(resource)
74     {
75         resource._target = this;
76
77         this.addResource(resource);
78     }
79
80     addScript(script)
81     {
82         this._extraScriptCollection.add(script);
83
84         this.dispatchEventToListeners(WebInspector.Target.Event.ScriptAdded, {script});
85     }
86 };
87
88 WebInspector.Target.Type = {
89     Main: Symbol("main"),
90     Worker: Symbol("worker"),
91 };
92
93 WebInspector.Target.Event = {
94     ResourceAdded: "target-resource-added",
95     ScriptAdded: "target-script-added",
96 };
97
98 WebInspector.MainTarget = class MainTarget extends WebInspector.Target
99 {
100     constructor(connection)
101     {
102         super("", "", WebInspector.Target.Type.Main, InspectorBackend.mainConnection);
103     }
104
105     // Protected (Target)
106
107     get displayName()
108     {
109         if (WebInspector.debuggableType === WebInspector.DebuggableType.Web)
110             return WebInspector.UIString("Main Frame");
111         return WebInspector.UIString("Main Context");
112     }
113
114     get mainResource()
115     {
116         let mainFrame = WebInspector.frameResourceManager.mainFrame;
117         return mainFrame ? mainFrame.mainResource : null;
118     }
119
120     initialize()
121     {
122         this._executionContext = new WebInspector.ExecutionContext(this, WebInspector.RuntimeManager.TopLevelContextExecutionIdentifier, this.displayName, true, null);
123     }
124 }
125
126 WebInspector.WorkerTarget = class WorkerTarget extends WebInspector.Target
127 {
128     constructor(workerId, name, connection)
129     {
130         super(workerId, name, WebInspector.Target.Type.Worker, connection);
131     }
132
133     // Protected (Target)
134
135     get displayName()
136     {
137         return WebInspector.displayNameForURL(this._name);
138     }
139
140     initialize()
141     {
142         WebInspector.frameResourceManager.adoptOrphanedResourcesForTarget(this);
143
144         if (this.RuntimeAgent) {
145             this._executionContext = new WebInspector.ExecutionContext(this, WebInspector.RuntimeManager.TopLevelContextExecutionIdentifier, this.displayName, false, null);
146             this.RuntimeAgent.enable();
147             if (WebInspector.showJavaScriptTypeInformationSetting && WebInspector.showJavaScriptTypeInformationSetting.value)
148                 this.RuntimeAgent.enableTypeProfiler();
149             if (WebInspector.enableControlFlowProfilerSetting && WebInspector.enableControlFlowProfilerSetting.value)
150                 this.RuntimeAgent.enableControlFlowProfiler();
151         }
152
153         if (this.DebuggerAgent)
154             WebInspector.debuggerManager.initializeTarget(this);
155
156         if (this.ConsoleAgent)
157             this.ConsoleAgent.enable();
158     }
159 }