Skip to content

Commit

Permalink
glue code for CAPI custom user via callbacks (#7615)
Browse files Browse the repository at this point in the history
* glue code for CAPI custom user via callbacks

* review feedback

* remove request_refresh_user from the SyncUser interface, it is unused

* further feedback addressed
  • Loading branch information
ironage authored Apr 29, 2024
1 parent 431ffe6 commit 129a7d6
Show file tree
Hide file tree
Showing 12 changed files with 536 additions and 97 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
### Internals
* Follow on to ([PR #7300](https://github.com/realm/realm-core/pull/7300)) to allow SDKs to construct a fake user for testing SyncManager::get_user -> App::create_fake_user_for_testing ([PR #7632](https://github.com/realm/realm-core/pull/7632))
* Fix build-apple-device.sh, broken in [#7603](https://github.com/realm/realm-core/pull/7603) ([PR #7640](https://github.com/realm/realm-core/pull/7640)).

* Added a CAPI interface for SDKs to bring their own managed users with core's app services turned off. ([PR #7615](https://github.com/realm/realm-core/pull/7615)).
----------------------------------------------

# 14.6.0 Release notes
Expand Down
1 change: 0 additions & 1 deletion bindgen/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,6 @@ classes:
path_for_realm: '(config: SyncConfig&, custom_file_name: std::optional<std::string>&) -> std::string'
access_token_refresh_required: '() -> bool'
# # request_log_out: '(cb: AsyncCallback<(err: std::optional<AppError>)>&&)'
request_refresh_user: '(cb: AsyncCallback<(err: std::optional<AppError>)>&&)'
request_refresh_location: '(cb: AsyncCallback<(err: std::optional<AppError>)>&&)'
request_access_token: '(cb: AsyncCallback<(err: std::optional<AppError>)>&&)'
track_realm: '(path: std::string_view)'
Expand Down
131 changes: 102 additions & 29 deletions src/realm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3451,6 +3451,34 @@ RLM_API char* realm_user_get_custom_data(const realm_user_t*) RLM_API_NOEXCEPT;
*/
RLM_API char* realm_user_get_profile_data(const realm_user_t*);

/**
* Return the identiy for the user passed as argument
* @param user ptr to the user for which the identiy has to be retrieved
* @return a ptr to the identity string. This must be manually released with realm_free().
*/
RLM_API char* realm_user_get_identity(const realm_user_t* user) RLM_API_NOEXCEPT;

/**
* Retrieve the state for the user passed as argument
* @param user ptr to the user for which the state has to be retrieved
* @return realm_user_state_e value
*/
RLM_API realm_user_state_e realm_user_get_state(const realm_user_t* user) RLM_API_NOEXCEPT;

RLM_API bool realm_user_is_logged_in(const realm_user_t*) RLM_API_NOEXCEPT;

/**
* Return the access token associated with the user.
* @return a string that rapresents the access token
*/
RLM_API char* realm_user_get_access_token(const realm_user_t*);

/**
* Return the refresh token associated with the user.
* @return a string that represents the refresh token
*/
RLM_API char* realm_user_get_refresh_token(const realm_user_t*);

typedef struct realm_app_user_subscription_token realm_app_user_subscription_token_t;
typedef void (*realm_sync_on_user_state_changed_t)(realm_userdata_t userdata, realm_user_state_e s);
/**
Expand Down Expand Up @@ -3482,35 +3510,6 @@ RLM_API realm_app_t* realm_user_get_app(const realm_user_t*) RLM_API_NOEXCEPT;

#endif // REALM_APP_SERVICES

/**
* Return the identiy for the user passed as argument
* @param user ptr to the user for which the identiy has to be retrieved
* @return a ptr to the identity string. This must be manually released with realm_free().
*/
RLM_API char* realm_user_get_identity(const realm_user_t* user) RLM_API_NOEXCEPT;

/**
* Retrieve the state for the user passed as argument
* @param user ptr to the user for which the state has to be retrieved
* @return realm_user_state_e value
*/
RLM_API realm_user_state_e realm_user_get_state(const realm_user_t* user) RLM_API_NOEXCEPT;

RLM_API bool realm_user_is_logged_in(const realm_user_t*) RLM_API_NOEXCEPT;

/**
* Return the access token associated with the user.
* @return a string that rapresents the access token
*/
RLM_API char* realm_user_get_access_token(const realm_user_t*);

/**
* Return the refresh token associated with the user.
* @return a string that represents the refresh token
*/
RLM_API char* realm_user_get_refresh_token(const realm_user_t*);


/* Sync */
typedef enum realm_sync_client_reconnect_mode {
RLM_SYNC_CLIENT_RECONNECT_MODE_NORMAL,
Expand Down Expand Up @@ -3562,8 +3561,15 @@ typedef enum realm_sync_error_action {
RLM_SYNC_ERROR_ACTION_REVERT_TO_PBS,
} realm_sync_error_action_e;

typedef enum realm_sync_file_action {
RLM_SYNC_FILE_ACTION_DELETE_REALM,
RLM_SYNC_FILE_ACTION_BACK_UP_THEN_DELETE_REALM,
} realm_sync_file_action_e;


typedef struct realm_sync_session realm_sync_session_t;
typedef struct realm_async_open_task realm_async_open_task_t;
typedef struct realm_sync_manager realm_sync_manager_t;

typedef struct realm_sync_error_user_info {
const char* key;
Expand All @@ -3576,6 +3582,73 @@ typedef struct realm_sync_error_compensating_write_info {
realm_value_t primary_key;
} realm_sync_error_compensating_write_info_t;

// The following interface allows C-API users to
// bring their own users. This API shouldn't be mixed
// with core's own implementation of User so it is
// only defined with app services are compiled out
#if !REALM_APP_SERVICES
/**
* Generic completion callback for asynchronous Realm User operations.
* @param userdata This must be the faithfully forwarded data parameter that was provided along with this callback.
* @param error Pointer to an error object if the operation failed, otherwise null if it completed successfully.
*/
typedef void (*realm_user_void_completion_func_t)(realm_userdata_t userdata, const realm_app_error_t* error);


typedef const char* (*realm_user_get_access_token_cb_t)(realm_userdata_t userdata);
typedef const char* (*realm_user_get_refresh_token_cb_t)(realm_userdata_t userdata);
typedef realm_user_state_e (*realm_user_state_cb_t)(realm_userdata_t userdata);
typedef bool (*realm_user_access_token_refresh_required_cb_t)(realm_userdata_t userdata);
typedef realm_sync_manager_t* (*realm_user_get_sync_manager_cb_t)(realm_userdata_t userdata);
typedef void (*realm_user_request_log_out_cb_t)(realm_userdata_t userdata);
typedef void (*realm_user_request_refresh_location_cb_t)(realm_userdata_t userdata,
realm_user_void_completion_func_t cb,
realm_userdata_t cb_data);
typedef void (*realm_user_request_access_token_cb_t)(realm_userdata_t userdata, realm_user_void_completion_func_t cb,
realm_userdata_t cb_data);
typedef void (*realm_user_track_realm_cb_t)(realm_userdata_t userdata, const char* path);
typedef const char* (*realm_user_create_file_action_cb_t)(realm_userdata_t userdata, realm_sync_file_action_e action,
const char* original_path,
const char* requested_recovery_dir);
typedef struct realm_sync_user_create_config {
realm_userdata_t userdata;
realm_free_userdata_func_t free_func;
const char* app_id;
const char* user_id;
realm_user_get_access_token_cb_t access_token_cb;
realm_user_get_refresh_token_cb_t refresh_token_cb;
realm_user_state_cb_t state_cb;
realm_user_access_token_refresh_required_cb_t atrr_cb;
realm_user_get_sync_manager_cb_t sync_manager_cb;
realm_user_request_log_out_cb_t request_log_out_cb;
realm_user_request_refresh_location_cb_t request_refresh_location_cb;
realm_user_request_access_token_cb_t request_access_token_cb;
realm_user_track_realm_cb_t track_realm_cb;
realm_user_create_file_action_cb_t create_fa_cb;
} realm_sync_user_create_config_t;

/*
* Construct a SyncUser instance that uses SDK provided
* callbacks instead of core's User implementation. This type
* of user should not be used with core's App implementation.
*/
RLM_API realm_user_t* realm_user_new(realm_sync_user_create_config_t config) RLM_API_NOEXCEPT;

/**
* Create realm_sync_manager_t* instance given a valid realm sync client configuration.
*
* @return A non-null pointer if no error occurred.
*/
RLM_API realm_sync_manager_t* realm_sync_manager_create(const realm_sync_client_config_t*);

/**
* See SyncManager::set_sync_route()
*/
RLM_API void realm_sync_manager_set_route(const realm_sync_manager_t* session, const char* route, bool is_verified);


#endif // !REALM_APP_SERVICES

// This type should never be returned from a function.
// It's only meant as an asynchronous callback argument.
// Pointers to this struct and its pointer members are only valid inside the scope
Expand Down
Loading

0 comments on commit 129a7d6

Please sign in to comment.