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 "shell/command_line_util.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/logging.h" | |
9 #include "base/strings/string_split.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "shell/context.h" | |
12 #include "shell/switches.h" | |
13 | |
14 namespace mojo { | |
15 namespace shell { | |
16 | |
17 namespace { | |
18 bool IsEmpty(const std::string& s) { | |
19 return s.empty(); | |
20 } | |
21 | |
22 // The value of app_url_and_args is "<mojo_app_url> [<args>...]", where args | |
23 // is a list of "configuration" arguments separated by spaces. If one or more | |
24 // arguments are specified they will be available when the Mojo application | |
25 // is initialized. See ApplicationImpl::args(). | |
26 GURL GetAppURLAndSetArgs(const std::string& app_url_and_args, | |
27 Context* context) { | |
28 // SplitString() returns empty strings for extra delimeter characters (' '). | |
29 std::vector<std::string> argv; | |
30 base::SplitString(app_url_and_args, ' ', &argv); | |
31 argv.erase(std::remove_if(argv.begin(), argv.end(), IsEmpty), argv.end()); | |
32 | |
33 if (argv.empty()) | |
34 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
| |
35 GURL app_url(argv[0]); | |
36 if (!app_url.is_valid()) { | |
37 LOG(ERROR) << "Error: invalid URL: " << argv[0]; | |
38 return app_url; | |
39 } | |
40 if (argv.size() > 1) | |
41 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
| |
42 return app_url; | |
43 } | |
44 | |
45 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
| |
46 const std::string kArgsForSwitches[] = { | |
47 "-" + std::string(switches::kArgsFor), | |
48 "--" + std::string(switches::kArgsFor), | |
49 }; | |
50 for (size_t i = 0; i < arraysize(kArgsForSwitches); i++) { | |
51 std::string argsfor_switch(kArgsForSwitches[i]); | |
sky
2014/12/17 16:44:05
const std::string&
qsr
2014/12/18 14:21:35
Done.
| |
52 if (arg.compare(0, argsfor_switch.size(), argsfor_switch) == 0) { | |
53 *value = arg.substr(argsfor_switch.size() + 1, std::string::npos); | |
54 return true; | |
55 } | |
56 } | |
57 return false; | |
58 } | |
59 | |
60 } // namespace | |
61 | |
62 void ApplyApplicationArgs(Context* context, const std::string args) { | |
63 std::string args_for_value; | |
64 if (IsArgsFor(args, &args_for_value)) | |
65 GetAppURLAndSetArgs(args_for_value, context); | |
66 } | |
67 | |
68 void RunCommandLineApps(Context* context) { | |
69 const auto& command_line = *base::CommandLine::ForCurrentProcess(); | |
70 for (const auto& arg : command_line.GetArgs()) { | |
71 std::string arg2; | |
72 #if defined(OS_WIN) | |
73 arg2 = base::UTF16ToUTF8(arg); | |
74 #else | |
75 arg2 = arg; | |
76 #endif | |
77 GURL url = GetAppURLAndSetArgs(arg2, context); | |
78 if (!url.is_valid()) | |
79 return; | |
80 context->Run(url); | |
81 } | |
82 } | |
83 | |
84 } // namespace shell | |
85 } // namespace mojo | |
OLD | NEW |