Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(483)

Side by Side Diff: shell/command_line_util.cc

Issue 816473002: Update mojo shell so that --args-for can be used on android (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Refactor and add unit tests Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 GURL GetAppURLAndSetArgs(const std::string& app_url_and_args,
23 Context* context) {
24 std::vector<std::string> args;
25 GURL app_url = GetAppURLAndArgs(app_url_and_args, &args);
26
27 if (args.size() > 1)
28 context->application_manager()->SetArgsForURL(args, app_url);
29 return app_url;
30 }
31 } // namespace
32
33 bool ParseArgsFor(const std::string& arg, std::string* value) {
34 const std::string kArgsForSwitches[] = {
35 "-" + std::string(switches::kArgsFor) + "=",
36 "--" + std::string(switches::kArgsFor) + "=",
37 };
38 for (size_t i = 0; i < arraysize(kArgsForSwitches); i++) {
39 const std::string& argsfor_switch = kArgsForSwitches[i];
40 if (arg.compare(0, argsfor_switch.size(), argsfor_switch) == 0) {
41 *value = arg.substr(argsfor_switch.size(), std::string::npos);
42 return true;
43 }
44 }
45 return false;
46 }
47
48 GURL GetAppURLAndArgs(const std::string& app_url_and_args,
49 std::vector<std::string>* args) {
50 // SplitString() returns empty strings for extra delimeter characters (' ').
51 base::SplitString(app_url_and_args, ' ', args);
52 args->erase(std::remove_if(args->begin(), args->end(), IsEmpty), args->end());
sky 2014/12/18 17:01:15 nit: I'm pretty sure you could use mem_fun here an
qsr 2014/12/18 17:17:56 Done.
53
54 if (args->empty())
55 return GURL();
56 GURL app_url((*args)[0]);
57 if (!app_url.is_valid()) {
58 LOG(ERROR) << "Error: invalid URL: " << (*args)[0];
59 return app_url;
60 }
61 if (args->size() == 1)
62 args->clear();
63 return app_url;
64 }
65
66 void ApplyApplicationArgs(Context* context, const std::string& args) {
67 std::string args_for_value;
68 if (ParseArgsFor(args, &args_for_value))
69 GetAppURLAndSetArgs(args_for_value, context);
70 }
71
72 void RunCommandLineApps(Context* context) {
73 const auto& command_line = *base::CommandLine::ForCurrentProcess();
74 for (const auto& arg : command_line.GetArgs()) {
75 std::string arg2;
76 #if defined(OS_WIN)
77 arg2 = base::UTF16ToUTF8(arg);
78 #else
79 arg2 = arg;
80 #endif
81 GURL url = GetAppURLAndSetArgs(arg2, context);
82 if (!url.is_valid())
83 return;
84 context->Run(url);
85 }
86 }
87
88 } // namespace shell
89 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698