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 #include "chrome/test/webdriver/cookie.h" |
| 5 |
| 6 #include <time.h> |
| 7 |
| 8 #include "base/string_util.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "base/values.h" |
| 11 #include "base/json/json_reader.h" |
| 12 #include "base/json/json_writer.h" |
| 13 #include "chrome/test/webdriver/cookie.h" |
| 14 #include "chrome/test/webdriver/utility_functions.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 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 #ifdef OS_WIN |
| 78 char* buff = ctime(&clock); |
| 79 if (NULL == buff) { |
| 80 valid_ = false; |
| 81 return; |
| 82 } |
| 83 #else |
| 84 char buff[128]; |
| 85 memset(buff, 0, sizeof(buff)); |
| 86 |
| 87 if (NULL == ctime_r(&clock, buff)) { |
| 88 valid_ = false; |
| 89 return; |
| 90 } |
| 91 #endif |
| 92 expires_ = std::string(buff); |
| 93 } |
| 94 |
| 95 valid_ = true; |
| 96 } |
| 97 |
| 98 // Convert's to webdriver's cookie spec, see: |
| 99 // http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/c
ookie |
| 100 DictionaryValue* Cookie::ToDictionary() { |
| 101 DictionaryValue* cookie = new DictionaryValue(); |
| 102 |
| 103 // Required parameters for WebDriver protocol. |
| 104 cookie->SetString("name", name_); |
| 105 cookie->SetString("value", value_); |
| 106 cookie->SetString("path", path_); |
| 107 cookie->SetString("domain", domain_); |
| 108 cookie->SetBoolean("secure", secure_); |
| 109 cookie->SetString("expiry", expires_); |
| 110 |
| 111 // Chrome specific additons which are not a part of the JSON over HTTP spec. |
| 112 cookie->SetBoolean("http_only", http_); |
| 113 |
| 114 return cookie; |
| 115 } |
| 116 |
| 117 // Returns a string representation of the JSON webdriver format. This is |
| 118 // used when logging cookies sent/recieved on the server. |
| 119 std::string Cookie::ToJSONString() { |
| 120 scoped_ptr<DictionaryValue> cookie(ToDictionary()); |
| 121 std::string json; |
| 122 |
| 123 base::JSONWriter::Write(reinterpret_cast<Value*>(cookie.get()), |
| 124 false, &json); |
| 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 if (domain_ != "") { |
| 139 cookie += "domain=" + domain_ + ";"; |
| 140 } |
| 141 if (secure_) { |
| 142 cookie += "secure;"; |
| 143 } |
| 144 if (expires_ != "") { |
| 145 cookie += expires_ + ";"; |
| 146 } |
| 147 if (http_) { |
| 148 cookie += "http_only;"; |
| 149 } |
| 150 |
| 151 return cookie; |
| 152 } |
| 153 |
| 154 } // namespace webdriver |
| 155 |
OLD | NEW |