1 // Copyright (c) 2012 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 7 #include <string> 8 #include <vector> 9 10 #include "base/memory/scoped_ptr.h" 11 #include "base/strings/string_split.h" 12 #include "base/strings/string_util.h" 13 #include "base/values.h" 14 #include "chrome/test/webdriver/commands/response.h" 15 #include "chrome/test/webdriver/webdriver_session.h" 16 17 namespace webdriver { 18 19 CookieCommand::CookieCommand(const std::vector<std::string>& path_segments, 20 const DictionaryValue* const parameters) 21 : WebDriverCommand(path_segments, parameters) {} 22 23 CookieCommand::~CookieCommand() {} 24 25 bool CookieCommand::DoesDelete() { 26 return true; 27 } 28 29 bool CookieCommand::DoesGet() { 30 return true; 31 } 32 33 bool CookieCommand::DoesPost() { 34 return true; 35 } 36 37 void CookieCommand::ExecuteGet(Response* const response) { 38 std::string url; 39 Error* error = session_->GetURL(&url); 40 scoped_ptr<ListValue> cookies; 41 if (!error) 42 error = session_->GetCookies(url, &cookies); 43 if (error) { 44 response->SetError(error); 45 return; 46 } 47 response->SetValue(cookies.release()); 48 } 49 50 void CookieCommand::ExecutePost(Response* const response) { 51 const DictionaryValue* cookie_dict; 52 if (!GetDictionaryParameter("cookie", &cookie_dict)) { 53 response->SetError(new Error( 54 kBadRequest, "Missing or invalid |cookie| parameter")); 55 return; 56 } 57 scoped_ptr<DictionaryValue> cookie_dict_copy(cookie_dict->DeepCopy()); 58 59 std::string domain; 60 if (cookie_dict_copy->GetString("domain", &domain)) { 61 std::vector<std::string> split_domain; 62 base::SplitString(domain, ':', &split_domain); 63 if (split_domain.size() > 2) { 64 response->SetError(new Error( 65 kInvalidCookieDomain, "Cookie domain has too many colons")); 66 return; 67 } else if (split_domain.size() == 2) { 68 // Remove the port number. 69 cookie_dict_copy->SetString("domain", split_domain[0]); 70 } 71 } 72 73 std::string url; 74 Error* error = session_->GetURL(&url); 75 if (!error) 76 error = session_->SetCookie(url, cookie_dict_copy.get()); 77 if (error) { 78 response->SetError(error); 79 return; 80 } 81 } 82 83 void CookieCommand::ExecuteDelete(Response* const response) { 84 std::string url; 85 Error* error = session_->GetURL(&url); 86 scoped_ptr<ListValue> cookies; 87 if (!error) 88 error = session_->GetCookies(url, &cookies); 89 if (error) { 90 response->SetError(error); 91 return; 92 } 93 94 for (size_t i = 0; i < cookies->GetSize(); ++i) { 95 DictionaryValue* cookie_dict; 96 if (!cookies->GetDictionary(i, &cookie_dict)) { 97 response->SetError(new Error( 98 kUnknownError, "GetCookies returned non-dict type")); 99 return; 100 } 101 std::string name; 102 if (!cookie_dict->GetString("name", &name)) { 103 response->SetError(new Error( 104 kUnknownError, 105 "GetCookies returned cookie with missing or invalid 'name'")); 106 return; 107 } 108 error = session_->DeleteCookie(url, name); 109 if (error) { 110 response->SetError(error); 111 return; 112 } 113 } 114 } 115 116 NamedCookieCommand::NamedCookieCommand( 117 const std::vector<std::string>& path_segments, 118 const DictionaryValue* const parameters) 119 : WebDriverCommand(path_segments, parameters) {} 120 121 NamedCookieCommand::~NamedCookieCommand() {} 122 123 bool NamedCookieCommand::Init(Response* const response) { 124 if (!WebDriverCommand::Init(response)) 125 return false; 126 127 // There should be at least 5 segments to match 128 // /session/:sessionId/cookie/:name 129 cookie_name_ = GetPathVariable(4); 130 if (cookie_name_ == "") { 131 response->SetError(new Error(kBadRequest, "No cookie name specified")); 132 return false; 133 } 134 135 return true; 136 } 137 138 bool NamedCookieCommand::DoesDelete() { 139 return true; 140 } 141 142 void NamedCookieCommand::ExecuteDelete(Response* const response) { 143 std::string url; 144 Error* error = session_->GetURL(&url); 145 if (!error) 146 error = session_->DeleteCookie(url, cookie_name_); 147 if (error) { 148 response->SetError(error); 149 return; 150 } 151 } 152 153 } // namespace webdriver 154