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"
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"
23 class RunnerThread
: public base::SimpleThread
{
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
),
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_
);
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
);
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() {
71 void StaticApplicationLoader::Load(const GURL
& url
,
72 InterfaceRequest
<Application
> request
) {
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());
82 new RunnerThread(url
, request
.Pass(), base::ThreadTaskRunnerHandle::Get(),
83 exit_callback
, factory_
));
87 void StaticApplicationLoader::StopAppThread() {
90 if (!quit_callback_
.is_null())