Allow Flutter apps on Fuchsia to shut down cleanly (flutter/engine#4366)

The UIDartState is now always owned by the isolate and always freed in
the isolate cleanup callback.

In the isolate shutdown callback, if the isolate being shut down is the
main isolate, the RuntimeController is informed which in turn notifies
the RuntimeHolder and thus the ApplicationControllerImpl. The
ApplicationControllerImpl tears down the whole Flutter application.

This fixes Fuchsia bug: MI4-328
This commit is contained in:
Ian McKellar
2017-11-15 13:28:21 -08:00
committed by GitHub
parent cb46299d06
commit ce4849d743
8 changed files with 36 additions and 13 deletions

View File

@@ -20,6 +20,7 @@ class Window;
class IsolateClient {
public:
virtual void DidCreateSecondaryIsolate(Dart_Isolate isolate) = 0;
virtual void DidShutdownMainIsolate() = 0;
protected:
virtual ~IsolateClient();
@@ -47,6 +48,8 @@ class UIDartState : public tonic::DartState {
PassRefPtr<FontSelector> font_selector();
bool is_controller_state() const { return is_controller_state_; }
void set_is_controller_state(bool value) { is_controller_state_ = value; }
bool shutting_down() const { return shutting_down_; }
void set_shutting_down(bool value) { shutting_down_ = value; }
private:
void DidSetIsolate() override;
@@ -58,6 +61,7 @@ class UIDartState : public tonic::DartState {
std::unique_ptr<Window> window_;
RefPtr<FontSelector> font_selector_;
bool is_controller_state_;
bool shutting_down_ = false;
};
} // namespace blink

View File

@@ -49,12 +49,14 @@ DartController::~DartController() {
if (ui_dart_state_) {
ui_dart_state_->set_isolate_client(nullptr);
// Don't use a tonic::DartIsolateScope here since we never exit the isolate.
Dart_EnterIsolate(ui_dart_state_->isolate());
// Clear the message notify callback.
Dart_SetMessageNotifyCallback(nullptr);
Dart_ShutdownIsolate();
delete ui_dart_state_;
if (!ui_dart_state_->shutting_down()) {
// Don't use a tonic::DartIsolateScope here since we never exit the
// isolate.
Dart_EnterIsolate(ui_dart_state_->isolate());
// Clear the message notify callback.
Dart_SetMessageNotifyCallback(nullptr);
Dart_ShutdownIsolate();
}
}
}

View File

@@ -39,17 +39,13 @@ class DartController {
UIDartState* dart_state() const { return ui_dart_state_; }
void IsolateShuttingDown();
private:
bool SendStartMessage(Dart_Handle root_library,
const std::string& entrypoint = main_entrypoint_);
static const std::string main_entrypoint_;
// The DartState associated with the main isolate. This is not deleted
// during isolate shutdown, instead it is deleted when the controller
// object is deleted.
// The DartState associated with the main isolate.
UIDartState* ui_dart_state_;
FXL_DISALLOW_COPY_AND_ASSIGN(DartController);

View File

@@ -140,12 +140,24 @@ void IsolateShutdownCallback(void* callback_data) {
Dart_Handle sticky_error = Dart_GetStickyError();
FXL_CHECK(LogIfError(sticky_error));
}
UIDartState* dart_state = static_cast<UIDartState*>(callback_data);
if ((dart_state != NULL) && !dart_state->is_controller_state()) {
delete dart_state;
// If the isolate that's shutting down is the main one, tell the higher layers
// of the stack.
if ((dart_state != NULL) && dart_state->is_controller_state()) {
dart_state->set_shutting_down(true);
if (dart_state->isolate_client()) {
dart_state->isolate_client()->DidShutdownMainIsolate();
}
}
}
// The cleanup callback frees the DartState object.
void IsolateCleanupCallback(void* callback_data) {
UIDartState* dart_state = static_cast<UIDartState*>(callback_data);
delete dart_state;
}
bool DartFileModifiedCallback(const char* source_url, int64_t since_ms) {
if (strncmp(source_url, kFileUriPrefix, kFileUriPrefixLength) != 0u) {
// Assume modified.
@@ -581,6 +593,7 @@ void InitDartVM(const uint8_t* vm_snapshot_data,
params.vm_snapshot_instructions = vm_snapshot_instructions;
params.create = IsolateCreateCallback;
params.shutdown = IsolateShutdownCallback;
params.cleanup = IsolateCleanupCallback;
params.thread_exit = ThreadExitCallback;
params.get_service_assets = GetVMServiceAssetsArchiveCallback;
params.entropy_source = DartIO::EntropySource;

View File

@@ -142,6 +142,10 @@ void RuntimeController::DidCreateSecondaryIsolate(Dart_Isolate isolate) {
client_->DidCreateSecondaryIsolate(isolate);
}
void RuntimeController::DidShutdownMainIsolate() {
client_->DidShutdownMainIsolate();
}
Dart_Port RuntimeController::GetMainPort() {
if (!dart_controller_) {
return ILLEGAL_PORT;

View File

@@ -61,6 +61,7 @@ class RuntimeController : public WindowClient, public IsolateClient {
void HandlePlatformMessage(fxl::RefPtr<PlatformMessage> message) override;
void DidCreateSecondaryIsolate(Dart_Isolate isolate) override;
void DidShutdownMainIsolate() override;
RuntimeDelegate* client_;
std::string language_code_;

View File

@@ -12,4 +12,6 @@ void RuntimeDelegate::DidCreateMainIsolate(Dart_Isolate isolate) {}
void RuntimeDelegate::DidCreateSecondaryIsolate(Dart_Isolate isolate) {}
void RuntimeDelegate::DidShutdownMainIsolate() {}
} // namespace blink

View File

@@ -25,6 +25,7 @@ class RuntimeDelegate {
virtual void DidCreateMainIsolate(Dart_Isolate isolate);
virtual void DidCreateSecondaryIsolate(Dart_Isolate isolate);
virtual void DidShutdownMainIsolate();
protected:
virtual ~RuntimeDelegate();