| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <algorithm> |
| 6 #include <iostream> | 6 #include <iostream> |
| 7 | 7 |
| 8 #include "base/at_exit.h" | 8 #include "base/at_exit.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #if defined(OS_WIN) | 28 #if defined(OS_WIN) |
| 29 void SplitString(const base::string16& str, std::vector<std::string>* argv) { | 29 void SplitString(const base::string16& str, std::vector<std::string>* argv) { |
| 30 base::SplitString(base::UTF16ToUTF8(str), ' ', argv); | 30 base::SplitString(base::UTF16ToUTF8(str), ' ', argv); |
| 31 } | 31 } |
| 32 #elif defined(OS_POSIX) | 32 #elif defined(OS_POSIX) |
| 33 void SplitString(const std::string& str, std::vector<std::string>* argv) { | 33 void SplitString(const std::string& str, std::vector<std::string>* argv) { |
| 34 base::SplitString(str, ' ', argv); | 34 base::SplitString(str, ' ', argv); |
| 35 } | 35 } |
| 36 #endif | 36 #endif |
| 37 | 37 |
| 38 bool is_empty(const std::string& s) { | 38 bool IsEmpty(const std::string& s) { |
| 39 return s.empty(); | 39 return s.empty(); |
| 40 } | 40 } |
| 41 | 41 |
| 42 // The value of app_url_and_args is "<mojo_app_url> [<args>...]", where args | 42 // The value of app_url_and_args is "<mojo_app_url> [<args>...]", where args |
| 43 // is a list of "configuration" arguments separated by spaces. If one or more | 43 // is a list of "configuration" arguments separated by spaces. If one or more |
| 44 // arguments are specified they will be available when the Mojo application | 44 // arguments are specified they will be available when the Mojo application |
| 45 // is initialized. See ApplicationImpl::args(). | 45 // is initialized. See ApplicationImpl::args(). |
| 46 GURL GetAppURLAndSetArgs(const base::CommandLine::StringType& app_url_and_args, | 46 GURL GetAppURLAndSetArgs(const base::CommandLine::StringType& app_url_and_args, |
| 47 mojo::shell::Context* context) { | 47 mojo::shell::Context* context) { |
| 48 // SplitString() returns empty strings for extra delimeter characters (' '). | 48 // SplitString() returns empty strings for extra delimeter characters (' '). |
| 49 std::vector<std::string> argv; | 49 std::vector<std::string> argv; |
| 50 SplitString(app_url_and_args, &argv); | 50 SplitString(app_url_and_args, &argv); |
| 51 argv.erase(std::remove_if(argv.begin(), argv.end(), is_empty), argv.end()); | 51 argv.erase(std::remove_if(argv.begin(), argv.end(), IsEmpty), argv.end()); |
| 52 | 52 |
| 53 if (argv.empty()) | 53 if (argv.empty()) |
| 54 return GURL::EmptyGURL(); | 54 return GURL::EmptyGURL(); |
| 55 GURL app_url(argv[0]); | 55 GURL app_url(argv[0]); |
| 56 if (argv.size() > 1) | 56 if (argv.size() > 1) |
| 57 context->application_manager()->SetArgsForURL(argv, app_url); | 57 context->application_manager()->SetArgsForURL(argv, app_url); |
| 58 return app_url; | 58 return app_url; |
| 59 } | 59 } |
| 60 | 60 |
| 61 void RunApps(mojo::shell::Context* context) { | 61 void RunApps(mojo::shell::Context* context) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 for (const StringPair& pair : pairs) { | 94 for (const StringPair& pair : pairs) { |
| 95 const GURL from(pair.first); | 95 const GURL from(pair.first); |
| 96 const GURL to(pair.second); | 96 const GURL to(pair.second); |
| 97 if (!from.is_valid() || !to.is_valid()) | 97 if (!from.is_valid() || !to.is_valid()) |
| 98 return false; | 98 return false; |
| 99 resolver->AddCustomMapping(from, to); | 99 resolver->AddCustomMapping(from, to); |
| 100 } | 100 } |
| 101 return true; | 101 return true; |
| 102 } | 102 } |
| 103 | 103 |
| 104 bool isArgsFor(const std::string& arg, std::string* value) { | 104 bool IsArgsFor(const std::string& arg, std::string* value) { |
| 105 const std::string kArgsForSwitches[] = { | 105 const std::string kArgsForSwitches[] = { |
| 106 "-" + std::string(switches::kArgsFor), | 106 "-" + std::string(switches::kArgsFor), |
| 107 "--" + std::string(switches::kArgsFor), | 107 "--" + std::string(switches::kArgsFor), |
| 108 }; | 108 }; |
| 109 for (size_t i = 0; i < arraysize(kArgsForSwitches); i++) { | 109 for (size_t i = 0; i < arraysize(kArgsForSwitches); i++) { |
| 110 std::string argsfor_switch(kArgsForSwitches[i]); | 110 std::string argsfor_switch(kArgsForSwitches[i]); |
| 111 if (arg.compare(0, argsfor_switch.size(), argsfor_switch) == 0) { | 111 if (arg.compare(0, argsfor_switch.size(), argsfor_switch) == 0) { |
| 112 *value = arg.substr(argsfor_switch.size() + 1, std::string::npos); | 112 *value = arg.substr(argsfor_switch.size() + 1, std::string::npos); |
| 113 return true; | 113 return true; |
| 114 } | 114 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 command_line.GetSwitchValueASCII(switches::kURLMappings), | 163 command_line.GetSwitchValueASCII(switches::kURLMappings), |
| 164 shell_context.mojo_url_resolver())) { | 164 shell_context.mojo_url_resolver())) { |
| 165 Usage(); | 165 Usage(); |
| 166 return 0; | 166 return 0; |
| 167 } | 167 } |
| 168 | 168 |
| 169 // The mojo_shell --args-for command-line switch is handled specially | 169 // The mojo_shell --args-for command-line switch is handled specially |
| 170 // because it can appear more than once. The base::CommandLine class | 170 // because it can appear more than once. The base::CommandLine class |
| 171 // collapses multiple occurrences of the same switch. | 171 // collapses multiple occurrences of the same switch. |
| 172 for (int i = 1; i < argc; i++) { | 172 for (int i = 1; i < argc; i++) { |
| 173 std::string argsForValue; | 173 std::string args_for_value; |
| 174 if (isArgsFor(argv[i], &argsForValue)) | 174 if (IsArgsFor(argv[i], &args_for_value)) |
| 175 GetAppURLAndSetArgs(argsForValue, &shell_context); | 175 GetAppURLAndSetArgs(args_for_value, &shell_context); |
| 176 } | 176 } |
| 177 | 177 |
| 178 message_loop.PostTask(FROM_HERE, base::Bind(RunApps, &shell_context)); | 178 message_loop.PostTask(FROM_HERE, base::Bind(RunApps, &shell_context)); |
| 179 message_loop.Run(); | 179 message_loop.Run(); |
| 180 } | 180 } |
| 181 } | 181 } |
| 182 return 0; | 182 return 0; |
| 183 } | 183 } |
| OLD | NEW |