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