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