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/base.html"> 8<script> 9'use strict'; 10 11tr.exportTo('tr.b', function() { 12 function RegisteredTypeInfo(constructor, metadata) { 13 this.constructor = constructor; 14 this.metadata = metadata; 15 }; 16 17 var BASIC_REGISTRY_MODE = 'BASIC_REGISTRY_MODE'; 18 var TYPE_BASED_REGISTRY_MODE = 'TYPE_BASED_REGISTRY_MODE'; 19 var ALL_MODES = {BASIC_REGISTRY_MODE: true, TYPE_BASED_REGISTRY_MODE: true}; 20 21 function ExtensionRegistryOptions(mode) { 22 if (mode === undefined) 23 throw new Error('Mode is required'); 24 if (!ALL_MODES[mode]) 25 throw new Error('Not a mode.'); 26 27 this.mode_ = mode; 28 this.defaultMetadata_ = {}; 29 this.defaultConstructor_ = undefined; 30 this.mandatoryBaseClass_ = undefined; 31 this.defaultTypeInfo_ = undefined; 32 this.frozen_ = false; 33 } 34 ExtensionRegistryOptions.prototype = { 35 freeze: function() { 36 if (this.frozen_) 37 throw new Error('Frozen'); 38 this.frozen_ = true; 39 }, 40 41 get mode() { 42 return this.mode_; 43 }, 44 45 get defaultMetadata() { 46 return this.defaultMetadata_; 47 }, 48 49 set defaultMetadata(defaultMetadata) { 50 if (this.frozen_) 51 throw new Error('Frozen'); 52 this.defaultMetadata_ = defaultMetadata; 53 this.defaultTypeInfo_ = undefined; 54 }, 55 56 get defaultConstructor() { 57 return this.defaultConstructor_; 58 }, 59 60 set defaultConstructor(defaultConstructor) { 61 if (this.frozen_) 62 throw new Error('Frozen'); 63 this.defaultConstructor_ = defaultConstructor; 64 this.defaultTypeInfo_ = undefined; 65 }, 66 67 get defaultTypeInfo() { 68 if (this.defaultTypeInfo_ === undefined && this.defaultConstructor_) { 69 this.defaultTypeInfo_ = new RegisteredTypeInfo( 70 this.defaultConstructor, 71 this.defaultMetadata); 72 } 73 return this.defaultTypeInfo_; 74 }, 75 76 validateConstructor: function(constructor) { 77 if (!this.mandatoryBaseClass) 78 return; 79 var curProto = constructor.prototype.__proto__; 80 var ok = false; 81 while (curProto) { 82 if (curProto === this.mandatoryBaseClass.prototype) { 83 ok = true; 84 break; 85 } 86 curProto = curProto.__proto__; 87 } 88 if (!ok) 89 throw new Error(constructor + 'must be subclass of ' + registry); 90 } 91 }; 92 93 return { 94 BASIC_REGISTRY_MODE: BASIC_REGISTRY_MODE, 95 TYPE_BASED_REGISTRY_MODE: TYPE_BASED_REGISTRY_MODE, 96 97 ExtensionRegistryOptions: ExtensionRegistryOptions, 98 RegisteredTypeInfo: RegisteredTypeInfo 99 }; 100}); 101 102</script> 103