Home | History | Annotate | Download | only in sdk
      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 {string} name
     11  * @param {!InspectorBackendClass.Connection} connection
     12  * @param {function(!WebInspector.Target)=} callback
     13  */
     14 WebInspector.Target = function(name, connection, callback)
     15 {
     16     Protocol.Agents.call(this, connection.agentsMap());
     17     this._name = name;
     18     this._connection = connection;
     19     /** @type {boolean} */
     20     this.isMainFrontend = false;
     21     this._id = WebInspector.Target._nextId++;
     22     /** @type {boolean} */
     23     this.canScreencast = false;
     24     this.pageAgent().canScreencast(this._initializeCapability.bind(this, "canScreencast", null));
     25 
     26     /** @type {boolean} */
     27     this.hasTouchInputs = false;
     28     this.pageAgent().hasTouchInputs(this._initializeCapability.bind(this, "hasTouchInputs", null));
     29 
     30     if (WebInspector.experimentsSettings.timelinePowerProfiler.isEnabled())
     31         this.powerAgent().canProfilePower(this._initializeCapability.bind(this, "canProfilePower", null));
     32 
     33     this.workerAgent().canInspectWorkers(this._initializeCapability.bind(this, "isMainFrontend", this._loadedWithCapabilities.bind(this, callback)));
     34 
     35     /** @type {!WebInspector.Lock} */
     36     this.profilingLock = new WebInspector.Lock();
     37 }
     38 
     39 WebInspector.Target._nextId = 1;
     40 
     41 WebInspector.Target.prototype = {
     42 
     43     /**
     44      * @return {number}
     45      */
     46     id: function()
     47     {
     48         return this._id;
     49     },
     50 
     51     /**
     52      *
     53      * @return {string}
     54      */
     55     name: function()
     56     {
     57         return this._name;
     58     },
     59 
     60     /**
     61      * @param {string} name
     62      * @param {function()|null} callback
     63      * @param {?Protocol.Error} error
     64      * @param {*} result
     65      */
     66     _initializeCapability: function(name, callback, error, result)
     67     {
     68         this[name] = result;
     69         if (!Capabilities[name])
     70             Capabilities[name] = result;
     71         if (callback)
     72             callback();
     73     },
     74 
     75     /**
     76      * @param {function(!WebInspector.Target)=} callback
     77      */
     78     _loadedWithCapabilities: function(callback)
     79     {
     80         /** @type {!WebInspector.ConsoleModel} */
     81         this.consoleModel = new WebInspector.ConsoleModel(this);
     82         // This and similar lines are needed for compatibility.
     83         if (!WebInspector.console)
     84             WebInspector.console = this.consoleModel;
     85 
     86         /** @type {!WebInspector.NetworkManager} */
     87         this.networkManager = new WebInspector.NetworkManager(this);
     88         if (!WebInspector.networkManager)
     89             WebInspector.networkManager = this.networkManager;
     90 
     91         /** @type {!WebInspector.ResourceTreeModel} */
     92         this.resourceTreeModel = new WebInspector.ResourceTreeModel(this);
     93         if (!WebInspector.resourceTreeModel)
     94             WebInspector.resourceTreeModel = this.resourceTreeModel;
     95 
     96         /** @type {!WebInspector.NetworkLog} */
     97         this.networkLog = new WebInspector.NetworkLog(this);
     98         if (!WebInspector.networkLog)
     99             WebInspector.networkLog = this.networkLog;
    100 
    101         /** @type {!WebInspector.DebuggerModel} */
    102         this.debuggerModel = new WebInspector.DebuggerModel(this);
    103         if (!WebInspector.debuggerModel)
    104             WebInspector.debuggerModel = this.debuggerModel;
    105 
    106         /** @type {!WebInspector.RuntimeModel} */
    107         this.runtimeModel = new WebInspector.RuntimeModel(this);
    108         if (!WebInspector.runtimeModel)
    109             WebInspector.runtimeModel = this.runtimeModel;
    110 
    111         /** @type {!WebInspector.DOMModel} */
    112         this.domModel = new WebInspector.DOMModel(this);
    113         if (!WebInspector.domModel)
    114             WebInspector.domModel = this.domModel;
    115 
    116         /** @type {!WebInspector.CSSStyleModel} */
    117         this.cssModel = new WebInspector.CSSStyleModel(this);
    118         if (!WebInspector.cssModel)
    119             WebInspector.cssModel = this.cssModel;
    120 
    121         /** @type {!WebInspector.WorkerManager} */
    122         this.workerManager = new WebInspector.WorkerManager(this, this.isMainFrontend);
    123         if (!WebInspector.workerManager)
    124             WebInspector.workerManager = this.workerManager;
    125 
    126         if (this.canProfilePower)
    127             WebInspector.powerProfiler = new WebInspector.PowerProfiler();
    128 
    129         /** @type {!WebInspector.TimelineManager} */
    130         this.timelineManager = new WebInspector.TimelineManager(this);
    131         if (!WebInspector.timelineManager)
    132             WebInspector.timelineManager = this.timelineManager;
    133 
    134         /** @type {!WebInspector.DatabaseModel} */
    135         this.databaseModel = new WebInspector.DatabaseModel(this);
    136         if (!WebInspector.databaseModel)
    137             WebInspector.databaseModel = this.databaseModel;
    138 
    139         /** @type {!WebInspector.DOMStorageModel} */
    140         this.domStorageModel = new WebInspector.DOMStorageModel(this);
    141         if (!WebInspector.domStorageModel)
    142             WebInspector.domStorageModel = this.domStorageModel;
    143 
    144         /** @type {!WebInspector.CPUProfilerModel} */
    145         this.cpuProfilerModel = new WebInspector.CPUProfilerModel(this);
    146         if (!WebInspector.cpuProfilerModel)
    147             WebInspector.cpuProfilerModel = this.cpuProfilerModel;
    148 
    149         new WebInspector.DebuggerScriptMapping(this.debuggerModel, WebInspector.workspace, WebInspector.networkWorkspaceBinding);
    150 
    151         if (callback)
    152             callback(this);
    153     },
    154 
    155     /**
    156      * @override
    157      * @param {string} domain
    158      * @param {!Object} dispatcher
    159      */
    160     registerDispatcher: function(domain, dispatcher)
    161     {
    162         this._connection.registerDispatcher(domain, dispatcher);
    163     },
    164 
    165     /**
    166      * @return {boolean}
    167      */
    168     isWorkerTarget: function()
    169     {
    170         return !this.isMainFrontend;
    171     },
    172 
    173     /**
    174      * @return {boolean}
    175      */
    176     isMobile: function()
    177     {
    178         // FIXME: either add a separate capability or rename canScreencast to isMobile.
    179         return this.canScreencast;
    180     },
    181 
    182     __proto__: Protocol.Agents.prototype
    183 }
    184 
    185 /**
    186  * @constructor
    187  * @param {!WebInspector.Target} target
    188  */
    189 WebInspector.TargetAware = function(target)
    190 {
    191     this._target = target;
    192 }
    193 
    194 WebInspector.TargetAware.prototype = {
    195     /**
    196      * @return {!WebInspector.Target}
    197      */
    198     target: function()
    199     {
    200         return this._target;
    201     }
    202 }
    203 
    204 /**
    205  * @constructor
    206  * @extends {WebInspector.Object}
    207  * @param {!WebInspector.Target} target
    208  */
    209 WebInspector.TargetAwareObject = function(target)
    210 {
    211     WebInspector.Object.call(this);
    212     this._target = target;
    213 }
    214 
    215 WebInspector.TargetAwareObject.prototype = {
    216     /**
    217      * @return {!WebInspector.Target}
    218      */
    219     target: function()
    220     {
    221         return this._target;
    222     },
    223 
    224     __proto__: WebInspector.Object.prototype
    225 }
    226 
    227 /**
    228  * @constructor
    229  */
    230 WebInspector.TargetManager = function()
    231 {
    232     /** @type {!Array.<!WebInspector.Target>} */
    233     this._targets = [];
    234     /** @type {!Array.<!WebInspector.TargetManager.Observer>} */
    235     this._observers = [];
    236 }
    237 
    238 WebInspector.TargetManager.prototype = {
    239 
    240     /**
    241      * @param {!WebInspector.TargetManager.Observer} targetObserver
    242      */
    243     observeTargets: function(targetObserver)
    244     {
    245         this.targets().forEach(targetObserver.targetAdded.bind(targetObserver));
    246         this._observers.push(targetObserver);
    247     },
    248 
    249     /**
    250      * @param {!WebInspector.TargetManager.Observer} targetObserver
    251      */
    252     unobserveTargets: function(targetObserver)
    253     {
    254         this._observers.remove(targetObserver);
    255     },
    256 
    257     /**
    258      * @param {string} name
    259      * @param {!InspectorBackendClass.Connection} connection
    260      * @param {function(!WebInspector.Target)=} callback
    261      */
    262     createTarget: function(name, connection, callback)
    263     {
    264         var target = new WebInspector.Target(name, connection, callbackWrapper.bind(this));
    265 
    266         /**
    267          * @this {WebInspector.TargetManager}
    268          * @param {!WebInspector.Target} newTarget
    269          */
    270         function callbackWrapper(newTarget)
    271         {
    272             this.addTarget(newTarget);
    273             if (callback)
    274                 callback(newTarget);
    275         }
    276     },
    277 
    278     /**
    279      * @param {!WebInspector.Target} newTarget
    280      */
    281     addTarget: function(newTarget)
    282     {
    283         this._targets.push(newTarget);
    284         var copy = this._observers;
    285         for (var i = 0; i < copy.length; ++i)
    286             copy[i].targetAdded(newTarget);
    287     },
    288 
    289     /**
    290      * @param {!WebInspector.Target} target
    291      */
    292     removeTarget: function(target)
    293     {
    294         this._targets.remove(target);
    295         var copy = this._observers;
    296         for (var i = 0; i < copy.length; ++i)
    297             copy[i].targetRemoved(target);
    298     },
    299 
    300     /**
    301      * @return {!Array.<!WebInspector.Target>}
    302      */
    303     targets: function()
    304     {
    305         return this._targets;
    306     },
    307 
    308     /**
    309      * @return {?WebInspector.Target}
    310      */
    311     activeTarget: function()
    312     {
    313         return this._targets[0];
    314     }
    315 }
    316 
    317 /**
    318  * @interface
    319  */
    320 WebInspector.TargetManager.Observer = function()
    321 {
    322 }
    323 
    324 WebInspector.TargetManager.Observer.prototype = {
    325     /**
    326      * @param {!WebInspector.Target} target
    327      */
    328     targetAdded: function(target) { },
    329 
    330     /**
    331      * @param {!WebInspector.Target} target
    332      */
    333     targetRemoved: function(target) { },
    334 }
    335 
    336 /**
    337  * @type {!WebInspector.TargetManager}
    338  */
    339 WebInspector.targetManager;
    340