27
27
%include " app/src/swig/serial_dispose.i"
28
28
%include " app/src/swig/future.i"
29
29
30
+ // Start of code added to the C++ module file
30
31
%{
31
32
#include < algorithm>
32
33
#include < map>
33
34
#include < vector>
35
+ #include " app/src/callback.h"
34
36
#include " app/src/cpp_instance_manager.h"
35
37
#include " remote_config/src/include/firebase/remote_config.h"
36
38
37
39
namespace firebase {
38
40
namespace remote_config {
39
41
42
+ // Declare the C++ callback delegate equivalent to C# RemoteConfigUtil.ConfigUpdateDelegate
43
+ typedef void (SWIGSTDCALL *ConfigUpdateListener)(const char * app_name, ConfigUpdate* config_update, int error);
44
+
40
45
// Reference count manager for C++ RemoteConfig instance, using pointer as the
41
46
// key for searching.
42
47
static CppInstanceManager<RemoteConfig> g_rc_instances;
43
48
49
+ // App -> C++ ConfigUpdateListener map.
50
+ static std::map<App*, firebase::remote_config::ConfigUpdateListenerRegistration> g_registered_listeners;
51
+
52
+ // Should be set to the C# function FirebaseRemoteConfig.ConfigUpdateMethod
53
+ static ConfigUpdateListener g_config_updated = nullptr ;
54
+
44
55
// Internal struct used to pass the Remote Config stored data from C++ to C#.
45
56
struct ConfigValueInternal {
46
57
// The raw data.
@@ -49,36 +60,61 @@ struct ConfigValueInternal {
49
60
ValueSource source;
50
61
};
51
62
63
+ // Wrapper that calls g_config_updated. Should be used with the
64
+ // callback logic to guarantee it is on the Unity thread.
65
+ static void CallConfigUpdate (ConfigUpdate cu, RemoteConfigError error, const char * name) {
66
+ if (g_config_updated) {
67
+ // Should be calling FirebaseRemoteConfig.ConfigUpdateMethod
68
+ g_config_updated (name, &cu, static_cast <int >(error));
69
+ }
70
+ }
71
+ // Called by C# to register a RemoteConfig instance for config updates,
72
+ // provided via the callback method provided.
73
+ void SetConfigUpdateCallback (RemoteConfig* rc, firebase::remote_config::ConfigUpdateListener on_config_updated) {
74
+ // If given a method, save it, and add a new listener for Config Updates.
75
+ if (on_config_updated) {
76
+ if (!g_config_updated) {
77
+ g_config_updated = on_config_updated;
78
+ }
79
+ std::string app_name = rc->app ()->name ();
80
+ ConfigUpdateListenerRegistration registration = rc->AddOnConfigUpdateListener ([app_name](ConfigUpdate&& cu, RemoteConfigError error) {
81
+ // Queue up a call to g_config_updated
82
+ ConfigUpdate cuLocal = std::move (cu);
83
+ firebase::callback::AddCallback (
84
+ new firebase::callback::CallbackValue2String1<ConfigUpdate, RemoteConfigError>(
85
+ cuLocal, error, app_name.c_str (), CallConfigUpdate));
86
+ });
87
+
88
+ // Save the registration in a map so that it can be removed later if needed.
89
+ g_registered_listeners[rc->app ()] = registration;
90
+ } else {
91
+ // Remove the listener, and cleanup the callback if no more remain.
92
+ ConfigUpdateListenerRegistration registration = g_registered_listeners[rc->app ()];
93
+ g_registered_listeners.erase (rc->app ());
94
+ registration.Remove ();
95
+
96
+ if (g_registered_listeners.empty ()) {
97
+ g_config_updated = nullptr ;
98
+ }
99
+ }
100
+ }
101
+
52
102
} // remote_config
53
103
} // firebase
54
- %}
104
+
105
+ %} // End of code added to the C++ module file
55
106
56
107
// All outputs of CharVector should instead be IEnumerable<byte>
57
108
%typemap(cstype, out=" global::System.Collections.Generic.IEnumerable<byte>" ) std::vector<unsigned char > " CharVector" ;
58
109
// All outputs of StringList should instead be IEnumerable<string>
59
110
%typemap(cstype, out=" global::System.Collections.Generic.IEnumerable<string>" ) std::vector<std::string> " StringList" ;
60
111
61
- // Ignore all the deprecated static functions
62
- %ignore firebase::remote_config::Initialize;
63
- %ignore firebase::remote_config::Terminate;
64
- %ignore firebase::remote_config::SetDefaults;
65
- %ignore firebase::remote_config::GetConfigSetting;
66
- %ignore firebase::remote_config::SetConfigSetting;
67
- %ignore firebase::remote_config::GetBoolean;
68
- %ignore firebase::remote_config::GetLong;
69
- %ignore firebase::remote_config::GetDouble;
70
- %ignore firebase::remote_config::GetString;
71
- %ignore firebase::remote_config::GetData;
72
- %ignore firebase::remote_config::GetKeysByPrefix;
73
- %ignore firebase::remote_config::GetKeys;
74
- %ignore firebase::remote_config::Fetch;
75
- %ignore firebase::remote_config::ActivateFetched;
76
- %ignore firebase::remote_config::GetInfo;
77
-
78
112
// Ignore unused structs and enums
79
113
%ignore firebase::remote_config::ValueInfo;
80
114
%ignore firebase::remote_config::ConfigKeyValue;
81
115
%ignore firebase::remote_config::ConfigSetting;
116
+ %ignore firebase::remote_config::RemoteConfigError;
117
+ %ignore firebase::remote_config::ConfigUpdateListenerRegistration;
82
118
83
119
// Configure the ConfigInfo class
84
120
%csmethodmodifiers fetch_time " internal" ;
@@ -131,6 +167,8 @@ struct ConfigValueInternal {
131
167
132
168
%rename (ConfigSettingsInternal) firebase::remote_config::ConfigSettings;
133
169
170
+ %rename (ConfigUpdateInternal) firebase::remote_config::ConfigUpdate;
171
+
134
172
// Configure properties for get / set methods on the FirebaseRemoteConfigInternal class.
135
173
%attribute(firebase::remote_config::RemoteConfig, firebase::App*, App, app);
136
174
@@ -147,6 +185,7 @@ struct ConfigValueInternal {
147
185
%ignore firebase::remote_config::RemoteConfig::GetData;
148
186
// Ignore GetAll, as we do not rely on the C++ function for it.
149
187
%ignore firebase::remote_config::RemoteConfig::GetAll;
188
+ %ignore firebase::remote_config::RemoteConfig::AddOnConfigUpdateListener;
150
189
151
190
%typemap(cscode) firebase::remote_config::RemoteConfig %{
152
191
// Generates a key that uniquely identifies this instance.
@@ -176,8 +215,13 @@ struct ConfigValueInternal {
176
215
System.GC .SuppressFinalize (this );
177
216
}
178
217
218
+ SWIG_MAP_CFUNC_TO_CSDELEGATE (
219
+ firebase::remote_config::ConfigUpdateListener,
220
+ Firebase.RemoteConfig.RemoteConfigUtil.ConfigUpdateDelegate)
221
+
179
222
%include "remote_config/src/include/firebase/remote_config.h"
180
223
224
+ // Start of C++ definitions that Swig needs to know of, to generate C# for
181
225
namespace firebase {
182
226
namespace remote_config {
183
227
@@ -186,10 +230,17 @@ struct ConfigValueInternal {
186
230
ValueSource source;
187
231
};
188
232
233
+ void SetConfigUpdateCallback (firebase::remote_config::RemoteConfig* rc,
234
+ firebase::remote_config::ConfigUpdateListener config_listener);
235
+
189
236
}
190
- }
237
+ } // End of C++ definitions
238
+
191
239
192
240
%pragma(csharp) modulecode=%{
241
+
242
+ internal delegate void ConfigUpdateDelegate (string appName, System.IntPtr configUpdatePtr, int error);
243
+
193
244
// / The C++ mapping requires a StringStringMap, so convert the more generic
194
245
// / dictionary into that map.
195
246
internal static StringStringMap ConvertDictionaryToMap (
0 commit comments