Skip to content

Instantly share code, notes, and snippets.

@my2iu

my2iu/*.gwt.xml Secret

Created October 16, 2020 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save my2iu/b6da78680a86399c4ab7a53c6237e440 to your computer and use it in GitHub Desktop.
Save my2iu/b6da78680a86399c4ab7a53c6237e440 to your computer and use it in GitHub Desktop.
Modification of cross-site linker for simple web workers
<define-linker name="webworker" class="com.wobastic.gwt.webworker.linker.WebWorkerLinker" />
<add-linker name="webworker"/>
This should be an empty file
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.wobastic.gwt.webworker.linker;
import com.google.gwt.core.ext.LinkerContext;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.linker.LinkerOrder;
import com.google.gwt.core.ext.linker.LinkerOrder.Order;
import com.google.gwt.core.ext.linker.Shardable;
import com.google.gwt.core.linker.CrossSiteIframeLinker;
import com.google.gwt.dev.About;
import com.google.gwt.dev.util.DefaultTextOutput;
import com.google.gwt.dev.util.TextOutput;
/**
* This linker uses an iframe to hold the code and a script tag to download the
* code. It can download code cross-site, because it uses a script tag to
* download it and because it never uses XHR. The iframe, meanwhile, makes it
* trivial to install additional code as the app runs.
*/
@LinkerOrder(Order.PRIMARY)
@Shardable
public class WebWorkerLinker extends CrossSiteIframeLinker {
@Override
public String getDescription() {
return "WebWorker";
}
/**
* Returns the name of the {@code ComputeScriptBase} script. By default,
* returns {@code "com/google/gwt/core/ext/linker/impl/computeScriptBase.js"}.
*
* @param context a LinkerContext
*/
protected String getJsComputeScriptBase(LinkerContext context) {
return getStringConfigurationProperty(context, "computeScriptBaseJs",
"com/wobastic/gwt/webworker/linker/EmptyTemplate.js");
}
/**
* Returns the name of the {@code JsInstallLocation} script. By default,
* returns {@code "com/google/gwt/core/ext/linker/impl/installLocationIframe.js"}.
*
* @param context a LinkerContext
*/
protected String getJsInstallLocation(LinkerContext context) {
return "com/wobastic/gwt/webworker/linker/EmptyTemplate.js";
}
/**
* Returns the name of the {@code JsInstallScript} script. The default is chosen
* based on the value of {@link #shouldInstallCode}.
*
* <p> If you override this, verify that {@link #shouldInstallCode} is
* set consistently or fragment loading won't work.
*/
protected String getJsInstallScript(LinkerContext context) {
return "com/wobastic/gwt/webworker/linker/EmptyTemplate.js";
}
/**
* Returns the name of the {@code JsLoadExternalStylesheets} script. By default,
* returns {@code "com/google/gwt/core/ext/linker/impl/loadExternalStylesheets.js"}.
*
* @param context a LinkerContext
*/
protected String getJsLoadExternalStylesheets(LinkerContext context) {
return "com/wobastic/gwt/webworker/linker/EmptyTemplate.js";
}
/**
* Returns the name of the {@code JsProcessMetas} script. By default,
* returns {@code "com/google/gwt/core/ext/linker/impl/processMetas.js"}.
*
* @param context a LinkerContext
*/
protected String getJsProcessMetas(LinkerContext context) {
return "com/wobastic/gwt/webworker/linker/EmptyTemplate.js";
}
@Override
protected String getModulePrefix(TreeLogger logger, LinkerContext context, String strongName)
throws UnableToCompleteException {
TextOutput out = new DefaultTextOutput(context.isOutputCompact());
// Note: this code is included in both the primary fragment and devmode.js
// $wnd is the main window that the GWT code will affect and also the
// location where the bootstrap function was defined. In iframe-based linkers,
// $wnd is set to window.parent. Usually, in others, $wnd = window.
// By default, $wnd is not set when the module starts, but a replacement for
// installLocationIframe.js may set it.
out.print("var $wnd = $wnd || self;");
out.newlineOpt();
out.print("var __gwtModuleFunction = $wnd." + context.getModuleFunctionName() + ";");
out.newlineOpt();
out.print("var $sendStats = __gwtModuleFunction.__sendStats;");
out.newlineOpt();
out.print("$sendStats('moduleStartup', 'moduleEvalStart');");
out.newlineOpt();
out.print("var $gwt_version = \"" + About.getGwtVersionNum() + "\";");
out.newlineOpt();
out.print("var $strongName = '" + strongName + "';");
out.newlineOpt();
out.print("var $gwt = {};");
out.newlineOpt();
//out.print("var $doc = $wnd.document;");
//out.newlineOpt();
out.print("var $moduleName, $moduleBase;");
out.newlineOpt();
// The functions for runAsync are set up in the bootstrap script so they
// can be overridden in the same way as other bootstrap code is, however
// they will be called from, and expected to run in the scope of the GWT code
// (usually an iframe) so, here we set up those pointers.
out.print("function __gwtStartLoadingFragment(frag) {");
out.newlineOpt();
String fragDir = getFragmentSubdir(logger, context) + '/';
out.print("var fragFile = '" + fragDir + "' + $strongName + '/' + frag + '" + FRAGMENT_EXTENSION + "';");
out.newlineOpt();
out.print("return __gwtModuleFunction.__startLoadingFragment(fragFile);");
out.newlineOpt();
out.print("}");
out.newlineOpt();
out.print("function __gwtInstallCode(code) {return __gwtModuleFunction.__installRunAsyncCode(code);}");
out.newlineOpt();
// The functions for property access are set up in the bootstrap script however
// they will be called from, and expected to run in the scope of the GWT code
// (usually an iframe) so, here we set up those pointers.
out.print("function __gwt_isKnownPropertyValue(propName, propValue) {");
out.newlineOpt();
out.print("return __gwtModuleFunction.__gwt_isKnownPropertyValue(propName, propValue);");
out.newlineOpt();
out.print("}");
out.newlineOpt();
out.print("function __gwt_getMetaProperty(name) {");
out.newlineOpt();
out.print("return __gwtModuleFunction.__gwt_getMetaProperty(name);");
out.newlineOpt();
out.print("}");
out.newlineOpt();
// Even though we call the $sendStats function in the code written in this
// linker, some of the compilation code still needs the $stats and
// $sessionId
// variables to be available.
out.print("var $stats = $wnd.__gwtStatsEvent ? function(a) {");
out.newlineOpt();
out.print("return $wnd.__gwtStatsEvent && $wnd.__gwtStatsEvent(a);");
out.newlineOpt();
out.print("} : null;");
out.newlineOpt();
out.print("var $sessionId = $wnd.__gwtStatsSessionId ? $wnd.__gwtStatsSessionId : null;");
out.newlineOpt();
return out.toString();
}
@Override
protected String getSelectionScriptTemplate(TreeLogger logger, LinkerContext context) {
return "com/wobastic/gwt/webworker/linker/WebWorkerTemplate.js";
}
/**
* Determines the strategy for installing JavaScript code into the iframe.
* If set to false, a &lt;script&gt; tag pointing to the js file is added
* directly to the iframe. Otherwise, GWT downloads the JavaScript code
* as a list of strings and then adds it to the iframe.
*/
protected boolean shouldInstallCode(LinkerContext context) {
return false;
}
/**
* Returns whether to use "self" for $wnd and $doc references. Defaults to false.
* Useful for worker threads.
*/
protected boolean shouldUseSelfForWindowAndDocument(LinkerContext context) {
return true;
}
}
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
__BEGIN_TRY_BLOCK__
function __MODULE_FUNC__() {
var $wnd = __WINDOW_DEF__;
//var $doc = __DOCUMENT_DEF__;
sendStats('bootstrap', 'begin');
/****************************************************************************
* Internal Helper Functions
***************************************************************************/
function isHostedMode() {
var query = $wnd.location.search;
return ((query.indexOf('gwt.codesvr.__MODULE_NAME__=') != -1) ||
(query.indexOf('gwt.codesvr=') != -1));
}
// Helper function to send statistics to the __gwtStatsEvent function if it
// exists.
function sendStats(evtGroupString, typeString) {
if ($wnd.__gwtStatsEvent) {
$wnd.__gwtStatsEvent({
moduleName: '__MODULE_NAME__',
sessionId: $wnd.__gwtStatsSessionId,
subSystem: 'startup',
evtGroup: evtGroupString,
millis:(new Date()).getTime(),
type: typeString,
});
}
}
/****************************************************************************
* Exposed Functions and Variables
***************************************************************************/
// These are set by various parts of the bootstrapping code, but they always
// need to exist, so give them all default values here.
// Exposed for the convenience of the devmode.js and md5.js files
__MODULE_FUNC__.__sendStats = sendStats;
// Exposed for the call made to gwtOnLoad. Some are not figured out yet, so
// assign them later, once the values are known.
__MODULE_FUNC__.__moduleName = '__MODULE_NAME__';
__MODULE_FUNC__.__errFn = null;
__MODULE_FUNC__.__moduleBase = 'DUMMY';
__MODULE_FUNC__.__softPermutationId = 0;
// Exposed for devmode.js
__MODULE_FUNC__.__computePropValue = null;
// Exposed for super dev mode
__MODULE_FUNC__.__getPropMap = null;
// Exposed for runAsync
__MODULE_FUNC__.__installRunAsyncCode = function() {};
__MODULE_FUNC__.__gwtStartLoadingFragment = function() { return null; };
// Exposed for property provider code
__MODULE_FUNC__.__gwt_isKnownPropertyValue = function() { return false; };
__MODULE_FUNC__.__gwt_getMetaProperty = function() { return null; };
// Exposed for permutations code
var __propertyErrorFunction = null;
// Set up our entry in the page-wide registry of active modules.
// It must be set up before calling computeScriptBase() and
// getCompiledCodeFilename().
var activeModules =
($wnd.__gwt_activeModules = ($wnd.__gwt_activeModules || {}));
activeModules["__MODULE_NAME__"] = {moduleName: "__MODULE_NAME__"};
__MODULE_FUNC__.__moduleStartupDone = function(permProps) {
// Make embedded properties available to Super Dev Mode.
// (They override any properties already exported.)
var oldBindings = activeModules["__MODULE_NAME__"].bindings;
activeModules["__MODULE_NAME__"].bindings = function() {
var props = oldBindings ? oldBindings() : {};
var embeddedProps = permProps[__MODULE_FUNC__.__softPermutationId];
for (var i = 0; i < embeddedProps.length; i++) {
var pair = embeddedProps[i];
props[pair[0]] = pair[1];
}
return props;
};
};
/****************************************************************************
* Internal Helper functions that have been broken out into their own .js
* files for readability and for easy sharing between linkers. The linker
* code will inject these functions in these placeholders.
***************************************************************************/
// Provides getInstallLocationDoc() function.
__INSTALL_LOCATION__
// Provides the installScript() function.
__INSTALL_SCRIPT__
function installScript(filename) {
sendStats('moduleStartup', 'moduleRequested');
importScripts(filename);
sendStats('moduleStartup', 'scriptImported');
}
// Sets the __MODULE_FUNC__.__installRunAsyncCode and
// __MODULE_FUNC__.__startLoadingFragment functions
__RUN_ASYNC__
// Provides the processMetas() function which sets the __gwt_getMetaProperty
// __propertyErrorFunction and __MODULE_FUNC__.__errFn variables if needed
__PROCESS_METAS__
// Provides the computeScriptBase() function
__COMPUTE_SCRIPT_BASE__
function computeScriptBase() {
function getDirectoryOfFile(path){
var hashIndex = path.lastIndexOf('#');
if (hashIndex == -1) {
hashIndex = path.length;
}
var queryIndex = path.indexOf('?');
if (queryIndex == -1) {
queryIndex = path.length;
}
var slashIndex = path.lastIndexOf('/', Math.min(queryIndex, hashIndex));
return slashIndex >= 0?path.substring(0, slashIndex + 1):'';
}
function isLocationOk(){
var loc = $wnd.location;
return loc.href == loc.protocol + '//' + loc.host + loc.pathname + loc.search + loc.hash;
}
var tempBase = getDirectoryOfFile($wnd.location.href);
return tempBase;
}
// Provides the computeUrlForResource() function
__COMPUTE_URL_FOR_RESOURCE__
// Provides the getCompiledCodeFilename() function which sets the following
// variables if needed:
// __gwt_isKnownPropertyValue
// __MODULE_FUNC__.__computePropValue
// __MODULE_FUNC__.__getPropMap
// __MODULE_FUNC__.__softPermutationId
__PERMUTATIONS__
// Provides the loadExternalStylesheets() function
__LOAD_STYLESHEETS__
/****************************************************************************
* Bootstrap startup code
***************************************************************************/
// Must be called before computeScriptBase() and getCompiledFilename()
//processMetas();
// Must be set before getCompiledFilename() is called
__MODULE_FUNC__.__moduleBase = computeScriptBase();
activeModules["__MODULE_NAME__"].moduleBase = __MODULE_FUNC__.__moduleBase;
// Must be done right before the "bootstrap" "end" stat is sent
var filename = getCompiledCodeFilename();
__DEV_MODE_REDIRECT_HOOK__
//loadExternalStylesheets();
sendStats('bootstrap', 'end');
installScript(filename);
return true; // success
}
__MODULE_FUNC__.succeeded = __MODULE_FUNC__();
__END_TRY_BLOCK_AND_START_CATCH__
__MODULE_FUNC_ERROR_CATCH__
__END_CATCH_BLOCK__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment