Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(441)

Side by Side Diff: content/child/v8_value_converter_impl.cc

Issue 2685743002: V8ValueConverterImpl: Use V8 to copy from array buffers. (Closed)
Patch Set: fix dcheck Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | content/child/v8_value_converter_impl_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/child/v8_value_converter_impl.h" 5 #include "content/child/v8_value_converter_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <cmath> 10 #include <cmath>
11 #include <memory> 11 #include <memory>
12 #include <string> 12 #include <string>
13 #include <utility> 13 #include <utility>
14 14
15 #include "base/bind.h" 15 #include "base/bind.h"
16 #include "base/bind_helpers.h" 16 #include "base/bind_helpers.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/memory/ptr_util.h" 18 #include "base/memory/ptr_util.h"
19 #include "base/values.h" 19 #include "base/values.h"
20 #include "third_party/WebKit/public/web/WebArrayBuffer.h"
21 #include "third_party/WebKit/public/web/WebArrayBufferConverter.h"
22 #include "third_party/WebKit/public/web/WebArrayBufferView.h"
23 #include "v8/include/v8.h" 20 #include "v8/include/v8.h"
24 21
25 namespace content { 22 namespace content {
26 23
27 // Default implementation of V8ValueConverter::Strategy 24 // Default implementation of V8ValueConverter::Strategy
28 25
29 bool V8ValueConverter::Strategy::FromV8Object( 26 bool V8ValueConverter::Strategy::FromV8Object(
30 v8::Local<v8::Object> value, 27 v8::Local<v8::Object> value,
31 std::unique_ptr<base::Value>* out, 28 std::unique_ptr<base::Value>* out,
32 v8::Isolate* isolate, 29 v8::Isolate* isolate,
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 LOG(ERROR) << "Failed to set property with key " << key; 329 LOG(ERROR) << "Failed to set property with key " << key;
333 } 330 }
334 331
335 return result; 332 return result;
336 } 333 }
337 334
338 v8::Local<v8::Value> V8ValueConverterImpl::ToArrayBuffer( 335 v8::Local<v8::Value> V8ValueConverterImpl::ToArrayBuffer(
339 v8::Isolate* isolate, 336 v8::Isolate* isolate,
340 v8::Local<v8::Object> creation_context, 337 v8::Local<v8::Object> creation_context,
341 const base::BinaryValue* value) const { 338 const base::BinaryValue* value) const {
342 blink::WebArrayBuffer buffer = 339 DCHECK(creation_context == isolate->GetCurrentContext());
jbroman 2017/02/07 23:22:03 This matches the DCHECK that already exists in Web
343 blink::WebArrayBuffer::create(value->GetSize(), 1); 340 v8::Local<v8::ArrayBuffer> buffer =
344 memcpy(buffer.data(), value->GetBuffer(), value->GetSize()); 341 v8::ArrayBuffer::New(isolate, value->GetSize());
345 return blink::WebArrayBufferConverter::toV8Value( 342 memcpy(buffer->GetContents().Data(), value->GetBuffer(), value->GetSize());
346 &buffer, creation_context, isolate); 343 return buffer;
347 } 344 }
348 345
349 std::unique_ptr<base::Value> V8ValueConverterImpl::FromV8ValueImpl( 346 std::unique_ptr<base::Value> V8ValueConverterImpl::FromV8ValueImpl(
350 FromV8ValueState* state, 347 FromV8ValueState* state,
351 v8::Local<v8::Value> val, 348 v8::Local<v8::Value> val,
352 v8::Isolate* isolate) const { 349 v8::Isolate* isolate) const {
353 CHECK(!val.IsEmpty()); 350 CHECK(!val.IsEmpty());
354 351
355 FromV8ValueState::Level state_level(state); 352 FromV8ValueState::Level state_level(state);
356 if (state->HasReachedMaxRecursionDepth()) 353 if (state->HasReachedMaxRecursionDepth())
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 487
491 std::unique_ptr<base::Value> V8ValueConverterImpl::FromV8ArrayBuffer( 488 std::unique_ptr<base::Value> V8ValueConverterImpl::FromV8ArrayBuffer(
492 v8::Local<v8::Object> val, 489 v8::Local<v8::Object> val,
493 v8::Isolate* isolate) const { 490 v8::Isolate* isolate) const {
494 if (strategy_) { 491 if (strategy_) {
495 std::unique_ptr<base::Value> out; 492 std::unique_ptr<base::Value> out;
496 if (strategy_->FromV8ArrayBuffer(val, &out, isolate)) 493 if (strategy_->FromV8ArrayBuffer(val, &out, isolate))
497 return out; 494 return out;
498 } 495 }
499 496
500 char* data = NULL; 497 if (val->IsArrayBuffer()) {
501 size_t length = 0; 498 auto contents = val.As<v8::ArrayBuffer>()->GetContents();
502 499 return base::BinaryValue::CreateWithCopiedBuffer(
503 std::unique_ptr<blink::WebArrayBuffer> array_buffer( 500 static_cast<const char*>(contents.Data()), contents.ByteLength());
504 blink::WebArrayBufferConverter::createFromV8Value(val, isolate)); 501 } else if (val->IsArrayBufferView()) {
505 std::unique_ptr<blink::WebArrayBufferView> view; 502 v8::Local<v8::ArrayBufferView> view = val.As<v8::ArrayBufferView>();
506 if (array_buffer) { 503 size_t byte_length = view->ByteLength();
507 data = reinterpret_cast<char*>(array_buffer->data()); 504 auto buffer = base::MakeUnique<char[]>(byte_length);
508 length = array_buffer->byteLength(); 505 view->CopyContents(buffer.get(), byte_length);
506 return base::MakeUnique<base::BinaryValue>(std::move(buffer), byte_length);
509 } else { 507 } else {
510 view.reset(blink::WebArrayBufferView::createFromV8Value(val)); 508 NOTREACHED() << "Only ArrayBuffer and ArrayBufferView should get here.";
511 if (view) { 509 return nullptr;
512 data = reinterpret_cast<char*>(view->baseAddress()) + view->byteOffset();
513 length = view->byteLength();
514 }
515 } 510 }
516
517 if (data)
518 return base::BinaryValue::CreateWithCopiedBuffer(data, length);
519 else
520 return nullptr;
521 } 511 }
522 512
523 std::unique_ptr<base::Value> V8ValueConverterImpl::FromV8Object( 513 std::unique_ptr<base::Value> V8ValueConverterImpl::FromV8Object(
524 v8::Local<v8::Object> val, 514 v8::Local<v8::Object> val,
525 FromV8ValueState* state, 515 FromV8ValueState* state,
526 v8::Isolate* isolate) const { 516 v8::Isolate* isolate) const {
527 ScopedUniquenessGuard uniqueness_guard(state, val); 517 ScopedUniquenessGuard uniqueness_guard(state, val);
528 if (!uniqueness_guard.is_valid()) 518 if (!uniqueness_guard.is_valid())
529 return base::Value::CreateNullValue(); 519 return base::Value::CreateNullValue();
530 520
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 continue; 609 continue;
620 610
621 result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()), 611 result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()),
622 std::move(child)); 612 std::move(child));
623 } 613 }
624 614
625 return std::move(result); 615 return std::move(result);
626 } 616 }
627 617
628 } // namespace content 618 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/child/v8_value_converter_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698