Skip to content

Conversation

@swift-kim
Copy link
Member

@swift-kim swift-kim commented Sep 9, 2021

Part of flutter-tizen/flutter-tizen#210.

Changes:

  • The reply channel and related code are completely removed. The sendLaunchRequest method now returns the response as a Map<String, dynamic> if waitForReply is true.
  • API changes: Change callerId to callerAppId and newly implement shouldReply.
  • Create a dedicated AppControlManager for managing AppControl instances. They were previously managed by AppControlChannel.
  • Minor changes. e.g. Change the type of AppControl IDs from int to int32_t, and use unique_ptr instead of shared_ptr.

Newly exported APIs:

  • The following new APIs were added to support automatic disposal of native app_control handles associated with Dart objects.
    • NativeInitializeDartApi
    • NativeCreateAppControl
    • NativeAttachAppControl
  • The usage can be found here. Native handles created in this way do not need to be destroyed explicitly. The destruction callback is automatically called during GC. (The create and dispose methods are still supported just for backward compatibility.)
  • Refer to dart:ffi GC finalizers dart-lang/sdk#35770 for detailed information.
  • Please suggest better design and naming for these APIs!
  • NativeInitializeDartApi may be moved outside app_control.cc if more FFI functions are added in the future.

Notes:

  • Any existing code that uses app_control channels should still work after this change unless the code uses the reply channel.
  • The Dart wrapper of app_control channels is being worked in https://github.com/swift-kim/plugins/tree/add-tizen-app-control/packages/tizen_app_control. The package contains an example app so you can test this change with it.
  • I checked all native handles are properly disposed after GC (no memory leak) using the example app:

    E/ConsoleMessage( 9853): app_control.cc: AppControl(49) > Create AppControl 1
    E/ConsoleMessage( 9853): app_control.cc: AppControl(49) > Create AppControl 2
    E/ConsoleMessage( 9853): app_control.cc: ~AppControl(75) > Destroy AppControl 1
    E/ConsoleMessage( 9853): app_control.cc: ~AppControl(75) > Destroy AppControl 2
    E/ConsoleMessage( 9853): app_control.cc: AppControl(49) > Create AppControl 3
    E/ConsoleMessage( 9853): app_control.cc: AppControl(62) > Create AppControl 4
    E/ConsoleMessage( 9853): app_control.cc: ~AppControl(75) > Destroy AppControl 3
    E/ConsoleMessage( 9853): app_control.cc: ~AppControl(75) > Destroy AppControl 4
    E/ConsoleMessage( 9853): app_control.cc: AppControl(49) > Create AppControl 5
    E/ConsoleMessage( 9853): app_control.cc: AppControl(62) > Create AppControl 6
    E/ConsoleMessage( 9853): app_control.cc: AppControl(49) > Create AppControl 7
    E/ConsoleMessage( 9853): app_control.cc: AppControl(62) > Create AppControl 8
    E/ConsoleMessage( 9853): app_control.cc: ~AppControl(75) > Destroy AppControl 7
    E/ConsoleMessage( 9853): app_control.cc: ~AppControl(75) > Destroy AppControl 5
    E/ConsoleMessage( 9853): app_control.cc: ~AppControl(75) > Destroy AppControl 6
    E/ConsoleMessage( 9853): app_control.cc: ~AppControl(75) > Destroy AppControl 8
    E/ConsoleMessage( 9853): app_control.cc: AppControl(49) > Create AppControl 9
    E/ConsoleMessage( 9853): app_control.cc: AppControl(62) > Create AppControl 10
    E/ConsoleMessage( 9853): app_control.cc: ~AppControl(75) > Destroy AppControl 10
    E/ConsoleMessage( 9853): app_control.cc: ~AppControl(75) > Destroy AppControl 9
  • I'll do some more refactoring after this change. I tried not to make style changes other than logic changes in this PR.

- Let the method channel directly return responses to the caller
- Add NativeInitializeDartApi, NativeCreateAppControl
- Create AppControlManager
- Add NativeAttachAppControl
- Move definitions to the header
@swift-kim swift-kim requested a review from a team September 13, 2021 04:16
}
auto id = app_control->id();
Dart_NewFinalizableHandle_DL(
handle, app_control.get(), 64,
Copy link
Member

Choose a reason for hiding this comment

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

Why is it 64? (Just curious.. 😄 )

Copy link
Member Author

Choose a reason for hiding this comment

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

The value is a hint for the garbage collector as far as the documentation says. Increasing the size may increase the chance of finalizable objects to be collected.

As a side note, the general finalizer support will be added to the Dart language in the near future so this (finalization through FFI) is a temporary solution until then.

}
AppControlResult ret = app_control_reply_to_launch_request(
reply->Handle(), this->handle_, result_e);
reply->handle(), this->handle_, result_e);

Choose a reason for hiding this comment

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

Is there no need the null checking for AppControl*? other functions have same issues.

Copy link
Member Author

Choose a reason for hiding this comment

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

https://github.com/swift-kim/plugins/blob/ffe8d2536693eb1d222cb354b5b958813662f654/packages/tizen_app_control/lib/app_control.dart#L64-L93

An exception will be thrown in the Dart side constructors in case of instantiation failure so it cannot be nullptr as far as I can see (although it can be "invalid" if the destructor already has been called. The platform will return error if the app_control handle is invalid.)

Also I'll closely review the overall implementation in a follow-up PR. I tried not to make many changes to the existing code in this PR.

Choose a reason for hiding this comment

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

ok I understand.


void AppControlChannel::Reply(
std::shared_ptr<AppControl> app_control,
AppControl* app_control,

Choose a reason for hiding this comment

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

ditto

Copy link
Member

@bwikbs bwikbs left a comment

Choose a reason for hiding this comment

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

I didn't see this and I've been looking at how it works for a long time. 😄
Results of the refactoring are expected !!(Very intuitive~!)
It's good try to use a finalizer for the part that is connected to dart in this way. 👍

Copy link

@WonyoungChoi WonyoungChoi left a comment

Choose a reason for hiding this comment

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

Great! clean code 👍

@swift-kim

This comment has been minimized.

Copy link

@pkosko pkosko 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.

@swift-kim swift-kim merged commit d9ef598 into flutter-tizen:flutter-2.2.1-tizen Sep 15, 2021
swift-kim added a commit that referenced this pull request Sep 27, 2021
* Remove the app_control reply channel

- Let the method channel directly return responses to the caller
- Add NativeInitializeDartApi, NativeCreateAppControl
- Create AppControlManager

* Convert shared_ptr to unique_ptr

* Fix memory leaks

- Add NativeAttachAppControl
- Move definitions to the header
swift-kim added a commit that referenced this pull request Nov 14, 2021
* Remove the app_control reply channel

- Let the method channel directly return responses to the caller
- Add NativeInitializeDartApi, NativeCreateAppControl
- Create AppControlManager

* Convert shared_ptr to unique_ptr

* Fix memory leaks

- Add NativeAttachAppControl
- Move definitions to the header
swift-kim added a commit that referenced this pull request Dec 9, 2021
* Remove the app_control reply channel

- Let the method channel directly return responses to the caller
- Add NativeInitializeDartApi, NativeCreateAppControl
- Create AppControlManager

* Convert shared_ptr to unique_ptr

* Fix memory leaks

- Add NativeAttachAppControl
- Move definitions to the header
swift-kim added a commit that referenced this pull request Dec 17, 2021
* Remove the app_control reply channel

- Let the method channel directly return responses to the caller
- Add NativeInitializeDartApi, NativeCreateAppControl
- Create AppControlManager

* Convert shared_ptr to unique_ptr

* Fix memory leaks

- Add NativeAttachAppControl
- Move definitions to the header
swift-kim added a commit that referenced this pull request Feb 7, 2022
* Remove the app_control reply channel

- Let the method channel directly return responses to the caller
- Add NativeInitializeDartApi, NativeCreateAppControl
- Create AppControlManager

* Convert shared_ptr to unique_ptr

* Fix memory leaks

- Add NativeAttachAppControl
- Move definitions to the header
swift-kim added a commit that referenced this pull request Feb 11, 2022
* Remove the app_control reply channel

- Let the method channel directly return responses to the caller
- Add NativeInitializeDartApi, NativeCreateAppControl
- Create AppControlManager

* Convert shared_ptr to unique_ptr

* Fix memory leaks

- Add NativeAttachAppControl
- Move definitions to the header
swift-kim added a commit that referenced this pull request May 12, 2022
* Remove the app_control reply channel

- Let the method channel directly return responses to the caller
- Add NativeInitializeDartApi, NativeCreateAppControl
- Create AppControlManager

* Convert shared_ptr to unique_ptr

* Fix memory leaks

- Add NativeAttachAppControl
- Move definitions to the header
swift-kim added a commit that referenced this pull request Aug 5, 2022
* Remove the app_control reply channel

- Let the method channel directly return responses to the caller
- Add NativeInitializeDartApi, NativeCreateAppControl
- Create AppControlManager

* Convert shared_ptr to unique_ptr

* Fix memory leaks

- Add NativeAttachAppControl
- Move definitions to the header
swift-kim added a commit that referenced this pull request Sep 1, 2022
* Remove the app_control reply channel

- Let the method channel directly return responses to the caller
- Add NativeInitializeDartApi, NativeCreateAppControl
- Create AppControlManager

* Convert shared_ptr to unique_ptr

* Fix memory leaks

- Add NativeAttachAppControl
- Move definitions to the header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants