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) BRDPC.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 (other == null)                       return false;
182            if (other.getClass() != this.getClass()) return false;
183        
184            detached o = (detached) other;
185        
186            return
187                    Objects.equals(this.reason, o.reason);
188        }
189        
190        /** Generates a Hash-Code for {@code 'this'} instance */
191        public int hashCode()
192        {
193            return
194                    Objects.hashCode(this.reason);
195        }
196    }
197    
198    
199    // Counter for keeping the WebSocket Request ID's distinct.
200    private static int counter = 1;
201    
202    /**
203     * Disables inspector domain notifications.
204     * 
205     * @return An instance of <CODE>{@link Script}&lt;String, {@link JsonObject},
206     * {@link Ret0}&gt;</CODE>
207     *
208     * <BR /><BR />This {@code Script} instance must be <B STYLE='color:red'>executed</B> before the
209     * browser receives the invocation-request.
210     *
211     * <BR /><BR />This Browser-Function <I>does not have</I> a return-value.  You may choose to
212     * <B STYLE='color: red'>await</B> the {@link Promise}{@code <JsonObject,} {@link Ret0}
213     * {@code >} to ensure the Browser Function has run to completion.
214     */
215    public static Script<String, JsonObject, Ret0> disable()
216    {
217        final int          webSocketID = 26000000 + counter++;
218        final boolean[]    optionals   = new boolean[0];
219        
220        // Convert Method Parameters into JSON.  Build the JSON Request-Object (as a String)
221        String requestJSON = WriteJSON.get(
222            parameterTypes.get("disable"),
223            parameterNames.get("disable"),
224            optionals, webSocketID,
225            "Inspector.disable"
226        );
227        
228        // This Remote Command does not have a Return-Value.
229        return new Script<>
230            (BRDPC.defaultSender, webSocketID, requestJSON, BRDPC.NoReturnValues);
231    }
232    
233    /**
234     * Enables inspector domain notifications.
235     * 
236     * @return An instance of <CODE>{@link Script}&lt;String, {@link JsonObject},
237     * {@link Ret0}&gt;</CODE>
238     *
239     * <BR /><BR />This {@code Script} instance must be <B STYLE='color:red'>executed</B> before the
240     * browser receives the invocation-request.
241     *
242     * <BR /><BR />This Browser-Function <I>does not have</I> a return-value.  You may choose to
243     * <B STYLE='color: red'>await</B> the {@link Promise}{@code <JsonObject,} {@link Ret0}
244     * {@code >} to ensure the Browser Function has run to completion.
245     */
246    public static Script<String, JsonObject, Ret0> enable()
247    {
248        final int          webSocketID = 26001000 + counter++;
249        final boolean[]    optionals   = new boolean[0];
250        
251        // Convert Method Parameters into JSON.  Build the JSON Request-Object (as a String)
252        String requestJSON = WriteJSON.get(
253            parameterTypes.get("enable"),
254            parameterNames.get("enable"),
255            optionals, webSocketID,
256            "Inspector.enable"
257        );
258        
259        // This Remote Command does not have a Return-Value.
260        return new Script<>
261            (BRDPC.defaultSender, webSocketID, requestJSON, BRDPC.NoReturnValues);
262    }
263    
264}