Home | History | Annotate | Download | only in components
      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  * @constructor
      7  * @implements {WebInspector.TargetManager.Observer}
      8  */
      9 WebInspector.ExecutionContextSelector = function()
     10 {
     11     WebInspector.targetManager.observeTargets(this);
     12     WebInspector.context.addFlavorChangeListener(WebInspector.ExecutionContext, this._executionContextChanged, this);
     13     WebInspector.context.addFlavorChangeListener(WebInspector.Target, this._targetChanged, this);
     14 
     15     WebInspector.targetManager.addModelListener(WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionContextCreated, this._onExecutionContextCreated, this);
     16     WebInspector.targetManager.addModelListener(WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionContextDestroyed, this._onExecutionContextDestroyed, this);
     17 }
     18 
     19 WebInspector.ExecutionContextSelector.prototype = {
     20 
     21     /**
     22      * @param {!WebInspector.Target} target
     23      */
     24     targetAdded: function(target)
     25     {
     26         // Defer selecting default target since we need all clients to get their
     27         // targetAdded notifications first.
     28         setImmediate(function() {
     29             if (!WebInspector.context.flavor(WebInspector.Target))
     30                 WebInspector.context.setFlavor(WebInspector.Target, target);
     31         });
     32     },
     33 
     34     /**
     35      * @param {!WebInspector.Target} target
     36      */
     37     targetRemoved: function(target)
     38     {
     39         var currentExecutionContext = WebInspector.context.flavor(WebInspector.ExecutionContext);
     40         if (currentExecutionContext && currentExecutionContext.target() === target)
     41             this._currentExecutionContextGone();
     42 
     43         var targets = WebInspector.targetManager.targets();
     44         if (WebInspector.context.flavor(WebInspector.Target) === target && targets.length)
     45             WebInspector.context.setFlavor(WebInspector.Target, targets[0]);
     46     },
     47 
     48     /**
     49      * @param {!WebInspector.Event} event
     50      */
     51     _executionContextChanged: function(event)
     52     {
     53         var newContext = /** @type {?WebInspector.ExecutionContext} */ (event.data);
     54         if (newContext)
     55             WebInspector.context.setFlavor(WebInspector.Target, newContext.target());
     56     },
     57 
     58     /**
     59      * @param {!WebInspector.Event} event
     60      */
     61     _targetChanged: function(event)
     62     {
     63         var newTarget = /** @type {?WebInspector.Target} */(event.data);
     64         var currentContext = WebInspector.context.flavor(WebInspector.ExecutionContext);
     65 
     66         if (!newTarget || (currentContext && currentContext.target() === newTarget))
     67             return;
     68 
     69         var executionContexts = newTarget.runtimeModel.executionContexts();
     70         if (!executionContexts.length)
     71             return;
     72 
     73         var newContext = executionContexts[0];
     74         for (var i = 1; i < executionContexts.length; ++i) {
     75             if (executionContexts[i].isMainWorldContext)
     76                 newContext = executionContexts[i];
     77         }
     78         WebInspector.context.setFlavor(WebInspector.ExecutionContext, newContext);
     79     },
     80 
     81     /**
     82      * @param {!WebInspector.Event} event
     83      */
     84     _onExecutionContextCreated: function(event)
     85     {
     86         var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (event.data);
     87         if (!WebInspector.context.flavor(WebInspector.ExecutionContext))
     88             WebInspector.context.setFlavor(WebInspector.ExecutionContext, executionContext);
     89     },
     90 
     91     /**
     92      * @param {!WebInspector.Event} event
     93      */
     94     _onExecutionContextDestroyed: function(event)
     95     {
     96         var executionContext = /** @type {!WebInspector.ExecutionContext}*/ (event.data);
     97         if (WebInspector.context.flavor(WebInspector.ExecutionContext) === executionContext)
     98             this._currentExecutionContextGone();
     99     },
    100 
    101     _currentExecutionContextGone: function()
    102     {
    103         var targets = WebInspector.targetManager.targets();
    104         var newContext = null;
    105         for (var i = 0; i < targets.length; ++i) {
    106             var executionContexts = targets[i].runtimeModel.executionContexts();
    107             if (executionContexts.length) {
    108                 newContext = executionContexts[0];
    109                 break;
    110             }
    111         }
    112         WebInspector.context.setFlavor(WebInspector.ExecutionContext, newContext);
    113     }
    114 
    115 }
    116 
    117 /**
    118  * @param {!Element} proxyElement
    119  * @param {!Range} wordRange
    120  * @param {boolean} force
    121  * @param {function(!Array.<string>, number=)} completionsReadyCallback
    122  */
    123 WebInspector.ExecutionContextSelector.completionsForTextPromptInCurrentContext = function(proxyElement, wordRange, force, completionsReadyCallback)
    124 {
    125     var executionContext = WebInspector.context.flavor(WebInspector.ExecutionContext);
    126     if (!executionContext) {
    127         completionsReadyCallback([]);
    128         return;
    129     }
    130 
    131     // Pass less stop characters to rangeOfWord so the range will be a more complete expression.
    132     var expressionRange = wordRange.startContainer.rangeOfWord(wordRange.startOffset, " =:[({;,!+-*/&|^<>", proxyElement, "backward");
    133     var expressionString = expressionRange.toString();
    134     var prefix = wordRange.toString();
    135     executionContext.completionsForExpression(expressionString, prefix, force, completionsReadyCallback);
    136 }