forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Advertise iOS Observatory port over mDNS/Bonjour (flutter#6533)
* Publish port of observatory over mDNS
- Loading branch information
Showing
9 changed files
with
238 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2018 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. | ||
|
||
#include "flutter/runtime/dart_service_isolate.h" | ||
#include "flutter/testing/testing.h" | ||
|
||
namespace blink { | ||
|
||
TEST(DartServiceIsolateTest, CanAddAndRemoveHandles) { | ||
ASSERT_EQ(DartServiceIsolate::AddServerStatusCallback(nullptr), 0); | ||
auto handle = DartServiceIsolate::AddServerStatusCallback([](const auto&) {}); | ||
ASSERT_NE(handle, 0); | ||
ASSERT_TRUE(DartServiceIsolate::RemoveServerStatusCallback(handle)); | ||
} | ||
|
||
} // namespace blink |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
shell/platform/darwin/ios/framework/Source/FlutterObservatoryPublisher.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2018 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. | ||
|
||
#ifndef FLUTTER_FLUTTEROBSERVATORYPUBLISHER_H_ | ||
#define FLUTTER_FLUTTEROBSERVATORYPUBLISHER_H_ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface FlutterObservatoryPublisher : NSObject | ||
|
||
@end | ||
|
||
#endif // FLUTTER_FLUTTEROBSERVATORYPUBLISHER_H_ |
104 changes: 104 additions & 0 deletions
104
shell/platform/darwin/ios/framework/Source/FlutterObservatoryPublisher.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2018 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. | ||
|
||
#define FML_USED_ON_EMBEDDER | ||
|
||
#import "FlutterObservatoryPublisher.h" | ||
|
||
#include "flutter/fml/logging.h" | ||
#include "flutter/fml/make_copyable.h" | ||
#include "flutter/fml/memory/weak_ptr.h" | ||
#include "flutter/fml/message_loop.h" | ||
#include "flutter/fml/platform/darwin/scoped_nsobject.h" | ||
#include "flutter/fml/task_runner.h" | ||
#include "flutter/runtime/dart_service_isolate.h" | ||
|
||
#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_RELEASE || \ | ||
FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DYNAMIC_RELEASE | ||
|
||
@implementation FlutterObservatoryPublisher { | ||
} | ||
|
||
#else | ||
|
||
@interface FlutterObservatoryPublisher () <NSNetServiceDelegate> | ||
@end | ||
|
||
@implementation FlutterObservatoryPublisher { | ||
fml::scoped_nsobject<NSNetService> _netService; | ||
|
||
blink::DartServiceIsolate::CallbackHandle _callbackHandle; | ||
std::unique_ptr<fml::WeakPtrFactory<FlutterObservatoryPublisher>> _weakFactory; | ||
} | ||
|
||
- (instancetype)init { | ||
self = [super init]; | ||
NSAssert(self, @"Super must not return null on init."); | ||
|
||
_weakFactory = std::make_unique<fml::WeakPtrFactory<FlutterObservatoryPublisher>>(self); | ||
|
||
fml::MessageLoop::EnsureInitializedForCurrentThread(); | ||
|
||
_callbackHandle = blink::DartServiceIsolate::AddServerStatusCallback( | ||
[weak = _weakFactory->GetWeakPtr(), | ||
runner = fml::MessageLoop::GetCurrent().GetTaskRunner()](const std::string& uri) { | ||
runner->PostTask([weak, uri]() { | ||
if (weak) { | ||
[weak.get() publishServiceProtocolPort:std::move(uri)]; | ||
} | ||
}); | ||
}); | ||
|
||
return self; | ||
} | ||
|
||
- (void)dealloc { | ||
[_netService.get() stop]; | ||
blink::DartServiceIsolate::RemoveServerStatusCallback(std::move(_callbackHandle)); | ||
[super dealloc]; | ||
} | ||
|
||
- (void)publishServiceProtocolPort:(std::string)uri { | ||
if (uri.empty()) { | ||
[_netService.get() stop]; | ||
return; | ||
} | ||
// 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]; | ||
|
||
// DNS name has to be a max of 63 bytes. Prefer to cut off the app name rather than | ||
// the device hostName. e.g. 'io.flutter.example@someones-iphone', or | ||
// 'ongAppNameBecauseThisCouldHappenAtSomePoint@somelongname-iphone' | ||
NSString* serviceName = [NSString | ||
stringWithFormat:@"%@@%@", | ||
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"], | ||
[NSProcessInfo processInfo].hostName]; | ||
if ([serviceName length] > 63) { | ||
serviceName = [serviceName substringFromIndex:[serviceName length] - 63]; | ||
} | ||
|
||
_netService.reset([[NSNetService alloc] initWithDomain:@"local." | ||
type:@"_dartobservatory._tcp." | ||
name:serviceName | ||
port:[[url port] intValue]]); | ||
|
||
[_netService.get() setDelegate:self]; | ||
[_netService.get() publish]; | ||
} | ||
|
||
- (void)netServiceDidPublish:(NSNetService*)sender { | ||
FML_DLOG(INFO) << "FlutterObservatoryPublisher is ready!"; | ||
} | ||
|
||
- (void)netService:(NSNetService*)sender didNotPublish:(NSDictionary*)errorDict { | ||
FML_LOG(ERROR) << "Could not register as server for FlutterObservatoryPublisher. Check your " | ||
"network settings and relaunch the application."; | ||
} | ||
|
||
#endif // FLUTTER_RUNTIME_MODE != FLUTTER_RUNTIME_MODE_RELEASE && FLUTTER_RUNTIME_MODE != | ||
// FLUTTER_RUNTIME_MODE_DYNAMIC_RELEASE | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters