1<!DOCTYPE html> 2<!-- 3Copyright (c) 2014 The Chromium Authors. All rights reserved. 4Use of this source code is governed by a BSD-style license that can be 5found in the LICENSE file. 6--> 7<link rel="import" href="/tracing/base/event.html"> 8<link rel="import" href="/tracing/base/extension_registry_base.html"> 9<script> 10'use strict'; 11 12tr.exportTo('tr.b', function() { 13 14 var RegisteredTypeInfo = tr.b.RegisteredTypeInfo; 15 var ExtensionRegistryOptions = tr.b.ExtensionRegistryOptions; 16 17 function decorateBasicExtensionRegistry(registry, extensionRegistryOptions) { 18 var savedStateStack = []; 19 registry.registeredTypeInfos_ = []; 20 21 registry.register = function(constructor, 22 opt_metadata) { 23 if (registry.findIndexOfRegisteredConstructor( 24 constructor) !== undefined) 25 throw new Error('Handler already registered for ' + constructor); 26 27 extensionRegistryOptions.validateConstructor(constructor); 28 29 var metadata = {}; 30 for (var k in extensionRegistryOptions.defaultMetadata) 31 metadata[k] = extensionRegistryOptions.defaultMetadata[k]; 32 if (opt_metadata) { 33 for (var k in opt_metadata) 34 metadata[k] = opt_metadata[k]; 35 } 36 37 var typeInfo = new RegisteredTypeInfo( 38 constructor, 39 metadata); 40 41 var e = new tr.b.Event('will-register'); 42 e.typeInfo = typeInfo; 43 registry.dispatchEvent(e); 44 45 registry.registeredTypeInfos_.push(typeInfo); 46 47 e = new tr.b.Event('registry-changed'); 48 registry.dispatchEvent(e); 49 }; 50 51 registry.pushCleanStateBeforeTest = function() { 52 savedStateStack.push(registry.registeredTypeInfos_); 53 registry.registeredTypeInfos_ = []; 54 55 var e = new tr.b.Event('registry-changed'); 56 registry.dispatchEvent(e); 57 }; 58 registry.popCleanStateAfterTest = function() { 59 registry.registeredTypeInfos_ = savedStateStack[0]; 60 savedStateStack.splice(0, 1); 61 62 var e = new tr.b.Event('registry-changed'); 63 registry.dispatchEvent(e); 64 }; 65 66 registry.findIndexOfRegisteredConstructor = function(constructor) { 67 for (var i = 0; i < registry.registeredTypeInfos_.length; i++) 68 if (registry.registeredTypeInfos_[i].constructor == constructor) 69 return i; 70 return undefined; 71 }; 72 73 registry.unregister = function(constructor) { 74 var foundIndex = registry.findIndexOfRegisteredConstructor(constructor); 75 if (foundIndex === undefined) 76 throw new Error(constructor + ' not registered'); 77 registry.registeredTypeInfos_.splice(foundIndex, 1); 78 79 var e = new tr.b.Event('registry-changed'); 80 registry.dispatchEvent(e); 81 }; 82 83 registry.getAllRegisteredTypeInfos = function() { 84 return registry.registeredTypeInfos_; 85 }; 86 87 registry.findTypeInfo = function(constructor) { 88 var foundIndex = this.findIndexOfRegisteredConstructor(constructor); 89 if (foundIndex !== undefined) 90 return this.registeredTypeInfos_[foundIndex]; 91 return undefined; 92 }; 93 94 registry.findTypeInfoMatching = function(predicate, opt_this) { 95 opt_this = opt_this ? opt_this : undefined; 96 for (var i = 0; i < registry.registeredTypeInfos_.length; ++i) { 97 var typeInfo = registry.registeredTypeInfos_[i]; 98 if (predicate.call(opt_this, typeInfo)) 99 return typeInfo; 100 } 101 return extensionRegistryOptions.defaultTypeInfo; 102 }; 103 104 registry.findTypeInfoWithName = function(name) { 105 if (typeof(name) !== 'string') 106 throw new Error('Name is not a string.'); 107 var typeInfo = registry.findTypeInfoMatching(function(ti) { 108 return ti.constructor.name === name; 109 }); 110 if (typeInfo) 111 return typeInfo; 112 return undefined; 113 }; 114 } 115 116 return { 117 _decorateBasicExtensionRegistry: decorateBasicExtensionRegistry 118 }; 119}); 120</script> 121