<commit_before>define(['constants', 'util'], function(C, util){
  'use strict';

  var $tab_content_template;
  var $tab_content_alert_template;
  var $tab_webview_template;
  var $tabs;

  function init(templates){
    $tabs = $('.app-right .tabs');
    $tab_content_template = templates['tab-content'];
    $tab_content_alert_template = templates['tab-content-alert'];
    $tab_webview_template = templates['tab-webview'];
  }

  function TabContent(tab_control, tab, $existing_webview) {
    this.tab_control = tab_control;
    this.tab = tab;
    this.$content = $tab_content_template.clone().appendTo($tabs).hide();
    this.elems = {
      $button_back_to_pin: this.$content.find('.button-back-to-pin'),
      $button_back: this.$content.find('.button-back'),
      $button_forward: this.$content.find('.button-forward'),
      $button_refresh: this.$content.find('.button-refresh'),
      $button_stop: this.$content.find('.button-stop'),
      $button_settings: this.$content.find('.button-settings'),
      $button_pin: this.$content.find('.button-pin'),

      $input_address_bar: this.$content.find('input.address-bar'),
      $input_tab_name: this.$content.find('input.tab-name'),
      $input_tab_color: this.$content.find('input.tab-color'),

      $find_input: this.$content.find('.find-text'),
      $find_next: this.$content.find('.button-find-next'),
      $find_prev: this.$content.find('.button-find-prev'),
      $find_info: this.$content.find('.find-info'),

      $alerts: this.$content.find('.alerts')
    };
    this._webview = null;
    this._setup_listeners();
    this.update_display();

    if ($existing_webview) {
      this._setup_webview($existing_webview);
    }
  }

  $.extend(TabContent.prototype, {
    _setup_webview: function($webview) {
      this._webview = $webview.get(0);
      $webview.appendTo(this.$content.find('.webview-wrapper'));

      this._webview.addEventListener("findupdate", function(e){
        if (e.searchText === '') {
          this.elems.$find_info.text('');
        } else {
          this.elems.$find_info.text(e.activeMatchOrdinal + ' of ' + e.numberOfMatches);
        }
      }.bind(this));

      this._webview.addEventListener("loadredirect", function(e){
        if(e.isTopLevel){
          this._url_changed(e.newUrl);
        }
      }.bind(this));

      this._webview.addEventListener("loadstart", function(e){
        if(e.isTopLevel){
          this._url_changed(e.url);
          this.dismiss_alerts();
        }
        this.tab.$node.addClass("loading").removeClass("ready");
        this.$content.addClass("loading").removeClass("ready");
        this._update_button_states();
      }.bind(this));

      this._webview.addEventListener("loadstop", function(){
        this._url_changed(this._webview.src);
        this._update_title();
        this.tab.$node.removeClass("loading").addClass("ready");
        this.$content.removeClass("loading").addClass("ready");
        this._update_button_states();
      }.bind(this));

      this._webview.addEventListener('newwindow', function(e) {
        var $webview = $tab_webview_template.clone();
        e.window.attach($webview.get(0));
        this.tab_control.open_new_tab(function(tab){
          tab.setup_tab_content($webview);
          if (e.windowOpenDisposition === 'ignore' ||
              e.windowOpenDisposition === 'new_background_tab') {
              // Quickly momentatily display the content to trigger webview to load
              // TODO: improve this hack
              tab.content.$content.css('z-index', -100).show();
              setTimeout(function(){
                tab.content.$content.css('z-index', 'initial').hide();
              }, 100);
          } else {
            tab.select_tab();
          }
        });
      }.bind(this));

      this._webview.addEventListener('close', function() {
        this.tab.close_tab();
      }.bind(this));

      // TODO: this is really not nice
      // we want to replace this with maybe some way of storing / remembering
      // granted permissions
      var _permission_responses = {};
      var _set_permission_response = function(permission, domain, val){
        if (_permission_responses[permission] === undefined){
          _permission_responses[permission] = {};
        }
        _permission_responses[permission][domain] = val;
      };
      var _get_permission_response = function(permission, domain){
        var _p = _permission_responses[permission];
        return _p ? _p[domain] : undefined;
      };

      this._webview.addEventListener('permissionrequest', function(e) {
        var _domain = null;
        var _text = null;
        switch (e.permission) {
          case "media":
            _domain = e.request.url;
            _text = "The website " + e.request.url + " wants to be given access to your webcam and microphone";
            break;
          case "geolocation":
            _domain = e.request.url;
            _text = "The website " + e.request.url + " wants to be given access to your geographical location";
            break;
          case "pointerLock":
            _domain = e.request.url;
            _text = "The website " + e.request.url + " wants to lock your mouse cursor";
            break;
          case "download":
            e.request.allow();
            return;
          case "loadplugin":
            _domain = "*";
            _text = "This website wants to run the plugin " + e.request.name;
            break;
          case "filesystem":
            _domain = e.request.url;
            _text = "The website " + e.request.url + " wants to be given access to files on your computer";
            break;
          case "fullscreen":
            _domain = e.request.origin;
            _text = "The website " + e.request.origin + " wants to go full screen";
            break;
          default:
            console.error("Unknown permission request: ", e);
            return;
        }
        if (_get_permission_response(e.permission, _domain)) {
          e.request.allow();
          return;
        }
        // TODO: find out if there is a way to wait for a user response before
        // handling a permission request (we want an async api for e.request)
        this.user_alert(_text, [
          {
            label: "Allow",
            callback: function(){
              _set_permission_response(e.permission, _domain, true);
              this.user_alert("Permission Granted, you may need to retry / refresh the page", [
                {
                  label: "OK"
                },
              ]);
            }.bind(this)
          },
          {
            label: "Deny",
            callback: function(){
              _set_permission_response(e.permission, _domain, false);
            }
          }
        ]);
        e.request.deny();
      }.bind(this));

      // Intercept requests for pinned tabs
      this._webview.request.onBeforeRequest.addListener(function(details) {
        if (this.tab.pinned && details.type === 'main_frame' &&
          details.method === 'GET' && details.url !== this.tab.url){<commit_msg>Use e.preventDefault() in permissionrequest<commit_after>define(['constants', 'util'], function(C, util){
  'use strict';

  var $tab_content_template;
  var $tab_content_alert_template;
  var $tab_webview_template;
  var $tabs;

  function init(templates){
    $tabs = $('.app-right .tabs');
    $tab_content_template = templates['tab-content'];
    $tab_content_alert_template = templates['tab-content-alert'];
    $tab_webview_template = templates['tab-webview'];
  }

  function TabContent(tab_control, tab, $existing_webview) {
    this.tab_control = tab_control;
    this.tab = tab;
    this.$content = $tab_content_template.clone().appendTo($tabs).hide();
    this.elems = {
      $button_back_to_pin: this.$content.find('.button-back-to-pin'),
      $button_back: this.$content.find('.button-back'),
      $button_forward: this.$content.find('.button-forward'),
      $button_refresh: this.$content.find('.button-refresh'),
      $button_stop: this.$content.find('.button-stop'),
      $button_settings: this.$content.find('.button-settings'),
      $button_pin: this.$content.find('.button-pin'),

      $input_address_bar: this.$content.find('input.address-bar'),
      $input_tab_name: this.$content.find('input.tab-name'),
      $input_tab_color: this.$content.find('input.tab-color'),

      $find_input: this.$content.find('.find-text'),
      $find_next: this.$content.find('.button-find-next'),
      $find_prev: this.$content.find('.button-find-prev'),
      $find_info: this.$content.find('.find-info'),

      $alerts: this.$content.find('.alerts')
    };
    this._webview = null;
    this._setup_listeners();
    this.update_display();

    if ($existing_webview) {
      this._setup_webview($existing_webview);
    }
  }

  $.extend(TabContent.prototype, {
    _setup_webview: function($webview) {
      this._webview = $webview.get(0);
      $webview.appendTo(this.$content.find('.webview-wrapper'));

      this._webview.addEventListener("findupdate", function(e){
        if (e.searchText === '') {
          this.elems.$find_info.text('');
        } else {
          this.elems.$find_info.text(e.activeMatchOrdinal + ' of ' + e.numberOfMatches);
        }
      }.bind(this));

      this._webview.addEventListener("loadredirect", function(e){
        if(e.isTopLevel){
          this._url_changed(e.newUrl);
        }
      }.bind(this));

      this._webview.addEventListener("loadstart", function(e){
        if(e.isTopLevel){
          this._url_changed(e.url);
          this.dismiss_alerts();
        }
        this.tab.$node.addClass("loading").removeClass("ready");
        this.$content.addClass("loading").removeClass("ready");
        this._update_button_states();
      }.bind(this));

      this._webview.addEventListener("loadstop", function(){
        this._url_changed(this._webview.src);
        this._update_title();
        this.tab.$node.removeClass("loading").addClass("ready");
        this.$content.removeClass("loading").addClass("ready");
        this._update_button_states();
      }.bind(this));

      this._webview.addEventListener('newwindow', function(e) {
        var $webview = $tab_webview_template.clone();
        e.window.attach($webview.get(0));
        this.tab_control.open_new_tab(function(tab){
          tab.setup_tab_content($webview);
          if (e.windowOpenDisposition === 'ignore' ||
              e.windowOpenDisposition === 'new_background_tab') {
              // Quickly momentatily display the content to trigger webview to load
              // TODO: improve this hack
              tab.content.$content.css('z-index', -100).show();
              setTimeout(function(){
                tab.content.$content.css('z-index', 'initial').hide();
              }, 100);
          } else {
            tab.select_tab();
          }
        });
      }.bind(this));

      this._webview.addEventListener('close', function() {
        this.tab.close_tab();
      }.bind(this));

      this._webview.addEventListener('permissionrequest', function(e) {
        var _text = null;
        switch (e.permission) {
          case "media":
            _text = "The website " + e.request.url + " wants to be given access to your webcam and microphone";
            break;
          case "geolocation":
            _text = "The website " + e.request.url + " wants to be given access to your geographical location";
            break;
          case "pointerLock":
            _text = "The website " + e.request.url + " wants to lock your mouse cursor";
            break;
          case "download":
            e.request.allow();
            return;
          case "loadplugin":
            _text = "This website wants to run the plugin " + e.request.name;
            break;
          case "filesystem":
            _text = "The website " + e.request.url + " wants to be given access to files on your computer";
            break;
          case "fullscreen":
            _text = "The website " + e.request.origin + " wants to go full screen";
            break;
          default:
            console.error("Unknown permission request: ", e);
            return;
        }
        this.user_alert(_text, [
          {
            label: "Allow",
            callback: e.request.allow
          },
          {
            label: "Deny",
            callback: e.request.deny
          }
        ]);
        e.preventDefault();
      }.bind(this));

      // Intercept requests for pinned tabs
      this._webview.request.onBeforeRequest.addListener(function(details) {
        if (this.tab.pinned && details.type === 'main_frame' &&
          details.method === 'GET' && details.url !== this.tab.url){
{'fraction_non_alphanumeric': 0.14420896222726207, 'fraction_numerical': 0.0012512708219285212, 'mean_word_length': 2.3018332042344434, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6297546', 'n_tokens_mistral': 3662, 'n_tokens_neox': 3601, 'n_words': 670}