Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f957658

Browse files
committed
Implemented isolate platform channels for desktop / embedders
1 parent a3bfe11 commit f957658

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

lib/ui/platform_dispatcher.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ class PlatformDispatcher {
584584
/// [token]. This is required if platform channels are to be used on a
585585
/// background isolate.
586586
void registerBackgroundIsolate(RootIsolateToken token) {
587-
if (!Platform.isIOS) {
587+
if (!(Platform.isIOS || Platform.isMacOS)) {
588588
// Issue: https://github.com/flutter/flutter/issues/13937
589589
throw UnimplementedError("Platform doesn't yet support platform channels on background isolates.");
590590
}

shell/platform/embedder/platform_view_embedder.cc

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,42 @@
44

55
#include "flutter/shell/platform/embedder/platform_view_embedder.h"
66

7+
#include "flutter/fml/make_copyable.h"
8+
79
namespace flutter {
810

11+
class PlatformViewEmbedder::EmbedderPlatformMessageHandler
12+
: public PlatformMessageHandler {
13+
public:
14+
EmbedderPlatformMessageHandler(
15+
PlatformViewEmbedder* parent,
16+
fml::RefPtr<fml::TaskRunner> platform_task_runner)
17+
: parent_(std::make_shared<PlatformViewEmbedder*>(parent)),
18+
platform_task_runner_(platform_task_runner) {}
19+
20+
virtual void HandlePlatformMessage(std::unique_ptr<PlatformMessage> message) {
21+
platform_task_runner_->PostTask(fml::MakeCopyable(
22+
[parent = parent_, message = std::move(message)]() mutable {
23+
if (*parent) {
24+
(*parent)->HandlePlatformMessage(std::move(message));
25+
} else {
26+
FML_DLOG(WARNING)
27+
<< "Dropping message on channel " << message->channel();
28+
}
29+
}));
30+
}
31+
virtual void InvokePlatformMessageResponseCallback(
32+
int response_id,
33+
std::unique_ptr<fml::Mapping> mapping) {}
34+
virtual void InvokePlatformMessageEmptyResponseCallback(int response_id) {}
35+
36+
void ClearParent() { parent_.reset(); }
37+
38+
private:
39+
std::shared_ptr<PlatformViewEmbedder*> parent_;
40+
fml::RefPtr<fml::TaskRunner> platform_task_runner_;
41+
};
42+
943
PlatformViewEmbedder::PlatformViewEmbedder(
1044
PlatformView::Delegate& delegate,
1145
flutter::TaskRunners task_runners,
@@ -17,7 +51,10 @@ PlatformViewEmbedder::PlatformViewEmbedder(
1751
embedder_surface_(
1852
std::make_unique<EmbedderSurfaceSoftware>(software_dispatch_table,
1953
external_view_embedder_)),
20-
platform_dispatch_table_(platform_dispatch_table) {}
54+
platform_dispatch_table_(platform_dispatch_table) {
55+
platform_message_handler_ = std::make_shared<EmbedderPlatformMessageHandler>(
56+
this, task_runners_.GetPlatformTaskRunner());
57+
}
2158

2259
#ifdef SHELL_ENABLE_GL
2360
PlatformViewEmbedder::PlatformViewEmbedder(
@@ -33,7 +70,10 @@ PlatformViewEmbedder::PlatformViewEmbedder(
3370
std::make_unique<EmbedderSurfaceGL>(gl_dispatch_table,
3471
fbo_reset_after_present,
3572
external_view_embedder_)),
36-
platform_dispatch_table_(platform_dispatch_table) {}
73+
platform_dispatch_table_(platform_dispatch_table) {
74+
platform_message_handler_ = std::make_shared<EmbedderPlatformMessageHandler>(
75+
this, task_runners_.GetPlatformTaskRunner());
76+
}
3777
#endif
3878

3979
#ifdef SHELL_ENABLE_METAL
@@ -46,7 +86,10 @@ PlatformViewEmbedder::PlatformViewEmbedder(
4686
: PlatformView(delegate, std::move(task_runners)),
4787
external_view_embedder_(external_view_embedder),
4888
embedder_surface_(std::move(embedder_surface)),
49-
platform_dispatch_table_(platform_dispatch_table) {}
89+
platform_dispatch_table_(platform_dispatch_table) {
90+
platform_message_handler_ = std::make_shared<EmbedderPlatformMessageHandler>(
91+
this, task_runners_.GetPlatformTaskRunner());
92+
}
5093
#endif
5194

5295
#ifdef SHELL_ENABLE_VULKAN
@@ -59,10 +102,15 @@ PlatformViewEmbedder::PlatformViewEmbedder(
59102
: PlatformView(delegate, std::move(task_runners)),
60103
external_view_embedder_(external_view_embedder),
61104
embedder_surface_(std::move(embedder_surface)),
62-
platform_dispatch_table_(platform_dispatch_table) {}
105+
platform_dispatch_table_(platform_dispatch_table) {
106+
platform_message_handler_ = std::make_shared<EmbedderPlatformMessageHandler>(
107+
this, task_runners_.GetPlatformTaskRunner());
108+
}
63109
#endif
64110

65-
PlatformViewEmbedder::~PlatformViewEmbedder() = default;
111+
PlatformViewEmbedder::~PlatformViewEmbedder() {
112+
platform_message_handler_->ClearParent();
113+
}
66114

67115
void PlatformViewEmbedder::UpdateSemantics(
68116
flutter::SemanticsNodeUpdates update,
@@ -146,4 +194,9 @@ void PlatformViewEmbedder::OnPreEngineRestart() const {
146194
}
147195
}
148196

197+
std::shared_ptr<PlatformMessageHandler>
198+
PlatformViewEmbedder::GetPlatformMessageHandler() const {
199+
return platform_message_handler_;
200+
}
201+
149202
} // namespace flutter

shell/platform/embedder/platform_view_embedder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ class PlatformViewEmbedder final : public PlatformView {
101101
void HandlePlatformMessage(std::unique_ptr<PlatformMessage> message) override;
102102

103103
private:
104+
class EmbedderPlatformMessageHandler;
104105
std::shared_ptr<EmbedderExternalViewEmbedder> external_view_embedder_;
105106
std::unique_ptr<EmbedderSurface> embedder_surface_;
107+
std::shared_ptr<EmbedderPlatformMessageHandler> platform_message_handler_;
106108
PlatformDispatchTable platform_dispatch_table_;
107109

108110
// |PlatformView|
@@ -124,6 +126,10 @@ class PlatformViewEmbedder final : public PlatformView {
124126
std::unique_ptr<std::vector<std::string>> ComputePlatformResolvedLocales(
125127
const std::vector<std::string>& supported_locale_data) override;
126128

129+
// |PlatformView|
130+
std::shared_ptr<PlatformMessageHandler> GetPlatformMessageHandler()
131+
const override;
132+
127133
FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewEmbedder);
128134
};
129135

0 commit comments

Comments
 (0)