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

Commit 174e349

Browse files
committed
stuart feedback 3
1 parent aa8d621 commit 174e349

File tree

5 files changed

+53
-7
lines changed

5 files changed

+53
-7
lines changed

shell/platform/common/client_wrapper/core_implementations.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ namespace flutter {
2626
// ========== binary_messenger_impl.h ==========
2727

2828
namespace {
29+
30+
using FlutterDesktopMessengerScopedLock =
31+
std::unique_ptr<FlutterDesktopMessenger,
32+
decltype(&FlutterDesktopMessengerUnlock)>;
33+
2934
// Passes |message| to |user_data|, which must be a BinaryMessageHandler, along
3035
// with a BinaryReply that will send a response on |message|'s response handle.
3136
//
@@ -42,9 +47,8 @@ void ForwardToHandler(FlutterDesktopMessengerRef messenger,
4247
BinaryReply reply_handler = [messenger_ptr, response_handle](
4348
const uint8_t* reply,
4449
size_t reply_size) mutable {
45-
// Note: This can be called on any thread.
46-
auto lock = std::unique_ptr<FlutterDesktopMessenger,
47-
decltype(&FlutterDesktopMessengerUnlock)>(
50+
// Note: This lambda can be called on any thread.
51+
auto lock = FlutterDesktopMessengerScopedLock(
4852
FlutterDesktopMessengerLock(messenger_ptr.get()),
4953
&FlutterDesktopMessengerUnlock);
5054
if (!FlutterDesktopMessengerIsAvailable(messenger_ptr.get())) {

shell/platform/embedder/embedder.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2242,12 +2242,12 @@ FlutterEngineResult FlutterPlatformMessageReleaseResponseHandle(
22422242
return kSuccess;
22432243
}
22442244

2245+
// Note: This can execute on any thread.
22452246
FlutterEngineResult FlutterEngineSendPlatformMessageResponse(
22462247
FLUTTER_API_SYMBOL(FlutterEngine) engine,
22472248
const FlutterPlatformMessageResponseHandle* handle,
22482249
const uint8_t* data,
22492250
size_t data_length) {
2250-
// Note: This can execute on any thread.
22512251
if (data_length != 0 && data == nullptr) {
22522252
return LOG_EMBEDDER_ERROR(
22532253
kInvalidArguments,

shell/platform/glfw/flutter_glfw.cc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,27 +156,43 @@ struct FlutterDesktopPluginRegistrar {
156156
struct FlutterDesktopMessenger {
157157
FlutterDesktopMessenger() = default;
158158

159+
/// Increments the reference count.
160+
///
161+
/// Thread-safe.
159162
void AddRef() { ref_count_.fetch_add(1); }
160163

164+
/// Decrements the reference count and deletes the object if the count has
165+
/// gone to zero.
166+
///
167+
/// Thread-safe.
161168
void Release() {
162169
int32_t old_count = ref_count_.fetch_sub(1);
163170
if (old_count <= 1) {
164171
delete this;
165172
}
166173
}
167174

175+
/// Getter for the engine field.
168176
FlutterDesktopEngineState* GetEngine() const { return engine_; }
177+
178+
/// Setter for the engine field.
179+
/// Thread-safe.
169180
void SetEngine(FlutterDesktopEngineState* engine) {
170181
std::scoped_lock lock(mutex_);
171182
engine_ = engine;
172183
}
173184

185+
/// Returns the mutex associated with the |FlutterDesktopMessenger|.
186+
///
187+
/// This mutex is used to synchronize reading or writing state inside the
188+
/// |FlutterDesktopMessenger| (ie |engine_|).
174189
std::mutex& GetMutex() { return mutex_; }
175190

176-
private:
177191
FlutterDesktopMessenger(const FlutterDesktopMessenger& value) = delete;
178192
FlutterDesktopMessenger& operator=(const FlutterDesktopMessenger& value) =
179193
delete;
194+
195+
private:
180196
// The engine that backs this messenger.
181197
FlutterDesktopEngineState* engine_;
182198
std::atomic<int32_t> ref_count_ = 0;

shell/platform/windows/public/flutter_windows.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,11 @@ FlutterDesktopEngineGetPluginRegistrar(FlutterDesktopEngineRef engine,
182182

183183
// Returns the messenger associated with the engine.
184184
//
185-
// This does not effect the reference count of the FlutterDesktopMessenger.
185+
// This does not provide an owning reference, so should *not* be balanced with a
186+
// call to |FlutterDesktopMessengerRelease|.
187+
//
188+
// Callers should use |FlutterDesktopMessengerAddRef| if the returned pointer
189+
// will potentially outlive 'engine', such as when passing it to another thread.
186190
FLUTTER_EXPORT FlutterDesktopMessengerRef
187191
FlutterDesktopEngineGetMessenger(FlutterDesktopEngineRef engine);
188192

shell/platform/windows/window_state.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,47 @@ struct FlutterDesktopPluginRegistrar {
4040
struct FlutterDesktopMessenger {
4141
public:
4242
FlutterDesktopMessenger() = default;
43+
44+
/// Getter for the engine field.
4345
flutter::FlutterWindowsEngine* GetEngine() const { return engine; }
46+
47+
/// Setter for the engine field.
48+
/// Thread-safe.
4449
void SetEngine(flutter::FlutterWindowsEngine* arg_engine) {
4550
std::scoped_lock lock(mutex_);
4651
engine = arg_engine;
4752
}
53+
54+
/// Increments the reference count.
55+
///
56+
/// Thread-safe.
4857
FlutterDesktopMessenger* AddRef() {
4958
ref_count_.fetch_add(1);
5059
return this;
5160
}
61+
62+
/// Decrements the reference count and deletes the object if the count has
63+
/// gone to zero.
64+
///
65+
/// Thread-safe.
5266
void Release() {
5367
int32_t old_count = ref_count_.fetch_sub(1);
5468
if (old_count <= 1) {
5569
delete this;
5670
}
5771
}
72+
73+
/// Returns the mutex associated with the |FlutterDesktopMessenger|.
74+
///
75+
/// This mutex is used to synchronize reading or writing state inside the
76+
/// |FlutterDesktopMessenger| (ie |engine|).
5877
std::mutex& GetMutex() { return mutex_; }
5978

79+
FlutterDesktopMessenger(const FlutterDesktopMessenger& value) = delete;
80+
FlutterDesktopMessenger& operator=(const FlutterDesktopMessenger& value) =
81+
delete;
82+
6083
private:
61-
FML_DISALLOW_COPY_AND_ASSIGN(FlutterDesktopMessenger);
6284
// The engine that owns this state object.
6385
flutter::FlutterWindowsEngine* engine = nullptr;
6486
std::mutex mutex_;

0 commit comments

Comments
 (0)