Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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 "chrome/test/chromedriver/capabilities_parser.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/string_util.h" | |
| 9 #include "base/stringprintf.h" | |
| 10 #include "chrome/test/chromedriver/chrome/status.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 // Parse information of proxy, add them as chrome switches to |args_list|. | |
| 15 Status ParseProxy(const base::DictionaryValue& proxy_dict, | |
| 16 base::ListValue* args_list) { | |
| 17 std::string proxy_type; | |
| 18 if (!proxy_dict.GetString("proxyType", &proxy_type)) | |
| 19 return Status(kUnknownError, "'proxyType' must be a string"); | |
| 20 proxy_type = StringToLowerASCII(proxy_type); | |
| 21 if (proxy_type == "direct") { | |
| 22 args_list->AppendString("no-proxy-server"); | |
| 23 } else if (proxy_type == "system") { | |
| 24 // Chrome default. | |
| 25 } else if (proxy_type == "pac") { | |
| 26 std::string proxy_pac_url; | |
| 27 if (!proxy_dict.GetString("proxyAutoconfigUrl", &proxy_pac_url)) | |
| 28 return Status(kUnknownError, "'proxyAutoconfigUrl' must be a string"); | |
| 29 args_list->AppendString("proxy-pac-url=" + proxy_pac_url); | |
| 30 } else if (proxy_type == "autodetect") { | |
| 31 args_list->AppendString("proxy-auto-detect"); | |
| 32 } else if (proxy_type == "manual") { | |
| 33 const char* proxy_servers_options[][2] = { | |
| 34 {"ftpProxy", "ftp"}, {"httpProxy", "http"}, {"sslProxy", "https"}}; | |
| 35 std::string proxy_servers; | |
| 36 for (size_t i = 0; i < arraysize(proxy_servers_options); ++i) { | |
| 37 if (!proxy_dict.HasKey(proxy_servers_options[i][0])) | |
| 38 continue; | |
| 39 std::string value; | |
| 40 if (!proxy_dict.GetString(proxy_servers_options[i][0], &value)) { | |
| 41 return Status( | |
| 42 kUnknownError, | |
| 43 base::StringPrintf("'%s' must be a string", | |
| 44 proxy_servers_options[i][0])); | |
| 45 } | |
| 46 // Converts into Chrome proxy scheme. | |
| 47 // Example: "http=localhost:9000;ftp=localhost:8000". | |
| 48 if (!proxy_servers.empty()) | |
| 49 proxy_servers += ";"; | |
| 50 proxy_servers += base::StringPrintf( | |
| 51 "%s=%s", proxy_servers_options[i][1], value.c_str()); | |
| 52 } | |
| 53 | |
| 54 std::string proxy_bypass_list; | |
| 55 if (proxy_dict.HasKey("noProxy")) { | |
| 56 if (!proxy_dict.GetString("noProxy", & proxy_bypass_list)) | |
| 57 return Status(kUnknownError, "'noProxy' must be a string"); | |
| 58 } | |
| 59 | |
| 60 if (proxy_servers.empty() && proxy_bypass_list.empty()) { | |
| 61 return Status(kUnknownError, "proxyType is 'manual' but no manual " | |
| 62 "proxy capabilities were found"); | |
| 63 } | |
| 64 if (!proxy_servers.empty()) | |
| 65 args_list->AppendString("proxy-server=" + proxy_servers); | |
| 66 if (!proxy_bypass_list.empty()) | |
| 67 args_list->AppendString("proxy-bypass-list=" + proxy_bypass_list); | |
| 68 } else { | |
| 69 return Status(kUnknownError, "unrecognized proxy type:" + proxy_type); | |
| 70 } | |
| 71 return Status(kOk); | |
| 72 } | |
| 73 | |
| 74 } // namespace | |
| 75 | |
| 76 Capabilities::Capabilities() | |
| 77 : prefs_dict(NULL), local_state_dict(NULL), extensions_list(NULL) {} | |
| 78 | |
| 79 Capabilities::~Capabilities() {} | |
| 80 | |
| 81 bool Capabilities::HasAndroidPackage() { | |
| 82 return !android_package.empty(); | |
| 83 } | |
| 84 | |
| 85 Status ParseCapabilities( | |
| 86 const base::DictionaryValue& desired_caps, | |
| 87 Capabilities* capabilities, | |
| 88 const base::Callback<bool(const base::FilePath&)>& file_checker) { | |
| 89 const base::Value* android_package; | |
| 90 if (desired_caps.Get("chromeOptions.android_package", &android_package)) { | |
| 91 if (!android_package->GetAsString(&capabilities->android_package) || | |
| 92 capabilities->android_package.empty()) { | |
| 93 return Status(kUnknownError, | |
| 94 "'android_package' must be a non-empty string"); | |
| 95 } | |
| 96 } else { | |
| 97 base::FilePath::StringType path_str; | |
| 98 base::FilePath chrome_exe; | |
| 99 if (desired_caps.GetString("chromeOptions.binary", &path_str)) { | |
| 100 chrome_exe = base::FilePath(path_str); | |
| 101 if (!file_checker.Run(chrome_exe)) { | |
| 102 std::string message = base::StringPrintf( | |
| 103 "no chrome binary at %" PRFilePath, | |
| 104 path_str.c_str()); | |
| 105 return Status(kUnknownError, message); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 const base::Value* log_path = NULL; | |
| 110 std::string chrome_log_path; | |
| 111 if (desired_caps.Get("chromeOptions.logPath", &log_path) && | |
|
kkania
2013/04/02 16:24:08
what do you think about iterating through ChromeOp
chrisgao (Use stgao instead)
2013/04/03 18:51:43
Done.
| |
| 112 !log_path->GetAsString(&chrome_log_path)) { | |
| 113 return Status(kUnknownError, | |
| 114 "chrome log path must be a string"); | |
| 115 } | |
| 116 | |
| 117 const base::Value* args = NULL; | |
| 118 const base::ListValue* args_list = NULL; | |
| 119 if (desired_caps.Get("chromeOptions.args", &args) && | |
| 120 !args->GetAsList(&args_list)) { | |
| 121 return Status(kUnknownError, | |
| 122 "command line arguments for chrome must be a list"); | |
| 123 } | |
| 124 | |
| 125 const base::Value* prefs = NULL; | |
| 126 const base::DictionaryValue* prefs_dict = NULL; | |
| 127 if (desired_caps.Get("chromeOptions.prefs", &prefs) && | |
| 128 !prefs->GetAsDictionary(&prefs_dict)) { | |
| 129 return Status(kUnknownError, "'prefs' must be a dictionary"); | |
| 130 } | |
| 131 | |
| 132 const base::Value* local_state = NULL; | |
| 133 const base::DictionaryValue* local_state_dict = NULL; | |
| 134 if (desired_caps.Get("chromeOptions.localState", &local_state) && | |
| 135 !local_state->GetAsDictionary(&local_state_dict)) { | |
| 136 return Status(kUnknownError, "'localState' must be a dictionary"); | |
| 137 } | |
| 138 | |
| 139 const base::Value* extensions = NULL; | |
| 140 const base::ListValue* extensions_list = NULL; | |
| 141 if (desired_caps.Get("chromeOptions.extensions", &extensions) | |
| 142 && !extensions->GetAsList(&extensions_list)) { | |
| 143 return Status(kUnknownError, | |
| 144 "chrome extensions must be a list"); | |
| 145 } | |
| 146 | |
| 147 const base::Value* proxy = NULL; | |
| 148 base::ListValue proxy_args_list; | |
| 149 if (desired_caps.Get("chromeOptions.proxy", &proxy)) { | |
| 150 const base::DictionaryValue* proxy_dict = NULL; | |
| 151 if (!proxy->GetAsDictionary(&proxy_dict)) | |
| 152 return Status(kUnknownError, "proxy must be a dictionary"); | |
| 153 Status status = ParseProxy(*proxy_dict, &proxy_args_list); | |
| 154 if (status.IsError()) | |
| 155 return status; | |
| 156 } | |
| 157 | |
| 158 capabilities->chrome_exe = chrome_exe; | |
| 159 capabilities->log_path = chrome_log_path; | |
| 160 capabilities->args_list.Swap(&proxy_args_list); | |
| 161 if (args_list) { | |
| 162 for (size_t i = 0; i < args_list->GetSize(); ++i) { | |
| 163 const base::Value* value = NULL; | |
| 164 args_list->Get(i, &value); | |
| 165 capabilities->args_list.Append(value->DeepCopy()); | |
| 166 } | |
| 167 } | |
| 168 capabilities->prefs_dict = prefs_dict; | |
| 169 capabilities->local_state_dict = local_state_dict; | |
| 170 capabilities->extensions_list = extensions_list; | |
| 171 } | |
| 172 return Status(kOk); | |
| 173 } | |
| OLD | NEW |