Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <time.h> | |
| 6 | |
| 7 #include "chrome/test/webdriver/cookie.h" | |
| 8 #include "chrome/test/webdriver/utility_functions.h" | |
| 9 | |
| 10 #include "base/string_util.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "base/values.h" | |
| 13 #include "base/json/json_reader.h" | |
| 14 #include "base/json/json_writer.h" | |
| 15 #include "net/base/cookie_monster.h" | |
| 16 | |
| 17 namespace webdriver { | |
| 18 | |
| 19 // Convert from a string format. | |
| 20 Cookie::Cookie(const std::string& cookie) { | |
| 21 net::CookieMonster::ParsedCookie pc(cookie); | |
| 22 expires_ = ""; | |
| 23 | |
| 24 if (!pc.IsValid()) { | |
| 25 return; | |
| 26 } | |
| 27 | |
| 28 name_ = pc.Name(); | |
| 29 value_ = pc.Value(); | |
| 30 secure_ = pc.IsSecure(); | |
| 31 http_ = pc.IsHttpOnly(); | |
| 32 | |
| 33 if (pc.HasPath()) { | |
| 34 path_ = pc.Path(); | |
| 35 } else { | |
| 36 path_ = ""; | |
| 37 } | |
| 38 | |
| 39 if (pc.HasDomain()) { | |
| 40 domain_ = pc.Domain(); | |
| 41 } else { | |
| 42 domain_ = ""; | |
| 43 } | |
| 44 | |
| 45 if (pc.HasExpires()) { | |
| 46 expires_ = pc.Expires(); | |
| 47 } else { | |
| 48 expires_ = ""; | |
| 49 } | |
| 50 | |
| 51 valid_ = true; | |
| 52 } | |
| 53 | |
| 54 // Convert from a webdriver JSON representation of a cookie. | |
| 55 Cookie::Cookie(const DictionaryValue& dict) { | |
| 56 int expiry; | |
| 57 valid_ = false; | |
| 58 | |
| 59 // Name and Value are required paramters. | |
| 60 if (!dict.GetString("name", &name_)) { | |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 if (!dict.GetString("value", &value_)) { | |
| 65 return; | |
| 66 } | |
| 67 | |
| 68 if (!dict.GetString("path", &path_)) { | |
| 69 path_ = ""; | |
| 70 } | |
| 71 | |
| 72 if (dict.GetString("domain", &domain_)) { | |
| 73 domain_ = ""; | |
| 74 } | |
| 75 | |
| 76 if (dict.GetBoolean("secure", &secure_)) { | |
| 77 secure_ = false; | |
| 78 } | |
| 79 | |
| 80 // Convert the time passed into human-readable format. | |
| 81 if (dict.GetInteger("expiry", &expiry)) { | |
| 82 time_t clock = (time_t) expiry; | |
| 83 char buff[128]; | |
| 84 | |
| 85 memset(buff, 0, sizeof(buff)); | |
| 86 if (NULL == ctime_r(&clock, buff)) { | |
| 87 valid_ = false; | |
| 88 return; | |
| 89 } | |
| 90 expires_ = std::string(buff); | |
| 91 } | |
| 92 | |
| 93 valid_ = true; | |
| 94 } | |
| 95 | |
| 96 // Convert's to webdriver's cookie spec, see: | |
| 97 // http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/c ookie | |
| 98 DictionaryValue* Cookie::ToJSON() { | |
| 99 DictionaryValue* cookie = new DictionaryValue(); | |
| 100 | |
| 101 // Required parameters for WebDriver protocol. | |
| 102 cookie->SetString("name", ASCIIToUTF16(name_)); | |
| 103 cookie->SetString("value", ASCIIToUTF16(value_)); | |
| 104 cookie->SetString("path", ASCIIToUTF16(path_)); | |
| 105 cookie->SetString("domain", ASCIIToUTF16(domain_)); | |
| 106 cookie->SetBoolean("secure", secure_); | |
| 107 | |
| 108 // Chrome specific additons which are not apart of the JSON over HTTP spec. | |
| 109 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.
| |
| 110 cookie->SetBoolean("http_only", http_); | |
| 111 | |
| 112 return cookie; | |
| 113 } | |
| 114 | |
| 115 // Returns a string representation of the JSON webdriver format. This is | |
| 116 // used when logging cookies sent/recieved on the server. | |
| 117 std::string Cookie::ToJSONString() { | |
| 118 DictionaryValue* cookie; | |
| 119 std::string json; | |
| 120 | |
| 121 cookie = ToJSON(); | |
| 122 base::JSONWriter::Write(reinterpret_cast<Value*>(&cookie), | |
| 123 false, &json); | |
| 124 delete cookie; | |
| 125 return json; | |
| 126 } | |
| 127 | |
| 128 // Returns a string representation of the cookie to be used by Automation Proxy. | |
| 129 // TODO(jmikhail): Add function to Automation Proxy that can set a cookie by | |
| 130 // using a DictionaryValue object. | |
| 131 std::string Cookie::ToString() { | |
| 132 std::string cookie = ""; | |
| 133 cookie += name_ + "=" + value_ + ";"; | |
| 134 | |
| 135 if (path_ != "") { | |
| 136 cookie += "path=" + path_ + ";"; | |
| 137 } | |
| 138 | |
| 139 if (domain_ != "") { | |
| 140 cookie += "domain=" + domain_ + ";"; | |
| 141 } | |
| 142 | |
| 143 if (secure_) { | |
| 144 cookie += "secure;"; | |
| 145 } | |
| 146 | |
| 147 if (expires_ != "") { | |
| 148 cookie += expires_ + ";"; | |
| 149 } | |
| 150 | |
| 151 if (http_) { | |
| 152 cookie += "http_only;"; | |
| 153 } | |
| 154 | |
| 155 return cookie; | |
| 156 } | |
| 157 | |
| 158 } // namespace webdriver | |
| 159 | |
| OLD | NEW |