diff options
author | Hidehiko Abe <hidehiko@google.com> | 2018-04-24 01:37:19 +0900 |
---|---|---|
committer | Hidehiko Abe <hidehiko@google.com> | 2018-04-24 03:09:01 +0900 |
commit | b268b43ac6fdbc4f3a2ed1429b99ace424906090 (patch) | |
tree | bd2d04362f66c36d4279f7a9735ba21ea3a2a021 /mojo/public/js/new_bindings/base.js | |
parent | 4a54b98aa445f521c6945e4f4a1e0ea788fa7da8 (diff) | |
download | libchrome-b268b43ac6fdbc4f3a2ed1429b99ace424906090.tar.gz |
Migrate libmojo repository into libchrome, part 2.
This CL moves following files.
- .gitignore
- Android.bp is merged into libchrome's Android.bp.
- base/android/*
- build/* except build_config.h which is exactly same with libchrome's.
- ipc/*
- mojo/* except mojo/public/tools/bindings/generators/__init__.py
which is unused and not in chrome repository.
- soong/* into libchrome_tools/
- third_party/{catapult,jinja2,markupsafe,ply}/*
- ui/gfx/{geometry,range}/mojo/*
Then, update several paths/build rules to be adapted.
Bug: 73606903
Test: Built locally. Ran on DUT.
Change-Id: I2a532a42aa68dcb215dbd71d8673192311509726
Diffstat (limited to 'mojo/public/js/new_bindings/base.js')
-rw-r--r-- | mojo/public/js/new_bindings/base.js | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/mojo/public/js/new_bindings/base.js b/mojo/public/js/new_bindings/base.js new file mode 100644 index 0000000000..db72d489db --- /dev/null +++ b/mojo/public/js/new_bindings/base.js @@ -0,0 +1,111 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +'use strict'; + +if (mojo && mojo.internal) { + throw new Error('The Mojo bindings library has been initialized.'); +} + +var mojo = mojo || {}; +mojo.internal = {}; +mojo.internal.global = this; +mojo.config = { + // Whether to automatically load mojom dependencies. + // For example, if foo.mojom imports bar.mojom, |autoLoadMojomDeps| set 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 |autoLoadMojomDeps| 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 |autoLoadMojomDeps| 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> + autoLoadMojomDeps: true +}; + +(function() { + var internal = mojo.internal; + + var LoadState = { + PENDING_LOAD: 1, + LOADED: 2 + }; + + var mojomRegistry = new Map(); + + function exposeNamespace(namespace) { + var current = internal.global; + var parts = namespace.split('.'); + + for (var part; parts.length && (part = parts.shift());) { + if (!current[part]) { + current[part] = {}; + } + current = current[part]; + } + + 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; +})(); |