| 1 | // Copyright 2013 The Flutter 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 "flutter/runtime/dart_vm_data.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | namespace flutter { |
| 10 | |
| 11 | std::shared_ptr<const DartVMData> DartVMData::Create( |
| 12 | const Settings& settings, |
| 13 | fml::RefPtr<const DartSnapshot> vm_snapshot, |
| 14 | fml::RefPtr<const DartSnapshot> isolate_snapshot) { |
| 15 | if (!vm_snapshot || !vm_snapshot->IsValid()) { |
| 16 | // Caller did not provide a valid VM snapshot. Attempt to infer one |
| 17 | // from the settings. |
| 18 | vm_snapshot = DartSnapshot::VMSnapshotFromSettings(settings); |
| 19 | if (!vm_snapshot) { |
| 20 | FML_LOG(ERROR) |
| 21 | << "VM snapshot invalid and could not be inferred from settings." ; |
| 22 | return {}; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | if (!isolate_snapshot || !isolate_snapshot->IsValid()) { |
| 27 | // Caller did not provide a valid isolate snapshot. Attempt to infer one |
| 28 | // from the settings. |
| 29 | isolate_snapshot = DartSnapshot::IsolateSnapshotFromSettings(settings); |
| 30 | if (!isolate_snapshot) { |
| 31 | FML_LOG(ERROR) << "Isolate snapshot invalid and could not be inferred " |
| 32 | "from settings." ; |
| 33 | return {}; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | fml::RefPtr<const DartSnapshot> service_isolate_snapshot = |
| 38 | DartSnapshot::VMServiceIsolateSnapshotFromSettings(settings); |
| 39 | |
| 40 | return std::shared_ptr<const DartVMData>(new DartVMData( |
| 41 | settings, // |
| 42 | std::move(vm_snapshot), // |
| 43 | std::move(isolate_snapshot), // |
| 44 | std::move(service_isolate_snapshot) // |
| 45 | )); |
| 46 | } |
| 47 | |
| 48 | DartVMData::DartVMData(const Settings& settings, |
| 49 | fml::RefPtr<const DartSnapshot> vm_snapshot, |
| 50 | fml::RefPtr<const DartSnapshot> isolate_snapshot, |
| 51 | fml::RefPtr<const DartSnapshot> service_isolate_snapshot) |
| 52 | : settings_(settings), |
| 53 | vm_snapshot_(std::move(vm_snapshot)), |
| 54 | isolate_snapshot_(std::move(isolate_snapshot)), |
| 55 | service_isolate_snapshot_(std::move(service_isolate_snapshot)) {} |
| 56 | |
| 57 | DartVMData::~DartVMData() = default; |
| 58 | |
| 59 | const Settings& DartVMData::GetSettings() const { |
| 60 | return settings_; |
| 61 | } |
| 62 | |
| 63 | const DartSnapshot& DartVMData::GetVMSnapshot() const { |
| 64 | return *vm_snapshot_; |
| 65 | } |
| 66 | |
| 67 | fml::RefPtr<const DartSnapshot> DartVMData::GetIsolateSnapshot() const { |
| 68 | return isolate_snapshot_; |
| 69 | } |
| 70 | |
| 71 | fml::RefPtr<const DartSnapshot> DartVMData::GetServiceIsolateSnapshot() const { |
| 72 | // Use the specialized snapshot for the service isolate if the embedder |
| 73 | // provides one. Otherwise, use the application snapshot. |
| 74 | return service_isolate_snapshot_ ? service_isolate_snapshot_ |
| 75 | : isolate_snapshot_; |
| 76 | } |
| 77 | |
| 78 | bool DartVMData::GetServiceIsolateSnapshotNullSafety() const { |
| 79 | if (service_isolate_snapshot_) { |
| 80 | // The specialized snapshot for the service isolate is always built |
| 81 | // using null safety. However, calling Dart_DetectNullSafety on |
| 82 | // the service isolate snapshot will not work as expected - it will |
| 83 | // instead return a cached value representing the app snapshot. |
| 84 | return true; |
| 85 | } else { |
| 86 | return isolate_snapshot_->IsNullSafetyEnabled(application_kernel_mapping: nullptr); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | } // namespace flutter |
| 91 | |