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