Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / sdk / Target.js
1 /*
2  * Copyright 2014 The Chromium Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 /**
8  * @constructor
9  * @extends {Protocol.Agents}
10  * @param {!InspectorBackendClass.Connection} connection
11  * @param {function(!WebInspector.Target)=} callback
12  */
13 WebInspector.Target = function(connection, callback)
14 {
15     Protocol.Agents.call(this, connection.agentsMap());
16     this._connection = connection;
17     /** @type {boolean} */
18     this.isMainFrontend = false;
19
20     /** @type {boolean} */
21     this.canScreencast = false;
22     this.pageAgent().canScreencast(this._initializeCapability.bind(this, "canScreencast", null));
23
24     /** @type {boolean} */
25     this.hasTouchInputs = false;
26     this.pageAgent().hasTouchInputs(this._initializeCapability.bind(this, "hasTouchInputs", null));
27
28     if (WebInspector.experimentsSettings.powerProfiler.isEnabled())
29         this.powerAgent().canProfilePower(this._initializeCapability.bind(this, "canProfilePower", null));
30
31     this.workerAgent().canInspectWorkers(this._initializeCapability.bind(this, "isMainFrontend", this._loadedWithCapabilities.bind(this, callback)));
32
33     /** @type {!WebInspector.Lock} */
34     this.profilingLock = new WebInspector.Lock();
35 }
36
37 WebInspector.Target.prototype = {
38     /**
39      * @param {string} name
40      * @param {function()|null} callback
41      */
42     _initializeCapability: function(name, callback, error, result)
43     {
44         this[name] = result;
45         if (!Capabilities[name])
46             Capabilities[name] = result;
47         if (callback)
48             callback();
49     },
50
51     /**
52      * @param {function(!WebInspector.Target)=} callback
53      */
54     _loadedWithCapabilities: function(callback)
55     {
56         /** @type {!WebInspector.ConsoleModel} */
57         this.consoleModel = new WebInspector.ConsoleModel(this);
58         // This and similar lines are needed for compatibility.
59         if (!WebInspector.console)
60             WebInspector.console = this.consoleModel;
61
62         /** @type {!WebInspector.NetworkManager} */
63         this.networkManager = new WebInspector.NetworkManager(this);
64         if (!WebInspector.networkManager)
65             WebInspector.networkManager = this.networkManager;
66
67         /** @type {!WebInspector.ResourceTreeModel} */
68         this.resourceTreeModel = new WebInspector.ResourceTreeModel(this);
69         if (!WebInspector.resourceTreeModel)
70             WebInspector.resourceTreeModel = this.resourceTreeModel;
71
72         /** @type {!WebInspector.NetworkLog} */
73         this.networkLog = new WebInspector.NetworkLog(this);
74         if (!WebInspector.networkLog)
75             WebInspector.networkLog = this.networkLog;
76
77         /** @type {!WebInspector.DebuggerModel} */
78         this.debuggerModel = new WebInspector.DebuggerModel(this);
79         if (!WebInspector.debuggerModel)
80             WebInspector.debuggerModel = this.debuggerModel;
81
82         /** @type {!WebInspector.RuntimeModel} */
83         this.runtimeModel = new WebInspector.RuntimeModel(this);
84         if (!WebInspector.runtimeModel)
85             WebInspector.runtimeModel = this.runtimeModel;
86
87         /** @type {!WebInspector.DOMModel} */
88         this.domModel = new WebInspector.DOMModel(this);
89         if (!WebInspector.domModel)
90             WebInspector.domModel = this.domModel;
91
92         /** @type {!WebInspector.CSSStyleModel} */
93         this.cssModel = new WebInspector.CSSStyleModel(this);
94         if (!WebInspector.cssModel)
95             WebInspector.cssModel = this.cssModel;
96
97         /** @type {!WebInspector.WorkerManager} */
98         this.workerManager = new WebInspector.WorkerManager(this, this.isMainFrontend);
99         if (!WebInspector.workerManager)
100             WebInspector.workerManager = this.workerManager;
101
102         if (this.canProfilePower)
103             WebInspector.powerProfiler = new WebInspector.PowerProfiler();
104
105         /** @type {!WebInspector.TimelineManager} */
106         this.timelineManager = new WebInspector.TimelineManager(this);
107         if (!WebInspector.timelineManager)
108             WebInspector.timelineManager = this.timelineManager;
109
110         /** @type {!WebInspector.DatabaseModel} */
111         this.databaseModel = new WebInspector.DatabaseModel(this);
112         if (!WebInspector.databaseModel)
113             WebInspector.databaseModel = this.databaseModel;
114
115         /** @type {!WebInspector.DOMStorageModel} */
116         this.domStorageModel = new WebInspector.DOMStorageModel(this);
117         if (!WebInspector.domStorageModel)
118             WebInspector.domStorageModel = this.domStorageModel;
119
120         /** @type {!WebInspector.CPUProfilerModel} */
121         this.cpuProfilerModel = new WebInspector.CPUProfilerModel(this);
122         if (!WebInspector.cpuProfilerModel)
123             WebInspector.cpuProfilerModel = this.cpuProfilerModel;
124
125         if (callback)
126             callback(this);
127     },
128
129     /**
130      * @override
131      * @param {string} domain
132      * @param {!Object} dispatcher
133      */
134     registerDispatcher: function(domain, dispatcher)
135     {
136         this._connection.registerDispatcher(domain, dispatcher);
137     },
138
139     /**
140      * @return {boolean}
141      */
142     isWorkerTarget: function()
143     {
144         return !this.isMainFrontend;
145     },
146
147     /**
148      * @return {boolean}
149      */
150     isMobile: function()
151     {
152         // FIXME: either add a separate capability or rename canScreencast to isMobile.
153         return this.canScreencast;
154     },
155
156     __proto__: Protocol.Agents.prototype
157 }
158
159 /**
160  * @constructor
161  * @param {!WebInspector.Target} target
162  */
163 WebInspector.TargetAware = function(target)
164 {
165     this._target = target;
166 }
167
168 WebInspector.TargetAware.prototype = {
169     /**
170      * @return {!WebInspector.Target}
171      */
172     target: function()
173     {
174         return this._target;
175     }
176 }
177
178 /**
179  * @constructor
180  * @extends {WebInspector.Object}
181  * @param {!WebInspector.Target} target
182  */
183 WebInspector.TargetAwareObject = function(target)
184 {
185     WebInspector.Object.call(this);
186     this._target = target;
187 }
188
189 WebInspector.TargetAwareObject.prototype = {
190     /**
191      * @return {!WebInspector.Target}
192      */
193     target: function()
194     {
195         return this._target;
196     },
197
198     __proto__: WebInspector.Object.prototype
199 }
200
201 /**
202  * @constructor
203  */
204 WebInspector.TargetManager = function()
205 {
206     /** @type {!Array.<!WebInspector.Target>} */
207     this._targets = [];
208     /** @type {!Array.<!WebInspector.TargetManager.Observer>} */
209     this._observers = [];
210 }
211
212 WebInspector.TargetManager.prototype = {
213
214     /**
215      * @param {!WebInspector.TargetManager.Observer} targetObserver
216      */
217     observeTargets: function(targetObserver)
218     {
219         WebInspector.targetManager.targets().forEach(targetObserver.targetAdded.bind(targetObserver));
220         this._observers.push(targetObserver);
221     },
222
223     /**
224      * @param {!InspectorBackendClass.Connection} connection
225      * @param {function(!WebInspector.Target)=} callback
226      */
227     createTarget: function(connection, callback)
228     {
229         var target = new WebInspector.Target(connection, callbackWrapper.bind(this));
230
231         /**
232          * @this {WebInspector.TargetManager}
233          * @param newTarget
234          */
235         function callbackWrapper(newTarget)
236         {
237             this._targets.push(newTarget);
238             var copy = this._observers;
239             for (var i = 0; i < copy.length; ++i)
240                 copy[i].targetAdded(newTarget);
241
242             if (callback)
243                 callback(newTarget);
244         }
245     },
246
247     /**
248      * @return {!Array.<!WebInspector.Target>}
249      */
250     targets: function()
251     {
252         return this._targets;
253     },
254
255     /**
256      * @return {?WebInspector.Target}
257      */
258     activeTarget: function()
259     {
260         return this._targets[0];
261     }
262 }
263
264 /**
265  * @interface
266  */
267 WebInspector.TargetManager.Observer = function()
268 {
269 }
270
271 WebInspector.TargetManager.Observer.prototype = {
272     /**
273      * @param {!WebInspector.Target} target
274      */
275     targetAdded: function(target) { },
276
277     /**
278      * @param {!WebInspector.Target} target
279      */
280     targetRemoved: function(target) { },
281 }
282
283 /**
284  * @type {!WebInspector.TargetManager}
285  */
286 WebInspector.targetManager;