Index: sky/engine/core/script/dart_snapshot_loader.cc |
diff --git a/sky/engine/core/script/dart_snapshot_loader.cc b/sky/engine/core/script/dart_snapshot_loader.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fc296b33dd181b3c4a81a1705e4e68e46cad6bfc |
--- /dev/null |
+++ b/sky/engine/core/script/dart_snapshot_loader.cc |
@@ -0,0 +1,66 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "sky/engine/config.h" |
+#include "sky/engine/core/script/dart_snapshot_loader.h" |
+ |
+#include "base/callback.h" |
+#include "base/trace_event/trace_event.h" |
+#include "sky/engine/platform/weborigin/KURL.h" |
+#include "sky/engine/tonic/dart_api_scope.h" |
+#include "sky/engine/tonic/dart_converter.h" |
+#include "sky/engine/tonic/dart_error.h" |
+#include "sky/engine/tonic/dart_isolate_scope.h" |
+#include "sky/engine/wtf/MainThread.h" |
+ |
+using mojo::common::DataPipeDrainer; |
+ |
+namespace blink { |
+ |
+DartSnapshotLoader::DartSnapshotLoader(DartState* dart_state) |
+ : dart_state_(dart_state->GetWeakPtr()) { |
+} |
+ |
+DartSnapshotLoader::~DartSnapshotLoader() { |
+} |
+ |
+void DartSnapshotLoader::LoadSnapshot(const KURL& url, |
+ mojo::URLResponsePtr response, |
+ const base::Closure& callback) { |
+ TRACE_EVENT_ASYNC_BEGIN0("sky", "DartSnapshotLoader::LoadSnapshot", this); |
+ callback_ = callback; |
+ |
+ if (!response) { |
eseidel
2015/06/23 06:21:39
I regret this design choice. We need to untangle
abarth-chromium
2015/06/23 14:50:43
Yeah, we should be able to untangle it then.
|
+ fetcher_ = adoptPtr(new MojoFetcher(this, url)); |
+ } else { |
+ OnReceivedResponse(response.Pass()); |
+ } |
+} |
+ |
+void DartSnapshotLoader::OnReceivedResponse(mojo::URLResponsePtr response) { |
+ if (response->status_code != 200) { |
eseidel
2015/06/23 06:21:39
Are you sure you only want 200? You ahd me change
abarth-chromium
2015/06/23 14:50:43
I just cargo-culted this from the DartLoader. I c
|
+ callback_.Run(); |
+ return; |
+ } |
+ drainer_ = adoptPtr(new DataPipeDrainer(this, response->body.Pass())); |
+} |
+ |
+void DartSnapshotLoader::OnDataAvailable(const void* data, size_t num_bytes) { |
+ buffer_.append(static_cast<const uint8_t*>(data), num_bytes); |
+} |
+ |
+void DartSnapshotLoader::OnDataComplete() { |
+ TRACE_EVENT_ASYNC_END0("sky", "DartSnapshotLoader::LoadSnapshot", this); |
+ |
+ { |
+ DartIsolateScope scope(dart_state_->isolate()); |
+ DartApiScope api_scope; |
+ |
+ LogIfError(Dart_LoadScriptFromSnapshot(buffer_.data(), buffer_.size())); |
+ } |
+ |
+ callback_.Run(); |
+} |
+ |
+} // namespace blink |