Chromium Code Reviews| Index: mojo/public/js/new_bindings/base.js |
| diff --git a/mojo/public/js/new_bindings/base.js b/mojo/public/js/new_bindings/base.js |
| index c147ec69968273d702a8a478f23612475659770a..b67393d4e0abfb7adfd85215fc02c7fb9696c4c4 100644 |
| --- a/mojo/public/js/new_bindings/base.js |
| +++ b/mojo/public/js/new_bindings/base.js |
| @@ -11,10 +11,53 @@ if (mojoBindings) { |
| var mojoBindings = {}; |
| mojoBindings.internal = {}; |
| mojoBindings.internal.global = this; |
| +mojoBindings.config = { |
| + // Whether to automatically load mojom dependencies. |
| + // For example, if foo.mojom imports bar.mojom, |auto_load_mojom_deps| sets |
|
Ken Rockot(use gerrit already)
2017/03/26 18:27:11
not: sets -> set
yzshen1
2017/03/26 19:51:11
Done.
|
| + // to true means that loading foo.mojom.js will insert a <script> tag to |
| + // load bar.mojom.js, if it hasn't been loaded. |
| + // |
| + // The URL of bar.mojom.js is determined by the relative path of bar.mojom |
| + // (relative to the position of foo.mojom at build time) and the URL of |
| + // foo.mojom.js. For exmple, if at build time the two mojom files are |
| + // located at: |
| + // a/b/c/foo.mojom |
| + // a/b/d/bar.mojom |
| + // and the URL of foo.mojom.js is: |
| + // http://example.org/scripts/b/c/foo.mojom.js |
| + // then the URL of bar.mojom.js will be: |
| + // http://example.org/scripts/b/d/bar.mojom.js |
| + // |
| + // If you would like bar.mojom.js to live at a different location, you need |
| + // to turn off |auto_load_mojom_deps| before loading foo.mojom.js, and |
| + // manually load bar.mojom.js yourself. Similarly, you need to turn off the |
| + // option if you merge bar.mojom.js and foo.mojom.js into a single file. |
| + // |
| + // Performance tip: Avoid loading the same mojom.js file multiple times. |
| + // Assume that |auto_load_mojom_deps| is set to true: |
| + // <!-- No duplicate loading; recommended. --> |
| + // <script src="http://example.org/scripts/b/c/foo.mojom.js"></script> |
| + // |
| + // <!-- No duplicate loading, although unnecessary. --> |
| + // <script src="http://example.org/scripts/b/d/bar.mojom.js"></script> |
| + // <script src="http://example.org/scripts/b/c/foo.mojom.js"></script> |
| + // |
| + // <!-- Load bar.mojom.js twice; should be avoided. --> |
| + // <script src="http://example.org/scripts/b/c/foo.mojom.js"></script> |
| + // <script src="http://example.org/scripts/b/d/bar.mojom.js"></script> |
| + auto_load_mojom_deps: true |
|
Ken Rockot(use gerrit already)
2017/03/26 18:27:11
not: shouldn't this be autoLoadMojomDeps?
yzshen1
2017/03/26 19:51:11
Yes! :) I am too used to C++...
|
| +}; |
| (function() { |
| var internal = mojoBindings.internal; |
| + var LoadState = { |
| + PENDING_LOAD: 1, |
| + LOADED: 2 |
| + }; |
| + |
| + var mojomRegistry = new Map(); |
| + |
| function exposeNamespace(namespace) { |
| var current = internal.global; |
| var parts = namespace.split('.'); |
| @@ -29,5 +72,40 @@ mojoBindings.internal.global = this; |
| return current; |
| } |
| + function isMojomPendingLoad(id) { |
| + return mojomRegistry.get(id) === LoadState.PENDING_LOAD; |
| + } |
| + |
| + function isMojomLoaded(id) { |
| + return mojomRegistry.get(id) === LoadState.LOADED; |
| + } |
| + |
| + function markMojomPendingLoad(id) { |
| + if (isMojomLoaded(id)) { |
| + throw new Error('The following mojom file has been loaded: ' + id); |
| + } |
| + |
| + mojomRegistry.set(id, LoadState.PENDING_LOAD); |
| + } |
| + |
| + function markMojomLoaded(id) { |
| + mojomRegistry.set(id, LoadState.LOADED); |
| + } |
| + |
| + function loadMojomIfNecessary(id, url) { |
| + if (mojomRegistry.has(id)) { |
| + return; |
| + } |
| + |
| + markMojomPendingLoad(id); |
| + internal.global.document.write('<script type="text/javascript" src="' + |
| + url + '"></script>'); |
| + } |
| + |
| internal.exposeNamespace = exposeNamespace; |
| + internal.isMojomPendingLoad = isMojomPendingLoad; |
| + internal.isMojomLoaded = isMojomLoaded; |
| + internal.markMojomPendingLoad = markMojomPendingLoad; |
| + internal.markMojomLoaded = markMojomLoaded; |
| + internal.loadMojomIfNecessary = loadMojomIfNecessary; |
| })(); |