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 "base/macros.h" | 5 #include "base/macros.h" |
6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/threading/sequenced_worker_pool.h" | 8 #include "base/threading/sequenced_worker_pool.h" |
9 #include "mojo/application/run_application_options_chromium.h" | 9 #include "mojo/application/run_application_options_chromium.h" |
| 10 #include "mojo/environment/scoped_chromium_init.h" |
10 #include "mojo/public/c/system/main.h" | 11 #include "mojo/public/c/system/main.h" |
11 #include "mojo/public/cpp/application/application_impl_base.h" | 12 #include "mojo/public/cpp/application/application_impl_base.h" |
12 #include "mojo/public/cpp/application/run_application.h" | 13 #include "mojo/public/cpp/application/run_application.h" |
13 #include "mojo/public/cpp/application/service_provider_impl.h" | 14 #include "mojo/public/cpp/application/service_provider_impl.h" |
14 #include "mojo/services/native_support/interfaces/process.mojom.h" | 15 #include "mojo/services/native_support/interfaces/process.mojom.h" |
15 #include "services/native_support/process_impl.h" | 16 #include "services/native_support/process_impl.h" |
16 | 17 |
17 namespace native_support { | 18 namespace native_support { |
18 | 19 |
19 // TODO(vtl): Having to manually choose an arbitrary number is dumb. | 20 // TODO(vtl): Having to manually choose an arbitrary number is dumb. |
20 const size_t kMaxWorkerThreads = 16; | 21 const size_t kMaxWorkerThreads = 16; |
21 | 22 |
22 class NativeSupportApp : public mojo::ApplicationImplBase { | 23 class NativeSupportApp : public mojo::ApplicationImplBase { |
23 public: | 24 public: |
24 NativeSupportApp() {} | 25 NativeSupportApp() {} |
25 ~NativeSupportApp() override { | 26 ~NativeSupportApp() override { |
26 // TODO(vtl): Doing this here is a bit of a hack, but we may not | 27 if (worker_pool_) |
27 // consistently call |OnQuit()|. | 28 worker_pool_->Shutdown(); |
28 OnQuit(); | |
29 } | 29 } |
30 | 30 |
31 private: | 31 private: |
32 // |mojo::ApplicationImplBase| overrides: | 32 // |mojo::ApplicationImplBase| overrides: |
33 bool OnAcceptConnection( | 33 bool OnAcceptConnection( |
34 mojo::ServiceProviderImpl* service_provider_impl) override { | 34 mojo::ServiceProviderImpl* service_provider_impl) override { |
35 service_provider_impl->AddService<Process>([this]( | 35 service_provider_impl->AddService<Process>([this]( |
36 const mojo::ConnectionContext& connection_context, | 36 const mojo::ConnectionContext& connection_context, |
37 mojo::InterfaceRequest<Process> process_request) { | 37 mojo::InterfaceRequest<Process> process_request) { |
38 if (!worker_pool_) { | 38 if (!worker_pool_) { |
39 worker_pool_ = new base::SequencedWorkerPool(kMaxWorkerThreads, | 39 worker_pool_ = new base::SequencedWorkerPool(kMaxWorkerThreads, |
40 "NativeSupportWorker"); | 40 "NativeSupportWorker"); |
41 } | 41 } |
42 new ProcessImpl(worker_pool_, connection_context, process_request.Pass()); | 42 new ProcessImpl(worker_pool_, connection_context, process_request.Pass()); |
43 }); | 43 }); |
44 return true; | 44 return true; |
45 } | 45 } |
46 | 46 |
47 void OnQuit() override { | |
48 if (worker_pool_) { | |
49 worker_pool_->Shutdown(); | |
50 worker_pool_ = nullptr; | |
51 } | |
52 } | |
53 | |
54 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | 47 scoped_refptr<base::SequencedWorkerPool> worker_pool_; |
55 | 48 |
56 DISALLOW_COPY_AND_ASSIGN(NativeSupportApp); | 49 DISALLOW_COPY_AND_ASSIGN(NativeSupportApp); |
57 }; | 50 }; |
58 | 51 |
59 } // namespace native_support | 52 } // namespace native_support |
60 | 53 |
61 MojoResult MojoMain(MojoHandle application_request) { | 54 MojoResult MojoMain(MojoHandle application_request) { |
| 55 mojo::ScopedChromiumInit init; |
62 native_support::NativeSupportApp native_support_app; | 56 native_support::NativeSupportApp native_support_app; |
63 // We need an I/O message loop, since we'll want to watch FDs. | 57 // We need an I/O message loop, since we'll want to watch FDs. |
64 mojo::RunApplicationOptionsChromium options(base::MessageLoop::TYPE_IO); | 58 mojo::RunApplicationOptionsChromium options(base::MessageLoop::TYPE_IO); |
65 return mojo::RunMainApplication(application_request, &native_support_app, | 59 return mojo::RunApplication(application_request, &native_support_app, |
66 &options); | 60 &options); |
67 } | 61 } |
OLD | NEW |