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" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/scoped_temp_dir.h" | 15 #include "base/scoped_temp_dir.h" |
16 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
17 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
18 #include "base/values.h" | 18 #include "base/values.h" |
19 #include "chrome/app/chrome_command_ids.h" | 19 #include "chrome/app/chrome_command_ids.h" |
20 #include "chrome/common/chrome_constants.h" | 20 #include "chrome/common/chrome_constants.h" |
21 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
22 #include "chrome/common/zip.h" | 22 #include "chrome/common/zip.h" |
23 #include "chrome/test/webdriver/commands/response.h" | 23 #include "chrome/test/webdriver/commands/response.h" |
24 #include "chrome/test/webdriver/session.h" | 24 #include "chrome/test/webdriver/session.h" |
25 #include "chrome/test/webdriver/session_manager.h" | 25 #include "chrome/test/webdriver/session_manager.h" |
26 #include "chrome/test/webdriver/webdriver_error.h" | 26 #include "chrome/test/webdriver/webdriver_error.h" |
27 | 27 |
| 28 namespace webdriver { |
| 29 |
28 namespace { | 30 namespace { |
29 | 31 |
30 bool WriteBase64DataToFile(const FilePath& filename, | 32 bool WriteBase64DataToFile(const FilePath& filename, |
31 const std::string& base64data, | 33 const std::string& base64data, |
32 std::string* error_msg) { | 34 std::string* error_msg) { |
33 std::string data; | 35 std::string data; |
34 if (!base::Base64Decode(base64data, &data)) { | 36 if (!base::Base64Decode(base64data, &data)) { |
35 *error_msg = "Invalid base64 encoded data."; | 37 *error_msg = "Invalid base64 encoded data."; |
36 return false; | 38 return false; |
37 } | 39 } |
38 if (!file_util::WriteFile(filename, data.c_str(), data.length())) { | 40 if (!file_util::WriteFile(filename, data.c_str(), data.length())) { |
39 *error_msg = "Could not write data to file."; | 41 *error_msg = "Could not write data to file."; |
40 return false; | 42 return false; |
41 } | 43 } |
42 return true; | 44 return true; |
43 } | 45 } |
44 | 46 |
| 47 Error* GetBooleanCapability( |
| 48 const base::DictionaryValue* dict, const std::string& key, bool* option) { |
| 49 Value* value = NULL; |
| 50 if (dict->GetWithoutPathExpansion(key, &value)) { |
| 51 if (!value->GetAsBoolean(option)) { |
| 52 return new Error(kUnknownError, key + " must be a boolean"); |
| 53 } |
| 54 } |
| 55 return NULL; |
| 56 } |
| 57 |
45 } // namespace | 58 } // namespace |
46 | 59 |
47 namespace webdriver { | |
48 | |
49 CreateSession::CreateSession(const std::vector<std::string>& path_segments, | 60 CreateSession::CreateSession(const std::vector<std::string>& path_segments, |
50 const DictionaryValue* const parameters) | 61 const DictionaryValue* const parameters) |
51 : Command(path_segments, parameters) {} | 62 : Command(path_segments, parameters) {} |
52 | 63 |
53 CreateSession::~CreateSession() {} | 64 CreateSession::~CreateSession() {} |
54 | 65 |
55 bool CreateSession::DoesPost() { return true; } | 66 bool CreateSession::DoesPost() { return true; } |
56 | 67 |
57 void CreateSession::ExecutePost(Response* const response) { | 68 void CreateSession::ExecutePost(Response* const response) { |
58 DictionaryValue *capabilities = NULL; | 69 DictionaryValue *capabilities = NULL; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 return; | 179 return; |
169 } | 180 } |
170 extensions.push_back(extension_file); | 181 extensions.push_back(extension_file); |
171 } | 182 } |
172 } else if (capabilities->HasKey(kExtensions)) { | 183 } else if (capabilities->HasKey(kExtensions)) { |
173 response->SetError(new Error( | 184 response->SetError(new Error( |
174 kBadRequest, "Extensions must be a list of base64 encoded strings")); | 185 kBadRequest, "Extensions must be a list of base64 encoded strings")); |
175 return; | 186 return; |
176 } | 187 } |
177 | 188 |
| 189 Session::Options options; |
| 190 Error* error = NULL; |
| 191 error = GetBooleanCapability(capabilities, "chrome.nativeEvents", |
| 192 &options.use_native_events); |
| 193 if (!error) { |
| 194 error = GetBooleanCapability(capabilities, "chrome.loadAsync", |
| 195 &options.load_async); |
| 196 } |
| 197 if (error) { |
| 198 response->SetError(error); |
| 199 return; |
| 200 } |
| 201 |
178 // Session manages its own liftime, so do not call delete. | 202 // Session manages its own liftime, so do not call delete. |
179 Session* session = new Session(); | 203 Session* session = new Session(options); |
180 Error* error = session->Init(browser_exe, | 204 error = session->Init(browser_exe, |
181 temp_user_data_dir, | 205 temp_user_data_dir, |
182 command_line_options); | 206 command_line_options); |
183 if (error) { | 207 if (error) { |
184 response->SetError(error); | 208 response->SetError(error); |
185 return; | 209 return; |
186 } | 210 } |
187 | 211 |
188 // Install extensions. | 212 // Install extensions. |
189 for (size_t i = 0; i < extensions.size(); ++i) { | 213 for (size_t i = 0; i < extensions.size(); ++i) { |
190 Error* error = session->InstallExtension(extensions[i]); | 214 Error* error = session->InstallExtension(extensions[i]); |
191 if (error) { | 215 if (error) { |
192 response->SetError(error); | 216 response->SetError(error); |
193 return; | 217 return; |
194 } | 218 } |
195 } | 219 } |
196 | 220 |
197 bool native_events_required = false; | |
198 Value* native_events_value = NULL; | |
199 if (capabilities->GetWithoutPathExpansion( | |
200 "chrome.nativeEvents", &native_events_value)) { | |
201 if (native_events_value->GetAsBoolean(&native_events_required)) { | |
202 session->set_use_native_events(native_events_required); | |
203 } | |
204 } | |
205 bool screenshot_on_error = false; | |
206 if (capabilities->GetBoolean( | |
207 "takeScreenshotOnError", &screenshot_on_error)) { | |
208 session->set_screenshot_on_error(screenshot_on_error); | |
209 } | |
210 | |
211 LOG(INFO) << "Created session " << session->id(); | 221 LOG(INFO) << "Created session " << session->id(); |
212 // Redirect to a relative URI. Although prohibited by the HTTP standard, | 222 // Redirect to a relative URI. Although prohibited by the HTTP standard, |
213 // this is what the IEDriver does. Finding the actual IP address is | 223 // this is what the IEDriver does. Finding the actual IP address is |
214 // difficult, and returning the hostname causes perf problems with the python | 224 // difficult, and returning the hostname causes perf problems with the python |
215 // bindings on Windows. | 225 // bindings on Windows. |
216 std::ostringstream stream; | 226 std::ostringstream stream; |
217 stream << SessionManager::GetInstance()->url_base() << "/session/" | 227 stream << SessionManager::GetInstance()->url_base() << "/session/" |
218 << session->id(); | 228 << session->id(); |
219 response->SetStatus(kSeeOther); | 229 response->SetStatus(kSeeOther); |
220 response->SetValue(Value::CreateStringValue(stream.str())); | 230 response->SetValue(Value::CreateStringValue(stream.str())); |
221 } | 231 } |
222 | 232 |
223 } // namespace webdriver | 233 } // namespace webdriver |
OLD | NEW |