<commit_before>/* See license.txt for terms of usage */

// ************************************************************************************************
// Module

define([], function factoryBrowser(require){

// ************************************************************************************************
// Browser

/**
 * Proxy to a debuggable web browser. A browser may be remote and contain one or more
 * JavaScript execution contexts. Each JavaScript execution context may contain one or
 * more compilation units. A browser provides notification to registered listeners describing
 * events that occur in the browser.
 *
 * @constructor
 * @type Browser
 * @return a new Browser
 * @version 1.0
 */
function Browser()
{
    this.contexts = {}; // map of contexts, indexed by context ID
    this.activeContext = null;
    this.handlers = {}; // map of event types to array of handler functions
    this.EVENT_TYPES = ["onBreak", "onConsoleDebug", "onConsoleError", "onConsoleInfo",
        "onConsoleLog", "onConsoleWarn", "onContextCreated", "onContextDestroyed",
        "onContextChanged", "onContextLoaded", "onInspectNode", "onResume", "onScript",
        "onSuspend", "onToggleBreakpoint", "onBreakpointError", "onDisconnect"];
    this.connected = false;
}

// ************************************************************************************************
// API
/*
 * Testing and sanity: clearAllBreakpoints
 */
Browser.prototype.clearAllBreakpoints = function()
{
    Firebug.Debugger.clearAllBreakpoints();
}


/**
 * Returns current status of tools
 *
 * @function
 * @returns  an array of Tools, an object with toolName and enabled boolean
 *
 */
Browser.prototype.getTools = function()
{
    return [];
};

/**
 * Returns the {@link BrowserContext} with the specified id or <code>null</code>
 * if none.
 *
 * @function
 * @param id identifier of an {@link BrowserContext}
 * @returns the {@link BrowserContext} with the specified id or <code>null</code>
 *
 */
Browser.prototype.getBrowserContext = function(id)
{
    var context = this.contexts[id];
    if (context)
        return context;
    return null;
};

/**
 * Returns the root contexts being browsed. A {@link BrowserContext} represents the
 * content that has been served up and is being rendered for a location (URL) that
 * has been navigated to.
 * <p>
 * This function does not require communication with the remote browser.
 * </p>
 * @function
 * @returns an array of {@link BrowserContext}'s
 */
Browser.prototype.getBrowserContexts = function()
{
    var knownContexts = [];
    for (var id in this.contexts)
        knownContexts.push(this.contexts[id]);
    return knownContexts;
};

/**
 * Returns the {@link BrowserContext} that currently has focus in the browser
 * or <code>null</code> if none.
 *
 * @function
 * @returns the {@link BrowserContext} that has focus or <code>null</code>
 */
Browser.prototype.getFocusBrowserContext = function()
{
    return this.activeContext;
};

/**
 * Returns whether this proxy is currently connected to the underlying browser it
 * represents.
 *
 *  @function
 *  @returns whether connected to the underlying browser
 */
Browser.prototype.isConnected = function()
{
    return this.connected;
};

/**
 * Registers a listener (function) for a specific type of event. Listener
 * call back functions are specified in {@link BrowserEventListener}.
 * <p>
 * The supported event types are:
 * <ul>
 *   <li>onBreak</li>
 *   <li>onConsoleDebug</li>
 *   <li>onConsoleError</li>
 *   <li>onConsoleInfo</li>
 *   <li>onConsoleLog</li>
 *   <li>onConsoleWarn</li>
 *   <li>onContextCreated</li>
 *   <li>onContextChanged</li>
 *   <li>onContextDestroyed</li>
 *   <li>onDisconnect</li>
 *   <li>onInspectNode</li>
 *   <li>onResume</li>
 *   <li>onScript</li>
 *   <li>onToggleBreakpoint</li>
 * </ul>
 * <ul>
 * <li>TODO: how can clients remove (deregister) listeners?</li>
 * </ul>
 * </p>
 * @function
 * @param eventType an event type ({@link String}) listed above
 * @param listener a listener (function) that handles the event as specified
 *   by {@link BrowserEventListener}
 * @exception Error if an unsupported event type is specified
 */
Browser.prototype.addEventListener = function(eventType, listener)
{
    var i = this.EVENT_TYPES.indexOf(eventType);
    if (i < 0)
    {
        // unsupported event type
        throw new Error("eventType '" + eventType + "' is not supported");
    }
    var list = this.handlers[eventType];
    if (!list)
    {
        list = [];
        this.handlers[eventType] = list;
    }
    list.push(listener);
};

/**
 * Disconnects this client from the browser it is associated with.
 *
 * @function
 */
Browser.prototype.disconnect = function()
{
};

//TODO: support to remove a listener

// ************************************************************************************************
// Private, subclasses may call these functions

/**
 * Notification the given context has been added to this browser.
 * Adds the context to the list of active contexts and notifies context
 * listeners.<commit_msg>[bti] move EVENT_TYPES to methods of Browser.listener<commit_after>/* See license.txt for terms of usage */

// ************************************************************************************************
// Module

define([], function factoryBrowser(require){

// ************************************************************************************************
// Browser

/**
 * Proxy to a debuggable web browser. A browser may be remote and contain one or more
 * JavaScript execution contexts. Each JavaScript execution context may contain one or
 * more compilation units. A browser provides notification to registered listeners describing
 * events that occur in the browser.
 *
 * @constructor
 * @type Browser
 * @return a new Browser
 * @version 1.0
 */
function Browser()
{
    this.contexts = {}; // map of contexts, indexed by context ID
    this.activeContext = null;
    this.handlers = {}; // map of event types to array of handler functions
    this.connected = false;
}

// ************************************************************************************************
// API

Browser.debug = {handlers: true};
Browser.onDebug = function()
{
    if (Browser.debug)
        throw new Error("Browser.debug set but no Brower.onDebug is defined");
}

Browser.unimplementedHandler = function()
{
    if (Browser.debug && Browser.debug.handlers) Browser.onDebug("Browser.listener unimplemented event handler called ", {handler: this, args: arguments});
}

Browser.listener =
{
    onBreak: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onConsoleDebug: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onConsoleError: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onConsoleInfo: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onConsoleLog: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onConsoleWarn: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onContextCreated: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onContextDestroyed: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onContextChanged: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onContextLoaded: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onInspectNode: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onResume: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onScript: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onSuspend: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onJavaScriptDebugging: function () {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onToggleBreakpoint: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onBreakpointError: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
    onDisconnect: function() {
        Browser.unimplementedHandler.apply(this, arguments);
    },
};
/*
 * Testing and sanity: clearAllBreakpoints
 */
Browser.prototype.clearAllBreakpoints = function()
{
    Firebug.Debugger.clearAllBreakpoints();
}


/**
 * Returns current status of tools
 *
 * @function
 * @returns  an array of Tools, an object with toolName and enabled boolean
 *
 */
Browser.prototype.getTools = function()
{
    return [];
};

/**
 * Returns the {@link BrowserContext} with the specified id or <code>null</code>
 * if none.
 *
 * @function
 * @param id identifier of an {@link BrowserContext}
 * @returns the {@link BrowserContext} with the specified id or <code>null</code>
 *
 */
Browser.prototype.getBrowserContext = function(id)
{
    var context = this.contexts[id];
    if (context)
        return context;
    return null;
};

/**
 * Returns the root contexts being browsed. A {@link BrowserContext} represents the
 * content that has been served up and is being rendered for a location (URL) that
 * has been navigated to.
 * <p>
 * This function does not require communication with the remote browser.
 * </p>
 * @function
 * @returns an array of {@link BrowserContext}'s
 */
Browser.prototype.getBrowserContexts = function()
{
    var knownContexts = [];
    for (var id in this.contexts)
        knownContexts.push(this.contexts[id]);
    return knownContexts;
};

/**
 * Returns the {@link BrowserContext} that currently has focus in the browser
 * or <code>null</code> if none.
 *
 * @function
 * @returns the {@link BrowserContext} that has focus or <code>null</code>
 */
Browser.prototype.getFocusBrowserContext = function()
{
    return this.activeContext;
};

/**
 * Returns whether this proxy is currently connected to the underlying browser it
 * represents.
 *
 *  @function
 *  @returns whether connected to the underlying browser
 */
Browser.prototype.isConnected = function()
{
    return this.connected;
};

/**
 * Registers a listener (function) for a specific type of event. Listener
 * call back functions are specified in {@link BrowserEventListener}.
 * <p>
 * The supported event types are:
 * <ul>
 *   <li>onBreak</li>
 *   <li>onConsoleDebug</li>
 *   <li>onConsoleError</li>
 *   <li>onConsoleInfo</li>
 *   <li>onConsoleLog</li>
 *   <li>onConsoleWarn</li>
 *   <li>onContextCreated</li>
 *   <li>onContextChanged</li>
 *   <li>onContextDestroyed</li>
 *   <li>onDisconnect</li>
 *   <li>onInspectNode</li>
 *   <li>onResume</li>
 *   <li>onScript</li>
 *   <li>onToggleBreakpoint</li>
 *   <li>onJavaScriptDebugging</li>
 * </ul>
 * <ul>
 * <li>TODO: how can clients remove (deregister) listeners?</li>
 * </ul>
 * </p>
 * @function
 * @param eventType an event type ({@link String}) listed above
 * @param listener a listener (function) that handles the event as specified
 *   by {@link BrowserEventListener}
 * @exception Error if an unsupported event type is specified
 */
Browser.prototype.addEventListener = function(eventType, listener)
{
    if ( !Browser.listener.hasOwnProperty(eventType) )
    {
        // unsupported event type
        throw new Error("eventType '" + eventType + "' is not supported");
    }

    var list = this.handlers[eventType];
    if (!list)
    {
        list = [];
        this.handlers[eventType] = list;
    }
    list.push(listener);
};

/**
 * Disconnects this client from the browser it is associated with.
 *
 * @function
 */
Browser.prototype.disconnect = function()
{
};

//TODO: support to remove a listener

// ************************************************************************************************
// Private, subclasses may call these functions

/**
 * Notification the given context has been added to this browser.
 * Adds the context to the list of active contexts and notifies context
 * listeners.
{'fraction_non_alphanumeric': 0.1774898919052727, 'fraction_numerical': 0.0004125752949913359, 'mean_word_length': 3.7905138339920947, 'pattern_counts': {'":': 0, '<': 98, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2642408', 'n_tokens_mistral': 3366, 'n_tokens_neox': 3266, 'n_words': 991}