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 "mojo/shell/public/cpp/application_runner.h" | |
6 | |
7 #include "base/at_exit.h" | |
8 #include "base/bind.h" | |
9 #include "base/command_line.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/message_loop/message_loop.h" | |
12 #include "base/process/launch.h" | |
13 #include "base/run_loop.h" | |
14 #include "mojo/shell/public/cpp/shell_client.h" | |
15 #include "mojo/shell/public/cpp/shell_connection.h" | |
16 | |
17 namespace mojo { | |
18 | |
19 int g_application_runner_argc; | |
20 const char* const* g_application_runner_argv; | |
21 | |
22 ApplicationRunner::ApplicationRunner(ShellClient* client) | |
23 : client_(scoped_ptr<ShellClient>(client)), | |
24 message_loop_type_(base::MessageLoop::TYPE_DEFAULT), | |
25 has_run_(false) {} | |
26 | |
27 ApplicationRunner::~ApplicationRunner() {} | |
28 | |
29 void ApplicationRunner::InitBaseCommandLine() { | |
30 base::CommandLine::Init(g_application_runner_argc, g_application_runner_argv); | |
31 } | |
32 | |
33 void ApplicationRunner::set_message_loop_type(base::MessageLoop::Type type) { | |
34 DCHECK_NE(base::MessageLoop::TYPE_CUSTOM, type); | |
35 DCHECK(!has_run_); | |
36 | |
37 message_loop_type_ = type; | |
38 } | |
39 | |
40 MojoResult ApplicationRunner::Run(MojoHandle shell_client_request_handle, | |
41 bool init_base) { | |
42 DCHECK(!has_run_); | |
43 has_run_ = true; | |
44 | |
45 scoped_ptr<base::AtExitManager> at_exit; | |
46 if (init_base) { | |
47 InitBaseCommandLine(); | |
48 at_exit.reset(new base::AtExitManager); | |
49 } | |
50 | |
51 { | |
52 scoped_ptr<base::MessageLoop> loop; | |
53 loop.reset(new base::MessageLoop(message_loop_type_)); | |
54 | |
55 connection_.reset(new ShellConnection( | |
56 client_.get(), | |
57 MakeRequest<shell::mojom::ShellClient>(MakeScopedHandle( | |
58 MessagePipeHandle(shell_client_request_handle))))); | |
59 base::RunLoop run_loop; | |
60 connection_->set_connection_lost_closure(run_loop.QuitClosure()); | |
61 run_loop.Run(); | |
62 // It's very common for the client to cache the app and terminate on errors. | |
63 // If we don't delete the client before the app we run the risk of the | |
64 // client having a stale reference to the app and trying to use it. | |
65 // Note that we destruct the message loop first because that might trigger | |
66 // connection error handlers and they might access objects created by the | |
67 // client. | |
68 loop.reset(); | |
69 client_.reset(); | |
70 connection_.reset(); | |
71 } | |
72 return MOJO_RESULT_OK; | |
73 } | |
74 | |
75 MojoResult ApplicationRunner::Run(MojoHandle shell_client_request_handle) { | |
76 bool init_base = true; | |
77 if (base::CommandLine::InitializedForCurrentProcess()) { | |
78 init_base = | |
79 !base::CommandLine::ForCurrentProcess()->HasSwitch("single-process"); | |
80 } | |
81 return Run(shell_client_request_handle, init_base); | |
82 } | |
83 | |
84 void ApplicationRunner::DestroyShellConnection() { | |
85 connection_.reset(); | |
86 } | |
87 | |
88 void ApplicationRunner::Quit() { | |
89 base::MessageLoop::current()->QuitWhenIdle(); | |
90 } | |
91 | |
92 } // namespace mojo | |
OLD | NEW |