Index: chrome/test/webdriver/commands/cookie_commands.cc |
diff --git a/chrome/test/webdriver/commands/cookie_commands.cc b/chrome/test/webdriver/commands/cookie_commands.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..13c46682403ee7c73972b489d136a4a766ccf7a8 |
--- /dev/null |
+++ b/chrome/test/webdriver/commands/cookie_commands.cc |
@@ -0,0 +1,188 @@ |
+// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/test/webdriver/commands/cookie_commands.h" |
+#include "chrome/test/webdriver/cookie.h" |
+ |
+#include <string> |
+#include <vector> |
+ |
+namespace webdriver { |
+ |
+void tokenize(const std::string& s, const char& delimiter, |
John Grabowski
2011/02/04 02:40:23
seems like something to put in an anonymous namesp
kkania
2011/02/04 17:04:44
hmm, looks like you forgot to fix this on the firs
Joe
2011/02/09 06:32:10
Done.
Joe
2011/02/09 06:32:10
Done.
|
+ std::vector<std::string>& tokens) { |
+ size_t start = 0; |
+ size_t end = s.find(delimiter); |
+ |
John Grabowski
2011/02/04 02:40:23
Hmm. Might it be better to create a static method
Joe
2011/02/09 06:32:10
Done.
|
+ while (end != std::string::npos) { |
+ if (end - start > 0) { |
+ tokens.push_back(s.substr(start, end - start)); |
+ } |
+ start = end + 1; |
+ end = s.find(delimiter, start); |
+ } |
+ |
+ if ((s.length() - start) > 0) { |
+ tokens.push_back(s.substr(start, s.length())); |
+ } |
+} |
+ |
+bool CookieCommand::Init(Response* const response) { |
+ if (WebDriverCommand::Init(response)) { |
+ if (tab_->GetCurrentURL(¤t_url_)) { |
+ return true; |
+ } |
+ SET_WEBDRIVER_ERROR(response, "Failed to query current page URL", |
+ kInternalServerError); |
+ return false; |
+ } |
+ |
+ SET_WEBDRIVER_ERROR(response, "Failed to init Cookie Command", |
+ kInternalServerError); |
+ return false; |
+} |
+ |
+void CookieCommand::ExecuteGet(Response* const response) { |
+ // TODO(JMikhail): Add GetJSONCookies to automation proxy since |
+ // GetCookies does not return the necessary information |
+ std::string cookies; |
+ std::vector<std::string> tokens; |
+ |
+ if (!tab_->GetCookies(current_url_, &cookies)) { |
+ response->set_status(kUnknownError); |
+ response->set_value(new StringValue("Failed to fetch cookies")); |
+ return; |
+ } |
+ |
+ tokenize(cookies, ';', tokens); |
+ ListValue* l = new ListValue(); |
+ for (std::vector<std::string>::iterator i = tokens.begin(); |
+ i != tokens.end(); ++i) { |
+ Cookie cookie(*i); |
+ if (cookie.IsValid()) { |
+ l->Append(cookie.ToJSON()); |
+ } else { |
+ LOG(ERROR) << "Failed to parse cookie: " << *i; |
+ } |
+ } |
+ |
+ response->set_status(kSuccess); |
+ response->set_value(l); |
+} |
+ |
+void CookieCommand::ExecutePost(Response* const response) { |
+ std::string cookie_string; |
+ DictionaryValue* cookie_dict; |
+ scoped_ptr<Cookie> cookie; |
+ |
+ // Check to see if the cookie was passed in as a string. |
+ if (GetStringASCIIParameter("cookie", &cookie_string)) { |
+ cookie.reset(new Cookie(cookie_string)); |
+ } else { |
+ // Check to see if the cookie was passed in as a JSON onject. |
+ if (GetDictionaryParameter("cookie", &cookie_dict)) { |
+ cookie.reset(new Cookie(*cookie_dict)); |
+ } else { |
+ // No valid cookie sent. |
+ response->set_value(new StringValue("Cookie key not found")); |
+ response->set_status(kBadRequest); |
+ return; |
+ } |
+ } |
+ |
+ // Make sure the cookie is formated preoperly. |
+ if (!cookie->IsValid()) { |
+ response->set_value(new StringValue("Invalid cookie")); |
+ response->set_status(kBadRequest); |
+ return; |
+ } |
+ |
+ if (!tab_->SetCookie(current_url_, cookie->ToString())) { |
+ response->set_status(kUnableToSetCookie); |
+ response->set_value(new StringValue("Failed to set cookie")); |
+ return; |
+ } |
+ |
+ response->set_status(kSuccess); |
+ response->set_value(new StringValue(cookie->ToString())); |
+} |
+ |
+void CookieCommand::ExecuteDelete(Response* const response) { |
+ std::string cookies; |
+ std::vector<std::string> tokens; |
+ |
+ if (!tab_->GetCookies(current_url_, &cookies)) { |
+ response->set_status(kUnknownError); |
+ response->set_value(new StringValue("Failed to fetch cookies")); |
+ return; |
+ } |
+ |
+ tokenize(cookies, ':', tokens); |
John Grabowski
2011/02/04 02:40:23
On line 58 you tokenize cookes on ;.
Here you toke
|
+ for (std::vector<std::string>::iterator i = tokens.begin(); |
+ i != tokens.end(); ++i) { |
+ Cookie cookie(*i); |
+ if (cookie.IsValid()) { |
+ if (!tab_->DeleteCookie(current_url_, cookie.get_name())) { |
+ LOG(INFO) << "Could not delete cookie: " << cookie.get_name() << "\n" |
+ << "Contents of cookie: " << cookie.ToString(); |
+ SET_WEBDRIVER_ERROR(response, "Could not delete all cookies", |
+ kInternalServerError); |
+ return; |
+ } |
+ } else { |
+ LOG(ERROR) << "Failed to parse cookie: " << *i; |
+ } |
+ } |
+ |
+ response->set_status(kSuccess); |
+ response->set_value(new StringValue("Cookies delete")); |
+} |
+ |
+bool NamedCookieCommand::Init(Response* const response) { |
+ if (WebDriverCommand::Init(response)) { |
+ if (!tab_->GetCurrentURL(¤t_url_)) { |
+ SET_WEBDRIVER_ERROR(response, "Failed to query current page URL", |
+ kInternalServerError); |
+ return false; |
+ } |
+ |
+ // There should be at least 5 segments to match |
+ // /session/:sessionId/cookie/:name |
+ cookie_name_ = GetPathVariable(4); |
+ SET_WEBDRIVER_ERROR(response, "No cookie name specified", |
+ kBadRequest); |
+ return false; |
+ } |
+ |
+ SET_WEBDRIVER_ERROR(response, "Failed to init Named Cookie Command", |
+ kInternalServerError); |
+ return false; |
+} |
+ |
+void NamedCookieCommand::ExecuteGet(Response* const response) { |
+ std::string cookie; |
+ |
+ if (!tab_->GetCookieByName(current_url_, cookie_name_, &cookie)) { |
+ response->set_status(kUnknownError); |
+ response->set_value(new StringValue("Failed to fetch cookie")); |
+ return; |
+ } |
+ |
+ response->set_status(kSuccess); |
+ response->set_value(new StringValue(cookie)); |
+} |
+ |
+void NamedCookieCommand::ExecuteDelete(Response* const response) { |
+ if (!tab_->DeleteCookie(current_url_, cookie_name_)) { |
+ response->set_status(kUnknownError); |
+ response->set_value(new StringValue("Failed to delete cookie")); |
+ return; |
+ } |
+ |
+ response->set_status(kSuccess); |
+ response->set_value(new StringValue("Cookie deleted")); |
+} |
+ |
+} // namespace webdriver |
+ |