Repository: electron/electron
File: shell/renderer/api/electron_api_context_bridge.cc (C++)

Code before:
    return v8::MaybeLocal<v8::Value>(cloned_arr);
  }

  // Clone certain DOM APIs only within Window contexts.
  if (source_execution_context->IsWindow()) {
    // Custom logic to "clone" Element references
    blink::WebElement elem = blink::WebElement::FromV8Value(
        destination_context->GetIsolate(), value);
    if (!elem.IsNull()) {
      v8::Context::Scope destination_context_scope(destination_context);
      return v8::MaybeLocal<v8::Value>(
          elem.ToV8Value(destination_context->GetIsolate()));
    }

    // Custom logic to "clone" Blob references
    blink::WebBlob blob =
        blink::WebBlob::FromV8Value(destination_context->GetIsolate(), value);
    if (!blob.IsNull()) {
      v8::Context::Scope destination_context_scope(destination_context);
      return v8::MaybeLocal<v8::Value>(
          blob.ToV8Value(destination_context->GetIsolate()));
    }

    // Custom logic to "clone" FileList references
    auto object_value = value.As<v8::Object>();
    v8::Maybe<bool> has_length = object_value->Has(
        source_context,
        gin::StringToV8(source_context->GetIsolate(), "length"));
    v8::Maybe<bool> has_item = object_value->Has(
        source_context, gin::StringToV8(source_context->GetIsolate(), "item"));
    if (has_length.FromJust() && has_item.FromJust()) {
      v8::Local<v8::Value> length, item_fn;
      if (object_value
              ->Get(source_context,
                    gin::StringToV8(source_context->GetIsolate(), "length"))
              .ToLocal(&length) &&
          length->IsNumber() &&
          object_value
              ->Get(source_context,
                    gin::StringToV8(source_context->GetIsolate(), "item"))
              .ToLocal(&item_fn) &&
          item_fn->IsFunction()) {
        v8::Context::Scope destination_scope(destination_context);
        uint32_t item_count = length->Uint32Value(source_context).FromJust();
        v8::Local<v8::Array> array =
            v8::Array::New(destination_context->GetIsolate(), item_count);

        for (uint32_t i = 0; i < item_count; i++) {
          v8::Local<v8::Value> file;
          if (object_value->Get(source_context, i).ToLocal(&file)) {
            auto passed_file = PassValueToOtherContextInner(

Review comment (performance):
This is a fairly expensive and inaccurate check to run on every object, just because an object happens to match this structure we're gonna do things differently?

Code after:
    return v8::MaybeLocal<v8::Value>(cloned_arr);
  }

  // Clone certain DOM APIs only within Window contexts.
  if (source_execution_context->IsWindow()) {
    // Custom logic to "clone" Element references
    blink::WebElement elem = blink::WebElement::FromV8Value(
        destination_context->GetIsolate(), value);
    if (!elem.IsNull()) {
      v8::Context::Scope destination_context_scope(destination_context);
      return v8::MaybeLocal<v8::Value>(
          elem.ToV8Value(destination_context->GetIsolate()));
    }

    // Custom logic to "clone" Blob references
    blink::WebBlob blob =
        blink::WebBlob::FromV8Value(destination_context->GetIsolate(), value);
    if (!blob.IsNull()) {
      v8::Context::Scope destination_context_scope(destination_context);
      return v8::MaybeLocal<v8::Value>(
          blob.ToV8Value(destination_context->GetIsolate()));
    }
  }

  // Proxy all objects
  if (IsPlainObject(value)) {
    auto object_value = value.As<v8::Object>();
    auto passed_value = CreateProxyForAPI(
        object_value, source_context, source_execution_context,
        destination_context, object_cache, support_dynamic_properties,
        recursion_depth + 1, error_target);
    if (passed_value.IsEmpty())
      return {};
    return v8::MaybeLocal<v8::Value>(passed_value.ToLocalChecked());
  }

  // Serializable objects
  blink::CloneableMessage ret;
  {
    v8::Local<v8::Context> error_context =
        error_target == BridgeErrorTarget::kSource ? source_context
                                                   : destination_context;
    v8::Context::Scope error_scope(error_context);
    // V8 serializer will throw an error if required
    if (!gin::ConvertFromV8(error_context->GetIsolate(), value, &ret)) {
      return {};
    }
  }

  {
    v8::Context::Scope destination_context_scope(destination_context);