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

Commit 1e66c0a

Browse files
authored
Made FlutterTaskQueue visible for Swift. (#47348)
fixes flutter/flutter#118832 This makes the `FlutterTaskQueue` protocol visible, but keeps the `dispatch:` selector private. Under the covers there are runtime checks to make sure `dispatch:` is available since we can't have compile time checks anymore. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I added new tests to check the change I am making or feature I am adding, or the PR is [test-exempt]. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I signed the [CLA]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat
1 parent 0bba9ee commit 1e66c0a

File tree

8 files changed

+41
-22
lines changed

8 files changed

+41
-22
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3614,6 +3614,7 @@ ORIGIN: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMeta
36143614
ORIGIN: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalSkia.mm + ../../../flutter/LICENSE
36153615
ORIGIN: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.h + ../../../flutter/LICENSE
36163616
ORIGIN: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.mm + ../../../flutter/LICENSE
3617+
ORIGIN: ../../../flutter/shell/platform/darwin/ios/flutter_task_queue_dispatch.h + ../../../flutter/LICENSE
36173618
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Headers/Flutter.h + ../../../flutter/LICENSE
36183619
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h + ../../../flutter/LICENSE
36193620
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterCallbackCache.h + ../../../flutter/LICENSE
@@ -6412,6 +6413,7 @@ FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalS
64126413
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalSkia.mm
64136414
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.h
64146415
FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextureMetal.mm
6416+
FILE: ../../../flutter/shell/platform/darwin/ios/flutter_task_queue_dispatch.h
64156417
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/Flutter.h
64166418
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h
64176419
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterCallbackCache.h

shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ typedef void (^FlutterBinaryMessageHandler)(NSData* _Nullable message, FlutterBi
3131

3232
typedef int64_t FlutterBinaryMessengerConnection;
3333

34-
@protocol FlutterTaskQueue;
34+
@protocol FlutterTaskQueue <NSObject>
35+
@end
3536

3637
/**
3738
* A facility for communicating with the Flutter side using asynchronous message

shell/platform/darwin/common/framework/Headers/FlutterChannels.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#import "FlutterBinaryMessenger.h"
99
#import "FlutterCodecs.h"
1010

11-
@protocol FlutterTaskQueue;
12-
1311
NS_ASSUME_NONNULL_BEGIN
1412
/**
1513
* A message reply callback.

shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelayTest.mm

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88

99
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
1010
#import "flutter/shell/platform/darwin/common/framework/Source/FlutterTestUtils.h"
11+
#import "flutter/shell/platform/darwin/ios/flutter_task_queue_dispatch.h"
1112
#import "flutter/testing/testing.h"
1213
#include "gtest/gtest.h"
1314

1415
FLUTTER_ASSERT_ARC
1516

16-
@protocol FlutterTaskQueue <NSObject>
17-
@end
18-
1917
@interface FlutterBinaryMessengerRelayTest : NSObject
2018
@end
2119

@@ -56,7 +54,7 @@ - (void)testSetMessageHandlerWithTaskQueue {
5654
FlutterBinaryMessengerRelay* relay =
5755
[[FlutterBinaryMessengerRelay alloc] initWithParent:messenger];
5856
NSString* channel = @"foobar";
59-
NSObject<FlutterTaskQueue>* taskQueue = OCMProtocolMock(@protocol(FlutterTaskQueue));
57+
NSObject<FlutterTaskQueue>* taskQueue = OCMProtocolMock(@protocol(FlutterTaskQueueDispatch));
6058
FlutterBinaryMessageHandler handler = ^(NSData* _Nullable, FlutterBinaryReply _Nonnull) {
6159
};
6260
[relay setMessageHandlerOnChannel:channel binaryMessageHandler:handler taskQueue:taskQueue];

shell/platform/darwin/common/framework/Source/FlutterChannelsTest.m

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
// found in the LICENSE file.
44

55
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
6+
#import "flutter/shell/platform/darwin/ios/flutter_task_queue_dispatch.h"
67

78
#import <OCMock/OCMock.h>
89
#import <XCTest/XCTest.h>
910

1011
FLUTTER_ASSERT_ARC
1112

12-
@protocol FlutterTaskQueue <NSObject>
13-
@end
14-
1513
@interface MockBinaryMessenger : NSObject <FlutterBinaryMessenger>
1614
@property(nonatomic, copy) NSString* channel;
1715
@property(nonatomic, strong) NSData* message;
@@ -243,7 +241,7 @@ - (void)testBasicMessageChannelTaskQueue {
243241
FlutterBinaryMessengerConnection connection = 123;
244242
id binaryMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
245243
id codec = OCMProtocolMock(@protocol(FlutterMethodCodec));
246-
id taskQueue = OCMProtocolMock(@protocol(FlutterTaskQueue));
244+
id taskQueue = OCMProtocolMock(@protocol(FlutterTaskQueueDispatch));
247245
FlutterBasicMessageChannel* channel =
248246
[[FlutterBasicMessageChannel alloc] initWithName:channelName
249247
binaryMessenger:binaryMessenger
@@ -271,7 +269,7 @@ - (void)testBasicMessageChannelInvokeHandlerAfterChannelReleased {
271269
FlutterBinaryMessengerConnection connection = 123;
272270
id binaryMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
273271
id codec = OCMProtocolMock(@protocol(FlutterMessageCodec));
274-
id taskQueue = OCMProtocolMock(@protocol(FlutterTaskQueue));
272+
id taskQueue = OCMProtocolMock(@protocol(FlutterTaskQueueDispatch));
275273
FlutterBasicMessageChannel* channel =
276274
[[FlutterBasicMessageChannel alloc] initWithName:channelName
277275
binaryMessenger:binaryMessenger
@@ -308,7 +306,7 @@ - (void)testMethodChannelInvokeHandlerAfterChannelReleased {
308306
@autoreleasepool {
309307
id binaryMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
310308
id codec = OCMProtocolMock(@protocol(FlutterMethodCodec));
311-
id taskQueue = OCMProtocolMock(@protocol(FlutterTaskQueue));
309+
id taskQueue = OCMProtocolMock(@protocol(FlutterTaskQueueDispatch));
312310
FlutterMethodChannel* channel = [[FlutterMethodChannel alloc] initWithName:channelName
313311
binaryMessenger:binaryMessenger
314312
codec:codec
@@ -340,7 +338,7 @@ - (void)testMethodChannelTaskQueue {
340338
FlutterBinaryMessengerConnection connection = 123;
341339
id binaryMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
342340
id codec = OCMProtocolMock(@protocol(FlutterMethodCodec));
343-
id taskQueue = OCMProtocolMock(@protocol(FlutterTaskQueue));
341+
id taskQueue = OCMProtocolMock(@protocol(FlutterTaskQueueDispatch));
344342
FlutterMethodChannel* channel = [[FlutterMethodChannel alloc] initWithName:channelName
345343
binaryMessenger:binaryMessenger
346344
codec:codec
@@ -365,7 +363,7 @@ - (void)testEventChannelTaskQueue {
365363
FlutterBinaryMessengerConnection connection = 123;
366364
id binaryMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
367365
id codec = OCMProtocolMock(@protocol(FlutterMethodCodec));
368-
id taskQueue = OCMProtocolMock(@protocol(FlutterTaskQueue));
366+
id taskQueue = OCMProtocolMock(@protocol(FlutterTaskQueueDispatch));
369367
id handler = OCMProtocolMock(@protocol(FlutterStreamHandler));
370368
FlutterEventChannel* channel = [[FlutterEventChannel alloc] initWithName:channelName
371369
binaryMessenger:binaryMessenger
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FLUTTER_TASK_QUEUE_DISPATCH_H_
6+
#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FLUTTER_TASK_QUEUE_DISPATCH_H_
7+
8+
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h"
9+
10+
/// The private implementation of `FlutterTaskQueue` that has method
11+
/// declarations.
12+
///
13+
/// `FlutterTaskQueue` doesn't have any methods publicly since it is supposed to
14+
/// be an opaque data structure. For Swift integration though `FlutterTaskQueue`
15+
/// is visible publicly with no methods.
16+
@protocol FlutterTaskQueueDispatch <FlutterTaskQueue>
17+
- (void)dispatch:(dispatch_block_t)block;
18+
@end
19+
20+
#endif

shell/platform/darwin/ios/platform_message_handler_ios.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
1313
#include "flutter/shell/common/platform_message_handler.h"
1414
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h"
15-
16-
@protocol FlutterTaskQueue
17-
- (void)dispatch:(dispatch_block_t)block;
18-
@end
15+
#import "flutter/shell/platform/darwin/ios/flutter_task_queue_dispatch.h"
1916

2017
namespace flutter {
2118

@@ -39,7 +36,7 @@ class PlatformMessageHandlerIos : public PlatformMessageHandler {
3936
NSObject<FlutterTaskQueue>* task_queue);
4037

4138
struct HandlerInfo {
42-
fml::scoped_nsprotocol<NSObject<FlutterTaskQueue>*> task_queue;
39+
fml::scoped_nsprotocol<NSObject<FlutterTaskQueueDispatch>*> task_queue;
4340
fml::ScopedBlock<FlutterBinaryMessageHandler> handler;
4441
};
4542

shell/platform/darwin/ios/platform_message_handler_ios.mm

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
static uint64_t platform_message_counter = 1;
1313

14-
@interface FLTSerialTaskQueue : NSObject <FlutterTaskQueue>
14+
@interface FLTSerialTaskQueue : NSObject <FlutterTaskQueueDispatch>
1515
@property(nonatomic, strong) dispatch_queue_t queue;
1616
@end
1717

@@ -118,12 +118,17 @@ - (void)dispatch:(dispatch_block_t)block {
118118
FlutterBinaryMessageHandler handler,
119119
NSObject<FlutterTaskQueue>* task_queue) {
120120
FML_CHECK(platform_task_runner_->RunsTasksOnCurrentThread());
121+
// Use `respondsToSelector` instead of `conformsToProtocol` to accomodate
122+
// injecting your own `FlutterTaskQueue`. This is not a supported usage but
123+
// not one worth breaking.
124+
FML_CHECK(!task_queue || [task_queue respondsToSelector:@selector(dispatch:)]);
121125
/// TODO(gaaclarke): This should be migrated to a lockfree datastructure.
122126
std::lock_guard lock(message_handlers_mutex_);
123127
message_handlers_.erase(channel);
124128
if (handler) {
125129
message_handlers_[channel] = {
126-
.task_queue = fml::scoped_nsprotocol([task_queue retain]),
130+
.task_queue = fml::scoped_nsprotocol(
131+
[static_cast<NSObject<FlutterTaskQueueDispatch>*>(task_queue) retain]),
127132
.handler =
128133
fml::ScopedBlock<FlutterBinaryMessageHandler>{handler, fml::OwnershipPolicy::kRetain},
129134
};

0 commit comments

Comments
 (0)