Skip to content

Commit f287595

Browse files
committed
Remove engine dependency of LifecycleChannel (#127)
* Remove engine dependency of LifecycleChannel * Implement StringSerializer * Remove StringSerializer and implement StringCodec
1 parent 717cd63 commit f287595

File tree

6 files changed

+82
-24
lines changed

6 files changed

+82
-24
lines changed

shell/platform/tizen/channels/key_event_channel.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <map>
88

9+
#include "flutter/shell/platform/common/json_message_codec.h"
910
#include "flutter/shell/platform/tizen/tizen_log.h"
1011

1112
namespace flutter {

shell/platform/tizen/channels/key_event_channel.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h"
1313
#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h"
14-
#include "flutter/shell/platform/common/json_message_codec.h"
1514
#include "rapidjson/document.h"
1615

1716
namespace flutter {

shell/platform/tizen/channels/lifecycle_channel.cc

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "lifecycle_channel.h"
66

7-
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
7+
#include "flutter/shell/platform/tizen/channels/string_codec.h"
88
#include "flutter/shell/platform/tizen/tizen_log.h"
99

1010
namespace flutter {
@@ -20,35 +20,32 @@ constexpr char kDetached[] = "AppLifecycleState.detached";
2020

2121
} // namespace
2222

23-
LifecycleChannel::LifecycleChannel(FlutterTizenEngine* engine)
24-
: engine_(engine) {}
23+
LifecycleChannel::LifecycleChannel(BinaryMessenger* messenger)
24+
: channel_(std::make_unique<BasicMessageChannel<EncodableValue>>(
25+
messenger,
26+
kChannelName,
27+
&StringCodec::GetInstance())) {}
2528

2629
LifecycleChannel::~LifecycleChannel() {}
2730

28-
void LifecycleChannel::SendLifecycleMessage(const char message[]) {
29-
engine_->SendPlatformMessage(kChannelName,
30-
reinterpret_cast<const uint8_t*>(message),
31-
strlen(message), nullptr, nullptr);
32-
}
33-
3431
void LifecycleChannel::AppIsInactive() {
35-
FT_LOGI("send app lifecycle state inactive.");
36-
SendLifecycleMessage(kInactive);
32+
FT_LOGI("Sending %s message.", kInactive);
33+
channel_->Send(EncodableValue(kInactive));
3734
}
3835

3936
void LifecycleChannel::AppIsResumed() {
40-
FT_LOGI("send app lifecycle state resumed.");
41-
SendLifecycleMessage(kResumed);
37+
FT_LOGI("Sending %s message.", kResumed);
38+
channel_->Send(EncodableValue(kResumed));
4239
}
4340

4441
void LifecycleChannel::AppIsPaused() {
45-
FT_LOGI("send app lifecycle state paused.");
46-
SendLifecycleMessage(kPaused);
42+
FT_LOGI("Sending %s message.", kPaused);
43+
channel_->Send(EncodableValue(kPaused));
4744
}
4845

4946
void LifecycleChannel::AppIsDetached() {
50-
FT_LOGI("send app lifecycle state detached.");
51-
SendLifecycleMessage(kDetached);
47+
FT_LOGI("Sending %s message.", kDetached);
48+
channel_->Send(EncodableValue(kDetached));
5249
}
5350

5451
} // namespace flutter

shell/platform/tizen/channels/lifecycle_channel.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,25 @@
55
#ifndef EMBEDDER_LIFECYCLE_CHANNEL_H_
66
#define EMBEDDER_LIFECYCLE_CHANNEL_H_
77

8-
namespace flutter {
8+
#include <memory>
9+
10+
#include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h"
11+
#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h"
912

10-
class FlutterTizenEngine;
13+
namespace flutter {
1114

1215
class LifecycleChannel {
1316
public:
14-
explicit LifecycleChannel(FlutterTizenEngine* engine);
17+
explicit LifecycleChannel(BinaryMessenger* messenger);
1518
virtual ~LifecycleChannel();
1619

1720
void AppIsInactive();
1821
void AppIsResumed();
1922
void AppIsPaused();
2023
void AppIsDetached();
21-
void SendLifecycleMessage(const char message[]);
2224

2325
private:
24-
FlutterTizenEngine* engine_{nullptr};
26+
std::unique_ptr<BasicMessageChannel<EncodableValue>> channel_;
2527
};
2628

2729
} // namespace flutter
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
2+
// Copyright 2013 The Flutter Authors. All rights reserved.
3+
// Use of this source code is governed by a BSD-style license that can be
4+
// found in the LICENSE file.
5+
6+
#ifndef EMBEDDER_STRING_CODEC_H_
7+
#define EMBEDDER_STRING_CODEC_H_
8+
9+
#include <memory>
10+
#include <string>
11+
#include <variant>
12+
13+
#include "flutter/shell/platform/common/client_wrapper/include/flutter/encodable_value.h"
14+
#include "flutter/shell/platform/common/client_wrapper/include/flutter/message_codec.h"
15+
16+
namespace flutter {
17+
18+
// A string message encoding/decoding mechanism for communications to/from the
19+
// Flutter engine via message channels.
20+
class StringCodec : public MessageCodec<EncodableValue> {
21+
public:
22+
~StringCodec() = default;
23+
24+
// Returns an instance of the codec.
25+
static const StringCodec& GetInstance() {
26+
static StringCodec sInstance;
27+
return sInstance;
28+
}
29+
30+
// Prevent copying.
31+
StringCodec(StringCodec const&) = delete;
32+
StringCodec& operator=(StringCodec const&) = delete;
33+
34+
protected:
35+
// |flutter::MessageCodec|
36+
std::unique_ptr<EncodableValue> DecodeMessageInternal(
37+
const uint8_t* binary_message,
38+
const size_t message_size) const override {
39+
return std::make_unique<EncodableValue>(std::string(
40+
reinterpret_cast<const char*>(binary_message), message_size));
41+
}
42+
43+
// |flutter::MessageCodec|
44+
std::unique_ptr<std::vector<uint8_t>> EncodeMessageInternal(
45+
const EncodableValue& message) const override {
46+
auto string_value = std::get<std::string>(message);
47+
return std::make_unique<std::vector<uint8_t>>(string_value.begin(),
48+
string_value.end());
49+
}
50+
51+
private:
52+
// Instances should be obtained via GetInstance.
53+
explicit StringCodec() = default;
54+
};
55+
56+
} // namespace flutter
57+
58+
#endif // EMBEDDER_STRING_CODEC_H_

shell/platform/tizen/flutter_tizen_engine.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ bool FlutterTizenEngine::RunEngine(
236236
internal_plugin_registrar_->messenger());
237237
localization_channel = std::make_unique<LocalizationChannel>(this);
238238
localization_channel->SendLocales();
239-
lifecycle_channel = std::make_unique<LifecycleChannel>(this);
239+
lifecycle_channel = std::make_unique<LifecycleChannel>(
240+
internal_plugin_registrar_->messenger());
240241

241242
if (IsHeaded()) {
242243
texture_registrar_ = std::make_unique<FlutterTizenTextureRegistrar>(this);

0 commit comments

Comments
 (0)