| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "sky/engine/core/script/dart_snapshot_loader.h" | 5 #include "sky/engine/tonic/dart_snapshot_loader.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/trace_event/trace_event.h" | 8 #include "base/trace_event/trace_event.h" |
| 9 #include "sky/engine/platform/weborigin/KURL.h" | |
| 10 #include "sky/engine/tonic/dart_api_scope.h" | 9 #include "sky/engine/tonic/dart_api_scope.h" |
| 11 #include "sky/engine/tonic/dart_converter.h" | 10 #include "sky/engine/tonic/dart_converter.h" |
| 12 #include "sky/engine/tonic/dart_error.h" | 11 #include "sky/engine/tonic/dart_error.h" |
| 13 #include "sky/engine/tonic/dart_isolate_scope.h" | 12 #include "sky/engine/tonic/dart_isolate_scope.h" |
| 14 #include "sky/engine/wtf/MainThread.h" | 13 #include "sky/engine/wtf/MainThread.h" |
| 15 | 14 |
| 16 using mojo::common::DataPipeDrainer; | 15 using mojo::common::DataPipeDrainer; |
| 17 | 16 |
| 18 namespace blink { | 17 namespace blink { |
| 19 | 18 |
| 20 DartSnapshotLoader::DartSnapshotLoader(DartState* dart_state) | 19 DartSnapshotLoader::DartSnapshotLoader(DartState* dart_state) |
| 21 : dart_state_(dart_state->GetWeakPtr()) { | 20 : dart_state_(dart_state->GetWeakPtr()) { |
| 22 } | 21 } |
| 23 | 22 |
| 24 DartSnapshotLoader::~DartSnapshotLoader() { | 23 DartSnapshotLoader::~DartSnapshotLoader() { |
| 25 } | 24 } |
| 26 | 25 |
| 27 void DartSnapshotLoader::LoadSnapshot(const KURL& url, | 26 void DartSnapshotLoader::LoadSnapshot(mojo::ScopedDataPipeConsumerHandle pipe, |
| 28 mojo::URLResponsePtr response, | |
| 29 const base::Closure& callback) { | 27 const base::Closure& callback) { |
| 30 TRACE_EVENT_ASYNC_BEGIN0("sky", "DartSnapshotLoader::LoadSnapshot", this); | 28 TRACE_EVENT_ASYNC_BEGIN0("sky", "DartSnapshotLoader::LoadSnapshot", this); |
| 29 |
| 31 callback_ = callback; | 30 callback_ = callback; |
| 32 | 31 drainer_ = adoptPtr(new DataPipeDrainer(this, pipe.Pass())); |
| 33 if (!response) { | |
| 34 fetcher_ = adoptPtr(new MojoFetcher(this, url)); | |
| 35 } else { | |
| 36 OnReceivedResponse(response.Pass()); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 void DartSnapshotLoader::OnReceivedResponse(mojo::URLResponsePtr response) { | |
| 41 if (response->status_code != 200) { | |
| 42 callback_.Run(); | |
| 43 return; | |
| 44 } | |
| 45 drainer_ = adoptPtr(new DataPipeDrainer(this, response->body.Pass())); | |
| 46 } | 32 } |
| 47 | 33 |
| 48 void DartSnapshotLoader::OnDataAvailable(const void* data, size_t num_bytes) { | 34 void DartSnapshotLoader::OnDataAvailable(const void* data, size_t num_bytes) { |
| 49 buffer_.append(static_cast<const uint8_t*>(data), num_bytes); | 35 buffer_.append(static_cast<const uint8_t*>(data), num_bytes); |
| 50 } | 36 } |
| 51 | 37 |
| 52 void DartSnapshotLoader::OnDataComplete() { | 38 void DartSnapshotLoader::OnDataComplete() { |
| 53 TRACE_EVENT_ASYNC_END0("sky", "DartSnapshotLoader::LoadSnapshot", this); | 39 TRACE_EVENT_ASYNC_END0("sky", "DartSnapshotLoader::LoadSnapshot", this); |
| 54 | 40 |
| 55 { | 41 { |
| 56 DartIsolateScope scope(dart_state_->isolate()); | 42 DartIsolateScope scope(dart_state_->isolate()); |
| 57 DartApiScope api_scope; | 43 DartApiScope api_scope; |
| 58 | 44 |
| 59 LogIfError(Dart_LoadScriptFromSnapshot(buffer_.data(), buffer_.size())); | 45 LogIfError(Dart_LoadScriptFromSnapshot(buffer_.data(), buffer_.size())); |
| 60 } | 46 } |
| 61 | 47 |
| 62 callback_.Run(); | 48 callback_.Run(); |
| 63 } | 49 } |
| 64 | 50 |
| 65 } // namespace blink | 51 } // namespace blink |
| OLD | NEW |