1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/shell/static_application_loader.h"
#include <utility>
#include "base/bind.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/simple_thread.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "mojo/shell/public/cpp/application_runner.h"
#include "mojo/shell/public/cpp/shell_client.h"
#include "mojo/shell/public/interfaces/application.mojom.h"
namespace mojo {
namespace shell {
namespace {
class RunnerThread : public base::SimpleThread {
public:
RunnerThread(const GURL& url,
InterfaceRequest<mojom::Application> request,
scoped_refptr<base::TaskRunner> exit_task_runner,
const base::Closure& exit_callback,
const StaticApplicationLoader::ApplicationFactory& factory)
: base::SimpleThread("Mojo Application: " + url.spec()),
request_(std::move(request)),
exit_task_runner_(exit_task_runner),
exit_callback_(exit_callback),
factory_(factory) {}
void Run() override {
scoped_ptr<ApplicationRunner> runner(
new ApplicationRunner(factory_.Run().release()));
runner->Run(request_.PassMessagePipe().release().value(),
false /* init_base */);
exit_task_runner_->PostTask(FROM_HERE, exit_callback_);
}
private:
InterfaceRequest<mojom::Application> request_;
scoped_refptr<base::TaskRunner> exit_task_runner_;
base::Closure exit_callback_;
StaticApplicationLoader::ApplicationFactory factory_;
DISALLOW_COPY_AND_ASSIGN(RunnerThread);
};
} // namespace
StaticApplicationLoader::StaticApplicationLoader(
const ApplicationFactory& factory)
: StaticApplicationLoader(factory, base::Closure()) {
}
StaticApplicationLoader::StaticApplicationLoader(
const ApplicationFactory& factory,
const base::Closure& quit_callback)
: factory_(factory), quit_callback_(quit_callback), weak_factory_(this) {
}
StaticApplicationLoader::~StaticApplicationLoader() {
if (thread_)
StopAppThread();
}
void StaticApplicationLoader::Load(
const GURL& url,
InterfaceRequest<mojom::Application> request) {
if (thread_)
return;
// If the application's thread quits on its own before this loader dies, we
// reset the Thread object, allowing future Load requests to be fulfilled
// with a new app instance.
auto exit_callback = base::Bind(&StaticApplicationLoader::StopAppThread,
weak_factory_.GetWeakPtr());
thread_.reset(new RunnerThread(url, std::move(request),
base::ThreadTaskRunnerHandle::Get(),
exit_callback, factory_));
thread_->Start();
}
void StaticApplicationLoader::StopAppThread() {
thread_->Join();
thread_.reset();
if (!quit_callback_.is_null())
quit_callback_.Run();
}
} // namespace shell
} // namespace mojo
|