Flutter Engine
flutter::DartServiceIsolate Class Reference

Utility methods for interacting with the DartVM managed service isolate present in debug and profile runtime modes. More...

#include <dart_service_isolate.h>

Public Types

using CallbackHandle = ptrdiff_t
 
using ObservatoryServerStateCallback = std::function< void(const std::string &observatory_uri)>
 

Static Public Member Functions

static bool Startup (std::string server_ip, intptr_t server_port, Dart_LibraryTagHandler embedder_tag_handler, bool disable_origin_check, bool disable_service_auth_codes, bool enable_service_port_fallback, char **error)
 Start the service isolate. This call may only be made in the Dart VM initiated isolate creation callback. It is only valid to make this call when the VM explicitly requests the creation of the service isolate. The VM does this by specifying the script URI to be DART_VM_SERVICE_ISOLATE_NAME. The isolate to be designated as the service isolate must already be created (but not running) when this call is made. More...
 
static CallbackHandle AddServerStatusCallback (const ObservatoryServerStateCallback &callback)
 Add a callback that will get invoked when the observatory starts up. If the observatory has already started before this call is made, the callback is invoked immediately. More...
 
static bool RemoveServerStatusCallback (CallbackHandle handle)
 Removed a callback previously registered via AddServiceStatusCallback. More...
 

Detailed Description

Utility methods for interacting with the DartVM managed service isolate present in debug and profile runtime modes.

Definition at line 22 of file dart_service_isolate.h.

Member Typedef Documentation

◆ CallbackHandle

The handle used to refer to callbacks registered with the service isolate.

Definition at line 27 of file dart_service_isolate.h.

◆ ObservatoryServerStateCallback

using flutter::DartServiceIsolate::ObservatoryServerStateCallback = std::function<void(const std::string& observatory_uri)>

A callback made by the Dart VM when the observatory is ready. The argument indicates the observatory URI.

Definition at line 34 of file dart_service_isolate.h.

Member Function Documentation

◆ AddServerStatusCallback()

DartServiceIsolate::CallbackHandle flutter::DartServiceIsolate::AddServerStatusCallback ( const ObservatoryServerStateCallback callback)
static

Add a callback that will get invoked when the observatory starts up. If the observatory has already started before this call is made, the callback is invoked immediately.

This method is thread safe.

Parameters
[in]callbackThe callback with information about the observatory.
Returns
A handle for the callback that can be used later in RemoveServerStatusCallback.

Definition at line 84 of file dart_service_isolate.cc.

Referenced by flutter::FlutterMain::GetSettings(), FlutterObservatoryPublisher::initWithEnableObservatoryPublication:, and flutter::TEST().

85  {
86  if (!callback) {
87  return 0;
88  }
89 
90  auto callback_pointer =
91  std::make_unique<DartServiceIsolate::ObservatoryServerStateCallback>(
92  callback);
93 
94  auto handle = reinterpret_cast<CallbackHandle>(callback_pointer.get());
95 
96  {
97  std::scoped_lock lock(callbacks_mutex_);
98  callbacks_.insert(std::move(callback_pointer));
99  }
100 
101  if (!g_observatory_uri.empty()) {
102  callback(g_observatory_uri);
103  }
104 
105  return handle;
106 }

◆ RemoveServerStatusCallback()

bool flutter::DartServiceIsolate::RemoveServerStatusCallback ( CallbackHandle  handle)
static

Removed a callback previously registered via AddServiceStatusCallback.

This method is thread safe.

Parameters
[in]handleThe handle
Returns
If the callback was unregistered. This may fail if there was no such callback with that handle.

Definition at line 108 of file dart_service_isolate.cc.

Referenced by FlutterObservatoryPublisher::initWithEnableObservatoryPublication:, and flutter::TEST().

109  {
110  std::scoped_lock lock(callbacks_mutex_);
111  auto found = std::find_if(
112  callbacks_.begin(), callbacks_.end(),
113  [callback_handle](const auto& item) {
114  return reinterpret_cast<CallbackHandle>(item.get()) == callback_handle;
115  });
116 
117  if (found == callbacks_.end()) {
118  return false;
119  }
120 
121  callbacks_.erase(found);
122  return true;
123 }

◆ Startup()

bool flutter::DartServiceIsolate::Startup ( std::string  server_ip,
intptr_t  server_port,
Dart_LibraryTagHandler  embedder_tag_handler,
bool  disable_origin_check,
bool  disable_service_auth_codes,
bool  enable_service_port_fallback,
char **  error 
)
static

Start the service isolate. This call may only be made in the Dart VM initiated isolate creation callback. It is only valid to make this call when the VM explicitly requests the creation of the service isolate. The VM does this by specifying the script URI to be DART_VM_SERVICE_ISOLATE_NAME. The isolate to be designated as the service isolate must already be created (but not running) when this call is made.

Parameters
[in]server_ipThe service protocol IP address.
[in]server_portThe service protocol port.
[in]embedder_tag_handlerThe library tag handler.
[in]disable_origin_checkIf websocket origin checks must be enabled.
[in]disable_service_auth_codesIf service auth codes must be enabled.
[in]enable_service_port_fallbackIf fallback to port 0 must be enabled when the bind fails.
errorThe error when this method returns false. This string must be freed by the caller using free.
Returns
If the startup was successful. Refer to the error for details on failure.

Definition at line 129 of file dart_service_isolate.cc.

References FML_CHECK, and SHUTDOWN_ON_ERROR.

Referenced by flutter::DartIsolate::Shutdown().

135  {
136  Dart_Isolate isolate = Dart_CurrentIsolate();
137  FML_CHECK(isolate);
138 
139  // Remember the embedder's library tag handler.
140  g_embedder_tag_handler = embedder_tag_handler;
141  FML_CHECK(g_embedder_tag_handler);
142 
143  // Setup native entries.
144  if (!g_natives) {
145  g_natives = new tonic::DartLibraryNatives();
146  g_natives->Register({
147  {"VMServiceIO_NotifyServerState", NotifyServerState, 1, true},
148  {"VMServiceIO_Shutdown", Shutdown, 0, true},
149  });
150  }
151 
152  Dart_Handle uri = Dart_NewStringFromCString("dart:vmservice_io");
153  Dart_Handle library = Dart_LookupLibrary(uri);
154  SHUTDOWN_ON_ERROR(library);
155  Dart_Handle result = Dart_SetRootLibrary(library);
156  SHUTDOWN_ON_ERROR(result);
157  result = Dart_SetNativeResolver(library, GetNativeFunction, GetSymbol);
158  SHUTDOWN_ON_ERROR(result);
159 
160  // Make runnable.
161  Dart_ExitScope();
162  Dart_ExitIsolate();
163  *error = Dart_IsolateMakeRunnable(isolate);
164  if (*error) {
165  Dart_EnterIsolate(isolate);
166  Dart_ShutdownIsolate();
167  return false;
168  }
169  Dart_EnterIsolate(isolate);
170  Dart_EnterScope();
171 
172  library = Dart_RootLibrary();
173  SHUTDOWN_ON_ERROR(library);
174 
175  // Set the HTTP server's ip.
176  result = Dart_SetField(library, Dart_NewStringFromCString("_ip"),
177  Dart_NewStringFromCString(server_ip.c_str()));
178  SHUTDOWN_ON_ERROR(result);
179  // If we have a port specified, start the server immediately.
180  bool auto_start = server_port >= 0;
181  if (server_port < 0) {
182  // Adjust server_port to port 0 which will result in the first available
183  // port when the HTTP server is started.
184  server_port = 0;
185  }
186  // Set the HTTP's servers port.
187  result = Dart_SetField(library, Dart_NewStringFromCString("_port"),
188  Dart_NewInteger(server_port));
189  SHUTDOWN_ON_ERROR(result);
190  result = Dart_SetField(library, Dart_NewStringFromCString("_autoStart"),
191  Dart_NewBoolean(auto_start));
192  SHUTDOWN_ON_ERROR(result);
193  result =
194  Dart_SetField(library, Dart_NewStringFromCString("_originCheckDisabled"),
195  Dart_NewBoolean(disable_origin_check));
196  SHUTDOWN_ON_ERROR(result);
197  result =
198  Dart_SetField(library, Dart_NewStringFromCString("_authCodesDisabled"),
199  Dart_NewBoolean(disable_service_auth_codes));
200  SHUTDOWN_ON_ERROR(result);
201  result = Dart_SetField(
202  library, Dart_NewStringFromCString("_enableServicePortFallback"),
203  Dart_NewBoolean(enable_service_port_fallback));
204  SHUTDOWN_ON_ERROR(result);
205  return true;
206 }
FlMethodResponse GError ** error
#define SHUTDOWN_ON_ERROR(handle)
#define FML_CHECK(condition)
Definition: logging.h:68

The documentation for this class was generated from the following files: