OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/mojo/mojo_application_host.h" | 5 #include "content/browser/mojo/mojo_application_host.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
| 9 #include "base/logging.h" |
9 #include "build/build_config.h" | 10 #include "build/build_config.h" |
10 #include "content/common/mojo/mojo_messages.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "ipc/ipc_sender.h" | |
13 #include "mojo/edk/embedder/embedder.h" | 11 #include "mojo/edk/embedder/embedder.h" |
14 #include "mojo/edk/embedder/platform_channel_pair.h" | |
15 | 12 |
16 namespace content { | 13 namespace content { |
17 namespace { | 14 namespace { |
18 | 15 |
19 class ApplicationSetupImpl : public mojom::ApplicationSetup { | 16 class ApplicationSetupImpl : public mojom::ApplicationSetup { |
20 public: | 17 public: |
21 ApplicationSetupImpl(ServiceRegistryImpl* service_registry, | 18 ApplicationSetupImpl(ServiceRegistryImpl* service_registry, |
22 mojo::InterfaceRequest<mojom::ApplicationSetup> request) | 19 mojo::InterfaceRequest<mojom::ApplicationSetup> request) |
23 : binding_(this, std::move(request)), | 20 : binding_(this, std::move(request)), |
24 service_registry_(service_registry) {} | 21 service_registry_(service_registry) {} |
25 | 22 |
26 ~ApplicationSetupImpl() override { | 23 ~ApplicationSetupImpl() override { |
27 } | 24 } |
28 | 25 |
29 private: | 26 private: |
30 // mojom::ApplicationSetup implementation. | 27 // mojom::ApplicationSetup implementation. |
31 void ExchangeInterfaceProviders( | 28 void ExchangeInterfaceProviders( |
32 shell::mojom::InterfaceProviderRequest services, | 29 shell::mojom::InterfaceProviderRequest services, |
33 shell::mojom::InterfaceProviderPtr exposed_services) override { | 30 shell::mojom::InterfaceProviderPtr exposed_services) override { |
34 service_registry_->Bind(std::move(services)); | 31 service_registry_->Bind(std::move(services)); |
35 service_registry_->BindRemoteServiceProvider(std::move(exposed_services)); | 32 service_registry_->BindRemoteServiceProvider(std::move(exposed_services)); |
36 } | 33 } |
37 | 34 |
38 mojo::Binding<mojom::ApplicationSetup> binding_; | 35 mojo::Binding<mojom::ApplicationSetup> binding_; |
39 ServiceRegistryImpl* service_registry_; | 36 ServiceRegistryImpl* service_registry_; |
40 }; | 37 }; |
41 | 38 |
42 } // namespace | 39 } // namespace |
43 | 40 |
44 MojoApplicationHost::MojoApplicationHost() : did_activate_(false) { | 41 MojoApplicationHost::MojoApplicationHost() |
| 42 : token_(mojo::edk::GenerateRandomToken()) { |
45 #if defined(OS_ANDROID) | 43 #if defined(OS_ANDROID) |
46 service_registry_android_ = | 44 service_registry_android_ = |
47 ServiceRegistryAndroid::Create(&service_registry_); | 45 ServiceRegistryAndroid::Create(&service_registry_); |
48 #endif | 46 #endif |
| 47 |
| 48 mojo::ScopedMessagePipeHandle pipe = |
| 49 mojo::edk::CreateParentMessagePipe(token_); |
| 50 DCHECK(pipe.is_valid()); |
| 51 application_setup_.reset(new ApplicationSetupImpl( |
| 52 &service_registry_, |
| 53 mojo::MakeRequest<mojom::ApplicationSetup>(std::move(pipe)))); |
49 } | 54 } |
50 | 55 |
51 MojoApplicationHost::~MojoApplicationHost() { | 56 MojoApplicationHost::~MojoApplicationHost() { |
52 } | 57 } |
53 | 58 |
54 bool MojoApplicationHost::Init() { | |
55 DCHECK(!client_handle_.is_valid()) << "Already initialized!"; | |
56 | |
57 mojo::edk::PlatformChannelPair channel_pair; | |
58 | |
59 scoped_refptr<base::TaskRunner> io_task_runner; | |
60 if (io_task_runner_override_) { | |
61 io_task_runner = io_task_runner_override_; | |
62 } else { | |
63 io_task_runner = | |
64 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO) | |
65 ->task_runner(); | |
66 } | |
67 | |
68 // Forward this to the client once we know its process handle. | |
69 client_handle_ = channel_pair.PassClientHandle(); | |
70 mojo::ScopedMessagePipeHandle pipe = channel_init_.Init( | |
71 channel_pair.PassServerHandle().release().handle, io_task_runner); | |
72 application_setup_.reset(new ApplicationSetupImpl( | |
73 &service_registry_, | |
74 mojo::MakeRequest<mojom::ApplicationSetup>(std::move(pipe)))); | |
75 return true; | |
76 } | |
77 | |
78 void MojoApplicationHost::Activate(IPC::Sender* sender, | |
79 base::ProcessHandle process_handle) { | |
80 DCHECK(!did_activate_); | |
81 DCHECK(client_handle_.is_valid()); | |
82 | |
83 base::PlatformFile client_file = client_handle_.release().handle; | |
84 did_activate_ = sender->Send(new MojoMsg_Activate( | |
85 IPC::GetPlatformFileForTransit(client_file, true))); | |
86 } | |
87 | |
88 std::string MojoApplicationHost::InitWithToken() { | |
89 DCHECK(!client_handle_.is_valid()) << "Already initialized!"; | |
90 DCHECK(!did_activate_); | |
91 | |
92 std::string token = mojo::edk::GenerateRandomToken(); | |
93 mojo::ScopedMessagePipeHandle pipe = | |
94 mojo::edk::CreateParentMessagePipe(token); | |
95 DCHECK(pipe.is_valid()); | |
96 application_setup_.reset(new ApplicationSetupImpl( | |
97 &service_registry_, | |
98 mojo::MakeRequest<mojom::ApplicationSetup>(std::move(pipe)))); | |
99 | |
100 did_activate_ = true; | |
101 return token; | |
102 } | |
103 | |
104 void MojoApplicationHost::OverrideIOTaskRunnerForTest( | |
105 scoped_refptr<base::TaskRunner> io_task_runner) { | |
106 io_task_runner_override_ = io_task_runner; | |
107 } | |
108 | |
109 | |
110 } // namespace content | 59 } // namespace content |
OLD | NEW |