OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/common/mojo/static_loader.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/macros.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/task_runner.h" | |
13 #include "base/thread_task_runner_handle.h" | |
14 #include "base/threading/simple_thread.h" | |
15 #include "mojo/public/cpp/bindings/interface_request.h" | |
16 #include "services/shell/public/cpp/application_runner.h" | |
17 #include "services/shell/public/cpp/shell_client.h" | |
18 #include "services/shell/public/interfaces/shell_client.mojom.h" | |
19 | |
20 namespace content { | |
21 | |
22 namespace { | |
23 | |
24 class RunnerThread : public base::SimpleThread { | |
25 public: | |
26 RunnerThread(const std::string& name, | |
27 shell::mojom::ShellClientRequest request, | |
28 scoped_refptr<base::TaskRunner> exit_task_runner, | |
29 const base::Closure& exit_callback, | |
30 const StaticLoader::ApplicationFactory& factory) | |
31 : base::SimpleThread("Mojo Application: " + name), | |
32 request_(std::move(request)), | |
33 exit_task_runner_(exit_task_runner), | |
34 exit_callback_(exit_callback), | |
35 factory_(factory) {} | |
36 | |
37 void Run() override { | |
38 std::unique_ptr<shell::ApplicationRunner> runner( | |
39 new shell::ApplicationRunner(factory_.Run().release())); | |
40 runner->Run(request_.PassMessagePipe().release().value(), | |
41 false /* init_base */); | |
42 exit_task_runner_->PostTask(FROM_HERE, exit_callback_); | |
43 } | |
44 | |
45 private: | |
46 shell::mojom::ShellClientRequest request_; | |
47 scoped_refptr<base::TaskRunner> exit_task_runner_; | |
48 base::Closure exit_callback_; | |
49 StaticLoader::ApplicationFactory factory_; | |
50 | |
51 DISALLOW_COPY_AND_ASSIGN(RunnerThread); | |
52 }; | |
53 | |
54 } // namespace | |
55 | |
56 StaticLoader::StaticLoader(const ApplicationFactory& factory) | |
57 : StaticLoader(factory, base::Closure()) { | |
58 } | |
59 | |
60 StaticLoader::StaticLoader(const ApplicationFactory& factory, | |
61 const base::Closure& quit_callback) | |
62 : factory_(factory), quit_callback_(quit_callback), weak_factory_(this) { | |
63 } | |
64 | |
65 StaticLoader::~StaticLoader() { | |
66 if (thread_) | |
67 StopAppThread(); | |
68 } | |
69 | |
70 void StaticLoader::Load(const std::string& name, | |
71 shell::mojom::ShellClientRequest request) { | |
72 if (thread_) | |
73 return; | |
74 | |
75 // If the application's thread quits on its own before this loader dies, we | |
76 // reset the Thread object, allowing future Load requests to be fulfilled | |
77 // with a new app instance. | |
78 auto exit_callback = base::Bind(&StaticLoader::StopAppThread, | |
79 weak_factory_.GetWeakPtr()); | |
80 thread_.reset(new RunnerThread(name, std::move(request), | |
81 base::ThreadTaskRunnerHandle::Get(), | |
82 exit_callback, factory_)); | |
83 thread_->Start(); | |
84 } | |
85 | |
86 void StaticLoader::StopAppThread() { | |
87 thread_->Join(); | |
88 thread_.reset(); | |
89 if (!quit_callback_.is_null()) | |
90 quit_callback_.Run(); | |
91 } | |
92 | |
93 } // namespace content | |
OLD | NEW |