OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/test/webdriver/commands/create_session.h" | 5 #include "chrome/test/webdriver/commands/create_session.h" |
6 | 6 |
7 #include <sstream> | 7 #include <sstream> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 bool CreateSession::DoesPost() { return true; } | 66 bool CreateSession::DoesPost() { return true; } |
67 | 67 |
68 void CreateSession::ExecutePost(Response* const response) { | 68 void CreateSession::ExecutePost(Response* const response) { |
69 DictionaryValue *capabilities = NULL; | 69 DictionaryValue *capabilities = NULL; |
70 if (!GetDictionaryParameter("desiredCapabilities", &capabilities)) { | 70 if (!GetDictionaryParameter("desiredCapabilities", &capabilities)) { |
71 response->SetError(new Error( | 71 response->SetError(new Error( |
72 kBadRequest, "Missing or invalid 'desiredCapabilities'")); | 72 kBadRequest, "Missing or invalid 'desiredCapabilities'")); |
73 return; | 73 return; |
74 } | 74 } |
75 | 75 |
76 CommandLine command_line_options(CommandLine::NO_PROGRAM); | 76 Automation::BrowserOptions browser_options; |
| 77 FilePath::StringType path; |
| 78 if (capabilities->GetStringWithoutPathExpansion("chrome.binary", &path)) |
| 79 browser_options.command = CommandLine(FilePath(path)); |
| 80 |
77 ListValue* switches = NULL; | 81 ListValue* switches = NULL; |
78 const char* kCustomSwitchesKey = "chrome.switches"; | 82 const char* kCustomSwitchesKey = "chrome.switches"; |
79 if (capabilities->GetListWithoutPathExpansion(kCustomSwitchesKey, | 83 if (capabilities->GetListWithoutPathExpansion(kCustomSwitchesKey, |
80 &switches)) { | 84 &switches)) { |
81 for (size_t i = 0; i < switches->GetSize(); ++i) { | 85 for (size_t i = 0; i < switches->GetSize(); ++i) { |
82 std::string switch_string; | 86 std::string switch_string; |
83 if (!switches->GetString(i, &switch_string)) { | 87 if (!switches->GetString(i, &switch_string)) { |
84 response->SetError(new Error( | 88 response->SetError(new Error( |
85 kBadRequest, "Custom switch is not a string")); | 89 kBadRequest, "Custom switch is not a string")); |
86 return; | 90 return; |
87 } | 91 } |
88 size_t separator_index = switch_string.find("="); | 92 size_t separator_index = switch_string.find("="); |
89 if (separator_index != std::string::npos) { | 93 if (separator_index != std::string::npos) { |
90 CommandLine::StringType switch_string_native; | 94 CommandLine::StringType switch_string_native; |
91 if (!switches->GetString(i, &switch_string_native)) { | 95 if (!switches->GetString(i, &switch_string_native)) { |
92 response->SetError(new Error( | 96 response->SetError(new Error( |
93 kBadRequest, "Custom switch is not a string")); | 97 kBadRequest, "Custom switch is not a string")); |
94 return; | 98 return; |
95 } | 99 } |
96 command_line_options.AppendSwitchNative( | 100 browser_options.command.AppendSwitchNative( |
97 switch_string.substr(0, separator_index), | 101 switch_string.substr(0, separator_index), |
98 switch_string_native.substr(separator_index + 1)); | 102 switch_string_native.substr(separator_index + 1)); |
99 } else { | 103 } else { |
100 command_line_options.AppendSwitch(switch_string); | 104 browser_options.command.AppendSwitch(switch_string); |
101 } | 105 } |
102 } | 106 } |
103 } else if (capabilities->HasKey(kCustomSwitchesKey)) { | 107 } else if (capabilities->HasKey(kCustomSwitchesKey)) { |
104 response->SetError(new Error( | 108 response->SetError(new Error( |
105 kBadRequest, "Custom switches must be a list")); | 109 kBadRequest, "Custom switches must be a list")); |
106 return; | 110 return; |
107 } | 111 } |
| 112 |
108 Value* verbose_value; | 113 Value* verbose_value; |
109 if (capabilities->GetWithoutPathExpansion("chrome.verbose", &verbose_value)) { | 114 if (capabilities->GetWithoutPathExpansion("chrome.verbose", &verbose_value)) { |
110 bool verbose; | 115 bool verbose = false; |
111 if (verbose_value->GetAsBoolean(&verbose) && verbose) { | 116 if (verbose_value->GetAsBoolean(&verbose)) { |
112 // Since logging is shared among sessions, if any session requests verbose | 117 // Since logging is shared among sessions, if any session requests verbose |
113 // logging, verbose logging will be enabled for all sessions. It is not | 118 // logging, verbose logging will be enabled for all sessions. It is not |
114 // possible to turn it off. | 119 // possible to turn it off. |
115 logging::SetMinLogLevel(logging::LOG_INFO); | 120 if (verbose) |
| 121 logging::SetMinLogLevel(logging::LOG_INFO); |
116 } else { | 122 } else { |
117 response->SetError(new Error( | 123 response->SetError(new Error( |
118 kBadRequest, "verbose must be a boolean true or false")); | 124 kBadRequest, "verbose must be a boolean true or false")); |
119 return; | 125 return; |
120 } | 126 } |
121 } | 127 } |
122 | 128 |
123 FilePath browser_exe; | 129 capabilities->GetStringWithoutPathExpansion( |
124 FilePath::StringType path; | 130 "chrome.channel", &browser_options.channel_id); |
125 if (capabilities->GetStringWithoutPathExpansion("chrome.binary", &path)) | |
126 browser_exe = FilePath(path); | |
127 | 131 |
128 ScopedTempDir temp_profile_dir; | 132 ScopedTempDir temp_profile_dir; |
129 FilePath temp_user_data_dir; | |
130 | |
131 std::string base64_profile; | 133 std::string base64_profile; |
132 if (capabilities->GetStringWithoutPathExpansion("chrome.profile", | 134 if (capabilities->GetStringWithoutPathExpansion("chrome.profile", |
133 &base64_profile)) { | 135 &base64_profile)) { |
134 if (!temp_profile_dir.CreateUniqueTempDir()) { | 136 if (!temp_profile_dir.CreateUniqueTempDir()) { |
135 response->SetError(new Error( | 137 response->SetError(new Error( |
136 kBadRequest, "Could not create temporary profile directory.")); | 138 kBadRequest, "Could not create temporary profile directory.")); |
137 return; | 139 return; |
138 } | 140 } |
139 FilePath temp_profile_zip( | 141 FilePath temp_profile_zip( |
140 temp_profile_dir.path().AppendASCII("profile.zip")); | 142 temp_profile_dir.path().AppendASCII("profile.zip")); |
141 std::string message; | 143 std::string message; |
142 if (!WriteBase64DataToFile(temp_profile_zip, base64_profile, &message)) { | 144 if (!WriteBase64DataToFile(temp_profile_zip, base64_profile, &message)) { |
143 response->SetError(new Error(kBadRequest, message)); | 145 response->SetError(new Error(kBadRequest, message)); |
144 return; | 146 return; |
145 } | 147 } |
146 | 148 |
147 temp_user_data_dir = temp_profile_dir.path().AppendASCII("user_data_dir"); | 149 browser_options.user_data_dir = |
148 if (!Unzip(temp_profile_zip, temp_user_data_dir)) { | 150 temp_profile_dir.path().AppendASCII("user_data_dir"); |
| 151 if (!Unzip(temp_profile_zip, browser_options.user_data_dir)) { |
149 response->SetError(new Error( | 152 response->SetError(new Error( |
150 kBadRequest, "Could not unarchive provided user profile")); | 153 kBadRequest, "Could not unarchive provided user profile")); |
151 return; | 154 return; |
152 } | 155 } |
153 } | 156 } |
154 | 157 |
155 const char* kExtensions = "chrome.extensions"; | 158 const char* kExtensions = "chrome.extensions"; |
156 ScopedTempDir extensions_dir; | 159 ScopedTempDir extensions_dir; |
157 ListValue* extensions_list = NULL; | 160 ListValue* extensions_list = NULL; |
158 std::vector<FilePath> extensions; | 161 std::vector<FilePath> extensions; |
(...skipping 20 matching lines...) Expand all Loading... |
179 return; | 182 return; |
180 } | 183 } |
181 extensions.push_back(extension_file); | 184 extensions.push_back(extension_file); |
182 } | 185 } |
183 } else if (capabilities->HasKey(kExtensions)) { | 186 } else if (capabilities->HasKey(kExtensions)) { |
184 response->SetError(new Error( | 187 response->SetError(new Error( |
185 kBadRequest, "Extensions must be a list of base64 encoded strings")); | 188 kBadRequest, "Extensions must be a list of base64 encoded strings")); |
186 return; | 189 return; |
187 } | 190 } |
188 | 191 |
189 Session::Options options; | 192 Session::Options session_options; |
190 Error* error = NULL; | 193 Error* error = NULL; |
191 error = GetBooleanCapability(capabilities, "chrome.nativeEvents", | 194 error = GetBooleanCapability(capabilities, "chrome.nativeEvents", |
192 &options.use_native_events); | 195 &session_options.use_native_events); |
193 if (!error) { | 196 if (!error) { |
194 error = GetBooleanCapability(capabilities, "chrome.loadAsync", | 197 error = GetBooleanCapability(capabilities, "chrome.loadAsync", |
195 &options.load_async); | 198 &session_options.load_async); |
196 } | 199 } |
197 if (error) { | 200 if (error) { |
198 response->SetError(error); | 201 response->SetError(error); |
199 return; | 202 return; |
200 } | 203 } |
201 | 204 |
202 // Session manages its own liftime, so do not call delete. | 205 // Session manages its own liftime, so do not call delete. |
203 Session* session = new Session(options); | 206 Session* session = new Session(session_options); |
204 error = session->Init(browser_exe, | 207 error = session->Init(browser_options); |
205 temp_user_data_dir, | |
206 command_line_options); | |
207 if (error) { | 208 if (error) { |
208 response->SetError(error); | 209 response->SetError(error); |
209 return; | 210 return; |
210 } | 211 } |
211 | 212 |
212 // Install extensions. | 213 // Install extensions. |
213 for (size_t i = 0; i < extensions.size(); ++i) { | 214 for (size_t i = 0; i < extensions.size(); ++i) { |
214 Error* error = session->InstallExtension(extensions[i]); | 215 Error* error = session->InstallExtension(extensions[i]); |
215 if (error) { | 216 if (error) { |
216 response->SetError(error); | 217 response->SetError(error); |
217 return; | 218 return; |
218 } | 219 } |
219 } | 220 } |
220 | 221 |
221 LOG(INFO) << "Created session " << session->id(); | 222 LOG(INFO) << "Created session " << session->id(); |
222 // Redirect to a relative URI. Although prohibited by the HTTP standard, | 223 // Redirect to a relative URI. Although prohibited by the HTTP standard, |
223 // this is what the IEDriver does. Finding the actual IP address is | 224 // this is what the IEDriver does. Finding the actual IP address is |
224 // difficult, and returning the hostname causes perf problems with the python | 225 // difficult, and returning the hostname causes perf problems with the python |
225 // bindings on Windows. | 226 // bindings on Windows. |
226 std::ostringstream stream; | 227 std::ostringstream stream; |
227 stream << SessionManager::GetInstance()->url_base() << "/session/" | 228 stream << SessionManager::GetInstance()->url_base() << "/session/" |
228 << session->id(); | 229 << session->id(); |
229 response->SetStatus(kSeeOther); | 230 response->SetStatus(kSeeOther); |
230 response->SetValue(Value::CreateStringValue(stream.str())); | 231 response->SetValue(Value::CreateStringValue(stream.str())); |
231 } | 232 } |
232 | 233 |
233 } // namespace webdriver | 234 } // namespace webdriver |
OLD | NEW |