isolate_configuration.cc 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
// Copyright 2018 The Flutter 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 "flutter/shell/common/isolate_configuration.h"

#include "flutter/runtime/dart_vm.h"

#ifdef ERROR
#undef ERROR
#endif

namespace shell {

IsolateConfiguration::IsolateConfiguration() = default;

IsolateConfiguration::~IsolateConfiguration() = default;

bool IsolateConfiguration::PrepareIsolate(
    fml::WeakPtr<blink::DartIsolate> isolate) {
  if (!isolate) {
    return false;
  }

  if (isolate->GetPhase() != blink::DartIsolate::Phase::LibrariesSetup) {
    FXL_DLOG(ERROR)
        << "Isolate was in incorrect phase to be prepared for running.";
    return false;
  }

  return DoPrepareIsolate(*isolate);
}

class PrecompiledIsolateConfiguration final : public IsolateConfiguration {
 public:
  PrecompiledIsolateConfiguration() = default;

  // |shell::IsolateConfiguration|
  bool DoPrepareIsolate(blink::DartIsolate& isolate) override {
    if (!blink::DartVM::IsRunningPrecompiledCode()) {
      return false;
    }
    return isolate.PrepareForRunningFromPrecompiledCode();
  }

 private:
  FXL_DISALLOW_COPY_AND_ASSIGN(PrecompiledIsolateConfiguration);
};

class SnapshotIsolateConfiguration : public IsolateConfiguration {
 public:
  SnapshotIsolateConfiguration(std::unique_ptr<fml::Mapping> snapshot)
      : snapshot_(std::move(snapshot)) {}

  // |shell::IsolateConfiguration|
  bool DoPrepareIsolate(blink::DartIsolate& isolate) override {
    if (blink::DartVM::IsRunningPrecompiledCode()) {
      return false;
    }
    return isolate.PrepareForRunningFromSnapshot(std::move(snapshot_));
  }

 private:
  std::unique_ptr<fml::Mapping> snapshot_;

  FXL_DISALLOW_COPY_AND_ASSIGN(SnapshotIsolateConfiguration);
};

class SourceIsolateConfiguration final : public IsolateConfiguration {
 public:
  SourceIsolateConfiguration(std::string main_path, std::string packages_path)
      : main_path_(std::move(main_path)),
        packages_path_(std::move(packages_path)) {}

  // |shell::IsolateConfiguration|
  bool DoPrepareIsolate(blink::DartIsolate& isolate) override {
    if (blink::DartVM::IsRunningPrecompiledCode()) {
      return false;
    }
    return isolate.PrepareForRunningFromSource(std::move(main_path_),
                                               std::move(packages_path_));
  }

 private:
  std::string main_path_;
  std::string packages_path_;

  FXL_DISALLOW_COPY_AND_ASSIGN(SourceIsolateConfiguration);
};

std::unique_ptr<IsolateConfiguration> IsolateConfiguration::InferFromSettings(
    const blink::Settings& settings,
    fxl::RefPtr<blink::AssetManager> asset_manager) {
  // Running in AOT mode.
  if (blink::DartVM::IsRunningPrecompiledCode()) {
    return CreateForPrecompiledCode();
  }

  // Run from sources.
  {
    const auto& main = settings.main_dart_file_path;
    const auto& packages = settings.packages_file_path;
    if (main.size() != 0 && packages.size() != 0) {
      return CreateForSource(std::move(main), std::move(packages));
    }
  }

  // Running from kernel snapshot.
  {
    std::vector<uint8_t> kernel;
    if (asset_manager && asset_manager->GetAsBuffer(
                             settings.application_kernel_asset, &kernel)) {
      return CreateForSnapshot(
          std::make_unique<fml::DataMapping>(std::move(kernel)));
    }
  }

  // Running from script snapshot.
  {
    std::vector<uint8_t> script_snapshot;
    if (asset_manager && asset_manager->GetAsBuffer(
                             settings.script_snapshot_path, &script_snapshot)) {
      return CreateForSnapshot(
          std::make_unique<fml::DataMapping>(std::move(script_snapshot)));
    }
  }

  return nullptr;
}

std::unique_ptr<IsolateConfiguration>
IsolateConfiguration::CreateForPrecompiledCode() {
  return std::make_unique<PrecompiledIsolateConfiguration>();
}

std::unique_ptr<IsolateConfiguration> IsolateConfiguration::CreateForSnapshot(
    std::unique_ptr<fml::Mapping> snapshot) {
  return std::make_unique<SnapshotIsolateConfiguration>(std::move(snapshot));
}

std::unique_ptr<IsolateConfiguration> IsolateConfiguration::CreateForSource(
    std::string main_path,
    std::string packages_path) {
  return std::make_unique<SourceIsolateConfiguration>(std::move(main_path),
                                                      std::move(packages_path));
}

}  // namespace shell