Chromium Code Reviews| Index: chrome/test/webdriver/cookie.cc |
| diff --git a/chrome/test/webdriver/cookie.cc b/chrome/test/webdriver/cookie.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cb607e49cc775b5655eb6158d35662e137323333 |
| --- /dev/null |
| +++ b/chrome/test/webdriver/cookie.cc |
| @@ -0,0 +1,159 @@ |
| +// 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 <time.h> |
| + |
| +#include "chrome/test/webdriver/cookie.h" |
| +#include "chrome/test/webdriver/utility_functions.h" |
| + |
| +#include "base/string_util.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "base/values.h" |
| +#include "base/json/json_reader.h" |
| +#include "base/json/json_writer.h" |
| +#include "net/base/cookie_monster.h" |
| + |
| +namespace webdriver { |
| + |
| +// Convert from a string format. |
| +Cookie::Cookie(const std::string& cookie) { |
| + net::CookieMonster::ParsedCookie pc(cookie); |
| + expires_ = ""; |
| + |
| + if (!pc.IsValid()) { |
| + return; |
| + } |
| + |
| + name_ = pc.Name(); |
| + value_ = pc.Value(); |
| + secure_ = pc.IsSecure(); |
| + http_ = pc.IsHttpOnly(); |
| + |
| + if (pc.HasPath()) { |
| + path_ = pc.Path(); |
| + } else { |
| + path_ = ""; |
| + } |
| + |
| + if (pc.HasDomain()) { |
| + domain_ = pc.Domain(); |
| + } else { |
| + domain_ = ""; |
| + } |
| + |
| + if (pc.HasExpires()) { |
| + expires_ = pc.Expires(); |
| + } else { |
| + expires_ = ""; |
| + } |
| + |
| + valid_ = true; |
| +} |
| + |
| +// Convert from a webdriver JSON representation of a cookie. |
| +Cookie::Cookie(const DictionaryValue& dict) { |
| + int expiry; |
| + valid_ = false; |
| + |
| + // Name and Value are required paramters. |
| + if (!dict.GetString("name", &name_)) { |
| + return; |
| + } |
| + |
| + if (!dict.GetString("value", &value_)) { |
| + return; |
| + } |
| + |
| + if (!dict.GetString("path", &path_)) { |
| + path_ = ""; |
| + } |
| + |
| + if (dict.GetString("domain", &domain_)) { |
| + domain_ = ""; |
| + } |
| + |
| + if (dict.GetBoolean("secure", &secure_)) { |
| + secure_ = false; |
| + } |
| + |
| + // Convert the time passed into human-readable format. |
| + if (dict.GetInteger("expiry", &expiry)) { |
| + time_t clock = (time_t) expiry; |
| + char buff[128]; |
| + |
| + memset(buff, 0, sizeof(buff)); |
| + if (NULL == ctime_r(&clock, buff)) { |
| + valid_ = false; |
| + return; |
| + } |
| + expires_ = std::string(buff); |
| + } |
| + |
| + valid_ = true; |
| +} |
| + |
| +// Convert's to webdriver's cookie spec, see: |
| +// http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/cookie |
| +DictionaryValue* Cookie::ToJSON() { |
| + DictionaryValue* cookie = new DictionaryValue(); |
| + |
| + // Required parameters for WebDriver protocol. |
| + cookie->SetString("name", ASCIIToUTF16(name_)); |
| + cookie->SetString("value", ASCIIToUTF16(value_)); |
| + cookie->SetString("path", ASCIIToUTF16(path_)); |
| + cookie->SetString("domain", ASCIIToUTF16(domain_)); |
| + cookie->SetBoolean("secure", secure_); |
| + |
| + // Chrome specific additons which are not apart of the JSON over HTTP spec. |
| + cookie->SetString("expires", ASCIIToUTF16(expires_)); |
|
kkania
2011/01/28 23:25:59
it looks like they added this to the spec; its nam
Joe
2011/02/03 10:02:05
Done.
|
| + cookie->SetBoolean("http_only", http_); |
| + |
| + return cookie; |
| +} |
| + |
| +// Returns a string representation of the JSON webdriver format. This is |
| +// used when logging cookies sent/recieved on the server. |
| +std::string Cookie::ToJSONString() { |
| + DictionaryValue* cookie; |
| + std::string json; |
| + |
| + cookie = ToJSON(); |
| + base::JSONWriter::Write(reinterpret_cast<Value*>(&cookie), |
| + false, &json); |
| + delete cookie; |
| + return json; |
| +} |
| + |
| +// Returns a string representation of the cookie to be used by Automation Proxy. |
| +// TODO(jmikhail): Add function to Automation Proxy that can set a cookie by |
| +// using a DictionaryValue object. |
| +std::string Cookie::ToString() { |
| + std::string cookie = ""; |
| + cookie += name_ + "=" + value_ + ";"; |
| + |
| + if (path_ != "") { |
| + cookie += "path=" + path_ + ";"; |
| + } |
| + |
| + if (domain_ != "") { |
| + cookie += "domain=" + domain_ + ";"; |
| + } |
| + |
| + if (secure_) { |
| + cookie += "secure;"; |
| + } |
| + |
| + if (expires_ != "") { |
| + cookie += expires_ + ";"; |
| + } |
| + |
| + if (http_) { |
| + cookie += "http_only;"; |
| + } |
| + |
| + return cookie; |
| +} |
| + |
| +} // namespace webdriver |
| + |