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, | |
kkania
2011/01/28 23:25:59
use Tokenize from base/string_util.h and remove th
Joe
2011/02/03 10:02:05
Done.
| |
14 std::vector<std::string>& tokens) { | |
15 size_t start = 0; | |
16 size_t end = s.find(delimiter); | |
17 | |
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", | |
kkania
2011/01/28 23:25:59
didn't WebDriverCommand::Init already set the resp
Joe
2011/02/03 10:02:05
We need to set the message to report that the cook
kkania
2011/02/04 17:04:44
At this point, WebDriverCommand has already set th
| |
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); | |
kkania
2011/01/28 23:25:59
use SET_WEBDRIVER_ERROR here and for all other err
Joe
2011/02/03 10:02:05
Done.
kkania
2011/02/04 17:04:44
I don't see SET_WEBDRIVER_ERROR still. Please fix.
| |
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)) { | |
kkania
2011/01/28 23:25:59
how come you check for this? looking at the spec,
Joe
2011/02/03 10:02:05
Since chromium can also handle a string format I i
kkania
2011/02/04 17:04:44
That is a nice feature, but please remove it. Only
| |
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())); | |
kkania
2011/01/28 23:25:59
how come you send it back as a string? i don't see
Joe
2011/02/03 10:02:05
I used the default return of chromium since it was
kkania
2011/02/04 17:04:44
What happens if you just leave the value blank?
| |
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); | |
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", | |
kkania
2011/01/28 23:25:59
don't need to set error here
Joe
2011/02/03 10:02:05
Done.
kkania
2011/02/04 17:04:44
I don't see any change here. Please fix.
| |
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 |