Flutter Engine
The Flutter Engine
platform_message_response_dart.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/window/platform_message_response_dart.h"
6
7#include <utility>
8
9#include "flutter/common/task_runners.h"
10#include "flutter/fml/make_copyable.h"
11#include "flutter/fml/trace_event.h"
15
16static std::atomic<uint64_t> platform_message_counter = 1;
17
18namespace flutter {
19namespace {
20
21void MappingFinalizer(void* isolate_callback_data, void* peer) {
22 delete static_cast<fml::Mapping*>(peer);
23}
24
25template <typename Callback, typename TaskRunner, typename Result>
26void PostCompletion(Callback&& callback,
27 const TaskRunner& ui_task_runner,
28 bool* is_complete,
29 const std::string& channel,
30 Result&& result) {
31 if (callback.is_empty()) {
32 return;
33 }
34 FML_DCHECK(!*is_complete);
35 *is_complete = true;
36 uint64_t platform_message_id = platform_message_counter.fetch_add(1);
37 TRACE_EVENT_ASYNC_BEGIN1("flutter", "PlatformChannel ScheduleResult",
38 platform_message_id, "channel", channel.c_str());
39 ui_task_runner->PostTask(fml::MakeCopyable(
40 [callback = std::move(callback), platform_message_id,
41 result = std::move(result), channel = channel]() mutable {
42 TRACE_EVENT_ASYNC_END0("flutter", "PlatformChannel ScheduleResult",
43 platform_message_id);
44 std::shared_ptr<tonic::DartState> dart_state =
45 callback.dart_state().lock();
46 if (!dart_state) {
47 return;
48 }
49 tonic::DartState::Scope scope(dart_state);
50 tonic::DartInvoke(callback.Release(), {result()});
51 }));
52}
53} // namespace
54
57 fml::RefPtr<fml::TaskRunner> ui_task_runner,
58 const std::string& channel)
59 : callback_(std::move(callback)),
60 ui_task_runner_(std::move(ui_task_runner)),
61 channel_(channel) {}
62
64 if (!callback_.is_empty()) {
66 [callback = std::move(callback_)]() mutable { callback.Clear(); }));
67 }
68}
69
70void PlatformMessageResponseDart::Complete(std::unique_ptr<fml::Mapping> data) {
71 PostCompletion(
73 [data = std::move(data)]() mutable {
74 Dart_Handle byte_buffer;
75 intptr_t size = data->GetSize();
77 const void* mapping = data->GetMapping();
78 byte_buffer = Dart_NewUnmodifiableExternalTypedDataWithFinalizer(
79 /*type=*/Dart_TypedData_kByteData,
80 /*data=*/mapping,
81 /*length=*/size,
82 /*peer=*/data.release(),
83 /*external_allocation_size=*/size,
84 /*callback=*/MappingFinalizer);
85 } else {
86 Dart_Handle mutable_byte_buffer =
87 tonic::DartByteData::Create(data->GetMapping(), data->GetSize());
88 Dart_Handle ui_lib = Dart_LookupLibrary(
89 tonic::DartConverter<std::string>().ToDart("dart:ui"));
90 FML_DCHECK(!(Dart_IsNull(ui_lib) || Dart_IsError(ui_lib)));
91 byte_buffer = Dart_Invoke(ui_lib,
92 tonic::DartConverter<std::string>().ToDart(
93 "_wrapUnmodifiableByteData"),
94 1, &mutable_byte_buffer);
95 FML_DCHECK(!(Dart_IsNull(byte_buffer) || Dart_IsError(byte_buffer)));
96 }
97
98 return byte_buffer;
99 });
100}
101
103 PostCompletion(std::move(callback_), ui_task_runner_, &is_complete_, channel_,
104 [] { return Dart_Null(); });
105}
106
107} // namespace flutter
void Complete(std::unique_ptr< fml::Mapping > data) override
PlatformMessageResponseDart(tonic::DartPersistentValue callback, fml::RefPtr< fml::TaskRunner > ui_task_runner, const std::string &channel)
virtual void PostTask(const fml::closure &task) override
Definition: task_runner.cc:24
static const size_t kExternalSizeThreshold
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT Dart_Handle Dart_Null(void)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
GAsyncResult * result
#define FML_DCHECK(condition)
Definition: logging.h:103
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition: switches.h:41
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57
std::function< void(MTLRenderPipelineDescriptor *)> Callback
Definition: ref_ptr.h:256
Dart_Handle DartInvoke(Dart_Handle closure, std::initializer_list< Dart_Handle > args)
Definition: dart_invoke.cc:20
static std::atomic< uint64_t > platform_message_counter
#define TRACE_EVENT_ASYNC_BEGIN1(category_group, name, id, arg1_name, arg1_val)
Definition: trace_event.h:164
#define TRACE_EVENT_ASYNC_END0(category_group, name, id)
Definition: trace_event.h:161