<commit_before>'use strict'

const ipcMain = require('electron').ipcMain
const webContents = require('electron').webContents

let supportedWebViewEvents = [
  'tab-replaced-at',
  'tab-detached-at',
  'load-start',
  'did-attach',
  'guest-ready',
  'will-detach',
  'did-detach',
  'did-finish-load',
  'did-fail-provisional-load',
  'did-fail-load',
  'dom-ready',
  'preferred-size-changed',
  'console-message',
  'did-navigate',
  'did-navigate-in-page',
  'security-style-changed',
  'close',
  'gpu-crashed',
  'plugin-crashed',
  'will-destroy',
  'destroyed',
  'page-favicon-updated',
  'enter-html-full-screen',
  'leave-html-full-screen',
  'media-started-playing',
  'media-paused',
  'found-in-page',
  'did-change-theme-color',
  'update-target-url',
  'context-menu',
  'enable-pepper-menu',
  'repost-form-warning',
  'content-blocked',
  'show-autofill-settings',
  'update-autofill-popup-data-list-values',
  'hide-autofill-popup',
  'show-autofill-popup',
  'did-run-insecure-content',
  'did-block-run-insecure-content'
]

let guests = {}
const registerGuest = function (guest, embedder) {
  const tabId = guest.getId()

  embedder.once('destroyed', function () {
    if (guests[tabId] === embedder) {
      guests[tabId] = false
    }
  })

  const oldEmbedder = guests[tabId]
  guests[tabId] = embedder
  if (oldEmbedder !== undefined) {
    return
  }

  // Dispatch events to embedder.
  const fn = function (event) {
    guest.on(event, function (_, ...args) {
      const embedder = guests[tabId]

      if (!embedder || embedder.isDestroyed())
        return

      let forceSend = false
      if (event === 'tab-replaced-at') {
        const newContents = args[2]
        registerGuest(newContents, embedder)
      }
      if (['destroyed', 'tab-detached-at'].includes(event)) {
        delete guests[tabId]
        forceSend = true
      }

      if (guest.isDestroyed() && !forceSend)
        return

      embedder.send.apply(embedder, ['ELECTRON_GUEST_VIEW_INTERNAL_DISPATCH_EVENT', tabId, event].concat(args))
    })
  }
  for (const event of supportedWebViewEvents) {
    fn(event)
  }

  // Dispatch guest's IPC messages to embedder.
  guest.on('ipc-message-host', function (_, [channel, ...args]) {
    const embedder = guests[tabId]
    if (!embedder || embedder.isDestroyed() || guest.isDestroyed())
      return

    embedder.send.apply(embedder, ['ELECTRON_GUEST_VIEW_INTERNAL_IPC_MESSAGE', tabId, channel].concat(args))
  })
}
<commit_msg>fix: embedder tab event duplication on insert to new tab strip<commit_after>'use strict'

const ipcMain = require('electron').ipcMain
const webContents = require('electron').webContents

let supportedWebViewEvents = [
  'tab-replaced-at',
  'tab-detached-at',
  'load-start',
  'did-attach',
  'guest-ready',
  'will-detach',
  'did-detach',
  'did-finish-load',
  'did-fail-provisional-load',
  'did-fail-load',
  'dom-ready',
  'preferred-size-changed',
  'console-message',
  'did-navigate',
  'did-navigate-in-page',
  'security-style-changed',
  'close',
  'gpu-crashed',
  'plugin-crashed',
  'will-destroy',
  'destroyed',
  'page-favicon-updated',
  'enter-html-full-screen',
  'leave-html-full-screen',
  'media-started-playing',
  'media-paused',
  'found-in-page',
  'did-change-theme-color',
  'update-target-url',
  'context-menu',
  'enable-pepper-menu',
  'repost-form-warning',
  'content-blocked',
  'show-autofill-settings',
  'update-autofill-popup-data-list-values',
  'hide-autofill-popup',
  'show-autofill-popup',
  'did-run-insecure-content',
  'did-block-run-insecure-content'
]

let guests = {}
const registerGuest = function (guest, embedder) {
  const tabId = guest.getId()

  embedder.once('destroyed', function () {
    if (guests[tabId] === embedder) {
      guests[tabId] = false
    }
  })

  const oldEmbedder = guests[tabId]
  guests[tabId] = embedder
  if (oldEmbedder !== undefined) {
    return
  }

  // Dispatch events to embedder.
  const fn = function (event) {
    guest.on(event, function (_, ...args) {
      const embedder = guests[tabId]

      if (!embedder || embedder.isDestroyed())
        return

      let forceSend = false
      if (event === 'tab-replaced-at') {
        const newContents = args[2]
        registerGuest(newContents, embedder)
      }
      if (['destroyed'].includes(event)) {
        delete guests[tabId]
        forceSend = true
      }

      if (guest.isDestroyed() && !forceSend)
        return

      embedder.send.apply(embedder, ['ELECTRON_GUEST_VIEW_INTERNAL_DISPATCH_EVENT', tabId, event].concat(args))
    })
  }
  for (const event of supportedWebViewEvents) {
    fn(event)
  }

  // Dispatch guest's IPC messages to embedder.
  guest.on('ipc-message-host', function (_, [channel, ...args]) {
    const embedder = guests[tabId]
    if (!embedder || embedder.isDestroyed() || guest.isDestroyed())
      return

    embedder.send.apply(embedder, ['ELECTRON_GUEST_VIEW_INTERNAL_IPC_MESSAGE', tabId, channel].concat(args))
  })
}

{'fraction_non_alphanumeric': 0.16105769230769232, 'fraction_numerical': 0.00040064102564102563, 'mean_word_length': 3.3266897746967072, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4917851', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1774, 'n_words': 303}