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