| 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 namespace { | |
| 22 | |
| 23 #if defined(OS_LINUX) | |
| 24 // Copied from ui/gfx/switches.cc to avoid a dependency on //ui/gfx | |
| 25 const char kEnableHarfBuzzRenderText[] = "enable-harfbuzz-rendertext"; | |
| 26 #endif | |
| 27 | |
| 28 bool IsEmpty(const std::string& s) { | |
| 29 return s.empty(); | |
| 30 } | |
| 31 | |
| 32 // The value of app_url_and_args is "<mojo_app_url> [<args>...]", where args | |
| 33 // is a list of "configuration" arguments separated by spaces. If one or more | |
| 34 // arguments are specified they will be available when the Mojo application | |
| 35 // is initialized. See ApplicationImpl::args(). | |
| 36 GURL GetAppURLAndSetArgs(const std::string& app_url_and_args, | |
| 37 mojo::shell::Context* context) { | |
| 38 // SplitString() returns empty strings for extra delimeter characters (' '). | |
| 39 std::vector<std::string> argv; | |
| 40 base::SplitString(app_url_and_args, ' ', &argv); | |
| 41 argv.erase(std::remove_if(argv.begin(), argv.end(), IsEmpty), argv.end()); | |
| 42 | |
| 43 if (argv.empty()) | |
| 44 return GURL::EmptyGURL(); | |
| 45 GURL app_url(argv[0]); | |
| 46 if (!app_url.is_valid()) { | |
| 47 LOG(ERROR) << "Error: invalid URL: " << argv[0]; | |
| 48 return app_url; | |
| 49 } | |
| 50 if (argv.size() > 1) | |
| 51 context->application_manager()->SetArgsForURL(argv, app_url); | |
| 52 return app_url; | |
| 53 } | |
| 54 | |
| 55 void RunApps(mojo::shell::Context* context) { | |
| 56 const auto& command_line = *base::CommandLine::ForCurrentProcess(); | |
| 57 for (const auto& arg : command_line.GetArgs()) { | |
| 58 std::string arg2; | |
| 59 #if defined(OS_WIN) | |
| 60 arg2 = base::UTF16ToUTF8(arg); | |
| 61 #else | |
| 62 arg2 = arg; | |
| 63 #endif | |
| 64 GURL url = GetAppURLAndSetArgs(arg2, context); | |
| 65 if (!url.is_valid()) | |
| 66 return; | |
| 67 context->Run(GetAppURLAndSetArgs(arg2, context)); | |
| 68 } | |
| 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 IsArgsFor(const std::string& arg, std::string* value) { | |
| 93 const std::string kArgsForSwitches[] = { | |
| 94 "-" + std::string(switches::kArgsFor), | |
| 95 "--" + std::string(switches::kArgsFor), | |
| 96 }; | |
| 97 for (size_t i = 0; i < arraysize(kArgsForSwitches); i++) { | |
| 98 std::string argsfor_switch(kArgsForSwitches[i]); | |
| 99 if (arg.compare(0, argsfor_switch.size(), argsfor_switch) == 0) { | |
| 100 *value = arg.substr(argsfor_switch.size() + 1, std::string::npos); | |
| 101 return true; | |
| 102 } | |
| 103 } | |
| 104 return false; | |
| 105 } | |
| 106 | |
| 107 } // namespace | |
| 108 | |
| 109 int main(int argc, char** argv) { | |
| 110 base::AtExitManager at_exit; | |
| 111 base::CommandLine::Init(argc, argv); | |
| 112 | |
| 113 const base::CommandLine& command_line = | |
| 114 *base::CommandLine::ForCurrentProcess(); | |
| 115 | |
| 116 const std::set<std::string> all_switches = switches::GetAllSwitches(); | |
| 117 const base::CommandLine::SwitchMap switches = command_line.GetSwitches(); | |
| 118 bool found_unknown_switch = false; | |
| 119 for (const auto& s : switches) { | |
| 120 if (all_switches.find(s.first) == all_switches.end()) { | |
| 121 std::cerr << "unknown switch: " << s.first << std::endl; | |
| 122 found_unknown_switch = true; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 if (found_unknown_switch || | |
| 127 (!command_line.HasSwitch(switches::kEnableExternalApplications) && | |
| 128 (command_line.HasSwitch(switches::kHelp) || | |
| 129 command_line.GetArgs().empty()))) { | |
| 130 Usage(); | |
| 131 return 0; | |
| 132 } | |
| 133 | |
| 134 #if defined(OS_LINUX) | |
| 135 // We use gfx::RenderText from multiple threads concurrently and the pango | |
| 136 // backend (currently the default on linux) is not close to threadsafe. Force | |
| 137 // use of the harfbuzz backend for now. | |
| 138 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 139 kEnableHarfBuzzRenderText); | |
| 140 #endif | |
| 141 mojo::shell::InitializeLogging(); | |
| 142 | |
| 143 // TODO(vtl): Unify parent and child process cases to the extent possible. | |
| 144 if (scoped_ptr<mojo::shell::ChildProcess> child_process = | |
| 145 mojo::shell::ChildProcess::Create( | |
| 146 *base::CommandLine::ForCurrentProcess())) { | |
| 147 child_process->Main(); | |
| 148 } else { | |
| 149 // We want the shell::Context to outlive the MessageLoop so that pipes are | |
| 150 // all gracefully closed / error-out before we try to shut the Context down. | |
| 151 mojo::shell::Context shell_context; | |
| 152 { | |
| 153 base::MessageLoop message_loop; | |
| 154 if (!shell_context.Init()) { | |
| 155 Usage(); | |
| 156 return 0; | |
| 157 } | |
| 158 | |
| 159 // The mojo_shell --args-for command-line switch is handled specially | |
| 160 // because it can appear more than once. The base::CommandLine class | |
| 161 // collapses multiple occurrences of the same switch. | |
| 162 for (int i = 1; i < argc; i++) { | |
| 163 std::string args_for_value; | |
| 164 if (IsArgsFor(argv[i], &args_for_value)) | |
| 165 GetAppURLAndSetArgs(args_for_value, &shell_context); | |
| 166 } | |
| 167 | |
| 168 message_loop.PostTask(FROM_HERE, base::Bind(RunApps, &shell_context)); | |
| 169 message_loop.Run(); | |
| 170 } | |
| 171 } | |
| 172 return 0; | |
| 173 } | |
| OLD | NEW |