Skip to content

Commit

Permalink
Has a binary messenger (flutter#9419)
Browse files Browse the repository at this point in the history
Made the engine and the view controllers have BinaryMessengers, not be
BinaryMessengers.  This allows us to break retain cycles and makes the
leaking channels we have not less dire.
  • Loading branch information
gaaclarke authored Jun 28, 2019
1 parent 7483665 commit 50a8e73
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 41 deletions.
3 changes: 3 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,9 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewCo
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Info.plist
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Internal.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelayTest.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterCallbackCache.mm
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterCallbackCache_Internal.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm
Expand Down
1 change: 1 addition & 0 deletions shell/platform/darwin/ios/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ shared_library("create_flutter_framework_dylib") {
sources = [
"framework/Source/FlutterAppDelegate.mm",
"framework/Source/FlutterAppDelegate_Internal.h",
"framework/Source/FlutterBinaryMessengerRelay.mm",
"framework/Source/FlutterCallbackCache.mm",
"framework/Source/FlutterCallbackCache_Internal.h",
"framework/Source/FlutterDartProject.mm",
Expand Down
9 changes: 7 additions & 2 deletions shell/platform/darwin/ios/framework/Headers/FlutterEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
* One of these methods must be invoked before calling `-setViewController:`.
*/
FLUTTER_EXPORT
@interface FlutterEngine
: NSObject <FlutterBinaryMessenger, FlutterTextureRegistry, FlutterPluginRegistry>
@interface FlutterEngine : NSObject <FlutterTextureRegistry, FlutterPluginRegistry>
/**
* Initialize this FlutterEngine with a `FlutterDartProject`.
*
Expand Down Expand Up @@ -237,6 +236,12 @@ FLUTTER_EXPORT
*/
@property(nonatomic, readonly) NSURL* observatoryUrl;

/**
* The `FlutterBinaryMessenger` associated with this FlutterEngine (used for communicating with
* channels).
*/
@property(nonatomic, readonly) NSObject<FlutterBinaryMessenger>* binaryMessenger;

@end

#endif // FLUTTER_FLUTTERENGINE_H_
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ extern NSNotificationName const FlutterSemanticsUpdateNotification;
* forth between a FlutterViewController and other `UIViewController`s.
*/
FLUTTER_EXPORT
@interface FlutterViewController
: UIViewController <FlutterBinaryMessenger, FlutterTextureRegistry, FlutterPluginRegistry>
@interface FlutterViewController : UIViewController <FlutterTextureRegistry, FlutterPluginRegistry>

/**
* Initializes this FlutterViewController with the specified `FlutterEngine`.
Expand Down Expand Up @@ -165,6 +164,16 @@ FLUTTER_EXPORT
*/
@property(weak, nonatomic, readonly) FlutterEngine* engine;

/**
* The `FlutterBinaryMessenger` associated with this FlutterViewController (used for communicating
* with channels).
*
* @deprecated Since |FlutterViewController| just forwards binary messenger calls to the
* |FlutterEngine|, just use the FlutterEngine.binaryMessenger.
*/
@property(nonatomic, readonly) NSObject<FlutterBinaryMessenger>* binaryMessenger
__attribute__((deprecated));

@end

#endif // FLUTTER_FLUTTERVIEWCONTROLLER_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"

#ifndef NDEBUG
FLUTTER_EXPORT
#endif
@interface FlutterBinaryMessengerRelay : NSObject <FlutterBinaryMessenger>
@property(nonatomic, assign) NSObject<FlutterBinaryMessenger>* parent;
- (instancetype)initWithParent:(NSObject<FlutterBinaryMessenger>*)parent;
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.h"

#include "flutter/fml/logging.h"

@implementation FlutterBinaryMessengerRelay
#pragma mark - FlutterBinaryMessenger

- (instancetype)initWithParent:(NSObject<FlutterBinaryMessenger>*)parent {
self = [super init];
if (self != nil) {
self.parent = parent;
}
return self;
}

- (void)sendOnChannel:(NSString*)channel message:(NSData*)message {
if (self.parent) {
[self.parent sendOnChannel:channel message:message binaryReply:nil];
} else {
FML_LOG(WARNING) << "Communicating on a dead channel.";
}
}

- (void)sendOnChannel:(NSString*)channel
message:(NSData*)message
binaryReply:(FlutterBinaryReply)callback {
if (self.parent) {
[self.parent sendOnChannel:channel message:message binaryReply:callback];
} else {
FML_LOG(WARNING) << "Communicating on a dead channel.";
}
}

- (void)setMessageHandlerOnChannel:(NSString*)channel
binaryMessageHandler:(FlutterBinaryMessageHandler)handler {
if (self.parent) {
[self.parent setMessageHandlerOnChannel:channel binaryMessageHandler:handler];
} else {
FML_LOG(WARNING) << "Communicating on a dead channel.";
}
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <OCMock/OCMock.h>
#import <XCTest/XCTest.h>
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.h"

#ifndef __has_feature
#define __has_feature(x) 0 /* for non-clang compilers */
#endif

#if !__has_feature(objc_arc)
#error ARC must be enabled!
#endif

@interface FlutterBinaryMessengerRelayTest : XCTestCase
@end

@implementation FlutterBinaryMessengerRelayTest

- (void)setUp {
}

- (void)tearDown {
}

- (void)testCreate {
id messenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
FlutterBinaryMessengerRelay* relay =
[[FlutterBinaryMessengerRelay alloc] initWithParent:messenger];
XCTAssertNotNil(relay);
XCTAssertEqual(messenger, relay.parent);
}

- (void)testPassesCallOn {
id messenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
FlutterBinaryMessengerRelay* relay =
[[FlutterBinaryMessengerRelay alloc] initWithParent:messenger];
char messageData[] = {'a', 'a', 'r', 'o', 'n'};
NSData* message = [NSData dataWithBytes:messageData length:sizeof(messageData)];
NSString* channel = @"foobar";
[relay sendOnChannel:channel message:message binaryReply:nil];
OCMVerify([messenger sendOnChannel:channel message:message binaryReply:nil]);
}

- (void)testDoesntPassCallOn {
id messenger = OCMStrictProtocolMock(@protocol(FlutterBinaryMessenger));
FlutterBinaryMessengerRelay* relay =
[[FlutterBinaryMessengerRelay alloc] initWithParent:messenger];
char messageData[] = {'a', 'a', 'r', 'o', 'n'};
NSData* message = [NSData dataWithBytes:messageData length:sizeof(messageData)];
NSString* channel = @"foobar";
relay.parent = nil;
[relay sendOnChannel:channel message:message binaryReply:nil];
}

@end
47 changes: 28 additions & 19 deletions shell/platform/darwin/ios/framework/Source/FlutterEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
#include "flutter/shell/common/switches.h"
#include "flutter/shell/common/thread_host.h"
#include "flutter/shell/platform/darwin/common/command_line.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterObservatoryPublisher.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.h"
#include "flutter/shell/platform/darwin/ios/ios_surface.h"
#include "flutter/shell/platform/darwin/ios/platform_view_ios.h"

@interface FlutterEngine () <FlutterTextInputDelegate>
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterObservatoryPublisher.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.h"
#import "flutter/shell/platform/darwin/ios/ios_surface.h"
#import "flutter/shell/platform/darwin/ios/platform_view_ios.h"

@interface FlutterEngine () <FlutterTextInputDelegate, FlutterBinaryMessenger>
// Maintains a dictionary of plugin names that have registered with the engine. Used by
// FlutterEngineRegistrar to implement a FlutterPluginRegistrar.
@property(nonatomic, readonly) NSMutableDictionary* pluginPublications;
Expand Down Expand Up @@ -65,6 +66,7 @@ @implementation FlutterEngine {
uint64_t _nextPointerFlowId;

BOOL _allowHeadlessExecution;
FlutterBinaryMessengerRelay* _binaryMessenger;
}

- (instancetype)initWithName:(NSString*)labelPrefix project:(FlutterDartProject*)projectOrNil {
Expand Down Expand Up @@ -92,6 +94,7 @@ - (instancetype)initWithName:(NSString*)labelPrefix
_platformViewsController.reset(new flutter::FlutterPlatformViewsController());

[self setupChannels];
_binaryMessenger = [[FlutterBinaryMessengerRelay alloc] initWithParent:self];

NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self
Expand All @@ -104,6 +107,8 @@ - (instancetype)initWithName:(NSString*)labelPrefix

- (void)dealloc {
[_pluginPublications release];
_binaryMessenger.parent = nil;
[_binaryMessenger release];

NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
Expand Down Expand Up @@ -238,42 +243,42 @@ - (void)resetChannels {
- (void)setupChannels {
_localizationChannel.reset([[FlutterMethodChannel alloc]
initWithName:@"flutter/localization"
binaryMessenger:self
binaryMessenger:self.binaryMessenger
codec:[FlutterJSONMethodCodec sharedInstance]]);

_navigationChannel.reset([[FlutterMethodChannel alloc]
initWithName:@"flutter/navigation"
binaryMessenger:self
binaryMessenger:self.binaryMessenger
codec:[FlutterJSONMethodCodec sharedInstance]]);

_platformChannel.reset([[FlutterMethodChannel alloc]
initWithName:@"flutter/platform"
binaryMessenger:self
binaryMessenger:self.binaryMessenger
codec:[FlutterJSONMethodCodec sharedInstance]]);

_platformViewsChannel.reset([[FlutterMethodChannel alloc]
initWithName:@"flutter/platform_views"
binaryMessenger:self
binaryMessenger:self.binaryMessenger
codec:[FlutterStandardMethodCodec sharedInstance]]);

_textInputChannel.reset([[FlutterMethodChannel alloc]
initWithName:@"flutter/textinput"
binaryMessenger:self
binaryMessenger:self.binaryMessenger
codec:[FlutterJSONMethodCodec sharedInstance]]);

_lifecycleChannel.reset([[FlutterBasicMessageChannel alloc]
initWithName:@"flutter/lifecycle"
binaryMessenger:self
binaryMessenger:self.binaryMessenger
codec:[FlutterStringCodec sharedInstance]]);

_systemChannel.reset([[FlutterBasicMessageChannel alloc]
initWithName:@"flutter/system"
binaryMessenger:self
binaryMessenger:self.binaryMessenger
codec:[FlutterJSONMessageCodec sharedInstance]]);

_settingsChannel.reset([[FlutterBasicMessageChannel alloc]
initWithName:@"flutter/settings"
binaryMessenger:self
binaryMessenger:self.binaryMessenger
codec:[FlutterJSONMessageCodec sharedInstance]]);

_textInputPlugin.reset([[FlutterTextInputPlugin alloc] init]);
Expand Down Expand Up @@ -508,6 +513,10 @@ - (void)performAction:(FlutterTextInputAction)action withClient:(int)client {
return _shell->Screenshot(type, base64Encode);
}

- (NSObject<FlutterBinaryMessenger>*)binaryMessenger {
return _binaryMessenger;
}

#pragma mark - FlutterBinaryMessenger

- (void)sendOnChannel:(NSString*)channel message:(NSData*)message {
Expand Down Expand Up @@ -615,7 +624,7 @@ - (void)dealloc {
}

- (NSObject<FlutterBinaryMessenger>*)messenger {
return _flutterEngine;
return _flutterEngine.binaryMessenger;
}

- (NSObject<FlutterTextureRegistry>*)textures {
Expand Down
Loading

0 comments on commit 50a8e73

Please sign in to comment.