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..8021fa078658daf906dc5ce750c1c3917236f42f |
--- /dev/null |
+++ b/chrome/test/webdriver/commands/cookie_commands.cc |
@@ -0,0 +1,175 @@ |
+// 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 <string> |
+#include <vector> |
+ |
+#include "base/scoped_ptr.h" |
+#include "base/string_util.h" |
+#include "base/values.h" |
+#include "chrome/test/webdriver/cookie.h" |
+#include "chrome/test/webdriver/session.h" |
+#include "chrome/test/webdriver/session_manager.h" |
+#include "chrome/test/webdriver/commands/response.h" |
+ |
+namespace webdriver { |
+ |
+bool CookieCommand::Init(Response* const response) { |
+ if (WebDriverCommand::Init(response)) { |
+ if (session_->GetURL(¤t_url_)) { |
+ return true; |
+ } |
+ SET_WEBDRIVER_ERROR(response, "Failed to query current page URL", |
+ kInternalServerError); |
+ return false; |
+ } |
+ |
+ 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 (!session_->GetCookies(current_url_, &cookies)) { |
+ SET_WEBDRIVER_ERROR(response, "Failed to fetch cookies", |
+ kUnknownError); |
+ return; |
+ } |
+ |
+ // Note that ';' separates cookies while ':' separates name/value pairs. |
+ Tokenize(cookies, ";", &tokens); |
+ scoped_ptr<ListValue> cookie_list(new ListValue()); |
+ for (std::vector<std::string>::iterator i = tokens.begin(); |
+ i != tokens.end(); ++i) { |
+ Cookie cookie(*i); |
+ if (cookie.valid()) { |
+ cookie_list->Append(cookie.ToDictionary()); |
+ } else { |
+ LOG(ERROR) << "Failed to parse cookie: " << *i; |
+ SET_WEBDRIVER_ERROR(response, "Could not get all cookies", |
+ kInternalServerError); |
+ return; |
+ } |
+ } |
+ |
+ response->set_status(kSuccess); |
+ response->set_value(cookie_list.release()); |
+} |
+ |
+void CookieCommand::ExecutePost(Response* const response) { |
+ std::string cookie_string; |
+ DictionaryValue* cookie_dict; |
+ |
+ // Check to see if the cookie was passed in as a JSON onject. |
+ if (!GetDictionaryParameter("cookie", &cookie_dict)) { |
+ // No valid cookie sent. |
+ SET_WEBDRIVER_ERROR(response, "Cookie key not found", |
+ kBadRequest); |
+ return; |
+ } |
+ Cookie cookie(*cookie_dict); |
+ |
+ // Make sure the cookie is formated preoperly. |
+ if (!cookie.valid()) { |
+ SET_WEBDRIVER_ERROR(response, "Invalid cookie", |
+ kBadRequest); |
+ return; |
+ } |
+ |
+ if (!session_->SetCookie(current_url_, cookie.ToString())) { |
+ SET_WEBDRIVER_ERROR(response, "Failed to set cookie", |
+ kUnableToSetCookie); |
+ 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 (!session_->GetCookies(current_url_, &cookies)) { |
+ SET_WEBDRIVER_ERROR(response, "Failed to fetch cookies", |
+ kUnableToSetCookie); |
+ return; |
+ } |
+ |
+ Tokenize(cookies, ":", &tokens); |
+ for (std::vector<std::string>::iterator i = tokens.begin(); |
+ i != tokens.end(); ++i) { |
+ Cookie cookie(*i); |
+ if (cookie.valid()) { |
+ if (!session_->DeleteCookie(current_url_, cookie.name())) { |
+ VLOG(1) << "Could not delete cookie: " << cookie.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; |
+ SET_WEBDRIVER_ERROR(response, "Could not delete all cookies", |
+ kInternalServerError); |
+ return; |
+ } |
+ } |
+ |
+ response->set_status(kSuccess); |
+} |
+ |
+bool NamedCookieCommand::Init(Response* const response) { |
+ if (WebDriverCommand::Init(response)) { |
+ if (!session_->GetURL(¤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); |
+ if (cookie_name_ == "") { |
+ SET_WEBDRIVER_ERROR(response, "No cookie name specified", |
+ kBadRequest); |
+ return false; |
+ } |
+ |
+ return true; |
+ } |
+ |
+ return false; |
+} |
+ |
+void NamedCookieCommand::ExecuteGet(Response* const response) { |
+ std::string cookie; |
+ |
+ if (!session_->GetCookieByName(current_url_, cookie_name_, &cookie)) { |
+ SET_WEBDRIVER_ERROR(response, "Failed to fetch cookie", |
+ kUnknownError); |
+ return; |
+ } |
+ |
+ response->set_status(kSuccess); |
+ response->set_value(new StringValue(cookie)); |
+} |
+ |
+void NamedCookieCommand::ExecuteDelete(Response* const response) { |
+ if (!session_->DeleteCookie(current_url_, cookie_name_)) { |
+ SET_WEBDRIVER_ERROR(response, "Failed to delete cookie", |
+ kUnknownError); |
+ return; |
+ } |
+ |
+ response->set_status(kSuccess); |
+} |
+ |
+} // namespace webdriver |
+ |