Web Inspector: Worker debugging should pause all targets and view call frames in...
[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("main", "", WebInspector.Target.Type.Main, InspectorBackend.mainConnection);
103     }
104
105     // Protected (Target)
106
107     get displayName()
108     {
109         switch (WebInspector.debuggableType) {
110         case WebInspector.DebuggableType.Web:
111             return WebInspector.UIString("Page");
112         case WebInspector.DebuggableType.JavaScript:
113             return WebInspector.UIString("JavaScript Context");
114         default:
115             console.error("Unexpected debuggable type: ", WebInspector.debuggableType);
116             return WebInspector.UIString("Main");
117         }
118     }
119
120     get mainResource()
121     {
122         let mainFrame = WebInspector.frameResourceManager.mainFrame;
123         return mainFrame ? mainFrame.mainResource : null;
124     }
125
126     initialize()
127     {
128         let displayName = WebInspector.debuggableType === WebInspector.DebuggableType.Web ? WebInspector.UIString("Main Frame") : this.displayName;
129         this._executionContext = new WebInspector.ExecutionContext(this, WebInspector.RuntimeManager.TopLevelContextExecutionIdentifier, displayName, true, null);
130     }
131 }
132
133 WebInspector.WorkerTarget = class WorkerTarget extends WebInspector.Target
134 {
135     constructor(workerId, name, connection)
136     {
137         super(workerId, name, WebInspector.Target.Type.Worker, connection);
138     }
139
140     // Protected (Target)
141
142     get displayName()
143     {
144         return WebInspector.displayNameForURL(this._name);
145     }
146
147     initialize()
148     {
149         WebInspector.frameResourceManager.adoptOrphanedResourcesForTarget(this);
150
151         if (this.RuntimeAgent) {
152             this._executionContext = new WebInspector.ExecutionContext(this, WebInspector.RuntimeManager.TopLevelContextExecutionIdentifier, this.displayName, false, null);
153             this.RuntimeAgent.enable();
154             if (WebInspector.showJavaScriptTypeInformationSetting && WebInspector.showJavaScriptTypeInformationSetting.value)
155                 this.RuntimeAgent.enableTypeProfiler();
156             if (WebInspector.enableControlFlowProfilerSetting && WebInspector.enableControlFlowProfilerSetting.value)
157                 this.RuntimeAgent.enableControlFlowProfiler();
158         }
159
160         if (this.DebuggerAgent)
161             WebInspector.debuggerManager.initializeTarget(this);
162
163         if (this.ConsoleAgent)
164             this.ConsoleAgent.enable();
165     }
166 }