Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glue code for CAPI custom user via callbacks #7615

Merged
merged 5 commits into from
Apr 29, 2024
Merged

glue code for CAPI custom user via callbacks #7615

merged 5 commits into from
Apr 29, 2024

Conversation

ironage
Copy link
Contributor

@ironage ironage commented Apr 23, 2024

Allows SDKs using the C-API to compile out core's app services implementation and create a custom SyncUser by plugging callbacks into each of SyncUser's virtual methods.

No changelog because the feature hasn't shipped yet.

@ironage ironage added no-changelog no-jira-ticket Skip checking the PR title for Jira reference labels Apr 23, 2024
@ironage ironage self-assigned this Apr 23, 2024
@cla-bot cla-bot bot added the cla: yes label Apr 23, 2024
Copy link
Member

@nicola-cab nicola-cab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, just a small suggestion for realm_user_new

src/realm.h Outdated Show resolved Hide resolved
Copy link
Member

@nirinchev nirinchev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks reasonable - I have a few questions/suggestions. I think all these callbacks should be required unless some are truly optional, otherwise it's not really obvious to SDK developers what they should provide and what - not.

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand why this is a callback into the SDK (this is probably a decision made in the previous PR, but it's confusing to me).

src/realm/object-store/c_api/app.cpp Outdated Show resolved Hide resolved
m_request_log_out_cb(m_userdata);
}
}
void request_refresh_user(CompletionHandler&& callback) override
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this used for and how is it different from request_access_token?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was intended to refresh the user's profile data / identities. But that is actually never used from the sync session. I'll remove it from the SyncUser interface.

src/realm.h Outdated
Comment on lines 4119 to 4129
/**
* 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the SDK should ever be creating the sync manager or setting its route.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sync manager now has a separate lifetime that is managed by core's App implementation. It is no longer tied to the lifetime of any specific user. This allows a SyncUser instance to outlive its sync sessions in a removed/offline state (in the past we had issues with cyclical shared pointers that kept each other alive). Due to that design the SDK must provide a backing SyncManager instance through the SyncUser interface. That's how things currently stand.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not introduced by this PR, so probably safe to punt on it, but we'll probably need to discuss these lifetimes, as the current design doesn't make a lot of sense to me. To be able to construct the sync manager from the user, we'll need the managed app configuration (where all the sync manager settings are currently configured), which would be extremely awkward to stick on the managed user. So we'll probably have to add a backreference to the managed app that owns the user, tying the lifetime of the managed (and thus the native) user and app instances together.

So the way I see it, the only case where a user might briefly outlive an app instance is when the garbage collector detects that both are eligible for collection and collects the app instance before the user one, but in that case, attempting to create a sync manager in the brief moment when an app is collected, but a user isn't would never work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user should not be creating the manager; the manager should be created in advance and then the user should hold a reference to the manager. The API has always been designed around passing around SyncUsers and obtaining everything needed from that.

src/realm.h Outdated Show resolved Hide resolved
src/realm/object-store/c_api/app.cpp Show resolved Hide resolved
Copy link

coveralls-official bot commented Apr 24, 2024

Pull Request Test Coverage Report for Build james.stone_534

Details

  • 9 of 38 (23.68%) changed or added relevant lines in 3 files are covered.
  • 86 unchanged lines in 15 files lost coverage.
  • Overall coverage decreased (-0.005%) to 91.793%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/realm/object-store/c_api/types.hpp 2 15 13.33%
src/realm/object-store/c_api/app.cpp 6 22 27.27%
Files with Coverage Reduction New Missed Lines %
src/realm/sync/noinst/client_impl_base.cpp 1 85.88%
test/test_index_string.cpp 1 94.63%
test/test_table.cpp 1 99.6%
src/realm/sync/network/http.hpp 2 82.41%
src/realm/table_view.cpp 2 94.18%
test/test_lang_bind_helper.cpp 2 94.24%
src/realm/table.cpp 3 90.83%
src/realm/mixed.cpp 4 84.79%
src/realm/sync/noinst/client_reset.cpp 4 93.07%
src/realm/util/assert.hpp 4 87.1%
Totals Coverage Status
Change from base Build 2246: -0.005%
Covered Lines: 243611
Relevant Lines: 265393

💛 - Coveralls

Copy link
Contributor

@kneth kneth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bindgen/spec.yml needs an update (remove request_refresh_user from SyncUser)

@nirinchev
Copy link
Member

We should probably move all the C API for getting user details, such as realm_user_get_identity, realm_user_is_logged_in, etc. to an if-defed section of the file as the SDK shouldn't be calling them if it's managing its own users.

@nicola-cab nicola-cab self-requested a review April 25, 2024 17:57
@ironage ironage requested a review from kneth April 25, 2024 18:19
Copy link
Member

@nicola-cab nicola-cab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@kneth kneth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ironage ironage merged commit 129a7d6 into master Apr 29, 2024
36 of 38 checks passed
@ironage ironage deleted the js/app-capi branch April 29, 2024 17:54
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes no-changelog no-jira-ticket Skip checking the PR title for Jira reference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants