| 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" | |
| 10 #include "build/build_config.h" | 9 #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" |
| 11 #include "mojo/edk/embedder/embedder.h" | 13 #include "mojo/edk/embedder/embedder.h" |
| 14 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 12 | 15 |
| 13 namespace content { | 16 namespace content { |
| 14 namespace { | 17 namespace { |
| 15 | 18 |
| 16 class ApplicationSetupImpl : public mojom::ApplicationSetup { | 19 class ApplicationSetupImpl : public mojom::ApplicationSetup { |
| 17 public: | 20 public: |
| 18 ApplicationSetupImpl(ServiceRegistryImpl* service_registry, | 21 ApplicationSetupImpl(ServiceRegistryImpl* service_registry, |
| 19 mojo::InterfaceRequest<mojom::ApplicationSetup> request) | 22 mojo::InterfaceRequest<mojom::ApplicationSetup> request) |
| 20 : binding_(this, std::move(request)), | 23 : binding_(this, std::move(request)), |
| 21 service_registry_(service_registry) {} | 24 service_registry_(service_registry) {} |
| 22 | 25 |
| 23 ~ApplicationSetupImpl() override { | 26 ~ApplicationSetupImpl() override { |
| 24 } | 27 } |
| 25 | 28 |
| 26 private: | 29 private: |
| 27 // mojom::ApplicationSetup implementation. | 30 // mojom::ApplicationSetup implementation. |
| 28 void ExchangeInterfaceProviders( | 31 void ExchangeInterfaceProviders( |
| 29 shell::mojom::InterfaceProviderRequest services, | 32 shell::mojom::InterfaceProviderRequest services, |
| 30 shell::mojom::InterfaceProviderPtr exposed_services) override { | 33 shell::mojom::InterfaceProviderPtr exposed_services) override { |
| 31 service_registry_->Bind(std::move(services)); | 34 service_registry_->Bind(std::move(services)); |
| 32 service_registry_->BindRemoteServiceProvider(std::move(exposed_services)); | 35 service_registry_->BindRemoteServiceProvider(std::move(exposed_services)); |
| 33 } | 36 } |
| 34 | 37 |
| 35 mojo::Binding<mojom::ApplicationSetup> binding_; | 38 mojo::Binding<mojom::ApplicationSetup> binding_; |
| 36 ServiceRegistryImpl* service_registry_; | 39 ServiceRegistryImpl* service_registry_; |
| 37 }; | 40 }; |
| 38 | 41 |
| 39 } // namespace | 42 } // namespace |
| 40 | 43 |
| 41 MojoApplicationHost::MojoApplicationHost() | 44 MojoApplicationHost::MojoApplicationHost() : did_activate_(false) { |
| 42 : token_(mojo::edk::GenerateRandomToken()) { | |
| 43 #if defined(OS_ANDROID) | 45 #if defined(OS_ANDROID) |
| 44 service_registry_android_ = | 46 service_registry_android_ = |
| 45 ServiceRegistryAndroid::Create(&service_registry_); | 47 ServiceRegistryAndroid::Create(&service_registry_); |
| 46 #endif | 48 #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)))); | |
| 54 } | 49 } |
| 55 | 50 |
| 56 MojoApplicationHost::~MojoApplicationHost() { | 51 MojoApplicationHost::~MojoApplicationHost() { |
| 57 } | 52 } |
| 58 | 53 |
| 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 |
| 59 } // namespace content | 110 } // namespace content |
| OLD | NEW |