| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/at_exit.h" | |
| 6 #include "base/bind.h" | |
| 7 #include "base/bind_helpers.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/run_loop.h" | |
| 11 #include "mojo/edk/embedder/embedder.h" | |
| 12 #include "mojo/edk/embedder/simple_platform_support.h" | |
| 13 #include "mojo/shell/external_application_registrar_connection.h" | |
| 14 #include "mojo/shell/in_process_dynamic_service_runner.h" | |
| 15 #include "mojo/shell/init.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 namespace { | |
| 19 const char kAppPath[] = "app-path"; | |
| 20 const char kAppURL[] = "app-url"; | |
| 21 const char kShellPath[] = "shell-path"; | |
| 22 } | |
| 23 | |
| 24 class Launcher { | |
| 25 public: | |
| 26 explicit Launcher(base::CommandLine* command_line) | |
| 27 : app_path_(command_line->GetSwitchValuePath(kAppPath)), | |
| 28 app_url_(command_line->GetSwitchValueASCII(kAppURL)), | |
| 29 loop_(base::MessageLoop::TYPE_IO), | |
| 30 connection_( | |
| 31 base::FilePath(command_line->GetSwitchValuePath(kShellPath))), | |
| 32 connect_result_(0) {} | |
| 33 ~Launcher() {} | |
| 34 | |
| 35 int Connect() { | |
| 36 DCHECK(!run_loop_.get()); | |
| 37 run_loop_.reset(new base::RunLoop); | |
| 38 connection_.Connect( | |
| 39 base::Bind(&Launcher::OnConnected, base::Unretained(this))); | |
| 40 run_loop_->Run(); | |
| 41 run_loop_.reset(); | |
| 42 return connect_result_; | |
| 43 } | |
| 44 | |
| 45 bool Register() { | |
| 46 DCHECK(!run_loop_.get()); | |
| 47 DCHECK(connect_result_ == 0); | |
| 48 run_loop_.reset(new base::RunLoop); | |
| 49 connection_.Register( | |
| 50 app_url_, base::Bind(&Launcher::OnRegistered, base::Unretained(this))); | |
| 51 run_loop_->Run(); | |
| 52 run_loop_.reset(); | |
| 53 return shell_handle_.is_valid(); | |
| 54 } | |
| 55 | |
| 56 void Run() { | |
| 57 DCHECK(!run_loop_.get()); | |
| 58 DCHECK(shell_handle_.is_valid()); | |
| 59 mojo::shell::InProcessDynamicServiceRunner service_runner(nullptr); | |
| 60 run_loop_.reset(new base::RunLoop); | |
| 61 service_runner.Start( | |
| 62 app_path_, | |
| 63 shell_handle_.Pass(), | |
| 64 base::Bind(&Launcher::OnAppCompleted, base::Unretained(this))); | |
| 65 run_loop_->Run(); | |
| 66 run_loop_.reset(); | |
| 67 } | |
| 68 | |
| 69 private: | |
| 70 void OnConnected(int result) { | |
| 71 connect_result_ = result; | |
| 72 run_loop_->Quit(); | |
| 73 } | |
| 74 | |
| 75 void OnRegistered(mojo::ShellPtr shell) { | |
| 76 shell_handle_ = shell.PassMessagePipe(); | |
| 77 run_loop_->Quit(); | |
| 78 } | |
| 79 | |
| 80 void OnAppCompleted() { | |
| 81 run_loop_->Quit(); | |
| 82 } | |
| 83 | |
| 84 const base::FilePath app_path_; | |
| 85 const GURL app_url_; | |
| 86 base::MessageLoop loop_; | |
| 87 mojo::shell::ExternalApplicationRegistrarConnection connection_; | |
| 88 int connect_result_; | |
| 89 mojo::ScopedMessagePipeHandle shell_handle_; | |
| 90 scoped_ptr<base::RunLoop> run_loop_; | |
| 91 }; | |
| 92 | |
| 93 #if defined(OS_WIN) | |
| 94 int main(int argc, wchar_t** argv) { | |
| 95 #else | |
| 96 int main(int argc, char** argv) { | |
| 97 #endif | |
| 98 base::AtExitManager at_exit; | |
| 99 mojo::embedder::Init(scoped_ptr<mojo::embedder::PlatformSupport>( | |
| 100 new mojo::embedder::SimplePlatformSupport())); | |
| 101 | |
| 102 base::CommandLine::Init(argc, argv); | |
| 103 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
| 104 mojo::shell::InitializeLogging(); | |
| 105 | |
| 106 Launcher launcher(command_line); | |
| 107 int result = launcher.Connect(); | |
| 108 if (result < 0) { | |
| 109 LOG(ERROR) << "Error(" << result << ") connecting on socket " | |
| 110 << command_line->GetSwitchValueASCII(kShellPath); | |
| 111 return MOJO_RESULT_INVALID_ARGUMENT; | |
| 112 } | |
| 113 | |
| 114 if (!launcher.Register()) { | |
| 115 LOG(ERROR) << "Error registering " | |
| 116 << command_line->GetSwitchValueASCII(kAppURL); | |
| 117 return MOJO_RESULT_INVALID_ARGUMENT; | |
| 118 } | |
| 119 | |
| 120 launcher.Run(); | |
| 121 return MOJO_RESULT_OK; | |
| 122 } | |
| OLD | NEW |