<commit_before>// Google BSD license http://code.google.com/google_bsd_license.html
// Copyright 2011 Google Inc. <EMAIL>

/*global define console window */


define(['appendFrame', 'crx2app/appEnd/proxyChromePipe', 'crx2app/rpc/ChromeProxy'], 
function(appendFrame,              chromeExtensionPipe,               ChromeProxy)  {

  var debug = true;

  function showInspectorIframe() {
    var inspectorElt = window.document.getElementById('WebInspector');
    inspectorElt.classList.remove('hide');
    return appendFrame('WebInspector', "inspector/front-end/devtools.html");
  }

// TODO put this into a .js file loaded statically by override

  // Base URL for crx2app

  // stand alone crx2app
  // var iframeDomain ="chrome-extension://bbjpappmojnmallpnfgfkjmjnhhplgog";
  // Sirius crx2app
  var iframeDomain = window.crx2appBase;

  var connection = chromeExtensionPipe.createFrom(iframeDomain);

  window.beforeUnloadQueue = [];

  window.onbeforeunload = function runQueue() {
    window.beforeUnloadQueue.forEach(function(fnc) {
      fnc();
    });
  };

  window.beforeUnloadQueue.push(function detach() {
    connection.detach();
  });

  function openNewTabId(chromeProxy, url, onNewTabId) {
    chromeProxy.openNewWindow( function(win) {
      var tabId = win.tabs[0].id;
      if (debug) {
        console.log('atopwi openNewTabId '+tabId);
      }
      onNewTabId(tabId);
    });
  }
 
 
 
  function Debuggee(chromeProxy, debuggeeSpec) {
    this.chrome = chromeProxy;
  }
  
  Debuggee.prototype = {
    
    parseDebuggee: function(debuggeeSpec) {
      var tabId = parseInt(debuggeeSpec.tabId, 10);
      if ( isNaN(tabId) ) {  // then we better have a URL
        this.url = decodeURIComponent(debuggeeSpec.url);
      } else {
        this.tabId = tabId;
      }
    },
    
    open: function(debuggeeSpec) {
      this.parseDebuggee(debuggeeSpec);
      openNewTabId(
        this.chrome,
        this.url, 
        function(newTabId) {
          this.tabId = newTabId;
          window.beforeUnloadQueue.push(function() {
            this.chrome.tabs.remove(newTabId, function() {
              if (debug) {
                console.log('atopwi removed '+newTabId);
              }
            });
          }.bind(this));
          this.attach();
        }.bind(this)
      );
    },
    
    attach: function() {
      this.chrome.debugger.attach(
        {tabId: this.tabId}, 
        '1.0', 
        this.onAttach.bind(this)
      );
    },
    
    onAttach: function() {
      if (debug) {
        console.log('atopwi chrome.debugger.attach complete '+this.tabId);
      }
       
      window.beforeUnloadQueue.unshift(function() {
          this.chrome.debugger.detach({tabId: this.tabId}, function() {
            if (debug) {
              console.log('atopwi detached from ' + this.tabId);
            }
          });
        }.bind(this));
            
      this.openInspector();
    },
    
    openInspector: function() {
      var inspectorElt = showInspectorIframe();
      this.inspectorWindow = inspectorElt.contentWindow;
  
      // Capture the DOMContentLoaded to monkey-patch inspectorWindow.
      //
      this.inspectorWindow.addEventListener(
        'DOMContentLoaded', 
        this.patchInspector.bind(this)
      );
    },
    
    patchInspector: function(event) {
      if (debug) {
        console.log("DOMContentLoaded on inspectorWindow ", this);
      }

      var backend = this.inspectorWindow.InspectorBackend;
      backend.sendMessageObjectToBackend = this.sendMessageObject.bind(this);
      
      this.chrome.jsonHandlers['chrome.debugger.remote'] = {
        jsonObjectHandler:  function(data) {
          if (debug) {
            console.log("jsonObjectHandler "+data.method, data);
          }
          backend.dispatch.apply(backend, [data]);
        }
      };
    
      this.inspectorWindow.InspectorFrontendHost.sendMessageToBackend = function() {
        throw new Error("Should not be called");
      };

      var WebInspector = this.inspectorWindow.WebInspector;
      WebInspector.attached = true; // small icons for embed in orion
    
      // Called asynchronously from WebInspector _initializeCapability
      this._doLoadedDoneWithCapabilities = 
        WebInspector._doLoadedDoneWithCapabilities;
    
      WebInspector._doLoadedDoneWithCapabilities = function() {
        var args = Array.prototype.slice.call(arguments, 0);
        this._doLoadedDoneWithCapabilities.apply(WebInspector, args);
      
        this.navigateToURL();
      }.bind(this);
    
      //WebInspector.doLoadedDone();
    },
    
    navigateToURL: function(inspectorReady) {
      if (this.url) { // then we started in a new tab, navigate
        if (debug) {
          console.log('atopwi setting URL:'+this.url);
        }
        this.chrome.tabs.update(
         this.tabId, 
         {url: this.url}, 
         this.onTabUpdate.bind(this)
        );
      }
    },<commit_msg>sync to crx2app openNewTab<commit_after>// Google BSD license http://code.google.com/google_bsd_license.html
// Copyright 2011 Google Inc. <EMAIL>

/*global define console window */


define(['appendFrame', 'crx2app/appEnd/proxyChromePipe', 'crx2app/rpc/ChromeProxy'], 
function(appendFrame,              chromeExtensionPipe,               ChromeProxy)  {

  var debug = true;

  function showInspectorIframe() {
    var inspectorElt = window.document.getElementById('WebInspector');
    inspectorElt.classList.remove('hide');
    return appendFrame('WebInspector', "inspector/front-end/devtools.html");
  }

// TODO put this into a .js file loaded statically by override

  // Base URL for crx2app

  // stand alone crx2app
  // var iframeDomain ="chrome-extension://bbjpappmojnmallpnfgfkjmjnhhplgog";
  // Sirius crx2app
  var iframeDomain = window.crx2appBase;

  var connection = chromeExtensionPipe.createFrom(iframeDomain);

  window.beforeUnloadQueue = [];

  window.onbeforeunload = function runQueue() {
    window.beforeUnloadQueue.forEach(function(fnc) {
      fnc();
    });
  };

  window.beforeUnloadQueue.push(function detach() {
    connection.detach();
  });

  function openNewTab(chromeProxy, url, onNewTab) {
    chromeProxy.openNewWindow( function(win) {
      var tabId = win.tabs[0].id;
      if (debug) {
        console.log('atopwi openNewTab '+tabId);
      }
      onNewTab(tabId);
    });
  }
 
 
 
  function Debuggee(chromeProxy, debuggeeSpec) {
    this.chrome = chromeProxy;
  }
  
  Debuggee.prototype = {
    
    parseDebuggee: function(debuggeeSpec) {
      var tabId = parseInt(debuggeeSpec.tabId, 10);
      if ( isNaN(tabId) ) {  // then we better have a URL
        this.url = decodeURIComponent(debuggeeSpec.url);
      } else {
        this.tabId = tabId;
      }
    },
    
    open: function(debuggeeSpec) {
      this.parseDebuggee(debuggeeSpec);
      this.chrome.openNewTab(
        this.url, 
        function(newTabId) {
          this.tabId = newTabId;
          window.beforeUnloadQueue.push(function() {
            this.chrome.tabs.remove(newTabId, function() {
              if (debug) {
                console.log('atopwi removed '+newTabId);
              }
            });
          }.bind(this));
          this.attach();
        }.bind(this)
      );
    },
    
    attach: function() {
      this.chrome.debugger.attach(
        {tabId: this.tabId}, 
        '1.0', 
        this.onAttach.bind(this)
      );
    },
    
    onAttach: function() {
      if (debug) {
        console.log('atopwi chrome.debugger.attach complete '+this.tabId);
      }
       
      window.beforeUnloadQueue.unshift(function() {
          this.chrome.debugger.detach({tabId: this.tabId}, function() {
            if (debug) {
              console.log('atopwi detached from ' + this.tabId);
            }
          });
        }.bind(this));
            
      this.openInspector();
    },
    
    openInspector: function() {
      var inspectorElt = showInspectorIframe();
      this.inspectorWindow = inspectorElt.contentWindow;
  
      // Capture the DOMContentLoaded to monkey-patch inspectorWindow.
      //
      this.inspectorWindow.addEventListener(
        'DOMContentLoaded', 
        this.patchInspector.bind(this)
      );
    },
    
    patchInspector: function(event) {
      if (debug) {
        console.log("DOMContentLoaded on inspectorWindow ", this);
      }

      var backend = this.inspectorWindow.InspectorBackend;
      backend.sendMessageObjectToBackend = this.sendMessageObject.bind(this);
      
      this.chrome.jsonHandlers['chrome.debugger.remote'] = {
        jsonObjectHandler:  function(data) {
          if (debug) {
            console.log("jsonObjectHandler "+data.method, data);
          }
          backend.dispatch.apply(backend, [data]);
        }
      };
    
      this.inspectorWindow.InspectorFrontendHost.sendMessageToBackend = function() {
        throw new Error("Should not be called");
      };

      var WebInspector = this.inspectorWindow.WebInspector;
      WebInspector.attached = true; // small icons for embed in orion
    
      // Called asynchronously from WebInspector _initializeCapability
      // which is called byt the load event vai doLoadedDone()
      this._doLoadedDoneWithCapabilities = 
        WebInspector._doLoadedDoneWithCapabilities;
    
      WebInspector._doLoadedDoneWithCapabilities = function() {
        var args = Array.prototype.slice.call(arguments, 0);
        this._doLoadedDoneWithCapabilities.apply(WebInspector, args);
      
        this.navigateToURL();
      }.bind(this);
    },
    
    navigateToURL: function(inspectorReady) {
      if (this.url) { // then we started in a new tab, navigate
        if (debug) {
          console.log('atopwi setting URL:'+this.url);
        }
        this.chrome.tabs.update(
         this.tabId, 
         {url: this.url}, 
         this.onTabUpdate.bind(this)
        );
      }
    },
{'fraction_non_alphanumeric': 0.11528771540864657, 'fraction_numerical': 0.0033256071752494204, 'mean_word_length': 2.300299301629531, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3464448', 'n_tokens_mistral': 3096, 'n_tokens_neox': 2974, 'n_words': 520}