Skip to content

Commit

Permalink
Merge 19f71c7 into a38a346
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Mar 16, 2022
2 parents a38a346 + 19f71c7 commit 2eafa04
Show file tree
Hide file tree
Showing 22 changed files with 198 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/realm-dart-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name : Setup Dart SDK
uses: dart-lang/setup-dart@main
with:
sdk: stable
sdk: main

- name: Install dependencies
run: dart pub get
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/realm-dart-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name : Setup Dart SDK
uses: dart-lang/setup-dart@main
with:
sdk: stable
sdk: main

- name: Install dependencies
run: dart pub get
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/realm-dart-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name : Setup Dart SDK
uses: dart-lang/setup-dart@main
with:
sdk: stable
sdk: main

- name: Install dependencies
run: dart pub get
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/realm-flutter-android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
channel: 'stable'
channel: 'master'

- name: Install dependencies
run: flutter pub get
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/realm-flutter-ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
channel: 'stable'
channel: 'master'

- name: Install dependencies
run: flutter pub get
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/realm-flutter-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
channel: 'stable'
channel: 'master'

- name: Enable Flutter Desktop support
run: flutter config --enable-linux-desktop
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/realm-flutter-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
channel: 'stable'
channel: 'master'

- name: Enable Flutter Desktop support
run: flutter config --enable-macos-desktop
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/realm-flutter-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
channel: 'stable'
channel: 'master'

- name: Enable Flutter Desktop support
run: flutter config --enable-windows-desktop
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/realm-generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name : Setup Dart SDK
uses: dart-lang/setup-dart@main
with:
sdk: stable
sdk: main

- name: Install generator dependencies
run: dart pub get
Expand Down
2 changes: 2 additions & 0 deletions ffigen/linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ headers:
- 'realm_dart.h'
- 'realm_dart_scheduler.h'
- 'realm_dart_collections.h'
- 'realm_dart_http_transport.h'
- 'realm_android_platform.h'
include-directives: #generate only for these headers
- 'realm.h'
- 'realm_dart.h'
- 'realm_dart_scheduler.h'
- 'realm_dart_collections.h'
- 'realm_dart_http_transport.h'
- 'realm_android_platform.h'
compiler-opts:
- '-DRLM_NO_ANON_UNIONS'
Expand Down
2 changes: 2 additions & 0 deletions ffigen/macos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ headers:
- 'realm_dart.h'
- 'realm_dart_scheduler.h'
- 'realm_dart_collections.h'
- 'realm_dart_http_transport.h'
- 'realm_android_platform.h'
include-directives: #generate only for these headers
- 'realm.h'
- 'realm_dart.h'
- 'realm_dart_scheduler.h'
- 'realm_dart_collections.h'
- 'realm_dart_http_transport.h'
- 'realm_android_platform.h'
compiler-opts:
- '-DRLM_NO_ANON_UNIONS'
Expand Down
1 change: 1 addition & 0 deletions ffigen/realm_dart_http_transport.h
2 changes: 2 additions & 0 deletions ffigen/windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ headers:
- 'realm_dart.h'
- 'realm_dart_scheduler.h'
- 'realm_dart_collections.h'
- 'realm_dart_http_transport.h'
- 'realm_android_platform.h'
include-directives: #generate only for these headers
- 'realm.h'
- 'realm_dart.h'
- 'realm_dart_scheduler.h'
- 'realm_dart_collections.h'
- 'realm_dart_http_transport.h'
- 'realm_android_platform.h'
compiler-opts:
- '-DRLM_NO_ANON_UNIONS'
Expand Down
2 changes: 2 additions & 0 deletions flutter/realm_flutter/ios/Classes/RealmPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#import "realm_dart.h"
#import "realm_dart_scheduler.h"
#import "realm_dart_collections.h"
#import "realm_dart_http_transport.h"
#import "platform.h"
@implementation RealmPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
Expand All @@ -43,6 +44,7 @@ void dummy(void) {
realm_results_get_object(NULL, 0);
realm_list_size(NULL, 0);
realm_dart_results_add_notification_callback(NULL, NULL, NULL, NULL);
realm_dart_http_transport_new(NULL, NULL);
realm_results_snapshot(NULL);
}

Expand Down
1 change: 1 addition & 0 deletions flutter/realm_flutter/ios/realm.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Pod::Spec.new do |s|
'src/realm_dart.cpp'
'src/realm_dart_scheduler.cpp'
'src/realm_dart_collections.cpp'
'src/realm_dart_http_transport.cpp'
s.public_header_files = 'Classes/**/*.h',
s.vendored_frameworks = 'realm_flutter_ios.xcframework'
s.dependency 'Flutter'
Expand Down
39 changes: 38 additions & 1 deletion lib/src/native/realm_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Generated by `package:ffigen`.
import 'dart:ffi' as ffi;

/// A Windows config for ffigen Usage: dart run ffigen --config windows.yaml
/// A MacOs config for ffigen Usage: dart run ffigen --config macos.yaml
class RealmLibrary {
/// Holds the symbol lookup function.
final ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
Expand Down Expand Up @@ -7555,6 +7555,29 @@ class RealmLibrary {
realm_dart_on_object_change_func_t,
ffi.Pointer<realm_scheduler_t>)>();

/// Create a new HTTP transport with these callbacks implementing its functionality.
///
/// This is a dart specific wrapper for realm_http_transport_new.
ffi.Pointer<realm_http_transport_t> realm_dart_http_transport_new(
realm_dart_http_request_func_t request_callback,
Object userdata,
) {
return _realm_dart_http_transport_new(
request_callback,
userdata,
);
}

late final _realm_dart_http_transport_newPtr = _lookup<
ffi.NativeFunction<
ffi.Pointer<realm_http_transport_t> Function(
realm_dart_http_request_func_t,
ffi.Handle)>>('realm_dart_http_transport_new');
late final _realm_dart_http_transport_new =
_realm_dart_http_transport_newPtr.asFunction<
ffi.Pointer<realm_http_transport_t> Function(
realm_dart_http_request_func_t, Object)>();

ffi.Pointer<ffi.Int8> realm_dart_get_files_path() {
return _realm_dart_get_files_path();
}
Expand Down Expand Up @@ -8478,3 +8501,17 @@ typedef realm_dart_on_collection_change_func_t = ffi.Pointer<
typedef realm_dart_on_object_change_func_t = ffi.Pointer<
ffi.NativeFunction<
ffi.Void Function(ffi.Handle, ffi.Pointer<realm_object_changes_t>)>>;

/// Callback function used by Core to make a HTTP request.
///
/// Complete the request by calling realm_dart_http_transport_complete_request(),
/// passing in the request_context pointer here and the received response.
/// Network request are expected to be asynchronous and can be completed on any thread.
///
/// @param userdata The userdata pointer passed to realm_dart_http_transport_new().
/// @param request The request to send.
/// @param request_context Internal state pointer of Core, needed by realm_http_transport_complete_request().
typedef realm_dart_http_request_func_t = ffi.Pointer<
ffi.NativeFunction<
ffi.Void Function(
ffi.Handle, realm_http_request_t, ffi.Pointer<ffi.Void>)>>;
1 change: 1 addition & 0 deletions scripts/build-ios.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ cp "$PROJECT_ROOT"/src/realm-core/src/realm.h _include/realm_dart_ios/
cp "$PROJECT_ROOT"/src/realm_dart.h _include/realm_dart_ios/
cp "$PROJECT_ROOT"/src/realm_dart_scheduler.h _include/realm_dart_ios/
cp "$PROJECT_ROOT"/src/realm_dart_collections.h _include/realm_dart_ios/
cp "$PROJECT_ROOT"/src/realm_dart_http_transport.h _include/realm_dart_ios/
cp -r "$PROJECT_ROOT"/src/dart-include _include/realm_dart_ios/


Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ set(SOURCES
realm_dart.cpp
realm_dart_scheduler.cpp
realm_dart_collections.cpp
realm_dart_http_transport.cpp
dart-include/dart_api_dl.c
)

set(HEADERS
realm_dart.h
realm_dart_scheduler.h
realm_dart_collections.h
realm_dart_http_transport.h
realm-core/src/realm.h
)

Expand Down
14 changes: 4 additions & 10 deletions src/realm_dart_collections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ class CallbackData {

void delete_handle() {
if (m_handle) {
//TODO: uncomment when the HACK is removed.
//Dart_DeleteWeakPersistentHandle_DL(m_handle);
Dart_DeleteWeakPersistentHandle_DL(m_handle);
m_handle = nullptr;
}
}

public:
CallbackData(Dart_Handle handle, Func callback)
: m_handle(Dart_NewFinalizableHandle_DL(handle, nullptr, 1, finalize_handle)), m_callback(callback)
: m_handle(Dart_NewWeakPersistentHandle_DL(handle, nullptr, 1, finalize_handle)), m_callback(callback)
{}

~CallbackData() {
Expand All @@ -56,21 +55,16 @@ class CallbackData {
void callback(const Type* changes) {
if (m_handle) {
HandleScope scope;
//TODO: HACK. We can not release Dart persitent handles in delete_handle on Isolate teardown since the IsolateGroup is destroyed before it.
//This works since Dart_WeakPersistentHandle is equivalent to Dart_FinalizableHandle. They both are FinalizablePersistentHandle internally.
Dart_WeakPersistentHandle weakHnd = reinterpret_cast<Dart_WeakPersistentHandle>(m_handle);
auto handle = Dart_HandleFromWeakPersistent_DL(weakHnd);

//clone changes object since the Dart callback is async and changes object is valid for the duration of this method only
//clone failures are handled in the Dart callback
const Type* cloned = static_cast<Type*>(realm_clone(changes));
auto handle = Dart_HandleFromWeakPersistent_DL(m_handle);
m_callback(handle, cloned);
}
}

private:
//TODO: We use FinalizableHandle since it is auto-deleting. Switch to Dart_WeakPersistentHandle when the HACK is removed
Dart_FinalizableHandle m_handle;
Dart_WeakPersistentHandle m_handle;
Func m_callback;
};

Expand Down
87 changes: 87 additions & 0 deletions src/realm_dart_http_transport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

#include "realm_dart_http_transport.h"
#include "dart_api_dl.h"

struct HandleScope {
HandleScope() {
Dart_EnterScope_DL();
}

~HandleScope() {
Dart_ExitScope_DL();
}
};

class RequestCallbackData {
static void finalize_handle(void* isolate_callback_data, void* peer) {
auto request_callback_data = static_cast<RequestCallbackData*>(peer);
request_callback_data->delete_handle();
}

void delete_handle() {
if (m_handle) {
Dart_DeleteWeakPersistentHandle_DL(m_handle);
m_handle = nullptr;
}
}

public:
RequestCallbackData(Dart_Handle handle, realm_dart_http_request_func_t callback)
: m_handle(Dart_NewWeakPersistentHandle_DL(handle, nullptr, 1, finalize_handle)), m_callback(callback)
{}

~RequestCallbackData() {
delete_handle();
}

void callback(const realm_http_request_t request, void* request_context) {
if (m_handle) {
HandleScope scope;
auto handle = Dart_HandleFromWeakPersistent_DL(m_handle);
m_callback(handle, request, request_context);
}
}

private:
Dart_WeakPersistentHandle m_handle;
realm_dart_http_request_func_t m_callback;
};

void free_request_callback_data(void* userdata) {
auto request_callback_data = static_cast<RequestCallbackData*>(userdata);
delete request_callback_data;
}

void on_request_callback(
void* userdata,
const realm_http_request_t request,
void* request_context)
{
auto request_callback_data = static_cast<RequestCallbackData*>(userdata);
request_callback_data->callback(request, request_context);
}

RLM_API realm_http_transport_t* realm_dart_http_transport_new(
realm_dart_http_request_func_t request_callback,
Dart_Handle userdata)
{
auto request_callback_data = new RequestCallbackData(userdata, request_callback);
return realm_http_transport_new(on_request_callback, request_callback_data, free_request_callback_data);
}

0 comments on commit 2eafa04

Please sign in to comment.