Flutter Windows Embedder
flutter_desktop_messenger.h
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 #ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_DESKTOP_MESSENGER_H_
6 #define FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_DESKTOP_MESSENGER_H_
7 
8 #include <atomic>
9 #include <mutex>
10 
11 #include "flutter/fml/macros.h"
13 
14 namespace flutter {
15 
16 class FlutterWindowsEngine;
17 
18 /// A messenger object used to invoke platform messages.
19 ///
20 /// On Windows, the message handler is essentially the |FlutterWindowsEngine|,
21 /// this allows a handle to the |FlutterWindowsEngine| that will become
22 /// invalidated if the |FlutterWindowsEngine| is destroyed.
24  public:
25  FlutterDesktopMessenger() = default;
26 
27  /// Convert to FlutterDesktopMessengerRef.
29  return reinterpret_cast<FlutterDesktopMessengerRef>(this);
30  }
31 
32  /// Convert from FlutterDesktopMessengerRef.
34  return reinterpret_cast<FlutterDesktopMessenger*>(ref);
35  }
36 
37  /// Getter for the engine field.
38  flutter::FlutterWindowsEngine* GetEngine() const { return engine; }
39 
40  /// Setter for the engine field.
41  /// Thread-safe.
43  std::scoped_lock lock(mutex_);
44  engine = arg_engine;
45  }
46 
47  /// Increments the reference count.
48  ///
49  /// Thread-safe.
51  ref_count_.fetch_add(1);
52  return this;
53  }
54 
55  /// Decrements the reference count and deletes the object if the count has
56  /// gone to zero.
57  ///
58  /// Thread-safe.
59  void Release() {
60  int32_t old_count = ref_count_.fetch_sub(1);
61  if (old_count <= 1) {
62  delete this;
63  }
64  }
65 
66  /// Returns the mutex associated with the |FlutterDesktopMessenger|.
67  ///
68  /// This mutex is used to synchronize reading or writing state inside the
69  /// |FlutterDesktopMessenger| (ie |engine|).
70  std::mutex& GetMutex() { return mutex_; }
71 
72  private:
73  // The engine that owns this state object.
74  flutter::FlutterWindowsEngine* engine = nullptr;
75  std::mutex mutex_;
76  std::atomic<int32_t> ref_count_ = 0;
77 
78  FML_DISALLOW_COPY_AND_ASSIGN(FlutterDesktopMessenger);
79 };
80 
81 } // namespace flutter
82 
83 #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_DESKTOP_MESSENGER_H_
flutter::FlutterWindowsEngine
Definition: flutter_windows_engine.h:90
flutter_messenger.h
flutter::FlutterDesktopMessenger::SetEngine
void SetEngine(flutter::FlutterWindowsEngine *arg_engine)
Definition: flutter_desktop_messenger.h:42
flutter::FlutterDesktopMessenger::ToRef
FlutterDesktopMessengerRef ToRef()
Convert to FlutterDesktopMessengerRef.
Definition: flutter_desktop_messenger.h:28
flutter::FlutterDesktopMessenger
Definition: flutter_desktop_messenger.h:23
flutter::FlutterDesktopMessenger::GetMutex
std::mutex & GetMutex()
Definition: flutter_desktop_messenger.h:70
flutter::FlutterDesktopMessenger::Release
void Release()
Definition: flutter_desktop_messenger.h:59
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::FlutterDesktopMessenger::GetEngine
flutter::FlutterWindowsEngine * GetEngine() const
Getter for the engine field.
Definition: flutter_desktop_messenger.h:38
flutter::FlutterDesktopMessenger::AddRef
FlutterDesktopMessenger * AddRef()
Definition: flutter_desktop_messenger.h:50
flutter::FlutterDesktopMessenger::FlutterDesktopMessenger
FlutterDesktopMessenger()=default
FlutterDesktopMessengerRef
struct FlutterDesktopMessenger * FlutterDesktopMessengerRef
Definition: flutter_messenger.h:19
flutter::FlutterDesktopMessenger::FromRef
static FlutterDesktopMessenger * FromRef(FlutterDesktopMessengerRef ref)
Convert from FlutterDesktopMessengerRef.
Definition: flutter_desktop_messenger.h:33