001package Torello.Browser;
002
003import java.util.*;
004import javax.json.*;
005import javax.json.stream.*;
006import java.io.*;
007
008import java.lang.reflect.Method;
009import java.lang.reflect.Parameter;
010import java.util.function.Function;
011
012import Torello.Java.Additional.*;
013import Torello.Java.JSON.*;
014
015import static Torello.Java.JSON.JFlag.*;
016
017import Torello.Java.StrCmpr;
018import Torello.JavaDoc.StaticFunctional;
019import Torello.JavaDoc.JDHeaderBackgroundImg;
020import Torello.JavaDoc.Excuse;
021
022/**
023 * <SPAN CLASS=COPIEDJDK><B><CODE>[No Description Provided by Google]</CODE></B></SPAN>
024 * 
025 * <EMBED CLASS='external-html' DATA-FILE-ID=CODE_GEN_NOTE>
026 */
027@StaticFunctional(Excused={"counter"}, Excuses={Excuse.CONFIGURATION})
028@JDHeaderBackgroundImg(EmbedTagFileID="WOOD_PLANK_NOTE")
029public class Inspector
030{
031    // ********************************************************************************************
032    // ********************************************************************************************
033    // Class Header Stuff
034    // ********************************************************************************************
035    // ********************************************************************************************
036
037
038    // No Pubic Constructors
039    private Inspector () { }
040
041    // These two Vector's are used by all the "Methods" exported by this class.  java.lang.reflect
042    // is used to generate the JSON String's.  It saves thousands of lines of Auto-Generated Code.
043    private static final Map<String, Vector<String>>    parameterNames = new HashMap<>();
044    private static final Map<String, Vector<Class<?>>>  parameterTypes = new HashMap<>();
045
046    // Some Methods do not take any parameters - for instance all the "enable()" and "disable()"
047    // I simply could not get ride of RAW-TYPES and UNCHECKED warnings... so there are now,
048    // offically, two empty-vectors.  One for String's, and the other for Classes.
049
050    private static final Vector<String>     EMPTY_VEC_STR = new Vector<>();
051    private static final Vector<Class<?>>   EMPTY_VEC_CLASS = new Vector<>();
052
053    static
054    {
055        for (Method m : Inspector.class.getMethods())
056        {
057            // This doesn't work!  The parameter names are all "arg0" ... "argN"
058            // It works for java.lang.reflect.Field, BUT NOT java.lang.reflect.Parameter!
059            //
060            // Vector<String> parameterNamesList = new Vector<>(); -- NOPE!
061
062            Vector<Class<?>> parameterTypesList = new Vector<>();
063        
064            for (Parameter p : m.getParameters()) parameterTypesList.add(p.getType());
065
066            parameterTypes.put(
067                m.getName(),
068                (parameterTypesList.size() > 0) ? parameterTypesList : EMPTY_VEC_CLASS
069            );
070        }
071    }
072
073    static
074    {
075        Vector<String> v = null;
076
077        parameterNames.put("disable", EMPTY_VEC_STR);
078
079        parameterNames.put("enable", EMPTY_VEC_STR);
080    }
081
082
083    // ********************************************************************************************
084    // ********************************************************************************************
085    // Types - Static Inner Classes
086    // ********************************************************************************************
087    // ********************************************************************************************
088
089    /**
090     * Fired when debugging target has crashed
091     *
092     * <BR /><BR />This is Marker-Event.  Marker-Event's are Events that do not posses
093     * any data, fields or state.  When they are fired, only the event name is supplied.
094     */
095    public static class targetCrashed
096        extends BrowserEvent
097        implements java.io.Serializable
098    {
099        /** For Object Serialization.  java.io.Serializable */
100        protected static final long serialVersionUID = 1;
101    
102        public boolean[] optionals() { return new boolean[0]; }
103    
104        /** JSON Object Constructor */
105        public targetCrashed(JsonObject jo)
106        { super("Inspector", "targetCrashed", 0); }
107    
108        @Override
109        public String toString() { return "Inspector.targetCrashed Marker Event\n"; }
110    }
111    
112    /**
113     * Fired when debugging target has reloaded after crash
114     *
115     * <BR /><BR />This is Marker-Event.  Marker-Event's are Events that do not posses
116     * any data, fields or state.  When they are fired, only the event name is supplied.
117     */
118    public static class targetReloadedAfterCrash
119        extends BrowserEvent
120        implements java.io.Serializable
121    {
122        /** For Object Serialization.  java.io.Serializable */
123        protected static final long serialVersionUID = 1;
124    
125        public boolean[] optionals() { return new boolean[0]; }
126    
127        /** JSON Object Constructor */
128        public targetReloadedAfterCrash(JsonObject jo)
129        { super("Inspector", "targetReloadedAfterCrash", 0); }
130    
131        @Override
132        public String toString() { return "Inspector.targetReloadedAfterCrash Marker Event\n"; }
133    }
134    
135    /** Fired when remote debugging connection is about to be terminated. Contains detach reason. */
136    public static class detached
137        extends BrowserEvent
138        implements java.io.Serializable
139    {
140        /** For Object Serialization.  java.io.Serializable */
141        protected static final long serialVersionUID = 1;
142        
143        public boolean[] optionals()
144        { return new boolean[] { false, }; }
145        
146        /** The reason why connection has been terminated. */
147        public final String reason;
148        
149        /**
150         * Constructor
151         *
152         * @param reason The reason why connection has been terminated.
153         */
154        public detached(String reason)
155        {
156            super("Inspector", "detached", 1);
157            
158            // Exception-Check(s) to ensure that if any parameters which are not declared as
159            // 'Optional', but have a 'null' value anyway, that a NullPointerException shall throw.
160            
161            if (reason == null) THROWS.throwNPE("reason");
162            
163            this.reason  = reason;
164        }
165        
166        /**
167         * JSON Object Constructor
168         * @param jo A Json-Object having data about an instance of {@code 'detached'}.
169         */
170        public detached (JsonObject jo)
171        {
172            super("Inspector", "detached", 1);
173        
174            this.reason  = ReadJSON.getString(jo, "reason", false, true);
175        }
176        
177        
178        /** Checks whether {@code 'this'} equals an input Java-{@code Object} */
179        public boolean equals(Object other)
180        {
181            if (this == other)                       return true;
182            if (other == null)                       return false;
183            if (other.getClass() != this.getClass()) return false;
184        
185            detached o = (detached) other;
186        
187            return
188                    Objects.equals(this.reason, o.reason);
189        }
190        
191        /** Generates a Hash-Code for {@code 'this'} instance */
192        public int hashCode()
193        {
194            return
195                    Objects.hashCode(this.reason);
196        }
197    }
198    
199    
200    // Counter for keeping the WebSocket Request ID's distinct.
201    private static int counter = 1;
202    
203    /**
204     * Disables inspector domain notifications.
205     * 
206     * @return An instance of <CODE>{@link Script}&lt;String, {@link JsonObject},
207     * {@link Ret0}&gt;</CODE>
208     *
209     * <BR /><BR />This {@code Script} instance must be <B STYLE='color:red'>executed</B> before the
210     * browser receives the invocation-request.
211     *
212     * <BR /><BR />This Browser-Function <I>does not have</I> a return-value.  You may choose to
213     * <B STYLE='color: red'>await</B> the {@link Promise}{@code <JsonObject,} {@link Ret0}
214     * {@code >} to ensure the Browser Function has run to completion.
215     */
216    public static Script<String, JsonObject, Ret0> disable()
217    {
218        final int          webSocketID = 26000000 + counter++;
219        final boolean[]    optionals   = new boolean[0];
220        
221        // Convert Method Parameters into JSON.  Build the JSON Request-Object (as a String)
222        String requestJSON = WriteJSON.get(
223            parameterTypes.get("disable"),
224            parameterNames.get("disable"),
225            optionals, webSocketID,
226            "Inspector.disable"
227        );
228        
229        // This Remote Command does not have a Return-Value.
230        return new Script<>
231            (webSocketID, requestJSON, VOID_RETURN.NoReturnValues);
232    }
233    
234    /**
235     * Enables inspector domain notifications.
236     * 
237     * @return An instance of <CODE>{@link Script}&lt;String, {@link JsonObject},
238     * {@link Ret0}&gt;</CODE>
239     *
240     * <BR /><BR />This {@code Script} instance must be <B STYLE='color:red'>executed</B> before the
241     * browser receives the invocation-request.
242     *
243     * <BR /><BR />This Browser-Function <I>does not have</I> a return-value.  You may choose to
244     * <B STYLE='color: red'>await</B> the {@link Promise}{@code <JsonObject,} {@link Ret0}
245     * {@code >} to ensure the Browser Function has run to completion.
246     */
247    public static Script<String, JsonObject, Ret0> enable()
248    {
249        final int          webSocketID = 26001000 + counter++;
250        final boolean[]    optionals   = new boolean[0];
251        
252        // Convert Method Parameters into JSON.  Build the JSON Request-Object (as a String)
253        String requestJSON = WriteJSON.get(
254            parameterTypes.get("enable"),
255            parameterNames.get("enable"),
256            optionals, webSocketID,
257            "Inspector.enable"
258        );
259        
260        // This Remote Command does not have a Return-Value.
261        return new Script<>
262            (webSocketID, requestJSON, VOID_RETURN.NoReturnValues);
263    }
264    
265}