Skip to content

Commit

Permalink
Merge pull request #2722 from atom/fix-array-buffer
Browse files Browse the repository at this point in the history
Support creating externalized ArrayBuffer for node::Buffer
  • Loading branch information
zcbenz committed Sep 7, 2015
2 parents aad1f50 + 10bc0c2 commit 3717f5b
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 41 deletions.
42 changes: 3 additions & 39 deletions atom/renderer/atom_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
#include <string>

#include "atom/common/api/atom_bindings.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/node_bindings.h"
#include "atom/common/node_includes.h"
#include "atom/common/options_switches.h"
#include "atom/renderer/atom_render_view_observer.h"
#include "atom/renderer/guest_view_container.h"
#include "atom/renderer/node_array_buffer_bridge.h"
#include "base/command_line.h"
#include "chrome/renderer/pepper/pepper_helper.h"
#include "chrome/renderer/printing/print_web_view_helper.h"
Expand All @@ -20,16 +21,12 @@
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_frame_observer.h"
#include "content/public/renderer/render_thread.h"
#include "third_party/WebKit/public/web/WebArrayBuffer.h"
#include "third_party/WebKit/public/web/WebArrayBufferConverter.h"
#include "third_party/WebKit/public/web/WebCustomElement.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebPluginParams.h"
#include "third_party/WebKit/public/web/WebKit.h"
#include "third_party/WebKit/public/web/WebRuntimeFeatures.h"

#include "atom/common/node_includes.h"

#if defined(OS_WIN)
#include <shlobj.h>
#endif
Expand All @@ -51,37 +48,6 @@ bool IsSwitchEnabled(base::CommandLine* command_line,
return true;
}

// global.Uint8Array;
v8::Local<v8::Function> GetUint8ArrayConstructor(
v8::Isolate* isolate, v8::Local<v8::Context> context) {
v8::Local<v8::Value> constructor = context->Global()->Get(
mate::StringToV8(isolate, "Uint8Array"));
return v8::Local<v8::Function>::Cast(constructor);
}

// new ArrayBuffer(size);
v8::Local<v8::ArrayBuffer> BlinkArrayBufferCreate(
v8::Isolate* isolate, size_t size) {
blink::WebArrayBuffer buffer = blink::WebArrayBuffer::create(size, 1);
return v8::Local<v8::ArrayBuffer>::Cast(
blink::WebArrayBufferConverter::toV8Value(
&buffer, isolate->GetCurrentContext()->Global(), isolate));
}

// new Uint8Array(array_buffer, offset, size);
v8::Local<v8::Uint8Array> BlinkUint8ArrayCreate(
v8::Local<v8::ArrayBuffer> ab, size_t offset, size_t size) {
v8::Local<v8::Context> context = ab->CreationContext();
v8::Isolate* isolate = context->GetIsolate();
v8::Local<v8::Function> constructor =
GetUint8ArrayConstructor(isolate, context);
v8::Local<v8::Value> args[] = {
ab, mate::ConvertToV8(isolate, offset), mate::ConvertToV8(isolate, size)
};
return v8::Local<v8::Uint8Array>::Cast(constructor->NewInstance(
context, arraysize(args), args).ToLocalChecked());
}

// Helper class to forward the messages to the client.
class AtomRenderFrameObserver : public content::RenderFrameObserver {
public:
Expand Down Expand Up @@ -120,9 +86,7 @@ void AtomRendererClient::WebKitInitialized() {
blink::WebCustomElement::addEmbedderCustomElementName("webview");
blink::WebCustomElement::addEmbedderCustomElementName("browserplugin");

// Override Node's ArrayBuffer with DOM's ArrayBuffer.
node::Buffer::SetArrayBufferCreator(&BlinkArrayBufferCreate,
&BlinkUint8ArrayCreate);
OverrideNodeArrayBuffer();

node_bindings_->Initialize();
node_bindings_->PrepareMessageLoop();
Expand Down
66 changes: 66 additions & 0 deletions atom/renderer/node_array_buffer_bridge.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/renderer/node_array_buffer_bridge.h"

#include "base/basictypes.h"
#include "atom/common/node_includes.h"
#include "native_mate/converter.h"
#include "third_party/WebKit/public/web/WebArrayBuffer.h"
#include "third_party/WebKit/public/web/WebArrayBufferConverter.h"

namespace atom {

namespace {

// global.Uint8Array;
v8::Local<v8::Function> GetUint8ArrayConstructor(
v8::Isolate* isolate, v8::Local<v8::Context> context) {
v8::Local<v8::Value> constructor = context->Global()->Get(
mate::StringToV8(isolate, "Uint8Array"));
return v8::Local<v8::Function>::Cast(constructor);
}

// new ArrayBuffer(size);
v8::Local<v8::ArrayBuffer> BlinkArrayBufferNew(
v8::Isolate* isolate, size_t size) {
blink::WebArrayBuffer buffer = blink::WebArrayBuffer::create(size, 1);
return v8::Local<v8::ArrayBuffer>::Cast(
blink::WebArrayBufferConverter::toV8Value(
&buffer, isolate->GetCurrentContext()->Global(), isolate));
}

// new ArrayBuffer(data, size);
v8::Local<v8::ArrayBuffer> BlinkArrayBufferNewWith(
v8::Isolate* isolate, void* data, size_t size) {
blink::WebArrayBuffer buffer = blink::WebArrayBuffer::createExternal(
data, size);
return v8::Local<v8::ArrayBuffer>::Cast(
blink::WebArrayBufferConverter::toV8Value(
&buffer, isolate->GetCurrentContext()->Global(), isolate));
}

// new Uint8Array(array_buffer, offset, size);
v8::Local<v8::Uint8Array> BlinkUint8ArrayNew(
v8::Local<v8::ArrayBuffer> ab, size_t offset, size_t size) {
// Use the DOM's Uint8Array constructor to create Uint8Array.
v8::Local<v8::Context> context = ab->CreationContext();
v8::Isolate* isolate = context->GetIsolate();
v8::Local<v8::Function> constructor =
GetUint8ArrayConstructor(isolate, context);
v8::Local<v8::Value> args[] = {
ab, mate::ConvertToV8(isolate, offset), mate::ConvertToV8(isolate, size)
};
return v8::Local<v8::Uint8Array>::Cast(constructor->NewInstance(
context, arraysize(args), args).ToLocalChecked());
}

} // namespace

void OverrideNodeArrayBuffer() {
node::Buffer::SetArrayBufferCreator(
BlinkArrayBufferNew, BlinkArrayBufferNewWith, BlinkUint8ArrayNew);
}

} // namespace atom
15 changes: 15 additions & 0 deletions atom/renderer/node_array_buffer_bridge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#ifndef ATOM_RENDERER_NODE_ARRAY_BUFFER_BRIDGE_H_
#define ATOM_RENDERER_NODE_ARRAY_BUFFER_BRIDGE_H_

namespace atom {

// Override Node's ArrayBuffer with DOM's ArrayBuffer.
void OverrideNodeArrayBuffer();

} // namespace atom

#endif // ATOM_RENDERER_NODE_ARRAY_BUFFER_BRIDGE_H_
2 changes: 2 additions & 0 deletions filenames.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@
'atom/renderer/atom_renderer_client.h',
'atom/renderer/guest_view_container.cc',
'atom/renderer/guest_view_container.h',
'atom/renderer/node_array_buffer_bridge.cc',
'atom/renderer/node_array_buffer_bridge.h',
'atom/utility/atom_content_utility_client.cc',
'atom/utility/atom_content_utility_client.h',
'chromium_src/chrome/browser/browser_process.cc',
Expand Down
2 changes: 1 addition & 1 deletion script/lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

BASE_URL = os.getenv('LIBCHROMIUMCONTENT_MIRROR') or \
'http://github-janky-artifacts.s3.amazonaws.com/libchromiumcontent'
LIBCHROMIUMCONTENT_COMMIT = 'f5489a774b719e9e979675c17b99ed45a05aacf7'
LIBCHROMIUMCONTENT_COMMIT = '8482fe555913dea3bde8a74f754524e2cfb02bc5'

PLATFORM = {
'cygwin': 'win32',
Expand Down
2 changes: 1 addition & 1 deletion vendor/node
Submodule node updated 220 files

0 comments on commit 3717f5b

Please sign in to comment.