Flutter Engine
The Flutter Engine
ui_dart_state.cc
Go to the documentation of this file.
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/lib/ui/ui_dart_state.h"
6
7#include <iostream>
8#include <utility>
9
10#include "flutter/fml/message_loop.h"
11#include "flutter/lib/ui/window/platform_configuration.h"
12#include "flutter/lib/ui/window/platform_message.h"
15
16#if defined(FML_OS_ANDROID)
17#include <android/log.h>
18#elif defined(FML_OS_IOS)
19extern "C" {
20// Cannot import the syslog.h header directly because of macro collision.
21extern void syslog(int, const char*, ...);
22}
23#endif
24
25using tonic::ToDart;
26
27namespace flutter {
28
30 : task_runners(task_runners) {}
31
33 const TaskRunners& task_runners,
35 fml::WeakPtr<IOManager> io_manager,
37 fml::WeakPtr<ImageDecoder> image_decoder,
38 fml::WeakPtr<ImageGeneratorRegistry> image_generator_registry,
39 std::string advisory_script_uri,
40 std::string advisory_script_entrypoint,
41 std::shared_ptr<VolatilePathTracker> volatile_path_tracker,
42 std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner,
43 bool enable_impeller,
44 impeller::RuntimeStageBackend runtime_stage_backend)
45 : task_runners(task_runners),
46 snapshot_delegate(std::move(snapshot_delegate)),
47 io_manager(std::move(io_manager)),
48 unref_queue(std::move(unref_queue)),
49 image_decoder(std::move(image_decoder)),
50 image_generator_registry(std::move(image_generator_registry)),
51 advisory_script_uri(std::move(advisory_script_uri)),
52 advisory_script_entrypoint(std::move(advisory_script_entrypoint)),
53 volatile_path_tracker(std::move(volatile_path_tracker)),
54 concurrent_task_runner(std::move(concurrent_task_runner)),
55 enable_impeller(enable_impeller),
56 runtime_stage_backend(runtime_stage_backend) {}
57
59 TaskObserverAdd add_callback,
60 TaskObserverRemove remove_callback,
61 std::string logger_prefix,
63 LogMessageCallback log_message_callback,
64 std::shared_ptr<IsolateNameServer> isolate_name_server,
65 bool is_root_isolate,
66 const UIDartState::Context& context)
67 : add_callback_(std::move(add_callback)),
68 remove_callback_(std::move(remove_callback)),
69 logger_prefix_(std::move(logger_prefix)),
70 is_root_isolate_(is_root_isolate),
71 unhandled_exception_callback_(std::move(unhandled_exception_callback)),
72 log_message_callback_(std::move(log_message_callback)),
73 isolate_name_server_(std::move(isolate_name_server)),
74 context_(context) {
75 AddOrRemoveTaskObserver(true /* add */);
76}
77
79 AddOrRemoveTaskObserver(false /* remove */);
80}
81
82const std::string& UIDartState::GetAdvisoryScriptURI() const {
83 return context_.advisory_script_uri;
84}
85
87 return context_.enable_impeller;
88}
89
91 return context_.runtime_stage_backend;
92}
93
94void UIDartState::DidSetIsolate() {
95 main_port_ = Dart_GetMainPortId();
96 std::ostringstream debug_name;
97 // main.dart$main-1234
98 debug_name << context_.advisory_script_uri << "$"
99 << context_.advisory_script_entrypoint << "-" << main_port_;
101}
102
107 tonic::ToDart("UI actions are only available on root isolate."));
108 }
109}
110
111void UIDartState::SetDebugName(const std::string& debug_name) {
112 debug_name_ = debug_name;
113 if (platform_configuration_) {
114 platform_configuration_->client()->UpdateIsolateDescription(debug_name_,
115 main_port_);
116 }
117}
118
120 return static_cast<UIDartState*>(DartState::Current());
121}
122
124 std::unique_ptr<PlatformConfiguration> platform_configuration) {
126 platform_configuration_ = std::move(platform_configuration);
127 if (platform_configuration_) {
128 platform_configuration_->client()->UpdateIsolateDescription(debug_name_,
129 main_port_);
130 }
131}
132
134 std::weak_ptr<PlatformMessageHandler> handler) {
136 platform_message_handler_ = std::move(handler);
137}
138
140 return context_.task_runners;
141}
142
144 return context_.io_manager;
145}
146
148 return context_.unref_queue;
149}
150
151std::shared_ptr<VolatilePathTracker> UIDartState::GetVolatilePathTracker()
152 const {
153 return context_.volatile_path_tracker;
154}
155
156std::shared_ptr<fml::ConcurrentTaskRunner>
158 return context_.concurrent_task_runner;
159}
160
163 return;
164 }
165
166 microtask_queue_.ScheduleMicrotask(closure);
167}
168
170 microtask_queue_.RunMicrotasks();
171}
172
173void UIDartState::AddOrRemoveTaskObserver(bool add) {
174 auto task_runner = context_.task_runners.GetUITaskRunner();
175 if (!task_runner) {
176 // This may happen in case the isolate has no thread affinity (for example,
177 // the service isolate).
178 return;
179 }
180 FML_DCHECK(add_callback_ && remove_callback_);
181 if (add) {
182 add_callback_(reinterpret_cast<intptr_t>(this),
183 [this]() { this->FlushMicrotasksNow(); });
184 } else {
185 remove_callback_(reinterpret_cast<intptr_t>(this));
186 }
187}
188
191 return context_.snapshot_delegate;
192}
193
195 return context_.image_decoder;
196}
197
199 const {
200 return context_.image_generator_registry;
201}
202
203std::shared_ptr<IsolateNameServer> UIDartState::GetIsolateNameServer() const {
204 return isolate_name_server_;
205}
206
209 if (error == tonic::kNoError) {
210 error = microtask_queue_.GetLastError();
211 }
212 return error;
213}
214
215void UIDartState::LogMessage(const std::string& tag,
216 const std::string& message) const {
217 if (log_message_callback_) {
218 log_message_callback_(tag, message);
219 } else {
220 // Fall back to previous behavior if unspecified.
221#if defined(FML_OS_ANDROID)
222 __android_log_print(ANDROID_LOG_INFO, tag.c_str(), "%.*s",
223 static_cast<int>(message.size()), message.c_str());
224#elif defined(FML_OS_IOS)
225 std::stringstream stream;
226 if (!tag.empty()) {
227 stream << tag << ": ";
228 }
229 stream << message;
230 std::string log = stream.str();
231 syslog(1 /* LOG_ALERT */, "%.*s", static_cast<int>(log.size()),
232 log.c_str());
233#else
234 if (!tag.empty()) {
235 std::cout << tag << ": ";
236 }
237 std::cout << message << std::endl;
238#endif
239 }
240}
241
243 std::unique_ptr<PlatformMessage> message) {
244 if (platform_configuration_) {
245 platform_configuration_->client()->HandlePlatformMessage(
246 std::move(message));
247 } else {
248 std::shared_ptr<PlatformMessageHandler> handler =
249 platform_message_handler_.lock();
250 if (handler) {
251 handler->HandlePlatformMessage(std::move(message));
252 } else {
253 return tonic::ToDart(
254 "No platform channel handler registered for background isolate.");
255 }
256 }
257
258 return Dart_Null();
259}
260
262 return IsRootIsolate() ? reinterpret_cast<int64_t>(this) : 0;
263}
264
266 char** error) {
268 return nullptr;
269}
270
271} // namespace flutter
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
fml::RefPtr< flutter::SkiaUnrefQueue > GetSkiaUnrefQueue() const
std::shared_ptr< fml::ConcurrentTaskRunner > GetConcurrentTaskRunner() const
PlatformConfiguration * platform_configuration() const
fml::WeakPtr< ImageDecoder > GetImageDecoder() const
UnhandledExceptionCallback unhandled_exception_callback() const
bool IsRootIsolate() const
void SetPlatformConfiguration(std::unique_ptr< PlatformConfiguration > platform_configuration)
std::shared_ptr< VolatilePathTracker > GetVolatilePathTracker() const
const std::string & logger_prefix() const
tonic::DartErrorHandleType GetLastError()
fml::WeakPtr< IOManager > GetIOManager() const
const TaskRunners & GetTaskRunners() const
fml::WeakPtr< ImageGeneratorRegistry > GetImageGeneratorRegistry() const
virtual Dart_Isolate CreatePlatformIsolate(Dart_Handle entry_point, char **error)
bool IsImpellerEnabled() const
Whether Impeller is enabled for this application.
std::shared_ptr< IsolateNameServer > GetIsolateNameServer() const
void SetDebugName(const std::string &name)
int64_t GetRootIsolateToken() const
void ScheduleMicrotask(Dart_Handle handle)
fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > GetSnapshotDelegate() const
const std::string & GetAdvisoryScriptURI() const
impeller::RuntimeStageBackend GetRuntimeStageBackend() const
The expected type for runtime stage shaders.
void LogMessage(const std::string &tag, const std::string &message) const
void SetPlatformMessageHandler(std::weak_ptr< PlatformMessageHandler > handler)
static UIDartState * Current()
static void ThrowIfUIOperationsProhibited()
Dart_Handle HandlePlatformMessage(std::unique_ptr< PlatformMessage > message)
UIDartState(TaskObserverAdd add_callback, TaskObserverRemove remove_callback, std::string logger_prefix, UnhandledExceptionCallback unhandled_exception_callback, LogMessageCallback log_message_callback, std::shared_ptr< IsolateNameServer > isolate_name_server, bool is_root_isolate_, const UIDartState::Context &context)
const std::string & debug_name() const
DartErrorHandleType isolate_last_error() const
void ScheduleMicrotask(Dart_Handle callback)
DartErrorHandleType GetLastError()
DartMessageHandler & message_handler()
Definition: dart_state.h:61
DART_EXPORT void Dart_EnterScope(void)
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
struct _Dart_Isolate * Dart_Isolate
Definition: dart_api.h:88
DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception)
DART_EXPORT Dart_Handle Dart_Null(void)
DART_EXPORT Dart_Port Dart_GetMainPortId(void)
DART_EXPORT bool Dart_IsClosure(Dart_Handle object)
const uint8_t uint32_t uint32_t GError ** error
#define FML_UNREACHABLE()
Definition: logging.h:109
#define FML_DCHECK(condition)
Definition: logging.h:103
Win32Message message
std::function< void(intptr_t)> TaskObserverRemove
Definition: settings.h:83
std::function< void(intptr_t, fml::closure)> TaskObserverAdd
Definition: settings.h:82
std::function< bool(const std::string &, const std::string &)> UnhandledExceptionCallback
Definition: settings.h:86
std::function< void(const std::string &, const std::string &)> LogMessageCallback
Definition: settings.h:89
std::function< void()> closure
Definition: closure.h:14
Definition: ref_ptr.h:256
Dart_Handle ToDart(const T &object)
DartErrorHandleType
Definition: dart_error.h:67
@ kNoError
Definition: dart_error.h:68
bool CheckAndHandleError(Dart_Handle handle)
Definition: dart_error.cc:33
The subset of state which is owned by the shell or engine and passed through the RuntimeController in...
Definition: ui_dart_state.h:45
fml::RefPtr< SkiaUnrefQueue > unref_queue
Definition: ui_dart_state.h:76
fml::WeakPtr< ImageGeneratorRegistry > image_generator_registry
Definition: ui_dart_state.h:84
fml::WeakPtr< IOManager > io_manager
The IO manager used by the isolate for asynchronous texture uploads.
Definition: ui_dart_state.h:72
Context(const TaskRunners &task_runners)
const TaskRunners task_runners
Definition: ui_dart_state.h:64
std::shared_ptr< VolatilePathTracker > volatile_path_tracker
Cache for tracking path volatility.
Definition: ui_dart_state.h:96
fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > snapshot_delegate
Definition: ui_dart_state.h:69
bool enable_impeller
Whether Impeller is enabled or not.
impeller::RuntimeStageBackend runtime_stage_backend
The expected backend for runtime stage shaders.
fml::WeakPtr< ImageDecoder > image_decoder
The image decoder.
Definition: ui_dart_state.h:79
std::shared_ptr< fml::ConcurrentTaskRunner > concurrent_task_runner
std::string advisory_script_entrypoint
Definition: ui_dart_state.h:93