From 4c92ea1506be4884134434ae142e97bb6d273bc6 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Fri, 20 Jan 2017 15:18:13 -0800 Subject: [PATCH] Add flags for disabling the diagnostic server or setting its port (flutter/engine#3352) Fixes https://github.com/flutter/flutter/issues/7557 --- engine/src/flutter/common/settings.h | 2 + .../common/diagnostic/diagnostic_server.cc | 5 ++- .../common/diagnostic/diagnostic_server.dart | 4 +- .../common/diagnostic/diagnostic_server.h | 2 +- engine/src/flutter/shell/common/shell.cc | 37 ++++++++++++++----- engine/src/flutter/shell/common/switches.h | 7 ++++ 6 files changed, 43 insertions(+), 14 deletions(-) diff --git a/engine/src/flutter/common/settings.h b/engine/src/flutter/common/settings.h index 48b9ed7003..f8bc41750a 100644 --- a/engine/src/flutter/common/settings.h +++ b/engine/src/flutter/common/settings.h @@ -17,6 +17,8 @@ struct Settings { // Port on target will be auto selected by the OS. A message will be printed // on the target with the port after it has been selected. uint32_t observatory_port = 0; + bool enable_diagnostic = false; + uint32_t diagnostic_port = 0; bool start_paused = false; bool trace_startup = false; bool endless_trace_buffer = false; diff --git a/engine/src/flutter/shell/common/diagnostic/diagnostic_server.cc b/engine/src/flutter/shell/common/diagnostic/diagnostic_server.cc index 2fc0c3cfc4..a7a3f02224 100644 --- a/engine/src/flutter/shell/common/diagnostic/diagnostic_server.cc +++ b/engine/src/flutter/shell/common/diagnostic/diagnostic_server.cc @@ -31,6 +31,7 @@ using tonic::DartLibraryNatives; using blink::EmbedderResources; using tonic::DartInvokeField; using tonic::LogIfError; +using tonic::ToDart; namespace { @@ -60,7 +61,7 @@ void SendNull(Dart_Port port_id) { DART_NATIVE_CALLBACK_STATIC(DiagnosticServer, HandleSkiaPictureRequest); -void DiagnosticServer::Start() { +void DiagnosticServer::Start(uint32_t port) { if (!g_natives) { g_natives = new DartLibraryNatives(); g_natives->Register({ @@ -91,7 +92,7 @@ void DiagnosticServer::Start() { FTL_CHECK(!LogIfError(Dart_FinalizeLoading(false))); - DartInvokeField(Dart_RootLibrary(), "diagnosticServerStart", {}); + DartInvokeField(Dart_RootLibrary(), "diagnosticServerStart", {ToDart(port)}); } void DiagnosticServer::HandleSkiaPictureRequest(Dart_Handle send_port) { diff --git a/engine/src/flutter/shell/common/diagnostic/diagnostic_server.dart b/engine/src/flutter/shell/common/diagnostic/diagnostic_server.dart index cc7aeaec72..6779713f7f 100644 --- a/engine/src/flutter/shell/common/diagnostic/diagnostic_server.dart +++ b/engine/src/flutter/shell/common/diagnostic/diagnostic_server.dart @@ -11,8 +11,8 @@ import 'dart:typed_data'; void handleSkiaPictureRequest(SendPort sendPort) native 'DiagnosticServer_HandleSkiaPictureRequest'; -void diagnosticServerStart() { - HttpServer.bind('127.0.0.1', 0).then((HttpServer server) { +void diagnosticServerStart(int port) { + HttpServer.bind('127.0.0.1', port).then((HttpServer server) { server.listen(dispatchRequest, cancelOnError: true); String ip = server.address.address.toString(); diff --git a/engine/src/flutter/shell/common/diagnostic/diagnostic_server.h b/engine/src/flutter/shell/common/diagnostic/diagnostic_server.h index 00553ca2e3..b5f9e1d00b 100644 --- a/engine/src/flutter/shell/common/diagnostic/diagnostic_server.h +++ b/engine/src/flutter/shell/common/diagnostic/diagnostic_server.h @@ -11,7 +11,7 @@ namespace shell { class DiagnosticServer { public: - static void Start(); + static void Start(uint32_t port); static void HandleSkiaPictureRequest(Dart_Handle send_port); private: diff --git a/engine/src/flutter/shell/common/shell.cc b/engine/src/flutter/shell/common/shell.cc index b60999b955..9c64802c0e 100644 --- a/engine/src/flutter/shell/common/shell.cc +++ b/engine/src/flutter/shell/common/shell.cc @@ -42,6 +42,18 @@ bool IsViewInvalid(const ftl::WeakPtr& platform_view) { return !platform_view; } +template bool GetSwitchValue(const base::CommandLine& command_line, + Switch sw, T* result) { + auto port_string = command_line.GetSwitchValueASCII(FlagForSwitch(sw)); + std::stringstream stream(port_string); + T value = 0; + if (stream >> value) { + *result = value; + return true; + } + return false; +} + class NonDiscardableMemory : public base::DiscardableMemory { public: explicit NonDiscardableMemory(size_t size) : data_(new uint8_t[size]) {} @@ -66,8 +78,8 @@ base::LazyInstance g_discardable; void ServiceIsolateHook(bool running_precompiled) { if (!running_precompiled) { const blink::Settings& settings = blink::Settings::Get(); - if (settings.enable_observatory) - DiagnosticServer::Start(); + if (settings.enable_diagnostic) + DiagnosticServer::Start(settings.diagnostic_port); } } @@ -135,19 +147,26 @@ void Shell::InitStandalone(std::string icu_data_path, // Set Observatory Port if (command_line.HasSwitch(FlagForSwitch(Switch::DeviceObservatoryPort))) { - auto port_string = command_line.GetSwitchValueASCII( - FlagForSwitch(Switch::DeviceObservatoryPort)); - std::stringstream stream(port_string); - uint32_t port = 0; - if (stream >> port) { - settings.observatory_port = port; - } else { + if (!GetSwitchValue(command_line, Switch::DeviceObservatoryPort, + &settings.observatory_port)) { FTL_LOG(INFO) << "Observatory port specified was malformed. Will default to " << settings.observatory_port; } } + settings.enable_diagnostic = + !command_line.HasSwitch(FlagForSwitch(Switch::DisableDiagnostic)); + + if (command_line.HasSwitch(FlagForSwitch(Switch::DeviceDiagnosticPort))) { + if (!GetSwitchValue(command_line, Switch::DeviceDiagnosticPort, + &settings.diagnostic_port)) { + FTL_LOG(INFO) + << "Diagnostic port specified was malformed. Will default to " + << settings.diagnostic_port; + } + } + settings.start_paused = command_line.HasSwitch(FlagForSwitch(Switch::StartPaused)); diff --git a/engine/src/flutter/shell/common/switches.h b/engine/src/flutter/shell/common/switches.h index 33692a5b1a..631b4ed8c0 100644 --- a/engine/src/flutter/shell/common/switches.h +++ b/engine/src/flutter/shell/common/switches.h @@ -43,6 +43,13 @@ DEF_SWITCH(DisableObservatory, "disable-observatory", "Disable the Dart Observatory. The observatory is never available " "in release mode.") +DEF_SWITCH(DeviceDiagnosticPort, + "diagnostic-port", + "A custom diagnostic server port.") +DEF_SWITCH(DisableDiagnostic, + "disable-diagnostic", + "Disable the diagnostic server. The diagnostic server is never " + "available in release mode.") DEF_SWITCH(EnableDartProfiling, "enable-dart-profiling", "Enable Dart profiling. Profiling information can be viewed from "