Skip to content

Commit

Permalink
add observatoryUrl property to FlutterEngine (flutter#8987)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnfield authored May 16, 2019
1 parent 34a5248 commit 9d7cd70
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
9 changes: 9 additions & 0 deletions shell/platform/darwin/ios/framework/Headers/FlutterEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ FLUTTER_EXPORT
*/
@property(nonatomic, readonly) FlutterBasicMessageChannel* settingsChannel;

/**
* The `NSURL` of the observatory for the service isolate.
*
* This is only set in debug and profile runtime modes, and only after the
* observatory service is ready. In release mode or before the observatory has
* started, it returns `nil`.
*/
@property(nonatomic, readonly) NSURL* observatoryUrl;

@end

#endif // FLUTTER_FLUTTERENGINE_H_
4 changes: 4 additions & 0 deletions shell/platform/darwin/ios/framework/Source/FlutterEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ - (FlutterBasicMessageChannel*)settingsChannel {
return _settingsChannel.get();
}

- (NSURL*)observatoryUrl {
return [_publisher.get() url];
}

- (void)resetChannels {
_localizationChannel.reset();
_navigationChannel.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

@interface FlutterObservatoryPublisher : NSObject

@property(nonatomic, readonly) NSURL* url;

@end

#endif // FLUTTER_FLUTTEROBSERVATORYPUBLISHER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ @interface FlutterObservatoryPublisher () <NSNetServiceDelegate>
@end

@implementation FlutterObservatoryPublisher {
fml::scoped_nsobject<NSURL> _url;
#if TARGET_IPHONE_SIMULATOR
DNSServiceRef _dnsServiceRef;
#else // TARGET_IPHONE_SIMULATOR
Expand All @@ -46,6 +47,10 @@ @implementation FlutterObservatoryPublisher {
std::unique_ptr<fml::WeakPtrFactory<FlutterObservatoryPublisher>> _weakFactory;
}

- (NSURL*)url {
return _url.get();
}

- (instancetype)init {
self = [super init];
NSAssert(self, @"Super must not return null on init.");
Expand Down Expand Up @@ -92,15 +97,14 @@ - (void)publishServiceProtocolPort:(std::string)uri {
}
// uri comes in as something like 'http://127.0.0.1:XXXXX/' where XXXXX is the port
// number.
NSURL* url =
[[[NSURL alloc] initWithString:[NSString stringWithUTF8String:uri.c_str()]] autorelease];
_url.reset([[NSURL alloc] initWithString:[NSString stringWithUTF8String:uri.c_str()]]);

NSString* serviceName =
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];

// Check to see if there's an authentication code. If there is, we'll provide
// it as a txt record so flutter tools can establish a connection.
auto path = std::string{[[url path] UTF8String]};
auto path = std::string{[[_url path] UTF8String]};
if (!path.empty()) {
// Remove leading "/"
path = path.substr(1);
Expand All @@ -116,7 +120,7 @@ - (void)publishServiceProtocolPort:(std::string)uri {
uint32_t interfaceIndex = if_nametoindex("lo0");
const char* registrationType = "_dartobservatory._tcp";
const char* domain = "local."; // default domain
uint16_t port = [[url port] intValue];
uint16_t port = [[_url port] intValue];

int err = DNSServiceRegister(&_dnsServiceRef, flags, interfaceIndex, [serviceName UTF8String],
registrationType, domain, NULL, htons(port), txtData.length,
Expand All @@ -131,7 +135,7 @@ - (void)publishServiceProtocolPort:(std::string)uri {
NSNetService* netServiceTmp = [[NSNetService alloc] initWithDomain:@"local."
type:@"_dartobservatory._tcp."
name:serviceName
port:[[url port] intValue]];
port:[[_url port] intValue]];
[netServiceTmp setTXTRecordData:txtData];
_netService.reset(netServiceTmp);
[_netService.get() setDelegate:self];
Expand Down

0 comments on commit 9d7cd70

Please sign in to comment.