Home | History | Annotate | Download | only in bindings
      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  */
      8 WebInspector.ContentScriptProjectDecorator = function()
      9 {
     10     WebInspector.targetManager.addModelListener(WebInspector.RuntimeModel, WebInspector.RuntimeModel.Events.ExecutionContextCreated, this._onContextCreated, this);
     11     WebInspector.workspace.addEventListener(WebInspector.Workspace.Events.ProjectAdded, this._onProjectAdded, this);
     12 }
     13 
     14 /**
     15  * @param {!WebInspector.Project} project
     16  * @param {!WebInspector.ExecutionContext} context
     17  */
     18 WebInspector.ContentScriptProjectDecorator._updateProjectWithExtensionName = function(project, context)
     19 {
     20     if (project.url().startsWith(context.origin))
     21         project.setDisplayName(context.name);
     22 }
     23 
     24 WebInspector.ContentScriptProjectDecorator.prototype = {
     25     /**
     26      * @param {!WebInspector.Event} event
     27      */
     28     _onContextCreated: function(event)
     29     {
     30         var context = /** @type {!WebInspector.ExecutionContext} */(event.data);
     31         if (!context.origin || !context.name)
     32             return;
     33 
     34         var projects = WebInspector.workspace.projects();
     35         projects = projects.filter(contentProjectWithName);
     36 
     37         for (var i = 0; i < projects.length; ++i)
     38             WebInspector.ContentScriptProjectDecorator._updateProjectWithExtensionName(projects[i], context);
     39 
     40         /**
     41          * @param {!WebInspector.Project} project
     42          * @return {boolean}
     43          */
     44         function contentProjectWithName(project)
     45         {
     46            return !!project.url() && project.type() === WebInspector.projectTypes.ContentScripts;
     47         }
     48     },
     49 
     50     /**
     51      * @param {!WebInspector.Event} event
     52      */
     53     _onProjectAdded: function(event)
     54     {
     55         var project = /** @type {!WebInspector.Project} */(event.data);
     56         if (project.type() !== WebInspector.projectTypes.ContentScripts)
     57             return;
     58 
     59         var targets = WebInspector.targetManager.targets();
     60         var contexts = [];
     61         for (var i = 0; i < targets.length; ++i)
     62             contexts = contexts.concat(targets[i].runtimeModel.executionContexts());
     63         contexts = contexts.filter(contextWithOriginAndName);
     64 
     65         for (var i = 0; i < contexts.length; ++i)
     66             WebInspector.ContentScriptProjectDecorator._updateProjectWithExtensionName(project, contexts[i]);
     67 
     68         /**
     69          * @param {!WebInspector.ExecutionContext} context
     70          * @return {boolean}
     71          */
     72         function contextWithOriginAndName(context)
     73         {
     74             return !!context.origin && !!context.name;
     75         }
     76     }
     77 }