Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[camera]remove "selfRef" for SavePhotoDelegate and ensure thread safety #4780

Conversation

hellohuanlin
Copy link
Contributor

@hellohuanlin hellohuanlin commented Feb 9, 2022

As discussed in the proposal:

selfReference is used to keep FLTSavePhotoDelegate around. This is a nice trick, but should only be used as a workaround. In our case this delegate object has a clear owner - the delegating object (FLTCam), which controls the lifecycle of this delegate.

The main idea is to have a map of refs in FLTCam (inProgressSavePhotoDelegates), and delete the ref in completion handler. We need to use the map (instead of a single pointer) because photo capture may overlap (e.g. live photos, though i don't think we support live photos yet)

Also fixes a thread safety issue when updating the delegate ref. Fix it together as it's an easy fix.

Both issues are tracked here: flutter/flutter#96429

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/plugins repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@hellohuanlin hellohuanlin force-pushed the camera_delegate_ref_on_session_queue_and_remove_self_ref branch 8 times, most recently from 11f590d to ecffecf Compare February 9, 2022 21:41
@hellohuanlin hellohuanlin marked this pull request as ready for review February 9, 2022 22:01
@hellohuanlin hellohuanlin force-pushed the camera_delegate_ref_on_session_queue_and_remove_self_ref branch 6 times, most recently from 44eb047 to fec77c1 Compare February 11, 2022 20:56
@hellohuanlin hellohuanlin force-pushed the camera_delegate_ref_on_session_queue_and_remove_self_ref branch from fec77c1 to d7466e8 Compare February 11, 2022 21:00
Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

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

The mocking looks much better.

+ (void)ensureToRunOnMainQueue:(void (^)(void))block {
if (!NSThread.isMainThread) {
dispatch_async(dispatch_get_main_queue(), block);
} else {
block();
}
}

+ (dispatch_queue_t)createQueueWithLabel:(const char *)label specific:(const char *)specific {
Copy link
Member

Choose a reason for hiding this comment

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

I generally don't like utility helpers that are only a few lines. It essentially makes developers who are probably already familiar with gcd APIs now have to do the mental jump of understanding what the camera createQueueWithLabel method is doing. This wraps a well-known standard library APIs around a bespoke helper method that doesn't do much and is more the letter than the spirit of DRY.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have a strong opinion here, so will update.

On the one hand, I think the benefit of createQueueWithLabel:specific util function is that it combines 2 calls, so that the caller site won't forget to set the queue specific data; And the benefit of isCurrentlyOnQueueWithSpecific: util function is that it makes the assertion readable like english sentence (e.g. NSAssert([QueueHelper isCurrentlyOnQueueWithSpecific:fooSpec]) vs NSAssert(dispatch_get_specific(fooSpec))

But on the other hand you also made a very good point that these are 2 extra functions that developers have to learn.

@hellohuanlin hellohuanlin force-pushed the camera_delegate_ref_on_session_queue_and_remove_self_ref branch from d7466e8 to e561222 Compare February 16, 2022 05:06
@hellohuanlin hellohuanlin requested a review from jmagman February 16, 2022 05:08
Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

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

LGTM


@interface FLTSavePhotoDelegate ()
/// The file path for the captured photo.
@property(readonly, nonatomic) NSString *path;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@property(readonly, nonatomic) NSString *path;
@property(readonly, nonatomic, copy) NSString *path;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh this is already readonly so i leave out copy

Co-authored-by: Jenn Magder <magder@google.com>
@hellohuanlin hellohuanlin added the waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land. label Feb 16, 2022
@@ -37,7 +38,10 @@ - (instancetype)initWithRegistry:(NSObject<FlutterTextureRegistry> *)registry
NSAssert(self, @"super init cannot be nil");
_registry = [[FLTThreadSafeTextureRegistry alloc] initWithTextureRegistry:registry];
_messenger = messenger;
_captureSessionQueue = dispatch_queue_create("io.flutter.camera.captureSessionQueue", NULL);
_captureSessionQueue = dispatch_queue_create(io.flutter.camera.captureSessionQueue, NULL);
Copy link
Member

Choose a reason for hiding this comment

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

error: use of undeclared identifier 'io'
      _captureSessionQueue = dispatch_queue_create(io.flutter.camera.captureSessionQueue, NULL);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

haha

@fluttergithubbot
Copy link

This pull request is not suitable for automatic merging in its current state.

  • The status or check suite publishable has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite darwin-lint_podspecs has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite ios-build_all_plugins has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite ios-build_all_plugins has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite ios-platform_tests has failed. Please fix the issues identified (or deflake) before re-applying this label.
  • The status or check suite ios-platform_tests has failed. Please fix the issues identified (or deflake) before re-applying this label.

@fluttergithubbot fluttergithubbot removed the waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land. label Feb 16, 2022
@hellohuanlin hellohuanlin added the waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land. label Feb 17, 2022
@fluttergithubbot fluttergithubbot merged commit 0b969a4 into flutter:main Feb 17, 2022
debokarmakar pushed a commit to nurture-farm/plugins that referenced this pull request Feb 17, 2022
* google/master: (340 commits)
  [camera]remove "selfRef" for SavePhotoDelegate and ensure thread safety (flutter#4780)
  Roll Flutter from 919d205 to adafd66 (5 revisions) (flutter#4876)
  [google_sign_in] Update platform interface analysis options (flutter#4872)
  Roll Flutter from b623279 to 919d205 (2 revisions) (flutter#4871)
  Roll Flutter from 14a2b13 to b623279 (5 revisions) (flutter#4870)
  [image_picker] Update platform interface analysis options (flutter#4837)
  [ci] Re-enable stable webview Android tests (flutter#4867)
  Roll Flutter from 286c975 to 14a2b13 (1 revision) (flutter#4865)
  [local_auth] support localizedFallbackTitle in IOSAuthMessages (flutter#3806)
  [image_picker] Update app-facing and web analysis options (flutter#4838)
  Roll Flutter from 8386344 to 286c975 (4 revisions) (flutter#4864)
  Roll Flutter from e9f83cf to 8386344 (6 revisions) (flutter#4862)
  [camera] Switch web package to new analysis options (flutter#4834)
  [flutter_plugin_tool] Fix iOS/macOS naming (flutter#4861)
  [webview_flutter] Fix debuggingEnabled on Android (flutter#4859)
  Roll Flutter from ca2a751 to e9f83cf (10 revisions) (flutter#4857)
  fix license (flutter#4858)
  [video_player] add allowBackgroundPlayback option platform interface changes (flutter#4807)
  [video_player] Updated Pigeon version (flutter#4726)
  Roll Flutter from 381cb28 to ca2a751 (5 revisions) (flutter#4850)
  ...

# Conflicts:
#	packages/camera/camera/CHANGELOG.md
#	packages/camera/camera/android/build.gradle
#	packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/Camera.java
#	packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/MethodCallHandlerImpl.java
#	packages/camera/camera/ios/Classes/CameraPlugin.m
#	packages/camera/camera/ios/camera.podspec
#	packages/camera/camera/lib/camera.dart
#	packages/camera/camera/lib/src/camera_controller.dart
#	packages/camera/camera/pubspec.yaml
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Feb 17, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Feb 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p: camera platform-ios waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants