Skip to content

Commit d9393f7

Browse files
authored
[linux] Split channels.h into separate files (google#118)
Splits MethodCall and MethodResult into separate files. This follows the more usual C++ convention of having one file per class, and makes the code structure more consistent with the Android Flutter plugin implementation (vs. following the iOS file structure, as it did before). This is purely a move of code, with no implementation changes. It is a precursor to the next plugin refactor, to avoid having substantial functionality change mixed with file moves. JsonMethodResult is left with MethodResult since it will be eliminated in the refactor anyway.
1 parent 4eee662 commit d9393f7

File tree

6 files changed

+110
-62
lines changed

6 files changed

+110
-62
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2018 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#ifndef LINUX_LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_METHOD_CALL_H_
15+
#define LINUX_LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_METHOD_CALL_H_
16+
17+
#include <json/json.h>
18+
19+
#include <memory>
20+
#include <string>
21+
22+
#include <flutter_embedder.h>
23+
24+
namespace flutter_desktop_embedding {
25+
26+
// An object encapsulating a method call from Flutter.
27+
// TODO: Move serialization details into a method codec class, to match mobile
28+
// Flutter plugin APIs.
29+
class MethodCall {
30+
public:
31+
// Creates a MethodCall with the given name and, optionally, arguments.
32+
explicit MethodCall(const std::string &method_name,
33+
const Json::Value &arguments = Json::Value());
34+
35+
// Returns a new MethodCall created from a JSON message received from the
36+
// Flutter engine.
37+
static std::unique_ptr<MethodCall> CreateFromMessage(
38+
const Json::Value &message);
39+
~MethodCall();
40+
41+
// The name of the method being called.
42+
const std::string &method_name() const { return method_name_; }
43+
44+
// The arguments to the method call, or a nullValue if there are none.
45+
const Json::Value &arguments() const { return arguments_; }
46+
47+
// Returns a version of the method call serialized in the format expected by
48+
// the Flutter engine.
49+
Json::Value AsMessage() const;
50+
51+
private:
52+
std::string method_name_;
53+
Json::Value arguments_;
54+
};
55+
56+
} // namespace flutter_desktop_embedding
57+
58+
#endif // LINUX_LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_METHOD_CALL_H_

linux/library/include/flutter_desktop_embedding/channels.h renamed to linux/library/include/flutter_desktop_embedding/method_result.h

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
#ifndef LINUX_LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_CHANNELS_H_
15-
#define LINUX_LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_CHANNELS_H_
14+
#ifndef LINUX_LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_METHOD_RESULT_H_
15+
#define LINUX_LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_METHOD_RESULT_H_
1616

1717
#include <json/json.h>
1818

@@ -23,36 +23,6 @@
2323

2424
namespace flutter_desktop_embedding {
2525

26-
// An object encapsulating a method call from Flutter.
27-
// TODO: Move serialization details into a method codec class, to match mobile
28-
// Flutter plugin APIs.
29-
class MethodCall {
30-
public:
31-
// Creates a MethodCall with the given name and, optionally, arguments.
32-
explicit MethodCall(const std::string &method_name,
33-
const Json::Value &arguments = Json::Value());
34-
35-
// Returns a new MethodCall created from a JSON message received from the
36-
// Flutter engine.
37-
static std::unique_ptr<MethodCall> CreateFromMessage(
38-
const Json::Value &message);
39-
~MethodCall();
40-
41-
// The name of the method being called.
42-
const std::string &method_name() const { return method_name_; }
43-
44-
// The arguments to the method call, or a nullValue if there are none.
45-
const Json::Value &arguments() const { return arguments_; }
46-
47-
// Returns a version of the method call serialized in the format expected by
48-
// the Flutter engine.
49-
Json::Value AsMessage() const;
50-
51-
private:
52-
std::string method_name_;
53-
Json::Value arguments_;
54-
};
55-
5626
// Encapsulates a result sent back to the Flutter engine in response to a
5727
// MethodCall. Only one method should be called on any given instance.
5828
class MethodResult {
@@ -117,4 +87,4 @@ class JsonMethodResult : public MethodResult {
11787

11888
} // namespace flutter_desktop_embedding
11989

120-
#endif // LINUX_LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_CHANNELS_H_
90+
#endif // LINUX_LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_METHOD_RESULT_H_

linux/library/include/flutter_desktop_embedding/plugin.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
#include <flutter_embedder.h>
2424

25-
#include "channels.h"
25+
#include "method_call.h"
26+
#include "method_result.h"
2627

2728
namespace flutter_desktop_embedding {
2829

linux/library/src/embedder.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626

2727
#include <flutter_embedder.h>
2828

29-
#include "linux/library/include/flutter_desktop_embedding/channels.h"
29+
#include "linux/library/include/flutter_desktop_embedding/method_call.h"
30+
#include "linux/library/include/flutter_desktop_embedding/method_result.h"
3031
#include "linux/library/src/internal/keyboard_hook_handler.h"
3132
#include "linux/library/src/internal/plugin_handler.h"
3233
#include "linux/library/src/internal/text_input_plugin.h"

linux/library/src/method_call.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2018 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#include "linux/library/include/flutter_desktop_embedding/method_call.h"
15+
16+
namespace flutter_desktop_embedding {
17+
18+
constexpr char kMessageMethodKey[] = "method";
19+
constexpr char kMessageArgumentsKey[] = "args";
20+
21+
MethodCall::MethodCall(const std::string &method_name,
22+
const Json::Value &arguments)
23+
: method_name_(method_name), arguments_(arguments) {}
24+
25+
MethodCall::~MethodCall() {}
26+
27+
std::unique_ptr<MethodCall> MethodCall::CreateFromMessage(
28+
const Json::Value &message) {
29+
Json::Value method = message[kMessageMethodKey];
30+
if (method.isNull()) {
31+
return nullptr;
32+
}
33+
Json::Value arguments = message[kMessageArgumentsKey];
34+
return std::make_unique<MethodCall>(method.asString(), arguments);
35+
}
36+
37+
Json::Value MethodCall::AsMessage() const {
38+
Json::Value message(Json::objectValue);
39+
message[kMessageMethodKey] = method_name_;
40+
message[kMessageArgumentsKey] = arguments_;
41+
return message;
42+
}
43+
44+
} // namespace flutter_desktop_embedding

linux/library/src/channels.cc renamed to linux/library/src/method_result.cc

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,12 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
#include "linux/library/include/flutter_desktop_embedding/channels.h"
14+
#include "linux/library/include/flutter_desktop_embedding/method_result.h"
1515

1616
#include <iostream>
1717

1818
namespace flutter_desktop_embedding {
1919

20-
constexpr char kMessageMethodKey[] = "method";
21-
constexpr char kMessageArgumentsKey[] = "args";
22-
23-
MethodCall::MethodCall(const std::string &method_name,
24-
const Json::Value &arguments)
25-
: method_name_(method_name), arguments_(arguments) {}
26-
27-
MethodCall::~MethodCall() {}
28-
29-
std::unique_ptr<MethodCall> MethodCall::CreateFromMessage(
30-
const Json::Value &message) {
31-
Json::Value method = message[kMessageMethodKey];
32-
if (method.isNull()) {
33-
return nullptr;
34-
}
35-
Json::Value arguments = message[kMessageArgumentsKey];
36-
return std::make_unique<MethodCall>(method.asString(), arguments);
37-
}
38-
39-
Json::Value MethodCall::AsMessage() const {
40-
Json::Value message(Json::objectValue);
41-
message[kMessageMethodKey] = method_name_;
42-
message[kMessageArgumentsKey] = arguments_;
43-
return message;
44-
}
45-
4620
void MethodResult::Success(const Json::Value &result) {
4721
SuccessInternal(result);
4822
}

0 commit comments

Comments
 (0)