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, |
kkania
2011/01/28 23:25:59
use Tokenize from base/string_util.h and remove th
Joe
2011/02/03 10:02:05
Done.
|
+ std::vector<std::string>& tokens) { |
+ size_t start = 0; |
+ size_t end = s.find(delimiter); |
+ |
+ 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", |
kkania
2011/01/28 23:25:59
didn't WebDriverCommand::Init already set the resp
Joe
2011/02/03 10:02:05
We need to set the message to report that the cook
kkania
2011/02/04 17:04:44
At this point, WebDriverCommand has already set th
|
+ 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); |
kkania
2011/01/28 23:25:59
use SET_WEBDRIVER_ERROR here and for all other err
Joe
2011/02/03 10:02:05
Done.
kkania
2011/02/04 17:04:44
I don't see SET_WEBDRIVER_ERROR still. Please fix.
|
+ 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)) { |
kkania
2011/01/28 23:25:59
how come you check for this? looking at the spec,
Joe
2011/02/03 10:02:05
Since chromium can also handle a string format I i
kkania
2011/02/04 17:04:44
That is a nice feature, but please remove it. Only
|
+ 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())); |
kkania
2011/01/28 23:25:59
how come you send it back as a string? i don't see
Joe
2011/02/03 10:02:05
I used the default return of chromium since it was
kkania
2011/02/04 17:04:44
What happens if you just leave the value blank?
|
+} |
+ |
+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); |
+ 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", |
kkania
2011/01/28 23:25:59
don't need to set error here
Joe
2011/02/03 10:02:05
Done.
kkania
2011/02/04 17:04:44
I don't see any change here. Please fix.
|
+ 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 |
+ |