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 (mojo && mojo.internal) { | 7 if (mojo && mojo.internal) { |
8 throw new Error('The Mojo bindings library has been initialized.'); | 8 throw new Error('The Mojo bindings library has been initialized.'); |
9 } | 9 } |
10 | 10 |
11 var mojo = mojo || {}; | 11 var mojo = mojo || {}; |
12 mojo.internal = {}; | 12 mojo.internal = {}; |
13 mojo.internal.global = this; | 13 mojo.internal.global = this; |
| 14 mojo.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 }; |
14 | 50 |
15 (function() { | 51 (function() { |
16 var internal = mojo.internal; | 52 var internal = mojo.internal; |
17 | 53 |
| 54 var LoadState = { |
| 55 PENDING_LOAD: 1, |
| 56 LOADED: 2 |
| 57 }; |
| 58 |
| 59 var mojomRegistry = new Map(); |
| 60 |
18 function exposeNamespace(namespace) { | 61 function exposeNamespace(namespace) { |
19 var current = internal.global; | 62 var current = internal.global; |
20 var parts = namespace.split('.'); | 63 var parts = namespace.split('.'); |
21 | 64 |
22 for (var part; parts.length && (part = parts.shift());) { | 65 for (var part; parts.length && (part = parts.shift());) { |
23 if (!current[part]) { | 66 if (!current[part]) { |
24 current[part] = {}; | 67 current[part] = {}; |
25 } | 68 } |
26 current = current[part]; | 69 current = current[part]; |
27 } | 70 } |
28 | 71 |
29 return current; | 72 return current; |
30 } | 73 } |
31 | 74 |
| 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 |
32 internal.exposeNamespace = exposeNamespace; | 105 internal.exposeNamespace = exposeNamespace; |
| 106 internal.isMojomPendingLoad = isMojomPendingLoad; |
| 107 internal.isMojomLoaded = isMojomLoaded; |
| 108 internal.markMojomPendingLoad = markMojomPendingLoad; |
| 109 internal.markMojomLoaded = markMojomLoaded; |
| 110 internal.loadMojomIfNecessary = loadMojomIfNecessary; |
33 })(); | 111 })(); |
OLD | NEW |