2 * Copyright (C) 2014 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @param {!Array.<!WebInspector.ModuleManager.ModuleDescriptor>} descriptors
35 WebInspector.ModuleManager = function(descriptors)
38 * @type {!Array.<!WebInspector.ModuleManager.Module>}
42 * @type {!Object.<string, !WebInspector.ModuleManager.Module>}
44 this._modulesMap = {};
46 * @type {!Array.<!WebInspector.ModuleManager.Extension>}
48 this._extensions = [];
51 * @type {!Object.<string, !function(new:Object)>}
53 this._cachedTypeClasses = {};
56 * @type {!Object.<string, !WebInspector.ModuleManager.ModuleDescriptor>}
58 this._descriptorsMap = {};
59 for (var i = 0; i < descriptors.length; ++i)
60 this._descriptorsMap[descriptors[i]["name"]] = descriptors[i];
63 WebInspector.ModuleManager.prototype = {
65 * @param {!Array.<string>} configuration
67 registerModules: function(configuration)
69 for (var i = 0; i < configuration.length; ++i)
70 this.registerModule(configuration[i]);
74 * @param {string} moduleName
76 registerModule: function(moduleName)
78 if (!this._descriptorsMap[moduleName])
79 throw new Error("Module is not defined: " + moduleName + " " + new Error().stack);
80 var module = new WebInspector.ModuleManager.Module(this, this._descriptorsMap[moduleName]);
81 this._modules.push(module);
82 this._modulesMap[moduleName] = module;
86 * @param {string} moduleName
88 loadModule: function(moduleName)
90 this._modulesMap[moduleName]._load();
94 * @param {string|!Function} type
95 * @param {?Object=} context
96 * @return {!Array.<!WebInspector.ModuleManager.Extension>}
98 extensions: function(type, context)
101 * @param {!WebInspector.ModuleManager.Extension} extension
104 function filter(extension)
106 if (extension._type !== type && extension._typeClass() !== type)
108 return !context || extension.isApplicable(context);
110 return this._extensions.filter(filter);
114 * @param {string|!Function} type
115 * @param {?Object=} context
116 * @return {?WebInspector.ModuleManager.Extension}
118 extension: function(type, context)
120 return this.extensions(type, context)[0] || null;
124 * @param {string|!Function} type
125 * @param {?Object=} context
126 * @return {!Array.<!Object>}
128 instances: function(type, context)
131 * @param {!WebInspector.ModuleManager.Extension} extension
134 function instantiate(extension)
136 return extension.instance();
138 return this.extensions(type, context).filter(instantiate).map(instantiate);
142 * @param {string|!Function} type
145 instance: function(type, context)
147 var extension = this.extension(type, context);
148 return extension ? extension.instance() : null;
152 * @param {string|!Function} type
153 * @param {string} nameProperty
154 * @param {string} orderProperty
155 * @return {function(string, string):number}
157 orderComparator: function(type, nameProperty, orderProperty)
159 var extensions = this.extensions(type);
160 var orderForName = {};
161 for (var i = 0; i < extensions.length; ++i) {
162 var descriptor = extensions[i].descriptor();
163 orderForName[descriptor[nameProperty]] = descriptor[orderProperty];
167 * @param {string} name1
168 * @param {string} name2
171 function result(name1, name2)
173 if (name1 in orderForName && name2 in orderForName)
174 return orderForName[name1] - orderForName[name2];
175 if (name1 in orderForName)
177 if (name2 in orderForName)
179 return name1.compareTo(name2);
185 * @return {?function(new:Object)}
187 resolve: function(typeName)
189 if (!this._cachedTypeClasses[typeName]) {
191 this._cachedTypeClasses[typeName] = /** @type function(new:Object) */ (window.eval(typeName.substring(1)));
195 return this._cachedTypeClasses[typeName];
203 WebInspector.ModuleManager.ModuleDescriptor = function()
211 * @type {!Array.<!WebInspector.ModuleManager.ExtensionDescriptor>}
216 * @type {!Array.<string>}
224 WebInspector.ModuleManager.ExtensionDescriptor = function()
232 * @type {string|undefined}
237 * @type {!Array.<string>|undefined}
244 * @param {!WebInspector.ModuleManager} manager
245 * @param {!WebInspector.ModuleManager.ModuleDescriptor} descriptor
247 WebInspector.ModuleManager.Module = function(manager, descriptor)
249 this._manager = manager;
250 this._descriptor = descriptor;
251 this._name = descriptor.name;
252 var extensions = /** @type {?Array.<!WebInspector.ModuleManager.ExtensionDescriptor>}*/ (descriptor.extensions);
253 for (var i = 0; extensions && i < extensions.length; ++i)
254 this._manager._extensions.push(new WebInspector.ModuleManager.Extension(this, extensions[i]));
255 this._loaded = false;
258 WebInspector.ModuleManager.Module.prototype = {
272 if (this._isLoading) {
273 var oldStackTraceLimit = Error.stackTraceLimit;
274 Error.stackTraceLimit = 50;
275 console.assert(false, "Module " + this._name + " is loaded from itself: " + new Error().stack);
276 Error.stackTraceLimit = oldStackTraceLimit;
280 this._isLoading = true;
281 var scripts = this._descriptor.scripts;
282 for (var i = 0; scripts && i < scripts.length; ++i)
283 loadScript(scripts[i]);
284 this._isLoading = false;
291 * @param {!WebInspector.ModuleManager.Module} module
292 * @param {!WebInspector.ModuleManager.ExtensionDescriptor} descriptor
294 WebInspector.ModuleManager.Extension = function(module, descriptor)
296 this._module = module;
297 this._descriptor = descriptor;
299 this._type = descriptor.type;
300 this._hasTypeClass = !!this._type.startsWith("@");
305 this._className = descriptor.className || null;
308 WebInspector.ModuleManager.Extension.prototype = {
312 descriptor: function()
314 return this._descriptor;
318 * @return {!WebInspector.ModuleManager.Module}
326 * @return {?function(new:Object)}
328 _typeClass: function()
330 if (!this._hasTypeClass)
332 return this._module._manager.resolve(this._type);
336 * @param {?Object} context
339 isApplicable: function(context)
341 var contextTypes = /** @type {!Array.<string>|undefined} */ (this._descriptor.contextTypes);
344 for (var i = 0; i < contextTypes.length; ++i) {
345 var contextType = /** @type {!Function} */ (window.eval(contextTypes[i]));
346 if (context instanceof contextType)
357 if (!this._className)
360 if (!this._instance) {
361 this._module._load();
363 var constructorFunction = window.eval(this._className);
364 if (!(constructorFunction instanceof Function))
367 this._instance = new constructorFunction();
369 return this._instance;
376 WebInspector.Renderer = function()
380 WebInspector.Renderer.prototype = {
382 * @param {!Object} object
385 render: function(object) {}
391 WebInspector.Revealer = function()
396 * @param {?Object} revealable
397 * @param {number=} lineNumber
399 WebInspector.Revealer.reveal = function(revealable, lineNumber)
403 var revealer = WebInspector.moduleManager.instance(WebInspector.Revealer, revealable);
405 revealer.reveal(revealable, lineNumber);
408 WebInspector.Revealer.prototype = {
410 * @param {!Object} object
412 reveal: function(object) {}
418 WebInspector.ActionDelegate = function()
422 WebInspector.ActionDelegate.prototype = {
424 * @param {!Event} event
427 handleAction: function(event) {}
430 WebInspector.moduleManager = new WebInspector.ModuleManager(allDescriptors);