OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 <algorithm> | |
6 #include <iostream> | |
7 | |
8 #include "base/at_exit.h" | |
9 #include "base/bind.h" | |
10 #include "base/command_line.h" | |
11 #include "base/logging.h" | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/strings/string_split.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "mojo/shell/child_process.h" | |
16 #include "mojo/shell/context.h" | |
17 #include "mojo/shell/init.h" | |
18 #include "mojo/shell/mojo_url_resolver.h" | |
19 #include "mojo/shell/switches.h" | |
20 | |
21 #if defined(COMPONENT_BUILD) | |
22 #include "ui/gl/gl_surface.h" | |
23 #endif | |
24 | |
25 namespace { | |
26 | |
27 #if defined(OS_LINUX) | |
28 // Copied from ui/gfx/switches.cc to avoid a dependency on //ui/gfx | |
29 const char kEnableHarfBuzzRenderText[] = "enable-harfbuzz-rendertext"; | |
30 #endif | |
31 | |
32 #if defined(OS_WIN) | |
33 void SplitString(const base::string16& str, std::vector<std::string>* argv) { | |
34 base::SplitString(base::UTF16ToUTF8(str), ' ', argv); | |
35 } | |
36 #elif defined(OS_POSIX) | |
37 void SplitString(const std::string& str, std::vector<std::string>* argv) { | |
38 base::SplitString(str, ' ', argv); | |
39 } | |
40 #endif | |
41 | |
42 bool is_empty(const std::string& s) { | |
43 return s.empty(); | |
44 } | |
45 | |
46 // The value of app_url_and_args is "<mojo_app_url> [<args>...]", where args | |
47 // is a list of "configuration" arguments separated by spaces. If one or more | |
48 // arguments are specified they will be available when the Mojo application | |
49 // is initialized. See ApplicationImpl::args(). | |
50 GURL GetAppURLAndSetArgs(const base::CommandLine::StringType& app_url_and_args, | |
51 mojo::shell::Context* context) { | |
52 // SplitString() returns empty strings for extra delimeter characters (' '). | |
53 std::vector<std::string> argv; | |
54 SplitString(app_url_and_args, &argv); | |
55 argv.erase(std::remove_if(argv.begin(), argv.end(), is_empty), argv.end()); | |
56 | |
57 if (argv.empty()) | |
58 return GURL::EmptyGURL(); | |
59 GURL app_url(argv[0]); | |
60 if (argv.size() > 1) | |
61 context->application_manager()->SetArgsForURL(argv, app_url); | |
62 return app_url; | |
63 } | |
64 | |
65 void RunApps(mojo::shell::Context* context) { | |
66 const auto& command_line = *base::CommandLine::ForCurrentProcess(); | |
67 for (const auto& arg : command_line.GetArgs()) | |
68 context->Run(GetAppURLAndSetArgs(arg, context)); | |
69 } | |
70 | |
71 void Usage() { | |
72 std::cerr << "Launch Mojo applications.\n"; | |
73 std::cerr | |
74 << "Usage: mojo_shell" | |
75 << " [--" << switches::kArgsFor << "=<mojo-app>]" | |
76 << " [--" << switches::kContentHandlers << "=<handlers>]" | |
77 << " [--" << switches::kEnableExternalApplications << "]" | |
78 << " [--" << switches::kDisableCache << "]" | |
79 << " [--" << switches::kEnableMultiprocess << "]" | |
80 << " [--" << switches::kOrigin << "=<url-lib-path>]" | |
81 << " [--" << switches::kURLMappings << "=from1=to1,from2=to2]" | |
82 << " <mojo-app> ...\n\n" | |
83 << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within " | |
84 << "quotes.\n" | |
85 << "Example: mojo_shell \"mojo://js_standalone test.js\".\n" | |
86 << "<url-lib-path> is searched for shared libraries named by mojo URLs.\n" | |
87 << "The value of <handlers> is a comma separated list like:\n" | |
88 << "text/html,mojo://html_viewer," | |
89 << "application/javascript,mojo://js_content_handler\n"; | |
90 } | |
91 | |
92 bool ConfigureURLMappings(const std::string& mappings, | |
93 mojo::shell::MojoURLResolver* resolver) { | |
94 base::StringPairs pairs; | |
95 if (!base::SplitStringIntoKeyValuePairs(mappings, '=', ',', &pairs)) | |
96 return false; | |
97 using StringPair = std::pair<std::string, std::string>; | |
98 for (const StringPair& pair : pairs) { | |
99 const GURL from(pair.first); | |
100 const GURL to(pair.second); | |
101 if (!from.is_valid() || !to.is_valid()) | |
102 return false; | |
103 resolver->AddCustomMapping(from, to); | |
104 } | |
105 return true; | |
106 } | |
107 | |
108 } // namespace | |
109 | |
110 int main(int argc, char** argv) { | |
111 base::AtExitManager at_exit; | |
112 base::CommandLine::Init(argc, argv); | |
113 | |
114 const base::CommandLine& command_line = | |
115 *base::CommandLine::ForCurrentProcess(); | |
116 if (command_line.HasSwitch(switches::kHelp) || | |
117 command_line.GetArgs().empty()){ | |
118 Usage(); | |
119 return 0; | |
120 } | |
121 | |
122 #if defined(OS_LINUX) | |
123 // We use gfx::RenderText from multiple threads concurrently and the pango | |
124 // backend (currently the default on linux) is not close to threadsafe. Force | |
125 // use of the harfbuzz backend for now. | |
126 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
127 kEnableHarfBuzzRenderText); | |
128 #endif | |
129 mojo::shell::InitializeLogging(); | |
130 | |
131 // TODO(vtl): Unify parent and child process cases to the extent possible. | |
132 if (scoped_ptr<mojo::shell::ChildProcess> child_process = | |
133 mojo::shell::ChildProcess::Create( | |
134 *base::CommandLine::ForCurrentProcess())) { | |
135 child_process->Main(); | |
136 } else { | |
137 #if defined(COMPONENT_BUILD) | |
138 gfx::GLSurface::InitializeOneOff(); | |
139 #endif | |
140 // We want the shell::Context to outlive the MessageLoop so that pipes are | |
141 // all gracefully closed / error-out before we try to shut the Context down. | |
142 mojo::shell::Context shell_context; | |
143 { | |
144 base::MessageLoop message_loop; | |
145 shell_context.Init(); | |
146 | |
147 if (command_line.HasSwitch(switches::kOrigin)) { | |
148 shell_context.mojo_url_resolver()->SetBaseURL( | |
149 GURL(command_line.GetSwitchValueASCII(switches::kOrigin))); | |
150 } | |
151 | |
152 if (command_line.HasSwitch(switches::kURLMappings) && | |
153 !ConfigureURLMappings( | |
154 command_line.GetSwitchValueASCII(switches::kURLMappings), | |
155 shell_context.mojo_url_resolver())) { | |
156 Usage(); | |
157 return 0; | |
158 } | |
159 | |
160 for (const auto& kv : command_line.GetSwitches()) { | |
161 if (kv.first == switches::kArgsFor) | |
162 GetAppURLAndSetArgs(kv.second, &shell_context); | |
163 } | |
164 | |
165 message_loop.PostTask(FROM_HERE, base::Bind(RunApps, &shell_context)); | |
166 message_loop.Run(); | |
167 } | |
168 } | |
169 return 0; | |
170 } | |
OLD | NEW |