Skip to content

Commit

Permalink
Ensure that application termination callbacks are serviced on the run…
Browse files Browse the repository at this point in the history
…ner thread. (flutter#5247)

This removes the Application::Delegate interface and instead uses a closure that captures the task runner. The interface was expected to be more complicated than it turned out to be.
  • Loading branch information
chinmaygarde authored May 12, 2018
1 parent b14a33c commit 06afdfe
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
10 changes: 5 additions & 5 deletions content_handler/application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace flutter {

std::pair<std::unique_ptr<fsl::Thread>, std::unique_ptr<Application>>
Application::Create(
Application::Delegate& delegate,
TerminationCallback termination_callback,
component::ApplicationPackage package,
component::ApplicationStartupInfo startup_info,
fidl::InterfaceRequest<component::ApplicationController> controller) {
Expand All @@ -29,7 +29,7 @@ Application::Create(

fxl::AutoResetWaitableEvent latch;
thread->TaskRunner()->PostTask([&]() mutable {
application.reset(new Application(delegate, //
application.reset(new Application(termination_callback, //
std::move(package), //
std::move(startup_info), //
std::move(controller) //
Expand All @@ -51,12 +51,12 @@ static std::string DebugLabelForURL(const std::string& url) {
}

Application::Application(
Application::Delegate& delegate,
TerminationCallback termination_callback,
component::ApplicationPackage,
component::ApplicationStartupInfo startup_info,
fidl::InterfaceRequest<component::ApplicationController>
application_controller_request)
: delegate_(delegate),
: termination_callback_(termination_callback),
debug_label_(DebugLabelForURL(startup_info.launch_info.url)),
application_controller_(this) {
application_controller_.set_error_handler([this]() { Kill(); });
Expand Down Expand Up @@ -234,7 +234,7 @@ void Application::Kill() {
}
wait_callbacks_.clear();

delegate_.OnApplicationTerminate(this);
termination_callback_(this);
// WARNING: Don't do anything past this point as this instance may have been
// collected.
}
Expand Down
11 changes: 4 additions & 7 deletions content_handler/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,13 @@ class Application final : public Engine::Delegate,
public component::ApplicationController,
public views_v1::ViewProvider {
public:
class Delegate {
public:
virtual void OnApplicationTerminate(const Application* application) = 0;
};
using TerminationCallback = std::function<void(const Application*)>;

// Creates a dedicated thread to run the application and constructions the
// application on it. The application can be accessed only on this thread.
// This is a synchronous operation.
static std::pair<std::unique_ptr<fsl::Thread>, std::unique_ptr<Application>>
Create(Application::Delegate& delegate,
Create(TerminationCallback termination_callback,
component::ApplicationPackage package,
component::ApplicationStartupInfo startup_info,
fidl::InterfaceRequest<component::ApplicationController> controller);
Expand All @@ -53,7 +50,7 @@ class Application final : public Engine::Delegate,

private:
blink::Settings settings_;
Delegate& delegate_;
TerminationCallback termination_callback_;
const std::string debug_label_;
UniqueFDIONS fdio_ns_ = UniqueFDIONSCreate();
fxl::UniqueFD application_directory_;
Expand All @@ -69,7 +66,7 @@ class Application final : public Engine::Delegate,
std::pair<bool, uint32_t> last_return_code_;

Application(
Application::Delegate& delegate,
TerminationCallback termination_callback,
component::ApplicationPackage package,
component::ApplicationStartupInfo startup_info,
fidl::InterfaceRequest<component::ApplicationController> controller);
Expand Down
18 changes: 17 additions & 1 deletion content_handler/application_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,24 @@ void ApplicationRunner::StartApplication(
component::ApplicationPackage package,
component::ApplicationStartupInfo startup_info,
fidl::InterfaceRequest<component::ApplicationController> controller) {
// Notes on application termination: Application typically terminate on the
// thread on which they were created. This usually means the thread was
// specifically created to host the application. But we want to ensure that
// access to the active applications collection is made on the same thread. So
// we capture the runner in the termination callback. There is no risk of
// there being multiple application runner instance in the process at the same
// time. So it is safe to use the raw pointer.
Application::TerminationCallback termination_callback =
[task_runner = fsl::MessageLoop::GetCurrent()->task_runner(), //
application_runner = this //
](const Application* application) {
task_runner->PostTask([application_runner, application]() {
application_runner->OnApplicationTerminate(application);
});
};

auto thread_application_pair =
Application::Create(*this, // delegate
Application::Create(termination_callback, // termination callback
std::move(package), // application pacakge
std::move(startup_info), // startup info
std::move(controller) // controller request
Expand Down
6 changes: 2 additions & 4 deletions content_handler/application_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ namespace flutter {

// Publishes the |component::ApplicationRunner| service and runs applications on
// their own threads.
class ApplicationRunner final : public Application::Delegate,
public component::ApplicationRunner {
class ApplicationRunner final : public component::ApplicationRunner {
public:
ApplicationRunner();

Expand Down Expand Up @@ -64,8 +63,7 @@ class ApplicationRunner final : public Application::Delegate,

void UnregisterApplication(const Application* application);

// |Application::Delegate|
void OnApplicationTerminate(const Application* application) override;
void OnApplicationTerminate(const Application* application);

void SetupICU();

Expand Down

0 comments on commit 06afdfe

Please sign in to comment.