@mobrowser/native 2.7.1 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36,7 +36,7 @@ message(CHECK_START "Fetching Protobuf")
36
36
  list(APPEND CMAKE_MESSAGE_INDENT " ")
37
37
 
38
38
  # Abseil options
39
- set(ABSL_USE_SYSTEM_INCLUDES ON CACHE BOOL "Use system includes for Abseil")
39
+ set(ABSL_USE_SYSTEM_INCLUDES OFF CACHE BOOL "Treat Abseil headers as system includes (-isystem)")
40
40
  set(ABSL_PROPAGATE_CXX_STD ON CACHE BOOL "Propagate C++ standard from consumers")
41
41
  set(ABSL_ENABLE_INSTALL OFF CACHE BOOL "Enable install rules for Abseil")
42
42
  set(ABSL_BUILD_TESTING OFF CACHE BOOL "Build Abseil tests")
package/include/rpc.h CHANGED
@@ -2,13 +2,93 @@
2
2
  // TeamDev PROPRIETARY and CONFIDENTIAL.
3
3
  // Use is subject to license terms.
4
4
 
5
- #ifndef MO_RPC_H_
6
- #define MO_RPC_H_
5
+ #ifndef NPM_TEMPLATES__MOBROWSER_NATIVE_INCLUDE_RPC_H_
6
+ #define NPM_TEMPLATES__MOBROWSER_NATIVE_INCLUDE_RPC_H_
7
7
 
8
+ #include <functional>
9
+ #include <optional>
10
+ #include <string>
11
+ #include <type_traits>
12
+ #include <utility>
13
+
14
+ #include "google/protobuf/message.h"
8
15
  #include "service.h"
9
16
 
10
17
  namespace mo::rpc {
18
+
19
+ template <class T>
20
+ concept ResponseMessageType =
21
+ std::is_base_of<google::protobuf::Message, T>::value;
22
+
23
+ struct SerializedResult {
24
+ bool ok;
25
+ std::string data;
26
+ std::string error;
27
+ };
28
+
29
+ template <ResponseMessageType T>
30
+ class Result {
31
+ public:
32
+ static Result<T> Ok(T value) {
33
+ return Result<T>(std::move(value), std::string());
34
+ }
35
+
36
+ static Result<T> Error(std::string error) {
37
+ return Result<T>(std::nullopt, std::move(error));
38
+ }
39
+
40
+ bool ok() const { return value_.has_value(); }
41
+
42
+ const std::optional<T>& value() const { return value_; }
43
+
44
+ std::optional<T>& value() { return value_; }
45
+
46
+ const std::string& error() const { return error_; }
47
+
48
+ private:
49
+ Result(std::optional<T> value, std::string error)
50
+ : value_(std::move(value)), error_(std::move(error)) {}
51
+
52
+ std::optional<T> value_;
53
+ std::string error_;
54
+ };
55
+
56
+ template <ResponseMessageType T>
57
+ using ResponseCallback = std::function<void(Result<T>)>;
58
+
11
59
  void RegisterService(Service* service);
60
+
61
+ void RequestSerialized(const char* service_name,
62
+ const char* method_name,
63
+ const google::protobuf::Message& request,
64
+ std::function<void(SerializedResult)> callback);
65
+
66
+ template <ResponseMessageType RequestMessage,
67
+ ResponseMessageType ResponseMessage>
68
+ void Request(const char* service_name,
69
+ const char* method_name,
70
+ const RequestMessage& request,
71
+ ResponseCallback<ResponseMessage> callback) {
72
+ RequestSerialized(
73
+ service_name,
74
+ method_name,
75
+ request,
76
+ [callback = std::move(callback)](SerializedResult result) mutable {
77
+ if (!result.ok) {
78
+ callback(Result<ResponseMessage>::Error(std::move(result.error)));
79
+ return;
80
+ }
81
+
82
+ ResponseMessage response;
83
+ if (!response.ParseFromString(result.data)) {
84
+ callback(
85
+ Result<ResponseMessage>::Error("Failed to parse RPC response."));
86
+ return;
87
+ }
88
+ callback(Result<ResponseMessage>::Ok(std::move(response)));
89
+ });
12
90
  }
13
91
 
14
- #endif // MO_RPC_H_
92
+ } // namespace mo::rpc
93
+
94
+ #endif // NPM_TEMPLATES__MOBROWSER_NATIVE_INCLUDE_RPC_H_
package/internal/rpc.cc CHANGED
@@ -17,4 +17,14 @@ namespace mo::rpc {
17
17
  void RegisterService(Service* service) {
18
18
  Transport::GetInstance()->RegisterService(service);
19
19
  }
20
+
21
+ void RequestSerialized(const char* service_name,
22
+ const char* method_name,
23
+ const google::protobuf::Message& request,
24
+ std::function<void(SerializedResult)> callback) {
25
+ Transport::GetInstance()->Request(service_name,
26
+ method_name,
27
+ request.SerializeAsString(),
28
+ std::move(callback));
29
+ }
20
30
  } // namespace mo::rpc
@@ -5,6 +5,7 @@
5
5
  #include "transport.h"
6
6
 
7
7
  #include <shared_mutex>
8
+ #include <utility>
8
9
  #include <vector>
9
10
 
10
11
  #include "../include/service.h"
@@ -20,7 +21,12 @@ void invoke(uint64_t id,
20
21
  size_t data_size,
21
22
  reply_callback reply,
22
23
  reject_callback reject) {
23
- mo::rpc::Transport::GetInstance()->Invoke(id, service_name, method_name, data, data_size, reply, reject);
24
+ mo::rpc::Transport::GetInstance()
25
+ ->Invoke(id, service_name, method_name, data, data_size, reply, reject);
26
+ }
27
+
28
+ void set_host_invoke(host_invoke_callback invoke) {
29
+ mo::rpc::Transport::GetInstance()->SetHostInvoke(invoke);
24
30
  }
25
31
 
26
32
  namespace mo::rpc {
@@ -37,6 +43,11 @@ void Transport::RegisterService(Service* service) {
37
43
  services_.emplace(service->GetDescriptor()->full_name(), service);
38
44
  }
39
45
 
46
+ void Transport::SetHostInvoke(host_invoke_callback invoke) {
47
+ std::lock_guard lock(host_mutex_);
48
+ host_invoke_ = invoke;
49
+ }
50
+
40
51
  void Transport::Invoke(uint64_t id,
41
52
  const char* service_name,
42
53
  const char* method_name,
@@ -63,4 +74,76 @@ void Transport::Invoke(uint64_t id,
63
74
  request->ParseFromArray(data, data_size);
64
75
  service->CallMethod(method, *request, id, reply, reject);
65
76
  }
77
+
78
+ void Transport::Request(std::string service_name,
79
+ std::string method_name,
80
+ std::string data,
81
+ std::function<void(SerializedResult)> callback) {
82
+ host_invoke_callback host_invoke = nullptr;
83
+ uint64_t id = 0;
84
+ std::function<void(SerializedResult)> unavailable_callback;
85
+ {
86
+ std::lock_guard lock(host_mutex_);
87
+ host_invoke = host_invoke_;
88
+ if (!host_invoke) {
89
+ unavailable_callback = std::move(callback);
90
+ } else {
91
+ id = next_request_id_++;
92
+ pending_requests_.emplace(id, std::move(callback));
93
+ }
94
+ }
95
+
96
+ if (unavailable_callback) {
97
+ unavailable_callback(
98
+ {false, std::string(), "Native RPC host is not available."});
99
+ return;
100
+ }
101
+
102
+ host_invoke(
103
+ id,
104
+ service_name.c_str(),
105
+ method_name.c_str(),
106
+ data.data(),
107
+ data.size(),
108
+ [](uint64_t id, const char* data, size_t data_size) {
109
+ Transport::GetInstance()->OnHostReply(id, data, data_size);
110
+ },
111
+ [](uint64_t id, const char* error) {
112
+ Transport::GetInstance()->OnHostReject(id, error);
113
+ });
114
+ }
115
+
116
+ void Transport::OnHostReply(uint64_t id, const char* data, size_t data_size) {
117
+ std::function<void(SerializedResult)> callback;
118
+ {
119
+ std::lock_guard lock(host_mutex_);
120
+ auto it = pending_requests_.find(id);
121
+ if (it == pending_requests_.end()) {
122
+ return;
123
+ }
124
+ callback = std::move(it->second);
125
+ pending_requests_.erase(it);
126
+ }
127
+ callback({true,
128
+ data_size == 0 ? std::string() : std::string(data, data_size),
129
+ std::string()});
130
+ }
131
+
132
+ void Transport::OnHostReject(uint64_t id, const char* error) {
133
+ std::function<void(SerializedResult)> callback;
134
+ {
135
+ std::lock_guard lock(host_mutex_);
136
+ auto it = pending_requests_.find(id);
137
+ if (it == pending_requests_.end()) {
138
+ return;
139
+ }
140
+ callback = std::move(it->second);
141
+ pending_requests_.erase(it);
142
+ }
143
+ SerializedResult result;
144
+ result.ok = false;
145
+ result.data = std::string();
146
+ result.error = error ? std::string(error) : std::string();
147
+ callback(result);
148
+ }
66
149
  } // namespace mo::rpc
@@ -2,17 +2,27 @@
2
2
  // TeamDev PROPRIETARY and CONFIDENTIAL.
3
3
  // Use is subject to license terms.
4
4
 
5
- #ifndef MOBROWSER_TRANSPORT_H
6
- #define MOBROWSER_TRANSPORT_H
5
+ #ifndef NPM_TEMPLATES__MOBROWSER_NATIVE_INTERNAL_TRANSPORT_H_
6
+ #define NPM_TEMPLATES__MOBROWSER_NATIVE_INTERNAL_TRANSPORT_H_
7
7
 
8
+ #include <functional>
9
+ #include <mutex>
8
10
  #include <shared_mutex>
9
11
  #include <string>
10
12
  #include <unordered_map>
13
+ #include "../include/rpc.h"
11
14
  #include "export.h"
12
15
 
13
16
  extern "C" {
14
17
  typedef void (*reply_callback)(uint64_t id, const char* data, size_t data_size);
15
18
  typedef void (*reject_callback)(uint64_t id, const char* error);
19
+ typedef void (*host_invoke_callback)(uint64_t id,
20
+ const char* service_name,
21
+ const char* method_name,
22
+ const char* data,
23
+ size_t data_size,
24
+ reply_callback reply,
25
+ reject_callback reject);
16
26
 
17
27
  MOBROWSER_EXPORT void invoke(uint64_t id,
18
28
  const char* service_name,
@@ -21,6 +31,8 @@ MOBROWSER_EXPORT void invoke(uint64_t id,
21
31
  size_t data_size,
22
32
  reply_callback reply,
23
33
  reject_callback reject);
34
+
35
+ MOBROWSER_EXPORT void set_host_invoke(host_invoke_callback invoke);
24
36
  }
25
37
 
26
38
  namespace mo::rpc {
@@ -34,6 +46,7 @@ class Transport {
34
46
  static Transport* GetInstance();
35
47
 
36
48
  void RegisterService(Service* service);
49
+ void SetHostInvoke(host_invoke_callback invoke);
37
50
 
38
51
  void Invoke(uint64_t id,
39
52
  const char* service_name,
@@ -43,10 +56,23 @@ class Transport {
43
56
  reply_callback reply,
44
57
  reject_callback reject);
45
58
 
59
+ void Request(std::string service_name,
60
+ std::string method_name,
61
+ std::string data,
62
+ std::function<void(SerializedResult)> callback);
63
+
64
+ void OnHostReply(uint64_t id, const char* data, size_t data_size);
65
+ void OnHostReject(uint64_t id, const char* error);
66
+
46
67
  private:
47
68
  std::shared_mutex mutex_;
48
69
  std::unordered_map<std::string, Service*> services_;
70
+ std::mutex host_mutex_;
71
+ host_invoke_callback host_invoke_ = nullptr;
72
+ uint64_t next_request_id_ = 1;
73
+ std::unordered_map<uint64_t, std::function<void(SerializedResult)>>
74
+ pending_requests_;
49
75
  };
50
76
  } // namespace mo::rpc
51
77
 
52
- #endif // MOBROWSER_TRANSPORT_H
78
+ #endif // NPM_TEMPLATES__MOBROWSER_NATIVE_INTERNAL_TRANSPORT_H_
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mobrowser/native",
3
- "version": "2.7.1",
3
+ "version": "2.8.0",
4
4
  "description": "Configuration files for MōBrowser native integration",
5
5
  "main": "cmake/MoBrowser.cmake",
6
6
  "files": [