| 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
| 11 #include "base/base_switches.h" | 11 #include "base/base_switches.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 19 #include "base/process/process.h" | 19 #include "base/process/process.h" |
| 20 #include "base/thread_task_runner_handle.h" | 20 #include "base/thread_task_runner_handle.h" |
| 21 #include "mojo/common/weak_binding_set.h" | 21 #include "mojo/common/weak_binding_set.h" |
| 22 #include "mojo/converters/network/network_type_converters.h" | 22 #include "mojo/converters/network/network_type_converters.h" |
| 23 #include "mojo/edk/embedder/embedder.h" | 23 #include "mojo/edk/embedder/embedder.h" |
| 24 #include "mojo/edk/embedder/platform_channel_pair.h" | 24 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 25 #include "mojo/edk/embedder/scoped_platform_handle.h" | 25 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 26 #include "mojo/shell/application_manager_apptests.mojom.h" | 26 #include "mojo/shell/application_manager_apptests.mojom.h" |
| 27 #include "mojo/shell/public/cpp/application_connection.h" | 27 #include "mojo/shell/public/cpp/application_connection.h" |
| 28 #include "mojo/shell/public/cpp/application_delegate.h" | 28 #include "mojo/shell/public/cpp/application_delegate.h" |
| 29 #include "mojo/shell/public/cpp/application_impl.h" | |
| 30 #include "mojo/shell/public/cpp/interface_factory.h" | 29 #include "mojo/shell/public/cpp/interface_factory.h" |
| 30 #include "mojo/shell/public/cpp/shell.h" |
| 31 #include "mojo/shell/public/interfaces/application_manager.mojom.h" | 31 #include "mojo/shell/public/interfaces/application_manager.mojom.h" |
| 32 #include "mojo/shell/runner/child/test_native_main.h" | 32 #include "mojo/shell/runner/child/test_native_main.h" |
| 33 #include "mojo/shell/runner/common/switches.h" | 33 #include "mojo/shell/runner/common/switches.h" |
| 34 #include "mojo/shell/runner/init.h" | 34 #include "mojo/shell/runner/init.h" |
| 35 | 35 |
| 36 using mojo::shell::test::mojom::CreateInstanceForHandleTestPtr; | 36 using mojo::shell::test::mojom::CreateInstanceForHandleTestPtr; |
| 37 using mojo::shell::test::mojom::Driver; | 37 using mojo::shell::test::mojom::Driver; |
| 38 | 38 |
| 39 namespace { | 39 namespace { |
| 40 | 40 |
| 41 class TargetApplicationDelegate : public mojo::ApplicationDelegate, | 41 class TargetApplicationDelegate : public mojo::ApplicationDelegate, |
| 42 public mojo::InterfaceFactory<Driver>, | 42 public mojo::InterfaceFactory<Driver>, |
| 43 public Driver { | 43 public Driver { |
| 44 public: | 44 public: |
| 45 TargetApplicationDelegate() : app_(nullptr), weak_factory_(this) {} | 45 TargetApplicationDelegate() : shell_(nullptr), weak_factory_(this) {} |
| 46 ~TargetApplicationDelegate() override {} | 46 ~TargetApplicationDelegate() override {} |
| 47 | 47 |
| 48 private: | 48 private: |
| 49 // mojo::ApplicationDelegate: | 49 // mojo::ApplicationDelegate: |
| 50 void Initialize(mojo::ApplicationImpl* app) override { | 50 void Initialize(mojo::Shell* shell, const std::string& url, |
| 51 app_ = app; | 51 uint32_t id) override { |
| 52 shell_ = shell; |
| 52 | 53 |
| 53 base::FilePath target_path; | 54 base::FilePath target_path; |
| 54 CHECK(base::PathService::Get(base::DIR_EXE, &target_path)); | 55 CHECK(base::PathService::Get(base::DIR_EXE, &target_path)); |
| 55 #if defined(OS_WIN) | 56 #if defined(OS_WIN) |
| 56 target_path = target_path.Append( | 57 target_path = target_path.Append( |
| 57 FILE_PATH_LITERAL("application_manager_apptest_target.exe")); | 58 FILE_PATH_LITERAL("application_manager_apptest_target.exe")); |
| 58 #else | 59 #else |
| 59 target_path = target_path.Append( | 60 target_path = target_path.Append( |
| 60 FILE_PATH_LITERAL("application_manager_apptest_target")); | 61 FILE_PATH_LITERAL("application_manager_apptest_target")); |
| 61 #endif | 62 #endif |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 #elif defined(OS_POSIX) | 103 #elif defined(OS_POSIX) |
| 103 options.fds_to_remap = &handle_passing_info; | 104 options.fds_to_remap = &handle_passing_info; |
| 104 #endif | 105 #endif |
| 105 target_ = base::LaunchProcess(child_command_line, options); | 106 target_ = base::LaunchProcess(child_command_line, options); |
| 106 DCHECK(target_.IsValid()); | 107 DCHECK(target_.IsValid()); |
| 107 receiver->SetPID(target_.Pid()); | 108 receiver->SetPID(target_.Pid()); |
| 108 mojo::edk::ChildProcessLaunched(target_.Handle(), | 109 mojo::edk::ChildProcessLaunched(target_.Handle(), |
| 109 platform_channel_pair.PassServerHandle()); | 110 platform_channel_pair.PassServerHandle()); |
| 110 } | 111 } |
| 111 | 112 |
| 112 bool AcceptConnection( | 113 bool AcceptConnection(mojo::ApplicationConnection* connection) override { |
| 113 mojo::ApplicationConnection* connection) override { | |
| 114 connection->AddService<Driver>(this); | 114 connection->AddService<Driver>(this); |
| 115 return true; | 115 return true; |
| 116 } | 116 } |
| 117 | 117 |
| 118 // mojo::InterfaceFactory<Driver>: | 118 // mojo::InterfaceFactory<Driver>: |
| 119 void Create(mojo::ApplicationConnection* connection, | 119 void Create(mojo::ApplicationConnection* connection, |
| 120 mojo::InterfaceRequest<Driver> request) override { | 120 mojo::InterfaceRequest<Driver> request) override { |
| 121 bindings_.AddBinding(this, std::move(request)); | 121 bindings_.AddBinding(this, std::move(request)); |
| 122 } | 122 } |
| 123 | 123 |
| 124 // Driver: | 124 // Driver: |
| 125 void QuitDriver() override { | 125 void QuitDriver() override { |
| 126 target_.Terminate(0, false); | 126 target_.Terminate(0, false); |
| 127 app_->Quit(); | 127 shell_->Quit(); |
| 128 } | 128 } |
| 129 | 129 |
| 130 static void OnMessagePipeCreated( | 130 static void OnMessagePipeCreated( |
| 131 base::WeakPtr<TargetApplicationDelegate> weak_self, | 131 base::WeakPtr<TargetApplicationDelegate> weak_self, |
| 132 scoped_refptr<base::TaskRunner> task_runner, | 132 scoped_refptr<base::TaskRunner> task_runner, |
| 133 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request, | 133 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request, |
| 134 mojo::ScopedMessagePipeHandle pipe) { | 134 mojo::ScopedMessagePipeHandle pipe) { |
| 135 task_runner->PostTask( | 135 task_runner->PostTask( |
| 136 FROM_HERE, | 136 FROM_HERE, |
| 137 base::Bind(&TargetApplicationDelegate::OnMessagePipeCreatedOnMainThread, | 137 base::Bind(&TargetApplicationDelegate::OnMessagePipeCreatedOnMainThread, |
| 138 weak_self, base::Passed(&request), base::Passed(&pipe))); | 138 weak_self, base::Passed(&request), base::Passed(&pipe))); |
| 139 } | 139 } |
| 140 | 140 |
| 141 void OnMessagePipeCreatedOnMainThread( | 141 void OnMessagePipeCreatedOnMainThread( |
| 142 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request, | 142 mojo::InterfaceRequest<mojo::shell::mojom::PIDReceiver> request, |
| 143 mojo::ScopedMessagePipeHandle pipe) { | 143 mojo::ScopedMessagePipeHandle pipe) { |
| 144 mojo::shell::mojom::CapabilityFilterPtr filter( | 144 mojo::shell::mojom::CapabilityFilterPtr filter( |
| 145 mojo::shell::mojom::CapabilityFilter::New()); | 145 mojo::shell::mojom::CapabilityFilter::New()); |
| 146 mojo::Array<mojo::String> test_interfaces; | 146 mojo::Array<mojo::String> test_interfaces; |
| 147 test_interfaces.push_back( | 147 test_interfaces.push_back( |
| 148 mojo::shell::test::mojom::CreateInstanceForHandleTest::Name_); | 148 mojo::shell::test::mojom::CreateInstanceForHandleTest::Name_); |
| 149 filter->filter.insert("mojo:mojo_shell_apptests", | 149 filter->filter.insert("mojo:mojo_shell_apptests", |
| 150 std::move(test_interfaces)); | 150 std::move(test_interfaces)); |
| 151 | 151 |
| 152 mojo::shell::mojom::ApplicationManagerPtr application_manager; | 152 mojo::shell::mojom::ApplicationManagerPtr application_manager; |
| 153 app_->ConnectToService("mojo:shell", &application_manager); | 153 shell_->ConnectToService("mojo:shell", &application_manager); |
| 154 application_manager->CreateInstanceForHandle( | 154 application_manager->CreateInstanceForHandle( |
| 155 mojo::ScopedHandle(mojo::Handle(pipe.release().value())), | 155 mojo::ScopedHandle(mojo::Handle(pipe.release().value())), |
| 156 "exe:application_manager_apptest_target", std::move(filter), | 156 "exe:application_manager_apptest_target", std::move(filter), |
| 157 std::move(request)); | 157 std::move(request)); |
| 158 } | 158 } |
| 159 | 159 |
| 160 mojo::ApplicationImpl* app_; | 160 mojo::Shell* shell_; |
| 161 base::Process target_; | 161 base::Process target_; |
| 162 mojo::WeakBindingSet<Driver> bindings_; | 162 mojo::WeakBindingSet<Driver> bindings_; |
| 163 base::WeakPtrFactory<TargetApplicationDelegate> weak_factory_; | 163 base::WeakPtrFactory<TargetApplicationDelegate> weak_factory_; |
| 164 | 164 |
| 165 DISALLOW_COPY_AND_ASSIGN(TargetApplicationDelegate); | 165 DISALLOW_COPY_AND_ASSIGN(TargetApplicationDelegate); |
| 166 }; | 166 }; |
| 167 | 167 |
| 168 } // namespace | 168 } // namespace |
| 169 | 169 |
| 170 int main(int argc, char** argv) { | 170 int main(int argc, char** argv) { |
| 171 base::AtExitManager at_exit; | 171 base::AtExitManager at_exit; |
| 172 base::CommandLine::Init(argc, argv); | 172 base::CommandLine::Init(argc, argv); |
| 173 | 173 |
| 174 mojo::shell::InitializeLogging(); | 174 mojo::shell::InitializeLogging(); |
| 175 | 175 |
| 176 TargetApplicationDelegate delegate; | 176 TargetApplicationDelegate delegate; |
| 177 return mojo::shell::TestNativeMain(&delegate); | 177 return mojo::shell::TestNativeMain(&delegate); |
| 178 } | 178 } |
| OLD | NEW |