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..6fe9f65d4241bcef436fe8f708d4dd0ecb57a20e |
| --- /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> |
|
kkania
2011/02/08 18:35:22
this is the wrong include order. See my earlier co
Joe
2011/02/09 06:32:10
Done.
|
| + |
| +#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()) { |
|
kkania
2011/02/08 18:35:22
change to:
valid_ = pc.IsValid();
if (!valid_)
r
Joe
2011/02/09 06:32:10
Done.
|
| + 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; |
| + } |
| + |
|
kkania
2011/02/08 18:35:22
limit use of your newlines in code.
Joe
2011/02/09 06:32:10
Done.
|
| + 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_)); |
|
kkania
2011/02/08 18:35:22
I don't think you need the ASCII to UTF16 conversi
Joe
2011/02/09 06:32:10
Done.
|
| + 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 a part of the JSON over HTTP spec. |
| + cookie->SetString("expiry", ASCIIToUTF16(expires_)); |
|
kkania
2011/02/08 18:35:22
expiry is part of the spec now. put it above this
Joe
2011/02/09 06:32:10
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; |
|
kkania
2011/02/08 18:35:22
use scoped_ptr
Joe
2011/02/09 06:32:10
Done.
|
| + std::string json; |
| + |
| + cookie = ToJSON(); |
| + base::JSONWriter::Write(reinterpret_cast<Value*>(&cookie), |
|
kkania
2011/02/08 18:35:22
use static_cast
Joe
2011/02/09 06:32:10
That's not possible
On 2011/02/08 18:35:22, kkania
|
| + 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_ + ";"; |
| + } |
| + |
|
kkania
2011/02/08 18:35:22
limit use of newlines
|
| + if (domain_ != "") { |
| + cookie += "domain=" + domain_ + ";"; |
| + } |
| + |
| + if (secure_) { |
| + cookie += "secure;"; |
| + } |
| + |
| + if (expires_ != "") { |
| + cookie += expires_ + ";"; |
| + } |
| + |
| + if (http_) { |
| + cookie += "http_only;"; |
| + } |
| + |
| + return cookie; |
| +} |
| + |
| +} // namespace webdriver |
| + |