Chromium Code Reviews| Index: shell/command_line_util.cc |
| diff --git a/shell/command_line_util.cc b/shell/command_line_util.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f4174d2158043efb95c826f79cb99b51ae5798f6 |
| --- /dev/null |
| +++ b/shell/command_line_util.cc |
| @@ -0,0 +1,85 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "shell/command_line_util.h" |
| + |
| +#include "base/command_line.h" |
| +#include "base/logging.h" |
| +#include "base/strings/string_split.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "shell/context.h" |
| +#include "shell/switches.h" |
| + |
| +namespace mojo { |
| +namespace shell { |
| + |
| +namespace { |
| +bool IsEmpty(const std::string& s) { |
| + return s.empty(); |
| +} |
| + |
| +// The value of app_url_and_args is "<mojo_app_url> [<args>...]", where args |
| +// is a list of "configuration" arguments separated by spaces. If one or more |
| +// arguments are specified they will be available when the Mojo application |
| +// is initialized. See ApplicationImpl::args(). |
| +GURL GetAppURLAndSetArgs(const std::string& app_url_and_args, |
| + Context* context) { |
| + // SplitString() returns empty strings for extra delimeter characters (' '). |
| + std::vector<std::string> argv; |
| + base::SplitString(app_url_and_args, ' ', &argv); |
| + argv.erase(std::remove_if(argv.begin(), argv.end(), IsEmpty), argv.end()); |
| + |
| + if (argv.empty()) |
| + return GURL::EmptyGURL(); |
|
sky
2014/12/17 16:44:05
GURL() is fine here. GURL::EmptyGURL() is only nec
qsr
2014/12/18 14:21:35
Done.
I agree that it is less useful in the curr
sky
2014/12/18 17:01:15
Mainly because it isn't overly obvious what that f
|
| + GURL app_url(argv[0]); |
| + if (!app_url.is_valid()) { |
| + LOG(ERROR) << "Error: invalid URL: " << argv[0]; |
| + return app_url; |
| + } |
| + if (argv.size() > 1) |
| + context->application_manager()->SetArgsForURL(argv, app_url); |
|
sky
2014/12/17 16:44:05
Is it expected that you supply the url in argv? I
qsr
2014/12/18 14:21:35
Hum... This code has just moved here. Now, I guess
|
| + return app_url; |
| +} |
| + |
| +bool IsArgsFor(const std::string& arg, std::string* value) { |
|
sky
2014/12/17 16:44:05
Add a description here. Especially what |value| is
qsr
2014/12/18 14:21:36
Added a description. Renamed this ParseArgsFor
|
| + const std::string kArgsForSwitches[] = { |
| + "-" + std::string(switches::kArgsFor), |
| + "--" + std::string(switches::kArgsFor), |
| + }; |
| + for (size_t i = 0; i < arraysize(kArgsForSwitches); i++) { |
| + std::string argsfor_switch(kArgsForSwitches[i]); |
|
sky
2014/12/17 16:44:05
const std::string&
qsr
2014/12/18 14:21:35
Done.
|
| + if (arg.compare(0, argsfor_switch.size(), argsfor_switch) == 0) { |
| + *value = arg.substr(argsfor_switch.size() + 1, std::string::npos); |
| + return true; |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +} // namespace |
| + |
| +void ApplyApplicationArgs(Context* context, const std::string args) { |
| + std::string args_for_value; |
| + if (IsArgsFor(args, &args_for_value)) |
| + GetAppURLAndSetArgs(args_for_value, context); |
| +} |
| + |
| +void RunCommandLineApps(Context* context) { |
| + const auto& command_line = *base::CommandLine::ForCurrentProcess(); |
| + for (const auto& arg : command_line.GetArgs()) { |
| + std::string arg2; |
| +#if defined(OS_WIN) |
| + arg2 = base::UTF16ToUTF8(arg); |
| +#else |
| + arg2 = arg; |
| +#endif |
| + GURL url = GetAppURLAndSetArgs(arg2, context); |
| + if (!url.is_valid()) |
| + return; |
| + context->Run(url); |
| + } |
| +} |
| + |
| +} // namespace shell |
| +} // namespace mojo |