Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(439)

Side by Side Diff: chrome/test/webdriver/commands/create_session.cc

Issue 8341044: Enhance and refactor ChromeDriver's capability handling. Log warning for (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ... Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/chrome_tests.gypi ('k') | chrome/test/webdriver/commands/session_with_id.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
8 #include <string> 7 #include <string>
9 8
10 #include "base/base64.h"
11 #include "base/command_line.h" 9 #include "base/command_line.h"
12 #include "base/file_path.h" 10 #include "base/file_path.h"
13 #include "base/file_util.h"
14 #include "base/logging.h" 11 #include "base/logging.h"
15 #include "base/scoped_temp_dir.h" 12 #include "base/scoped_temp_dir.h"
16 #include "base/string_number_conversions.h"
17 #include "base/stringprintf.h"
18 #include "base/values.h" 13 #include "base/values.h"
19 #include "chrome/app/chrome_command_ids.h"
20 #include "chrome/common/chrome_constants.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/zip.h"
23 #include "chrome/test/webdriver/commands/response.h" 14 #include "chrome/test/webdriver/commands/response.h"
15 #include "chrome/test/webdriver/webdriver_capabilities_parser.h"
24 #include "chrome/test/webdriver/webdriver_error.h" 16 #include "chrome/test/webdriver/webdriver_error.h"
25 #include "chrome/test/webdriver/webdriver_session.h" 17 #include "chrome/test/webdriver/webdriver_session.h"
26 #include "chrome/test/webdriver/webdriver_session_manager.h" 18 #include "chrome/test/webdriver/webdriver_session_manager.h"
27 19
28 namespace webdriver { 20 namespace webdriver {
29 21
30 namespace {
31
32 bool WriteBase64DataToFile(const FilePath& filename,
33 const std::string& base64data,
34 std::string* error_msg) {
35 std::string data;
36 if (!base::Base64Decode(base64data, &data)) {
37 *error_msg = "Invalid base64 encoded data.";
38 return false;
39 }
40 if (!file_util::WriteFile(filename, data.c_str(), data.length())) {
41 *error_msg = "Could not write data to file.";
42 return false;
43 }
44 return true;
45 }
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
58 } // namespace
59
60 CreateSession::CreateSession(const std::vector<std::string>& path_segments, 22 CreateSession::CreateSession(const std::vector<std::string>& path_segments,
61 const DictionaryValue* const parameters) 23 const DictionaryValue* const parameters)
62 : Command(path_segments, parameters) {} 24 : Command(path_segments, parameters) {}
63 25
64 CreateSession::~CreateSession() {} 26 CreateSession::~CreateSession() {}
65 27
66 bool CreateSession::DoesPost() { return true; } 28 bool CreateSession::DoesPost() { return true; }
67 29
68 void CreateSession::ExecutePost(Response* const response) { 30 void CreateSession::ExecutePost(Response* const response) {
69 DictionaryValue *capabilities = NULL; 31 DictionaryValue* dict;
70 if (!GetDictionaryParameter("desiredCapabilities", &capabilities)) { 32 if (!GetDictionaryParameter("desiredCapabilities", &dict)) {
71 response->SetError(new Error( 33 response->SetError(new Error(
72 kBadRequest, "Missing or invalid 'desiredCapabilities'")); 34 kBadRequest, "Missing or invalid 'desiredCapabilities'"));
73 return; 35 return;
74 } 36 }
75 37 ScopedTempDir temp_dir;
76 Automation::BrowserOptions browser_options; 38 if (!temp_dir.CreateUniqueTempDir()) {
77 FilePath::StringType path;
78 if (capabilities->GetStringWithoutPathExpansion("chrome.binary", &path))
79 browser_options.command = CommandLine(FilePath(path));
80
81 ListValue* switches = NULL;
82 const char* kCustomSwitchesKey = "chrome.switches";
83 if (capabilities->GetListWithoutPathExpansion(kCustomSwitchesKey,
84 &switches)) {
85 for (size_t i = 0; i < switches->GetSize(); ++i) {
86 std::string switch_string;
87 if (!switches->GetString(i, &switch_string)) {
88 response->SetError(new Error(
89 kBadRequest, "Custom switch is not a string"));
90 return;
91 }
92 size_t separator_index = switch_string.find("=");
93 if (separator_index != std::string::npos) {
94 CommandLine::StringType switch_string_native;
95 if (!switches->GetString(i, &switch_string_native)) {
96 response->SetError(new Error(
97 kBadRequest, "Custom switch is not a string"));
98 return;
99 }
100 browser_options.command.AppendSwitchNative(
101 switch_string.substr(0, separator_index),
102 switch_string_native.substr(separator_index + 1));
103 } else {
104 browser_options.command.AppendSwitch(switch_string);
105 }
106 }
107 } else if (capabilities->HasKey(kCustomSwitchesKey)) {
108 response->SetError(new Error( 39 response->SetError(new Error(
109 kBadRequest, "Custom switches must be a list")); 40 kUnknownError, "Unable to create temp directory for unpacking"));
110 return; 41 return;
111 } 42 }
112 43 Capabilities caps;
113 Value* verbose_value; 44 CapabilitiesParser parser(dict, temp_dir.path(), &caps);
114 if (capabilities->GetWithoutPathExpansion("chrome.verbose", &verbose_value)) { 45 Error* error = parser.Parse();
115 bool verbose = false;
116 if (verbose_value->GetAsBoolean(&verbose)) {
117 // Since logging is shared among sessions, if any session requests verbose
118 // logging, verbose logging will be enabled for all sessions. It is not
119 // possible to turn it off.
120 if (verbose)
121 logging::SetMinLogLevel(logging::LOG_INFO);
122 } else {
123 response->SetError(new Error(
124 kBadRequest, "verbose must be a boolean true or false"));
125 return;
126 }
127 }
128
129 capabilities->GetStringWithoutPathExpansion(
130 "chrome.channel", &browser_options.channel_id);
131
132 ScopedTempDir temp_profile_dir;
133 std::string base64_profile;
134 if (capabilities->GetStringWithoutPathExpansion("chrome.profile",
135 &base64_profile)) {
136 if (!temp_profile_dir.CreateUniqueTempDir()) {
137 response->SetError(new Error(
138 kBadRequest, "Could not create temporary profile directory."));
139 return;
140 }
141 FilePath temp_profile_zip(
142 temp_profile_dir.path().AppendASCII("profile.zip"));
143 std::string message;
144 if (!WriteBase64DataToFile(temp_profile_zip, base64_profile, &message)) {
145 response->SetError(new Error(kBadRequest, message));
146 return;
147 }
148
149 browser_options.user_data_dir =
150 temp_profile_dir.path().AppendASCII("user_data_dir");
151 if (!zip::Unzip(temp_profile_zip, browser_options.user_data_dir)) {
152 response->SetError(new Error(
153 kBadRequest, "Could not unarchive provided user profile"));
154 return;
155 }
156 }
157
158 const char* kExtensions = "chrome.extensions";
159 ScopedTempDir extensions_dir;
160 ListValue* extensions_list = NULL;
161 std::vector<FilePath> extensions;
162 if (capabilities->GetListWithoutPathExpansion(kExtensions,
163 &extensions_list)) {
164 if (!extensions_dir.CreateUniqueTempDir()) {
165 response->SetError(new Error(
166 kBadRequest, "Could create temporary extensions directory."));
167 return;
168 }
169 for (size_t i = 0; i < extensions_list->GetSize(); ++i) {
170 std::string base64_extension;
171 if (!extensions_list->GetString(i, &base64_extension)) {
172 response->SetError(new Error(
173 kBadRequest, "Extension must be a base64 encoded string."));
174 return;
175 }
176 FilePath extension_file(
177 extensions_dir.path().AppendASCII("extension" +
178 base::IntToString(i) + ".crx"));
179 std::string message;
180 if (!WriteBase64DataToFile(extension_file, base64_extension, &message)) {
181 response->SetError(new Error(kBadRequest, message));
182 return;
183 }
184 extensions.push_back(extension_file);
185 }
186 } else if (capabilities->HasKey(kExtensions)) {
187 response->SetError(new Error(
188 kBadRequest, "Extensions must be a list of base64 encoded strings"));
189 return;
190 }
191
192 Session::Options session_options;
193 Error* error = NULL;
194 error = GetBooleanCapability(capabilities, "chrome.nativeEvents",
195 &session_options.use_native_events);
196 if (!error) {
197 error = GetBooleanCapability(capabilities, "chrome.loadAsync",
198 &session_options.load_async);
199 }
200 if (!error) {
201 error = GetBooleanCapability(capabilities, "chrome.detach",
202 &browser_options.detach_process);
203 }
204 if (error) { 46 if (error) {
205 response->SetError(error); 47 response->SetError(error);
206 return; 48 return;
207 } 49 }
208 50
51 // Since logging is shared among sessions, if any session requests verbose
52 // logging, verbose logging will be enabled for all sessions. It is not
53 // possible to turn it off.
54 if (caps.verbose)
55 logging::SetMinLogLevel(logging::LOG_INFO);
56
57 Session::Options session_options;
58 session_options.load_async = caps.load_async;
59 session_options.use_native_events = caps.native_events;
60
61 Automation::BrowserOptions browser_options;
62 browser_options.command = caps.command;
63 browser_options.channel_id = caps.channel;
64 browser_options.detach_process = caps.detach;
65 browser_options.user_data_dir = caps.profile;
66
209 // Session manages its own liftime, so do not call delete. 67 // Session manages its own liftime, so do not call delete.
210 Session* session = new Session(session_options); 68 Session* session = new Session(session_options);
211 error = session->Init(browser_options); 69 error = session->Init(browser_options);
212 if (error) { 70 if (error) {
213 response->SetError(error); 71 response->SetError(error);
214 return; 72 return;
215 } 73 }
216 74
217 // Install extensions. 75 // Install extensions.
218 for (size_t i = 0; i < extensions.size(); ++i) { 76 for (size_t i = 0; i < caps.extensions.size(); ++i) {
219 Error* error = session->InstallExtension(extensions[i]); 77 Error* error = session->InstallExtension(caps.extensions[i]);
220 if (error) { 78 if (error) {
221 response->SetError(error); 79 response->SetError(error);
222 return; 80 return;
223 } 81 }
224 } 82 }
225 83
226 LOG(INFO) << "Created session " << session->id(); 84 LOG(INFO) << "Created session " << session->id();
227 // Redirect to a relative URI. Although prohibited by the HTTP standard, 85 // Redirect to a relative URI. Although prohibited by the HTTP standard,
228 // this is what the IEDriver does. Finding the actual IP address is 86 // this is what the IEDriver does. Finding the actual IP address is
229 // difficult, and returning the hostname causes perf problems with the python 87 // difficult, and returning the hostname causes perf problems with the python
230 // bindings on Windows. 88 // bindings on Windows.
231 std::ostringstream stream; 89 std::ostringstream stream;
232 stream << SessionManager::GetInstance()->url_base() << "/session/" 90 stream << SessionManager::GetInstance()->url_base() << "/session/"
233 << session->id(); 91 << session->id();
234 response->SetStatus(kSeeOther); 92 response->SetStatus(kSeeOther);
235 response->SetValue(Value::CreateStringValue(stream.str())); 93 response->SetValue(Value::CreateStringValue(stream.str()));
236 } 94 }
237 95
238 } // namespace webdriver 96 } // namespace webdriver
OLDNEW
« no previous file with comments | « chrome/chrome_tests.gypi ('k') | chrome/test/webdriver/commands/session_with_id.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698