Class 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 Classes 
      Modifier and Type Class Description
      private static class  HtmlUnitContextFactory.HtmlUnitErrorReporter  
      private class  HtmlUnitContextFactory.TimeoutContext
      Custom 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
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T> T callSecured​(org.htmlunit.corejs.javascript.ContextAction<T> action, HtmlPage page)
      Same as ContextFactoryContextFactory.call(ContextAction) but with handling of some exceptions.
      protected java.lang.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, java.lang.Object[] args)
      org.htmlunit.corejs.javascript.debug.Debugger getDebugger()
      Returns the JavaScript debugger to use to receive JavaScript execution debugging information.
      long getTimeout()
      Returns the number of milliseconds a script is allowed to execute before being terminated.
      protected boolean hasFeature​(org.htmlunit.corejs.javascript.Context cx, int featureIndex)
      boolean isDeminifyFunctionCode()
      Indicates code of calls like new Function("...some code...") should be deminified to be more readable when using the debugger.
      protected org.htmlunit.corejs.javascript.Context makeContext()
      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().
      protected java.lang.String preProcess​(HtmlPage htmlPage, java.lang.String sourceCode, java.lang.String sourceName, int lineNumber, HtmlElement htmlElement)
      Pre process the specified source code in the context of the given page using the processor specified in the WebClient.
      void setDebugger​(org.htmlunit.corejs.javascript.debug.Debugger debugger)
      Sets the JavaScript debugger to use to receive JavaScript execution debugging information.
      void setDeminifyFunctionCode​(boolean deminify)
      Configures if the code of new Function("...some code...") should be deminified to be more readable when using the debugger.
      void setTimeout​(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
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • INSTRUCTION_COUNT_THRESHOLD

        private static final int INSTRUCTION_COUNT_THRESHOLD
        See Also:
        Constant Field Values
      • webClient_

        private final WebClient webClient_
      • timeout_

        private long timeout_
      • debugger_

        private org.htmlunit.corejs.javascript.debug.Debugger debugger_
      • deminifyFunctionCode_

        private boolean deminifyFunctionCode_
    • Constructor Detail

      • HtmlUnitContextFactory

        public HtmlUnitContextFactory​(WebClient webClient)
        Creates a new instance of HtmlUnitContextFactory.
        Parameters:
        webClient - the web client using this factory
    • Method Detail

      • 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 be null)
      • 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 returns null.
        Returns:
        the JavaScript debugger to use to receive JavaScript execution debugging information
      • setDeminifyFunctionCode

        public void setDeminifyFunctionCode​(boolean deminify)
        Configures if the code of new 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 like new Function("...some code...") should be deminified to be more readable when using the debugger.
        Returns:
        the de-minify status
      • preProcess

        protected java.lang.String preProcess​(HtmlPage htmlPage,
                                              java.lang.String sourceCode,
                                              java.lang.String sourceName,
                                              int lineNumber,
                                              HtmlElement htmlElement)
        Pre process the specified source code in the context of the given page using the processor specified in the WebClient. This method delegates to the pre processor handler specified in the WebClient. If no pre processor handler is defined, the original source code is returned unchanged.
        Parameters:
        htmlPage - the page
        sourceCode - the code to process
        sourceName - a name for the chunk of code (used in error messages)
        lineNumber - the line number of the source code
        htmlElement - the HTML element that will act as the context
        Returns:
        the source code after being pre processed
        See Also:
        ScriptPreProcessor
      • makeContext

        protected org.htmlunit.corejs.javascript.Context makeContext()
        Overrides:
        makeContext in class org.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:
        observeInstructionCount in class org.htmlunit.corejs.javascript.ContextFactory
        Parameters:
        cx - the context calling us
        instructionCount - amount of script instruction executed since last call to observeInstructionCount
      • doTopCall

        protected java.lang.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,
                                             java.lang.Object[] args)
        Overrides:
        doTopCall in class org.htmlunit.corejs.javascript.ContextFactory
      • callSecured

        public final <T> T callSecured​(org.htmlunit.corejs.javascript.ContextAction<T> action,
                                       HtmlPage page)
        Same as ContextFactoryContextFactory.call(ContextAction) but with handling of some exceptions.
        Type Parameters:
        T - return type of the action
        Parameters:
        action - the contextAction
        page - the page
        Returns:
        the result of the call
      • hasFeature

        protected boolean hasFeature​(org.htmlunit.corejs.javascript.Context cx,
                                     int featureIndex)
        Overrides:
        hasFeature in class org.htmlunit.corejs.javascript.ContextFactory