summaryrefslogtreecommitdiffstats
path: root/chrome/test/webdriver/commands/cookie_commands.cc
blob: 8c5d8e15da9c38a0387567578564b302ef7c2904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/webdriver/commands/cookie_commands.h"

#include <string>
#include <vector>

#include "base/memory/scoped_ptr.h"
#include "base/string_split.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/test/webdriver/commands/response.h"
#include "chrome/test/webdriver/webdriver_session.h"

namespace webdriver {

CookieCommand::CookieCommand(const std::vector<std::string>& path_segments,
                             const DictionaryValue* const parameters)
    : WebDriverCommand(path_segments, parameters) {}

CookieCommand::~CookieCommand() {}

bool CookieCommand::DoesDelete() {
  return true;
}

bool CookieCommand::DoesGet() {
  return true;
}

bool CookieCommand::DoesPost() {
  return true;
}

void CookieCommand::ExecuteGet(Response* const response) {
  std::string url;
  Error* error = session_->GetURL(&url);
  ListValue* cookies;
  if (!error)
    error = session_->GetCookies(url, &cookies);
  if (error) {
    response->SetError(error);
    return;
  }
  response->SetValue(cookies);
}

void CookieCommand::ExecutePost(Response* const response) {
  const DictionaryValue* cookie_dict;
  if (!GetDictionaryParameter("cookie", &cookie_dict)) {
    response->SetError(new Error(
        kBadRequest, "Missing or invalid |cookie| parameter"));
    return;
  }
  scoped_ptr<DictionaryValue> cookie_dict_copy(cookie_dict->DeepCopy());

  std::string domain;
  if (cookie_dict_copy->GetString("domain", &domain)) {
    std::vector<std::string> split_domain;
    base::SplitString(domain, ':', &split_domain);
    if (split_domain.size() > 2) {
      response->SetError(new Error(
          kInvalidCookieDomain, "Cookie domain has too many colons"));
      return;
    } else if (split_domain.size() == 2) {
      // Remove the port number.
      cookie_dict_copy->SetString("domain", split_domain[0]);
    }
  }

  std::string url;
  Error* error = session_->GetURL(&url);
  if (!error)
    error = session_->SetCookie(url, cookie_dict_copy.get());
  if (error) {
    response->SetError(error);
    return;
  }
}

void CookieCommand::ExecuteDelete(Response* const response) {
  std::string url;
  Error* error = session_->GetURL(&url);
  ListValue* unscoped_cookies = NULL;
  if (!error)
    error = session_->GetCookies(url, &unscoped_cookies);
  scoped_ptr<ListValue> cookies(unscoped_cookies);
  if (error) {
    response->SetError(error);
    return;
  }

  for (size_t i = 0; i < cookies->GetSize(); ++i) {
    DictionaryValue* cookie_dict;
    if (!cookies->GetDictionary(i, &cookie_dict)) {
      response->SetError(new Error(
          kUnknownError, "GetCookies returned non-dict type"));
      return;
    }
    std::string name;
    if (!cookie_dict->GetString("name", &name)) {
      response->SetError(new Error(
          kUnknownError,
          "GetCookies returned cookie with missing or invalid 'name'"));
      return;
    }
    error = session_->DeleteCookie(url, name);
    if (error) {
      response->SetError(error);
      return;
    }
  }
}

NamedCookieCommand::NamedCookieCommand(
    const std::vector<std::string>& path_segments,
    const DictionaryValue* const parameters)
    : WebDriverCommand(path_segments, parameters) {}

NamedCookieCommand::~NamedCookieCommand() {}

bool NamedCookieCommand::Init(Response* const response) {
  if (!WebDriverCommand::Init(response))
    return false;

  // There should be at least 5 segments to match
  // /session/:sessionId/cookie/:name
  cookie_name_ = GetPathVariable(4);
  if (cookie_name_ == "") {
    response->SetError(new Error(kBadRequest, "No cookie name specified"));
    return false;
  }

  return true;
}

bool NamedCookieCommand::DoesDelete() {
  return true;
}

void NamedCookieCommand::ExecuteDelete(Response* const response) {
  std::string url;
  Error* error = session_->GetURL(&url);
  if (!error)
    error = session_->DeleteCookie(url, cookie_name_);
  if (error) {
    response->SetError(error);
    return;
  }
}

}  // namespace webdriver