| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 if (mojoBindings) { | 7 if (mojoBindings) { |
| 8 throw new Error('mojoBindings has been initialized.'); | 8 throw new Error('mojoBindings has been initialized.'); |
| 9 } | 9 } |
| 10 | 10 |
| 11 var mojoBindings = {}; | 11 var mojoBindings = {}; |
| 12 mojoBindings.internal = {}; | 12 mojoBindings.internal = {}; |
| 13 mojoBindings.internal.global = this; | 13 mojoBindings.internal.global = this; |
| 14 mojoBindings.config = { | |
| 15 // Whether to automatically load mojom dependencies. | |
| 16 // For example, if foo.mojom imports bar.mojom, |autoLoadMojomDeps| set to | |
| 17 // true means that loading foo.mojom.js will insert a <script> tag to load | |
| 18 // bar.mojom.js, if it hasn't been loaded. | |
| 19 // | |
| 20 // The URL of bar.mojom.js is determined by the relative path of bar.mojom | |
| 21 // (relative to the position of foo.mojom at build time) and the URL of | |
| 22 // foo.mojom.js. For exmple, if at build time the two mojom files are | |
| 23 // located at: | |
| 24 // a/b/c/foo.mojom | |
| 25 // a/b/d/bar.mojom | |
| 26 // and the URL of foo.mojom.js is: | |
| 27 // http://example.org/scripts/b/c/foo.mojom.js | |
| 28 // then the URL of bar.mojom.js will be: | |
| 29 // http://example.org/scripts/b/d/bar.mojom.js | |
| 30 // | |
| 31 // If you would like bar.mojom.js to live at a different location, you need | |
| 32 // to turn off |autoLoadMojomDeps| before loading foo.mojom.js, and manually | |
| 33 // load bar.mojom.js yourself. Similarly, you need to turn off the option if | |
| 34 // you merge bar.mojom.js and foo.mojom.js into a single file. | |
| 35 // | |
| 36 // Performance tip: Avoid loading the same mojom.js file multiple times. | |
| 37 // Assume that |autoLoadMojomDeps| is set to true: | |
| 38 // <!-- No duplicate loading; recommended. --> | |
| 39 // <script src="http://example.org/scripts/b/c/foo.mojom.js"></script> | |
| 40 // | |
| 41 // <!-- No duplicate loading, although unnecessary. --> | |
| 42 // <script src="http://example.org/scripts/b/d/bar.mojom.js"></script> | |
| 43 // <script src="http://example.org/scripts/b/c/foo.mojom.js"></script> | |
| 44 // | |
| 45 // <!-- Load bar.mojom.js twice; should be avoided. --> | |
| 46 // <script src="http://example.org/scripts/b/c/foo.mojom.js"></script> | |
| 47 // <script src="http://example.org/scripts/b/d/bar.mojom.js"></script> | |
| 48 autoLoadMojomDeps: true | |
| 49 }; | |
| 50 | 14 |
| 51 (function() { | 15 (function() { |
| 52 var internal = mojoBindings.internal; | 16 var internal = mojoBindings.internal; |
| 53 | 17 |
| 54 var LoadState = { | |
| 55 PENDING_LOAD: 1, | |
| 56 LOADED: 2 | |
| 57 }; | |
| 58 | |
| 59 var mojomRegistry = new Map(); | |
| 60 | |
| 61 function exposeNamespace(namespace) { | 18 function exposeNamespace(namespace) { |
| 62 var current = internal.global; | 19 var current = internal.global; |
| 63 var parts = namespace.split('.'); | 20 var parts = namespace.split('.'); |
| 64 | 21 |
| 65 for (var part; parts.length && (part = parts.shift());) { | 22 for (var part; parts.length && (part = parts.shift());) { |
| 66 if (!current[part]) { | 23 if (!current[part]) { |
| 67 current[part] = {}; | 24 current[part] = {}; |
| 68 } | 25 } |
| 69 current = current[part]; | 26 current = current[part]; |
| 70 } | 27 } |
| 71 | 28 |
| 72 return current; | 29 return current; |
| 73 } | 30 } |
| 74 | 31 |
| 75 function isMojomPendingLoad(id) { | |
| 76 return mojomRegistry.get(id) === LoadState.PENDING_LOAD; | |
| 77 } | |
| 78 | |
| 79 function isMojomLoaded(id) { | |
| 80 return mojomRegistry.get(id) === LoadState.LOADED; | |
| 81 } | |
| 82 | |
| 83 function markMojomPendingLoad(id) { | |
| 84 if (isMojomLoaded(id)) { | |
| 85 throw new Error('The following mojom file has been loaded: ' + id); | |
| 86 } | |
| 87 | |
| 88 mojomRegistry.set(id, LoadState.PENDING_LOAD); | |
| 89 } | |
| 90 | |
| 91 function markMojomLoaded(id) { | |
| 92 mojomRegistry.set(id, LoadState.LOADED); | |
| 93 } | |
| 94 | |
| 95 function loadMojomIfNecessary(id, url) { | |
| 96 if (mojomRegistry.has(id)) { | |
| 97 return; | |
| 98 } | |
| 99 | |
| 100 markMojomPendingLoad(id); | |
| 101 internal.global.document.write('<script type="text/javascript" src="' + | |
| 102 url + '"></script>'); | |
| 103 } | |
| 104 | |
| 105 internal.exposeNamespace = exposeNamespace; | 32 internal.exposeNamespace = exposeNamespace; |
| 106 internal.isMojomPendingLoad = isMojomPendingLoad; | |
| 107 internal.isMojomLoaded = isMojomLoaded; | |
| 108 internal.markMojomPendingLoad = markMojomPendingLoad; | |
| 109 internal.markMojomLoaded = markMojomLoaded; | |
| 110 internal.loadMojomIfNecessary = loadMojomIfNecessary; | |
| 111 })(); | 33 })(); |
| OLD | NEW |