Adds shutdown when FrameTreeServer goes away
[chromium-blink-merge.git] / mojo / shell / static_application_loader.cc
bloba0f90a87d9020cc184786954bec45a33cc8c9031
1 // Copyright 2015 The Chromium 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.
5 #include "mojo/shell/static_application_loader.h"
7 #include "base/bind.h"
8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/threading/simple_thread.h"
13 #include "mojo/application/public/cpp/application_delegate.h"
14 #include "mojo/application/public/cpp/application_runner.h"
15 #include "mojo/application/public/interfaces/application.mojom.h"
16 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
18 namespace mojo {
19 namespace shell {
21 namespace {
23 class RunnerThread : public base::SimpleThread {
24 public:
25 RunnerThread(const GURL& url,
26 InterfaceRequest<Application> request,
27 scoped_refptr<base::TaskRunner> exit_task_runner,
28 const base::Closure& exit_callback,
29 const StaticApplicationLoader::ApplicationFactory& factory)
30 : base::SimpleThread("Mojo Application: " + url.spec()),
31 request_(request.Pass()),
32 exit_task_runner_(exit_task_runner),
33 exit_callback_(exit_callback),
34 factory_(factory) {}
36 void Run() override {
37 scoped_ptr<ApplicationRunner> runner(
38 new ApplicationRunner(factory_.Run().release()));
39 runner->Run(request_.PassMessagePipe().release().value(),
40 false /* init_base */);
41 exit_task_runner_->PostTask(FROM_HERE, exit_callback_);
44 private:
45 InterfaceRequest<Application> request_;
46 scoped_refptr<base::TaskRunner> exit_task_runner_;
47 base::Closure exit_callback_;
48 StaticApplicationLoader::ApplicationFactory factory_;
50 DISALLOW_COPY_AND_ASSIGN(RunnerThread);
53 } // namespace
55 StaticApplicationLoader::StaticApplicationLoader(
56 const ApplicationFactory& factory)
57 : StaticApplicationLoader(factory, base::Closure()) {
60 StaticApplicationLoader::StaticApplicationLoader(
61 const ApplicationFactory& factory,
62 const base::Closure& quit_callback)
63 : factory_(factory), quit_callback_(quit_callback), weak_factory_(this) {
66 StaticApplicationLoader::~StaticApplicationLoader() {
67 if (thread_)
68 StopAppThread();
71 void StaticApplicationLoader::Load(const GURL& url,
72 InterfaceRequest<Application> request) {
73 if (thread_)
74 return;
76 // If the application's thread quits on its own before this loader dies, we
77 // reset the Thread object, allowing future Load requests to be fulfilled
78 // with a new app instance.
79 auto exit_callback = base::Bind(&StaticApplicationLoader::StopAppThread,
80 weak_factory_.GetWeakPtr());
81 thread_.reset(
82 new RunnerThread(url, request.Pass(), base::ThreadTaskRunnerHandle::Get(),
83 exit_callback, factory_));
84 thread_->Start();
87 void StaticApplicationLoader::StopAppThread() {
88 thread_->Join();
89 thread_.reset();
90 if (!quit_callback_.is_null())
91 quit_callback_.Run();
94 } // namespace shell
95 } // namespace mojo