Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / sdk / WorkerTargetManager.js
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5
6 /**
7  * @constructor
8  * @param {!WebInspector.Target} mainTarget
9  * @param {!WebInspector.TargetManager} targetManager
10  */
11 WebInspector.WorkerTargetManager = function(mainTarget, targetManager)
12 {
13     this._mainTarget = mainTarget;
14     this._targetManager = targetManager;
15     mainTarget.workerManager.addEventListener(WebInspector.WorkerManager.Events.WorkerAdded, this._onWorkerAdded, this);
16     mainTarget.workerManager.addEventListener(WebInspector.WorkerManager.Events.WorkersCleared, this._onWorkersCleared, this);
17     WebInspector.profilingLock().addEventListener(WebInspector.Lock.Events.StateChanged, this._onProfilingStateChanged, this);
18     this._onProfilingStateChanged();
19     this._lastAnonymousTargetId = 0;
20 }
21
22 WebInspector.WorkerTargetManager.prototype = {
23     _onProfilingStateChanged: function()
24     {
25         var acquired = WebInspector.profilingLock().isAcquired();
26         this._mainTarget.workerAgent().setAutoconnectToWorkers(!acquired);
27     },
28
29     /**
30      * @param {!WebInspector.Event} event
31      */
32     _onWorkerAdded: function(event)
33     {
34         var data = /** @type {{workerId: number, url: string, inspectorConnected: boolean}} */ (event.data);
35         new WebInspector.WorkerConnection(this._mainTarget, data.workerId, data.inspectorConnected, onConnectionReady.bind(this));
36
37         /**
38          * @this {WebInspector.WorkerTargetManager}
39          * @param {!InspectorBackendClass.Connection} connection
40          */
41         function onConnectionReady(connection)
42         {
43             var parsedURL = data.url.asParsedURL();
44             var workerId = parsedURL ? parsedURL.lastPathComponent : "#" + (++this._lastAnonymousTargetId);
45             this._targetManager.createTarget(WebInspector.UIString("Worker %s", workerId), connection, targetCreated);
46         }
47
48         /**
49          * @param {!WebInspector.Target} target
50          */
51         function targetCreated(target)
52         {
53             if (data.inspectorConnected)
54                 target.runtimeAgent().run();
55         }
56     },
57
58     _onWorkersCleared: function()
59     {
60         this._lastAnonymousTargetId = 0;
61     }
62 }
63
64 /**
65  * @constructor
66  * @extends {InspectorBackendClass.Connection}
67  * @param {!WebInspector.Target} target
68  * @param {number} workerId
69  * @param {boolean} inspectorConnected
70  * @param {!function(!InspectorBackendClass.Connection)} onConnectionReady
71  */
72 WebInspector.WorkerConnection = function(target, workerId, inspectorConnected, onConnectionReady)
73 {
74     InspectorBackendClass.Connection.call(this);
75     //FIXME: remove resourceTreeModel and others from worker targets
76     this.suppressErrorsForDomains(["Worker", "Page", "CSS", "DOM", "DOMStorage", "Database", "Network"]);
77     this._target = target;
78     this._workerId = workerId;
79     this._workerAgent = target.workerAgent();
80     target.workerManager.addEventListener(WebInspector.WorkerManager.Events.MessageFromWorker, this._dispatchMessageFromWorker, this);
81     target.workerManager.addEventListener(WebInspector.WorkerManager.Events.WorkerRemoved, this._onWorkerRemoved, this);
82     target.workerManager.addEventListener(WebInspector.WorkerManager.Events.WorkersCleared, this._close, this);
83     if (!inspectorConnected)
84         this._workerAgent.connectToWorker(workerId, onConnectionReady.bind(null, this));
85     else
86         onConnectionReady.call(null, this);
87 }
88
89 WebInspector.WorkerConnection.prototype = {
90
91     /**
92      * @param {!WebInspector.Event} event
93      */
94     _dispatchMessageFromWorker: function(event)
95     {
96         var data = /** @type {{workerId: number, command: string, message: !Object}} */ (event.data);
97         if (data.workerId === this._workerId)
98             this.dispatch(data.message);
99     },
100
101     /**
102      * @param {!Object} messageObject
103      */
104     sendMessage: function(messageObject)
105     {
106         this._workerAgent.sendMessageToWorker(this._workerId, messageObject);
107     },
108
109     /**
110      * @param {!WebInspector.Event} event
111      */
112     _onWorkerRemoved: function(event)
113     {
114         var workerId = /** @type {number} */ (event.data);
115         if (workerId === this._workerId)
116             this._close();
117     },
118
119     _close: function()
120     {
121         this._target.workerManager.removeEventListener(WebInspector.WorkerManager.Events.MessageFromWorker, this._dispatchMessageFromWorker, this);
122         this._target.workerManager.removeEventListener(WebInspector.WorkerManager.Events.WorkerRemoved, this._onWorkerRemoved, this);
123         this._target.workerManager.removeEventListener(WebInspector.WorkerManager.Events.WorkersCleared, this._close, this);
124         this.connectionClosed("worker_terminated");
125     },
126
127     __proto__: InspectorBackendClass.Connection.prototype
128 }