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 "base/string_util.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "base/values.h" | |
| 10 #include "base/json/json_reader.h" | |
| 11 #include "base/json/json_writer.h" | |
| 12 #include "net/base/cookie_monster.h" | |
| 13 | |
|
kkania
2011/02/09 16:53:13
all includes referring to headers in the chrome tr
| |
| 14 #include "chrome/test/webdriver/cookie.h" | |
|
kkania
2011/02/09 16:53:13
This goes at the top of the file. Please familiari
| |
| 15 #include "chrome/test/webdriver/utility_functions.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 valid_ = pc.IsValid(); | |
| 25 if (!valid_) | |
| 26 return; | |
| 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 | |
| 52 // Convert from a webdriver JSON representation of a cookie. | |
| 53 Cookie::Cookie(const DictionaryValue& dict) { | |
| 54 int expiry; | |
| 55 valid_ = false; | |
| 56 | |
| 57 // Name and Value are required paramters. | |
| 58 if (!dict.GetString("name", &name_)) { | |
| 59 return; | |
| 60 } | |
| 61 if (!dict.GetString("value", &value_)) { | |
| 62 return; | |
| 63 } | |
| 64 if (!dict.GetString("path", &path_)) { | |
| 65 path_ = ""; | |
| 66 } | |
| 67 if (dict.GetString("domain", &domain_)) { | |
| 68 domain_ = ""; | |
| 69 } | |
| 70 if (dict.GetBoolean("secure", &secure_)) { | |
| 71 secure_ = false; | |
| 72 } | |
| 73 | |
| 74 // Convert the time passed into human-readable format. | |
| 75 if (dict.GetInteger("expiry", &expiry)) { | |
| 76 time_t clock = (time_t) expiry; | |
| 77 char buff[128]; | |
| 78 | |
| 79 memset(buff, 0, sizeof(buff)); | |
| 80 if (NULL == ctime_r(&clock, buff)) { | |
| 81 valid_ = false; | |
| 82 return; | |
| 83 } | |
| 84 expires_ = std::string(buff); | |
| 85 } | |
| 86 | |
| 87 valid_ = true; | |
| 88 } | |
| 89 | |
| 90 // Convert's to webdriver's cookie spec, see: | |
| 91 // http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/c ookie | |
| 92 DictionaryValue* Cookie::ToDictionary() { | |
| 93 DictionaryValue* cookie = new DictionaryValue(); | |
| 94 | |
| 95 // Required parameters for WebDriver protocol. | |
| 96 cookie->SetString("name", name_); | |
| 97 cookie->SetString("value", value_); | |
| 98 cookie->SetString("path", path_); | |
| 99 cookie->SetString("domain", domain_); | |
| 100 cookie->SetBoolean("secure", secure_); | |
| 101 cookie->SetString("expiry", expires_); | |
| 102 | |
| 103 // Chrome specific additons which are not a part of the JSON over HTTP spec. | |
| 104 cookie->SetBoolean("http_only", http_); | |
| 105 | |
| 106 return cookie; | |
| 107 } | |
| 108 | |
| 109 // Returns a string representation of the JSON webdriver format. This is | |
| 110 // used when logging cookies sent/recieved on the server. | |
| 111 std::string Cookie::ToJSONString() { | |
| 112 scoped_ptr<DictionaryValue> cookie(ToDictionary()); | |
| 113 std::string json; | |
| 114 | |
| 115 base::JSONWriter::Write(reinterpret_cast<Value*>(&cookie), | |
|
kkania
2011/02/09 16:53:13
actually, you should be able to change "reinterpre
| |
| 116 false, &json); | |
| 117 return json; | |
| 118 } | |
| 119 | |
| 120 // Returns a string representation of the cookie to be used by Automation Proxy. | |
| 121 // TODO(jmikhail): Add function to Automation Proxy that can set a cookie by | |
| 122 // using a DictionaryValue object. | |
| 123 std::string Cookie::ToString() { | |
| 124 std::string cookie = ""; | |
| 125 cookie += name_ + "=" + value_ + ";"; | |
| 126 | |
| 127 if (path_ != "") { | |
| 128 cookie += "path=" + path_ + ";"; | |
| 129 } | |
| 130 if (domain_ != "") { | |
| 131 cookie += "domain=" + domain_ + ";"; | |
| 132 } | |
| 133 if (secure_) { | |
| 134 cookie += "secure;"; | |
| 135 } | |
| 136 if (expires_ != "") { | |
| 137 cookie += expires_ + ";"; | |
| 138 } | |
| 139 if (http_) { | |
| 140 cookie += "http_only;"; | |
| 141 } | |
| 142 | |
| 143 return cookie; | |
| 144 } | |
| 145 | |
| 146 } // namespace webdriver | |
| 147 | |
| OLD | NEW |