Flutter iOS Embedder
FlutterDartVMServicePublisher.mm
Go to the documentation of this file.
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 #define FML_USED_ON_EMBEDDER
6 
8 
9 #if FLUTTER_RELEASE
10 
11 @implementation FlutterDartVMServicePublisher
12 - (instancetype)initWithEnableVMServicePublication:(BOOL)enableVMServicePublication {
13  return [super init];
14 }
15 @end
16 
17 #else // FLUTTER_RELEASE
18 
19 #import <TargetConditionals.h>
20 // NSNetService works fine on physical devices before iOS 13.2.
21 // However, it doesn't expose the services to regular mDNS
22 // queries on the Simulator or on iOS 13.2+ devices.
23 //
24 // When debugging issues with this implementation, the following is helpful:
25 //
26 // 1) Running `dns-sd -Z _dartVmService`. This is a built-in macOS tool that
27 // can find advertized observatories using this method. If dns-sd can't find
28 // it, then the VM service is not getting advertised over any network
29 // interface that the host machine has access to.
30 // 2) The Python zeroconf package. The dns-sd tool can sometimes see things
31 // that aren't advertizing over a network interface - for example, simulators
32 // using NSNetService has been observed using dns-sd, but doesn't show up in
33 // the Python package (which is a high quality socket based implementation).
34 // If that happens, this code should be tweaked such that it shows up in both
35 // dns-sd's output and Python zeroconf's detection.
36 // 3) The Dart multicast_dns package, which is what Flutter uses to find the
37 // port and auth code. If the advertizement shows up in dns-sd and Python
38 // zeroconf but not multicast_dns, then it is a bug in multicast_dns.
39 #include <dns_sd.h>
40 #include <net/if.h>
41 
42 #include "flutter/fml/logging.h"
43 #include "flutter/fml/memory/weak_ptr.h"
44 #include "flutter/fml/message_loop.h"
45 #include "flutter/fml/platform/darwin/scoped_nsobject.h"
46 #include "flutter/runtime/dart_service_isolate.h"
47 
49 - (void)publishServiceProtocolPort:(NSURL*)uri;
50 - (void)stopService;
51 @end
52 
54 + (NSData*)createTxtData:(NSURL*)url;
55 
56 @property(readonly, class) NSString* serviceName;
57 @property(readonly) fml::scoped_nsobject<NSObject<FlutterDartVMServicePublisherDelegate>> delegate;
58 @property(nonatomic, readwrite) NSURL* url;
59 @property(readonly) BOOL enableVMServicePublication;
60 
61 @end
62 
64 @end
65 
67  DNSServiceRef _dnsServiceRef;
68  DNSServiceRef _legacyDnsServiceRef;
69 }
70 
71 - (void)stopService {
72  if (_dnsServiceRef) {
73  DNSServiceRefDeallocate(_dnsServiceRef);
74  _dnsServiceRef = NULL;
75  }
77  DNSServiceRefDeallocate(_legacyDnsServiceRef);
78  _legacyDnsServiceRef = NULL;
79  }
80 }
81 
82 - (void)publishServiceProtocolPort:(NSURL*)url {
83  // TODO(vashworth): Remove once done debugging https://github.com/flutter/flutter/issues/129836
84  FML_LOG(INFO) << "Publish Service Protocol Port";
85  DNSServiceFlags flags = kDNSServiceFlagsDefault;
86 #if TARGET_IPHONE_SIMULATOR
87  // Simulator needs to use local loopback explicitly to work.
88  uint32_t interfaceIndex = if_nametoindex("lo0");
89 #else // TARGET_IPHONE_SIMULATOR
90  // Physical devices need to request all interfaces.
91  uint32_t interfaceIndex = 0;
92 #endif // TARGET_IPHONE_SIMULATOR
93  const char* registrationType = "_dartVmService._tcp";
94  const char* legacyRegistrationType = "_dartobservatory._tcp";
95 
96  const char* domain = "local."; // default domain
97  uint16_t port = [[url port] unsignedShortValue];
98 
99  NSData* txtData = [FlutterDartVMServicePublisher createTxtData:url];
100  int err = DNSServiceRegister(&_dnsServiceRef, flags, interfaceIndex,
101  FlutterDartVMServicePublisher.serviceName.UTF8String,
102  registrationType, domain, NULL, htons(port), txtData.length,
103  txtData.bytes, RegistrationCallback, NULL);
104 
105  if (err == 0) {
106  DNSServiceSetDispatchQueue(_dnsServiceRef, dispatch_get_main_queue());
107  return;
108  }
109 
110  // TODO(bkonyi): remove once flutter_tools no longer looks for the legacy registration type.
111  // See https://github.com/dart-lang/sdk/issues/50233
112  //
113  // Try to fallback on the legacy registration type.
114  err = DNSServiceRegister(&_legacyDnsServiceRef, flags, interfaceIndex,
115  FlutterDartVMServicePublisher.serviceName.UTF8String,
116  legacyRegistrationType, domain, NULL, htons(port), txtData.length,
117  txtData.bytes, RegistrationCallback, NULL);
118 
119  if (err == 0) {
120  DNSServiceSetDispatchQueue(_legacyDnsServiceRef, dispatch_get_main_queue());
121  return;
122  }
123 
124  FML_LOG(ERROR) << "Failed to register Dart VM Service port with mDNS with error " << err << ".";
125  if (@available(iOS 14.0, *)) {
126  FML_LOG(ERROR) << "On iOS 14+, local network broadcast in apps need to be declared in "
127  << "the app's Info.plist. Debug and profile Flutter apps and modules host "
128  << "VM services on the local network to support debugging features such "
129  << "as hot reload and DevTools. To make your Flutter app or module "
130  << "attachable and debuggable, add a '" << registrationType << "' value "
131  << "to the 'NSBonjourServices' key in your Info.plist for the Debug/"
132  << "Profile configurations. " << "For more information, see "
133  << "https://flutter.dev/docs/development/add-to-app/ios/"
134  "project-setup#local-network-privacy-permissions";
135  }
136 }
137 
138 static void DNSSD_API RegistrationCallback(DNSServiceRef sdRef,
139  DNSServiceFlags flags,
140  DNSServiceErrorType errorCode,
141  const char* name,
142  const char* regType,
143  const char* domain,
144  void* context) {
145  if (errorCode == kDNSServiceErr_NoError) {
146  FML_DLOG(INFO) << "FlutterDartVMServicePublisher is ready!";
147  } else if (errorCode == kDNSServiceErr_PolicyDenied) {
148  FML_LOG(ERROR)
149  << "Could not register as server for FlutterDartVMServicePublisher, permission "
150  << "denied. Check your 'Local Network' permissions for this app in the Privacy section of "
151  << "the system Settings.";
152  } else {
153  FML_LOG(ERROR) << "Could not register as server for FlutterDartVMServicePublisher. Check your "
154  "network settings and relaunch the application.";
155  }
156 }
157 
158 @end
159 
161  flutter::DartServiceIsolate::CallbackHandle _callbackHandle;
162  std::unique_ptr<fml::WeakPtrFactory<FlutterDartVMServicePublisher>> _weakFactory;
163 }
164 
165 - (instancetype)initWithEnableVMServicePublication:(BOOL)enableVMServicePublication {
166  self = [super init];
167  NSAssert(self, @"Super must not return null on init.");
168 
169  _delegate.reset([[DartVMServiceDNSServiceDelegate alloc] init]);
170  _enableVMServicePublication = enableVMServicePublication;
171  _weakFactory = std::make_unique<fml::WeakPtrFactory<FlutterDartVMServicePublisher>>(self);
172 
173  fml::MessageLoop::EnsureInitializedForCurrentThread();
174 
175  _callbackHandle = flutter::DartServiceIsolate::AddServerStatusCallback(
176  [weak = _weakFactory->GetWeakPtr(),
177  runner = fml::MessageLoop::GetCurrent().GetTaskRunner()](const std::string& uri) {
178  if (!uri.empty()) {
179  runner->PostTask([weak, uri]() {
180  // uri comes in as something like 'http://127.0.0.1:XXXXX/' where XXXXX is the port
181  // number.
182  if (weak) {
183  NSURL* url = [[[NSURL alloc]
184  initWithString:[NSString stringWithUTF8String:uri.c_str()]] autorelease];
185  weak.get().url = url;
186  if (weak.get().enableVMServicePublication) {
187  [[weak.get() delegate] publishServiceProtocolPort:url];
188  }
189  }
190  });
191  }
192  });
193 
194  return self;
195 }
196 
197 + (NSString*)serviceName {
198  return NSBundle.mainBundle.bundleIdentifier;
199 }
200 
201 + (NSData*)createTxtData:(NSURL*)url {
202  // Check to see if there's an authentication code. If there is, we'll provide
203  // it as a txt record so flutter tools can establish a connection.
204  NSString* path = [[url path] substringFromIndex:MIN(1, [[url path] length])];
205  NSData* pathData = [path dataUsingEncoding:NSUTF8StringEncoding];
206  NSDictionary<NSString*, NSData*>* txtDict = @{
207  @"authCode" : pathData,
208  };
209  return [NSNetService dataFromTXTRecordDictionary:txtDict];
210 }
211 
212 - (void)dealloc {
213  // It will be destroyed and invalidate its weak pointers
214  // before any other members are destroyed.
215  _weakFactory.reset();
216 
217  [_delegate stopService];
218  [_url release];
219 
220  flutter::DartServiceIsolate::RemoveServerStatusCallback(_callbackHandle);
221  [super dealloc];
222 }
223 @end
224 
225 #endif // FLUTTER_RELEASE
FlutterDartVMServicePublisher::url
NSURL * url
Definition: FlutterDartVMServicePublisher.h:17
FlutterDartVMServicePublisher.h
FlutterDartVMServicePublisherDelegate-p
Definition: FlutterDartVMServicePublisher.mm:48
-[FlutterDartVMServicePublisherDelegate-p stopService]
void stopService()
_weakFactory
std::unique_ptr< fml::WeakPtrFactory< FlutterDartVMServicePublisher > > _weakFactory
Definition: FlutterDartVMServicePublisher.mm:160
_legacyDnsServiceRef
DNSServiceRef _legacyDnsServiceRef
Definition: FlutterDartVMServicePublisher.mm:66
DartVMServiceDNSServiceDelegate
Definition: FlutterDartVMServicePublisher.mm:63
FlutterDartVMServicePublisher
Definition: FlutterDartVMServicePublisher.h:10