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 "chrome/test/webdriver/commands/cookie_commands.h" | |
| 6 #include "chrome/test/webdriver/cookie.h" | |
| 7 | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 namespace webdriver { | |
| 12 | |
| 13 void tokenize(const std::string& s, const char& delimiter, | |
|
John Grabowski
2011/02/04 02:40:23
seems like something to put in an anonymous namesp
kkania
2011/02/04 17:04:44
hmm, looks like you forgot to fix this on the firs
Joe
2011/02/09 06:32:10
Done.
Joe
2011/02/09 06:32:10
Done.
| |
| 14 std::vector<std::string>& tokens) { | |
| 15 size_t start = 0; | |
| 16 size_t end = s.find(delimiter); | |
| 17 | |
|
John Grabowski
2011/02/04 02:40:23
Hmm. Might it be better to create a static method
Joe
2011/02/09 06:32:10
Done.
| |
| 18 while (end != std::string::npos) { | |
| 19 if (end - start > 0) { | |
| 20 tokens.push_back(s.substr(start, end - start)); | |
| 21 } | |
| 22 start = end + 1; | |
| 23 end = s.find(delimiter, start); | |
| 24 } | |
| 25 | |
| 26 if ((s.length() - start) > 0) { | |
| 27 tokens.push_back(s.substr(start, s.length())); | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 bool CookieCommand::Init(Response* const response) { | |
| 32 if (WebDriverCommand::Init(response)) { | |
| 33 if (tab_->GetCurrentURL(¤t_url_)) { | |
| 34 return true; | |
| 35 } | |
| 36 SET_WEBDRIVER_ERROR(response, "Failed to query current page URL", | |
| 37 kInternalServerError); | |
| 38 return false; | |
| 39 } | |
| 40 | |
| 41 SET_WEBDRIVER_ERROR(response, "Failed to init Cookie Command", | |
| 42 kInternalServerError); | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 void CookieCommand::ExecuteGet(Response* const response) { | |
| 47 // TODO(JMikhail): Add GetJSONCookies to automation proxy since | |
| 48 // GetCookies does not return the necessary information | |
| 49 std::string cookies; | |
| 50 std::vector<std::string> tokens; | |
| 51 | |
| 52 if (!tab_->GetCookies(current_url_, &cookies)) { | |
| 53 response->set_status(kUnknownError); | |
| 54 response->set_value(new StringValue("Failed to fetch cookies")); | |
| 55 return; | |
| 56 } | |
| 57 | |
| 58 tokenize(cookies, ';', tokens); | |
| 59 ListValue* l = new ListValue(); | |
| 60 for (std::vector<std::string>::iterator i = tokens.begin(); | |
| 61 i != tokens.end(); ++i) { | |
| 62 Cookie cookie(*i); | |
| 63 if (cookie.IsValid()) { | |
| 64 l->Append(cookie.ToJSON()); | |
| 65 } else { | |
| 66 LOG(ERROR) << "Failed to parse cookie: " << *i; | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 response->set_status(kSuccess); | |
| 71 response->set_value(l); | |
| 72 } | |
| 73 | |
| 74 void CookieCommand::ExecutePost(Response* const response) { | |
| 75 std::string cookie_string; | |
| 76 DictionaryValue* cookie_dict; | |
| 77 scoped_ptr<Cookie> cookie; | |
| 78 | |
| 79 // Check to see if the cookie was passed in as a string. | |
| 80 if (GetStringASCIIParameter("cookie", &cookie_string)) { | |
| 81 cookie.reset(new Cookie(cookie_string)); | |
| 82 } else { | |
| 83 // Check to see if the cookie was passed in as a JSON onject. | |
| 84 if (GetDictionaryParameter("cookie", &cookie_dict)) { | |
| 85 cookie.reset(new Cookie(*cookie_dict)); | |
| 86 } else { | |
| 87 // No valid cookie sent. | |
| 88 response->set_value(new StringValue("Cookie key not found")); | |
| 89 response->set_status(kBadRequest); | |
| 90 return; | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 // Make sure the cookie is formated preoperly. | |
| 95 if (!cookie->IsValid()) { | |
| 96 response->set_value(new StringValue("Invalid cookie")); | |
| 97 response->set_status(kBadRequest); | |
| 98 return; | |
| 99 } | |
| 100 | |
| 101 if (!tab_->SetCookie(current_url_, cookie->ToString())) { | |
| 102 response->set_status(kUnableToSetCookie); | |
| 103 response->set_value(new StringValue("Failed to set cookie")); | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 response->set_status(kSuccess); | |
| 108 response->set_value(new StringValue(cookie->ToString())); | |
| 109 } | |
| 110 | |
| 111 void CookieCommand::ExecuteDelete(Response* const response) { | |
| 112 std::string cookies; | |
| 113 std::vector<std::string> tokens; | |
| 114 | |
| 115 if (!tab_->GetCookies(current_url_, &cookies)) { | |
| 116 response->set_status(kUnknownError); | |
| 117 response->set_value(new StringValue("Failed to fetch cookies")); | |
| 118 return; | |
| 119 } | |
| 120 | |
| 121 tokenize(cookies, ':', tokens); | |
|
John Grabowski
2011/02/04 02:40:23
On line 58 you tokenize cookes on ;.
Here you toke
| |
| 122 for (std::vector<std::string>::iterator i = tokens.begin(); | |
| 123 i != tokens.end(); ++i) { | |
| 124 Cookie cookie(*i); | |
| 125 if (cookie.IsValid()) { | |
| 126 if (!tab_->DeleteCookie(current_url_, cookie.get_name())) { | |
| 127 LOG(INFO) << "Could not delete cookie: " << cookie.get_name() << "\n" | |
| 128 << "Contents of cookie: " << cookie.ToString(); | |
| 129 SET_WEBDRIVER_ERROR(response, "Could not delete all cookies", | |
| 130 kInternalServerError); | |
| 131 return; | |
| 132 } | |
| 133 } else { | |
| 134 LOG(ERROR) << "Failed to parse cookie: " << *i; | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 response->set_status(kSuccess); | |
| 139 response->set_value(new StringValue("Cookies delete")); | |
| 140 } | |
| 141 | |
| 142 bool NamedCookieCommand::Init(Response* const response) { | |
| 143 if (WebDriverCommand::Init(response)) { | |
| 144 if (!tab_->GetCurrentURL(¤t_url_)) { | |
| 145 SET_WEBDRIVER_ERROR(response, "Failed to query current page URL", | |
| 146 kInternalServerError); | |
| 147 return false; | |
| 148 } | |
| 149 | |
| 150 // There should be at least 5 segments to match | |
| 151 // /session/:sessionId/cookie/:name | |
| 152 cookie_name_ = GetPathVariable(4); | |
| 153 SET_WEBDRIVER_ERROR(response, "No cookie name specified", | |
| 154 kBadRequest); | |
| 155 return false; | |
| 156 } | |
| 157 | |
| 158 SET_WEBDRIVER_ERROR(response, "Failed to init Named Cookie Command", | |
| 159 kInternalServerError); | |
| 160 return false; | |
| 161 } | |
| 162 | |
| 163 void NamedCookieCommand::ExecuteGet(Response* const response) { | |
| 164 std::string cookie; | |
| 165 | |
| 166 if (!tab_->GetCookieByName(current_url_, cookie_name_, &cookie)) { | |
| 167 response->set_status(kUnknownError); | |
| 168 response->set_value(new StringValue("Failed to fetch cookie")); | |
| 169 return; | |
| 170 } | |
| 171 | |
| 172 response->set_status(kSuccess); | |
| 173 response->set_value(new StringValue(cookie)); | |
| 174 } | |
| 175 | |
| 176 void NamedCookieCommand::ExecuteDelete(Response* const response) { | |
| 177 if (!tab_->DeleteCookie(current_url_, cookie_name_)) { | |
| 178 response->set_status(kUnknownError); | |
| 179 response->set_value(new StringValue("Failed to delete cookie")); | |
| 180 return; | |
| 181 } | |
| 182 | |
| 183 response->set_status(kSuccess); | |
| 184 response->set_value(new StringValue("Cookie deleted")); | |
| 185 } | |
| 186 | |
| 187 } // namespace webdriver | |
| 188 | |
| OLD | NEW |