Class HtmlUnitContextFactory
java.lang.Object
org.htmlunit.corejs.javascript.ContextFactory
org.htmlunit.javascript.HtmlUnitContextFactory
public class HtmlUnitContextFactory
extends org.htmlunit.corejs.javascript.ContextFactory
ContextFactory that supports termination of scripts if they exceed a timeout. Based on example from
ContextFactory.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classprivate classCustom context to store execution time and handle timeouts.Nested classes/interfaces inherited from class org.htmlunit.corejs.javascript.ContextFactory
org.htmlunit.corejs.javascript.ContextFactory.GlobalSetter, org.htmlunit.corejs.javascript.ContextFactory.Listener -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final BrowserVersionprivate org.htmlunit.corejs.javascript.debug.Debuggerprivate booleanprivate static final intprivate longprivate final WebClient -
Constructor Summary
ConstructorsConstructorDescriptionHtmlUnitContextFactory(WebClient webClient) Creates a new instance of HtmlUnitContextFactory. -
Method Summary
Modifier and TypeMethodDescriptionfinal <T> TcallSecured(org.htmlunit.corejs.javascript.ContextAction<T> action, HtmlPage page) Same asContextFactoryContextFactory.call(ContextAction)but with handling of some exceptions.protected ObjectdoTopCall(org.htmlunit.corejs.javascript.Callable callable, org.htmlunit.corejs.javascript.Context cx, org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args) org.htmlunit.corejs.javascript.debug.DebuggerReturns the JavaScript debugger to use to receive JavaScript execution debugging information.longReturns the number of milliseconds a script is allowed to execute before being terminated.protected booleanhasFeature(org.htmlunit.corejs.javascript.Context cx, int featureIndex) booleanIndicates code of calls likenew Function("...some code...")should be deminified to be more readable when using the debugger.protected org.htmlunit.corejs.javascript.Contextprotected voidobserveInstructionCount(org.htmlunit.corejs.javascript.Context cx, int instructionCount) Run-time calls this when instruction counting is enabled and the counter reaches limit set by setInstructionObserverThreshold().protected StringpreProcess(HtmlPage htmlPage, String sourceCode, String sourceName, int lineNumber, HtmlElement htmlElement) Pre process the specified source code in the context of the given page using the processor specified in theWebClient.voidsetDebugger(org.htmlunit.corejs.javascript.debug.Debugger debugger) Sets the JavaScript debugger to use to receive JavaScript execution debugging information.voidsetDeminifyFunctionCode(boolean deminify) Configures if the code ofnew Function("...some code...")should be deminified to be more readable when using the debugger.voidsetTimeout(long timeout) Sets the number of milliseconds a script is allowed to execute before being terminated.Methods inherited from class org.htmlunit.corejs.javascript.ContextFactory
addListener, call, checkNotSealed, createClassLoader, enter, enterContext, enterContext, exit, getApplicationClassLoader, getE4xImplementationFactory, getGlobal, getGlobalSetter, hasExplicitGlobal, initApplicationClassLoader, initGlobal, isSealed, onContextCreated, onContextReleased, removeListener, seal
-
Field Details
-
INSTRUCTION_COUNT_THRESHOLD
private static final int INSTRUCTION_COUNT_THRESHOLD- See Also:
-
webClient_
-
browserVersion_
-
timeout_
private long timeout_ -
debugger_
private org.htmlunit.corejs.javascript.debug.Debugger debugger_ -
deminifyFunctionCode_
private boolean deminifyFunctionCode_
-
-
Constructor Details
-
HtmlUnitContextFactory
Creates a new instance of HtmlUnitContextFactory.- Parameters:
webClient- the web client using this factory
-
-
Method Details
-
setTimeout
public void setTimeout(long timeout) Sets the number of milliseconds a script is allowed to execute before being terminated. A value of 0 or less means no timeout.- Parameters:
timeout- the timeout value
-
getTimeout
public long getTimeout()Returns the number of milliseconds a script is allowed to execute before being terminated. A value of 0 or less means no timeout.- Returns:
- the timeout value (default value is
0)
-
setDebugger
public void setDebugger(org.htmlunit.corejs.javascript.debug.Debugger debugger) Sets the JavaScript debugger to use to receive JavaScript execution debugging information. The HtmlUnit default implementation (DebuggerImpl,DebugFrameImpl) may be used, or a custom debugger may be used instead. By default, no debugger is used.- Parameters:
debugger- the JavaScript debugger to use (may benull)
-
getDebugger
public org.htmlunit.corejs.javascript.debug.Debugger getDebugger()Returns the JavaScript debugger to use to receive JavaScript execution debugging information. By default, no debugger is used, and this method returnsnull.- Returns:
- the JavaScript debugger to use to receive JavaScript execution debugging information
-
setDeminifyFunctionCode
public void setDeminifyFunctionCode(boolean deminify) Configures if the code ofnew Function("...some code...")should be deminified to be more readable when using the debugger. This is a small performance cost.- Parameters:
deminify- the new value
-
isDeminifyFunctionCode
public boolean isDeminifyFunctionCode()Indicates code of calls likenew Function("...some code...")should be deminified to be more readable when using the debugger.- Returns:
- the de-minify status
-
preProcess
protected String preProcess(HtmlPage htmlPage, String sourceCode, String sourceName, int lineNumber, HtmlElement htmlElement) Pre process the specified source code in the context of the given page using the processor specified in theWebClient. This method delegates to the pre processor handler specified in theWebClient. If no pre processor handler is defined, the original source code is returned unchanged.- Parameters:
htmlPage- the pagesourceCode- the code to processsourceName- a name for the chunk of code (used in error messages)lineNumber- the line number of the source codehtmlElement- the HTML element that will act as the context- Returns:
- the source code after being pre processed
- See Also:
-
makeContext
protected org.htmlunit.corejs.javascript.Context makeContext()- Overrides:
makeContextin classorg.htmlunit.corejs.javascript.ContextFactory
-
observeInstructionCount
protected void observeInstructionCount(org.htmlunit.corejs.javascript.Context cx, int instructionCount) Run-time calls this when instruction counting is enabled and the counter reaches limit set by setInstructionObserverThreshold(). A script can be terminated by throwing an Error instance here.- Overrides:
observeInstructionCountin classorg.htmlunit.corejs.javascript.ContextFactory- Parameters:
cx- the context calling usinstructionCount- amount of script instruction executed since last call to observeInstructionCount
-
doTopCall
protected Object doTopCall(org.htmlunit.corejs.javascript.Callable callable, org.htmlunit.corejs.javascript.Context cx, org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args) - Overrides:
doTopCallin classorg.htmlunit.corejs.javascript.ContextFactory
-
callSecured
public final <T> T callSecured(org.htmlunit.corejs.javascript.ContextAction<T> action, HtmlPage page) Same asContextFactoryContextFactory.call(ContextAction)but with handling of some exceptions.- Type Parameters:
T- return type of the action- Parameters:
action- the contextActionpage- the page- Returns:
- the result of the call
-
hasFeature
protected boolean hasFeature(org.htmlunit.corejs.javascript.Context cx, int featureIndex) - Overrides:
hasFeaturein classorg.htmlunit.corejs.javascript.ContextFactory
-