| 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_TESTING_DART_ISOLATE_RUNNER_H_ |
| 6 | #define FLUTTER_TESTING_DART_ISOLATE_RUNNER_H_ |
| 7 | |
| 8 | #include "flutter/common/task_runners.h" |
| 9 | #include "flutter/fml/make_copyable.h" |
| 10 | #include "flutter/fml/paths.h" |
| 11 | #include "flutter/fml/synchronization/waitable_event.h" |
| 12 | #include "flutter/fml/thread.h" |
| 13 | #include "flutter/runtime/dart_isolate.h" |
| 14 | #include "flutter/runtime/dart_vm.h" |
| 15 | #include "flutter/runtime/dart_vm_lifecycle.h" |
| 16 | |
| 17 | namespace flutter { |
| 18 | namespace testing { |
| 19 | |
| 20 | class AutoIsolateShutdown { |
| 21 | public: |
| 22 | AutoIsolateShutdown() = default; |
| 23 | |
| 24 | AutoIsolateShutdown(std::shared_ptr<DartIsolate> isolate, |
| 25 | fml::RefPtr<fml::TaskRunner> runner); |
| 26 | |
| 27 | ~AutoIsolateShutdown(); |
| 28 | |
| 29 | bool IsValid() const { return isolate_ != nullptr && runner_; } |
| 30 | |
| 31 | [[nodiscard]] bool RunInIsolateScope( |
| 32 | const std::function<bool(void)>& closure); |
| 33 | |
| 34 | void Shutdown(); |
| 35 | |
| 36 | DartIsolate* get() { |
| 37 | FML_CHECK(isolate_); |
| 38 | return isolate_.get(); |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | std::shared_ptr<DartIsolate> isolate_; |
| 43 | fml::RefPtr<fml::TaskRunner> runner_; |
| 44 | |
| 45 | FML_DISALLOW_COPY_AND_ASSIGN(AutoIsolateShutdown); |
| 46 | }; |
| 47 | |
| 48 | void RunDartCodeInIsolate( |
| 49 | DartVMRef& vm_ref, |
| 50 | std::unique_ptr<AutoIsolateShutdown>& result, |
| 51 | const Settings& settings, |
| 52 | const TaskRunners& task_runners, |
| 53 | std::string entrypoint, |
| 54 | const std::vector<std::string>& args, |
| 55 | const std::string& fixtures_path, |
| 56 | fml::WeakPtr<IOManager> io_manager = {}, |
| 57 | std::shared_ptr<VolatilePathTracker> volatile_path_tracker = nullptr); |
| 58 | |
| 59 | std::unique_ptr<AutoIsolateShutdown> RunDartCodeInIsolate( |
| 60 | DartVMRef& vm_ref, |
| 61 | const Settings& settings, |
| 62 | const TaskRunners& task_runners, |
| 63 | std::string entrypoint, |
| 64 | const std::vector<std::string>& args, |
| 65 | const std::string& fixtures_path, |
| 66 | fml::WeakPtr<IOManager> io_manager = {}, |
| 67 | std::shared_ptr<VolatilePathTracker> volatile_path_tracker = nullptr); |
| 68 | |
| 69 | } // namespace testing |
| 70 | } // namespace flutter |
| 71 | |
| 72 | #endif // FLUTTER_TESTING_DART_ISOLATE_RUNNER_H_ |
| 73 | |