OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "tonic/dart_snapshot_loader.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/trace_event/trace_event.h" |
| 9 #include "tonic/dart_api_scope.h" |
| 10 #include "tonic/dart_converter.h" |
| 11 #include "tonic/dart_error.h" |
| 12 #include "tonic/dart_isolate_scope.h" |
| 13 |
| 14 using mojo::common::DataPipeDrainer; |
| 15 |
| 16 namespace blink { |
| 17 |
| 18 DartSnapshotLoader::DartSnapshotLoader(DartState* dart_state) |
| 19 : dart_state_(dart_state->GetWeakPtr()) { |
| 20 } |
| 21 |
| 22 DartSnapshotLoader::~DartSnapshotLoader() { |
| 23 } |
| 24 |
| 25 void DartSnapshotLoader::LoadSnapshot(mojo::ScopedDataPipeConsumerHandle pipe, |
| 26 const base::Closure& callback) { |
| 27 TRACE_EVENT_ASYNC_BEGIN0("sky", "DartSnapshotLoader::LoadSnapshot", this); |
| 28 |
| 29 callback_ = callback; |
| 30 drainer_ = std::unique_ptr<mojo::common::DataPipeDrainer>( |
| 31 new DataPipeDrainer(this, pipe.Pass())); |
| 32 } |
| 33 |
| 34 void DartSnapshotLoader::OnDataAvailable(const void* data, size_t num_bytes) { |
| 35 const uint8_t* bytes = static_cast<const uint8_t*>(data); |
| 36 buffer_.insert(buffer_.end(), bytes, bytes + num_bytes); |
| 37 } |
| 38 |
| 39 void DartSnapshotLoader::OnDataComplete() { |
| 40 TRACE_EVENT_ASYNC_END0("sky", "DartSnapshotLoader::LoadSnapshot", this); |
| 41 |
| 42 { |
| 43 DartIsolateScope scope(dart_state_->isolate()); |
| 44 DartApiScope api_scope; |
| 45 |
| 46 LogIfError(Dart_LoadScriptFromSnapshot(buffer_.data(), buffer_.size())); |
| 47 } |
| 48 |
| 49 callback_.Run(); |
| 50 } |
| 51 |
| 52 } // namespace blink |
OLD | NEW |