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