| 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 | #ifndef FLUTTER_RUNTIME_DART_VM_LIFECYCLE_H_ |
| 6 | #define FLUTTER_RUNTIME_DART_VM_LIFECYCLE_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | |
| 10 | #include "flutter/fml/macros.h" |
| 11 | #include "flutter/lib/ui/isolate_name_server/isolate_name_server.h" |
| 12 | #include "flutter/runtime/dart_vm.h" |
| 13 | #include "flutter/runtime/service_protocol.h" |
| 14 | #include "third_party/dart/runtime/include/dart_tools_api.h" |
| 15 | |
| 16 | namespace flutter { |
| 17 | |
| 18 | // A strong reference to the Dart VM. There can only be one VM running in the |
| 19 | // process at any given time. A reference to the VM may only be obtained via the |
| 20 | // |Create| method. In case there is already a running instance of the VM in the |
| 21 | // process, a strong reference to that VM is obtained and the arguments to the |
| 22 | // |Create| call ignored. If there is no VM already running in the process, a VM |
| 23 | // is initialized in a thread safe manner and returned to the caller. The VM |
| 24 | // will shutdown only when all callers relinquish their references (by |
| 25 | // collecting their instances of this class). |
| 26 | // |
| 27 | // DartVMRef instances may be created on any thread. |
| 28 | class DartVMRef { |
| 29 | public: |
| 30 | [[nodiscard]] static DartVMRef Create( |
| 31 | const Settings& settings, |
| 32 | fml::RefPtr<const DartSnapshot> vm_snapshot = nullptr, |
| 33 | fml::RefPtr<const DartSnapshot> isolate_snapshot = nullptr); |
| 34 | |
| 35 | DartVMRef(const DartVMRef&) = default; |
| 36 | |
| 37 | DartVMRef(DartVMRef&&); |
| 38 | |
| 39 | ~DartVMRef(); |
| 40 | |
| 41 | // This is an inherently racy way to check if a VM instance is running and |
| 42 | // should not be used outside of unit-tests where there is a known threading |
| 43 | // model. |
| 44 | static bool IsInstanceRunning(); |
| 45 | |
| 46 | static std::shared_ptr<const DartVMData> GetVMData(); |
| 47 | |
| 48 | static std::shared_ptr<ServiceProtocol> GetServiceProtocol(); |
| 49 | |
| 50 | static std::shared_ptr<IsolateNameServer> GetIsolateNameServer(); |
| 51 | |
| 52 | explicit operator bool() const { return static_cast<bool>(vm_); } |
| 53 | |
| 54 | DartVM* get() { |
| 55 | FML_DCHECK(vm_); |
| 56 | return vm_.get(); |
| 57 | } |
| 58 | |
| 59 | const DartVM* get() const { |
| 60 | FML_DCHECK(vm_); |
| 61 | return vm_.get(); |
| 62 | } |
| 63 | |
| 64 | DartVM* operator->() { |
| 65 | FML_DCHECK(vm_); |
| 66 | return vm_.get(); |
| 67 | } |
| 68 | |
| 69 | const DartVM* operator->() const { |
| 70 | FML_DCHECK(vm_); |
| 71 | return vm_.get(); |
| 72 | } |
| 73 | |
| 74 | DartVM* operator&() { |
| 75 | FML_DCHECK(vm_); |
| 76 | return vm_.get(); |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | friend class DartIsolate; |
| 81 | |
| 82 | std::shared_ptr<DartVM> vm_; |
| 83 | |
| 84 | explicit DartVMRef(std::shared_ptr<DartVM> vm); |
| 85 | |
| 86 | // Only used by Dart Isolate to register itself with the VM. |
| 87 | static DartVM* GetRunningVM(); |
| 88 | }; |
| 89 | |
| 90 | } // namespace flutter |
| 91 | |
| 92 | #endif // FLUTTER_RUNTIME_DART_VM_LIFECYCLE_H_ |
| 93 | |