Skip to content

Commit ff663c6

Browse files
authored
iOS: Key the platform view factory registries by NSString (flutter#191855)
`FlutterPlatformViewsController` was holding its view factories and their gesture blocking policies in `std::unordered_map`s keyed by `std::string`, so both the registration path and the create path converted an `NSString` to a `std::string` first. But all the mapped values are Obj-C types so we can migrate to na `NSMutableDictionary`. This matches how we do it in the macOS embedder. This removes the last of the `UTF8String` conversions in this file. As noted in flutter#191853 and flutter#191854 `std::string`'s `const char*` constructor determines the length using `strlen`, so it truncates at an embedded NUL and dereferences null if `UTF8String` ever returns NULL, which it does for a nil receiver. Spotted while working on: flutter#191616 No new tests since this just changes the implementation without changing any semantics. <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 4496c5f commit ff663c6

1 file changed

Lines changed: 14 additions & 24 deletions

File tree

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsController.mm‎

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ @interface FlutterPlatformViewsController ()
105105
@property(nonatomic, readonly) FlutterClippingMaskViewPool* maskViewPool;
106106

107107
@property(nonatomic, readonly)
108-
std::unordered_map<std::string, NSObject<FlutterPlatformViewFactory>*>& factories;
108+
NSMutableDictionary<NSString*, NSObject<FlutterPlatformViewFactory>*>* factories;
109109

110-
// The FlutterPlatformViewGestureRecognizersBlockingPolicy for each type of platform view.
110+
// The FlutterPlatformViewGestureRecognizersBlockingPolicy for each type of platform view, boxed in
111+
// an NSNumber.
111112
@property(nonatomic, readonly)
112-
std::unordered_map<std::string, FlutterPlatformViewGestureRecognizersBlockingPolicy>&
113-
gestureRecognizersBlockingPoliciesByType;
113+
NSMutableDictionary<NSString*, NSNumber*>* gestureRecognizersBlockingPoliciesByType;
114114

115115
/// The size of the current onscreen surface in physical pixels.
116116
@property(nonatomic, assign) DlISize frameSize;
@@ -238,9 +238,8 @@ @implementation FlutterPlatformViewsController {
238238
// `self.slices[viewId] = x`.
239239
std::unique_ptr<flutter::OverlayLayerPool> _layerPool;
240240
std::unordered_map<int64_t, std::unique_ptr<flutter::EmbedderViewSlice>> _slices;
241-
std::unordered_map<std::string, NSObject<FlutterPlatformViewFactory>*> _factories;
242-
std::unordered_map<std::string, FlutterPlatformViewGestureRecognizersBlockingPolicy>
243-
_gestureRecognizersBlockingPoliciesByType;
241+
NSMutableDictionary<NSString*, NSObject<FlutterPlatformViewFactory>*>* _factories;
242+
NSMutableDictionary<NSString*, NSNumber*>* _gestureRecognizersBlockingPoliciesByType;
244243
FlutterFMLTaskRunner* _platformTaskRunner;
245244
std::unordered_map<int64_t, PlatformViewData> _platformViews;
246245
std::unordered_map<int64_t, flutter::EmbeddedViewParams> _currentCompositionParams;
@@ -254,6 +253,8 @@ @implementation FlutterPlatformViewsController {
254253
- (id)init {
255254
if (self = [super init]) {
256255
_layerPool = std::make_unique<flutter::OverlayLayerPool>();
256+
_factories = [[NSMutableDictionary alloc] init];
257+
_gestureRecognizersBlockingPoliciesByType = [[NSMutableDictionary alloc] init];
257258
_maskViewPool =
258259
[[FlutterClippingMaskViewPool alloc] initWithCapacity:kFlutterClippingMaskViewPoolCapacity];
259260
_hadPlatformViews = NO;
@@ -298,16 +299,14 @@ - (void)onCreate:(FlutterMethodCall*)call result:(FlutterResult)result {
298299
details:nil]);
299300
return;
300301
}
301-
std::string viewType(viewTypeString.UTF8String);
302-
303302
if (self.platformViews.count(viewId) != 0) {
304303
result([FlutterError errorWithCode:@"recreating_view"
305304
message:@"trying to create an already created view"
306305
details:[NSString stringWithFormat:@"view id: '%lld'", viewId]]);
307306
return;
308307
}
309308

310-
NSObject<FlutterPlatformViewFactory>* factory = self.factories[viewType];
309+
NSObject<FlutterPlatformViewFactory>* factory = self.factories[viewTypeString];
311310
if (factory == nil) {
312311
result([FlutterError
313312
errorWithCode:@"unregistered_view_type"
@@ -352,7 +351,8 @@ - (void)onCreate:(FlutterMethodCall*)call result:(FlutterResult)result {
352351
FlutterPlatformViewGestureRecognizersBlockingPolicyWaitUntilTouchesEnded;
353352
} else if ([gestureBlockingPolicyValue
354353
isEqualToString:kGestureBlockingPolicyFallbackToPluginDefault]) {
355-
gestureBlockingPolicy = self.gestureRecognizersBlockingPoliciesByType[viewType];
354+
gestureBlockingPolicy = static_cast<FlutterPlatformViewGestureRecognizersBlockingPolicy>(
355+
self.gestureRecognizersBlockingPoliciesByType[viewTypeString].integerValue);
356356
} else {
357357
result([FlutterError
358358
errorWithCode:@"unknown_gesture_blocking_policy"
@@ -431,10 +431,9 @@ - (void)registerViewFactory:(NSObject<FlutterPlatformViewFactory>*)factory
431431
withId:(NSString*)factoryId
432432
gestureRecognizersBlockingPolicy:
433433
(FlutterPlatformViewGestureRecognizersBlockingPolicy)gestureRecognizerBlockingPolicy {
434-
std::string idString([factoryId UTF8String]);
435-
FML_CHECK(self.factories.count(idString) == 0);
436-
self.factories[idString] = factory;
437-
self.gestureRecognizersBlockingPoliciesByType[idString] = gestureRecognizerBlockingPolicy;
434+
FML_CHECK(self.factories[factoryId] == nil);
435+
self.factories[factoryId] = factory;
436+
self.gestureRecognizersBlockingPoliciesByType[factoryId] = @(gestureRecognizerBlockingPolicy);
438437
}
439438

440439
- (void)beginFrameWithSize:(DlISize)frameSize {
@@ -1105,15 +1104,6 @@ - (void)pushClipPathToVisitedPlatformViews:(const flutter::DlPath&)clipPath {
11051104
return _slices;
11061105
}
11071106

1108-
- (std::unordered_map<std::string, NSObject<FlutterPlatformViewFactory>*>&)factories {
1109-
return _factories;
1110-
}
1111-
1112-
- (std::unordered_map<std::string, FlutterPlatformViewGestureRecognizersBlockingPolicy>&)
1113-
gestureRecognizersBlockingPoliciesByType {
1114-
return _gestureRecognizersBlockingPoliciesByType;
1115-
}
1116-
11171107
- (std::unordered_map<int64_t, PlatformViewData>&)platformViews {
11181108
return _platformViews;
11191109
}

0 commit comments

Comments
 (0)