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/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 CreateSession::CreateSession(const std::vector<std::string>& path_segments, | 23 CreateSession::CreateSession(const std::vector<std::string>& path_segments, |
24 const DictionaryValue* const parameters) | 24 const DictionaryValue* const parameters) |
25 : Command(path_segments, parameters) {} | 25 : Command(path_segments, parameters) {} |
26 | 26 |
27 CreateSession::~CreateSession() {} | 27 CreateSession::~CreateSession() {} |
28 | 28 |
29 bool CreateSession::DoesPost() { return true; } | 29 bool CreateSession::DoesPost() { return true; } |
30 | 30 |
31 void CreateSession::ExecutePost(Response* const response) { | 31 void CreateSession::ExecutePost(Response* const response) { |
32 DictionaryValue *added_options = NULL, *desiredCapabilities = NULL; | 32 DictionaryValue *capabilities = NULL; |
33 CommandLine options(CommandLine::NO_PROGRAM); | 33 if (!GetDictionaryParameter("desiredCapabilities", &capabilities)) { |
34 FilePath user_browser_exe; | 34 response->SetError(new Error( |
35 | 35 kBadRequest, "Missing or invalid 'desiredCapabilities'")); |
36 if (!GetDictionaryParameter("desiredCapabilities", &desiredCapabilities)) { | 36 return; |
37 desiredCapabilities = NULL; | |
38 } | 37 } |
39 | 38 |
40 if ((desiredCapabilities != NULL) && | 39 CommandLine command_line_options(CommandLine::NO_PROGRAM); |
41 desiredCapabilities->GetDictionary("chrome.customSwitches", | 40 ListValue* switches = NULL; |
42 &added_options)) { | 41 const char* kCustomSwitchesKey = "chrome.switches"; |
43 DictionaryValue::key_iterator i = added_options->begin_keys(); | 42 if (capabilities->GetListWithoutPathExpansion(kCustomSwitchesKey, |
44 while (i != added_options->end_keys()) { | 43 &switches)) { |
45 FilePath::StringType value; | 44 for (size_t i = 0; i < switches->GetSize(); ++i) { |
46 if (!added_options->GetString(*i, &value)) { | 45 std::string switch_string; |
| 46 if (!switches->GetString(i, &switch_string)) { |
47 response->SetError(new Error( | 47 response->SetError(new Error( |
48 kBadRequest, "Invalid format for added options to browser")); | 48 kBadRequest, "Custom switch is not a string")); |
49 return; | 49 return; |
| 50 } |
| 51 size_t separator_index = switch_string.find("="); |
| 52 if (separator_index != std::string::npos) { |
| 53 CommandLine::StringType switch_string_native; |
| 54 if (!switches->GetString(i, &switch_string_native)) { |
| 55 response->SetError(new Error( |
| 56 kBadRequest, "Custom switch is not a string")); |
| 57 return; |
| 58 } |
| 59 command_line_options.AppendSwitchNative( |
| 60 switch_string.substr(0, separator_index), |
| 61 switch_string_native.substr(separator_index + 1)); |
50 } else { | 62 } else { |
51 if (!value.empty()) { | 63 command_line_options.AppendSwitch(switch_string); |
52 options.AppendSwitchNative(*i, value); | |
53 } else { | |
54 options.AppendSwitch(*i); | |
55 } | |
56 } | 64 } |
57 ++i; | |
58 } | 65 } |
59 FilePath::StringType path; | 66 } else if (capabilities->HasKey(kCustomSwitchesKey)) { |
60 desiredCapabilities->GetString("chrome.binary", &path); | 67 response->SetError(new Error( |
61 user_browser_exe = FilePath(path); | 68 kBadRequest, "Custom switches must be a list")); |
| 69 return; |
62 } | 70 } |
63 | 71 |
| 72 FilePath browser_exe; |
| 73 FilePath::StringType path; |
| 74 if (capabilities->GetStringWithoutPathExpansion("chrome.binary", &path)) |
| 75 browser_exe = FilePath(path); |
| 76 |
64 // Session manages its own liftime, so do not call delete. | 77 // Session manages its own liftime, so do not call delete. |
65 Session* session = new Session(); | 78 Session* session = new Session(); |
66 Error* error = session->Init(user_browser_exe, options); | 79 Error* error = session->Init(browser_exe, command_line_options); |
67 if (error) { | 80 if (error) { |
68 response->SetError(error); | 81 response->SetError(error); |
69 return; | 82 return; |
70 } | 83 } |
71 | 84 |
72 if (desiredCapabilities != NULL) { | 85 bool native_events_required = false; |
73 bool native_events_required = false; | 86 Value* native_events_value = NULL; |
74 if (desiredCapabilities->GetBoolean("chrome.nativeEvents", | 87 if (capabilities->GetWithoutPathExpansion( |
75 &native_events_required)) { | 88 "chrome.nativeEvents", &native_events_value)) { |
| 89 if (native_events_value->GetAsBoolean(&native_events_required)) { |
76 session->set_use_native_events(native_events_required); | 90 session->set_use_native_events(native_events_required); |
77 } | 91 } |
78 | 92 } |
79 bool screenshot_on_error = false; | 93 bool screenshot_on_error = false; |
80 if (desiredCapabilities->GetBoolean("takeScreenshotOnError", | 94 if (capabilities->GetBoolean( |
81 &screenshot_on_error)) { | 95 "takeScreenshotOnError", &screenshot_on_error)) { |
82 session->set_screenshot_on_error(screenshot_on_error); | 96 session->set_screenshot_on_error(screenshot_on_error); |
83 } | |
84 } | 97 } |
85 | 98 |
86 VLOG(1) << "Created session " << session->id(); | 99 VLOG(1) << "Created session " << session->id(); |
87 std::ostringstream stream; | 100 std::ostringstream stream; |
88 SessionManager* session_manager = SessionManager::GetInstance(); | 101 SessionManager* session_manager = SessionManager::GetInstance(); |
89 stream << "http://" << session_manager->GetAddress() << "/session/" | 102 stream << "http://" << session_manager->GetAddress() << "/session/" |
90 << session->id(); | 103 << session->id(); |
91 response->SetStatus(kSeeOther); | 104 response->SetStatus(kSeeOther); |
92 response->SetValue(Value::CreateStringValue(stream.str())); | 105 response->SetValue(Value::CreateStringValue(stream.str())); |
93 } | 106 } |
94 | 107 |
95 } // namespace webdriver | 108 } // namespace webdriver |
OLD | NEW |