summaryrefslogtreecommitdiffstats
path: root/chrome/test/webdriver/commands/command.h
blob: 97f0259e9fe1c0bb92021974ea437cae6e8448f6 (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
// Copyright (c) 2011 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.

#ifndef CHROME_TEST_WEBDRIVER_COMMANDS_COMMAND_H_
#define CHROME_TEST_WEBDRIVER_COMMANDS_COMMAND_H_

#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "build/build_config.h"

#if defined(OS_MACOSX)
#include "base/mac/scoped_nsautorelease_pool.h"
#endif

namespace webdriver {

class Error;
class Response;

// Base class for a command mapped to a URL in the WebDriver REST API. Each
// URL may respond to commands sent with a DELETE, GET/HEAD, or POST HTTP
// request. For more information on the WebDriver REST API, see
// http://code.google.com/p/selenium/wiki/JsonWireProtocol
class Command {
 public:
  Command(const std::vector<std::string>& path_segments,
          const DictionaryValue* const parameters);
  virtual ~Command();

  // Indicates which HTTP methods this command URL responds to.
  virtual bool DoesDelete();
  virtual bool DoesGet();
  virtual bool DoesPost();

  // Initializes this command for execution. If initialization fails, will
  // return |false| and populate the |response| with the necessary information
  // to return to the client.
  virtual bool Init(Response* const response);

  // Called after this command is executed. Returns NULL if no error occurs.
  // This is only called if |Init| is successful and regardless of whether
  // the execution results in a |Error|.
  virtual void Finish(Response* const response);

  // Executes the corresponding variant of this command URL.
  // Always called after |Init()| and called from the Execute function.
  // Any failure is handled as a return code found in Response.
  virtual void ExecuteDelete(Response* const response) {}
  virtual void ExecuteGet(Response* const response) {}
  virtual void ExecutePost(Response* const response) {}

 protected:

  // Returns the path variable encoded at the |i|th index (0-based) in the
  // request URL for this command. If the index is out of bounds, an empty
  // string will be returned.
  std::string GetPathVariable(const size_t i) const;

  // Returns whether the command has a parameter with the given |key|.
  bool HasParameter(const std::string& key) const;

  // Returns true if the command parameter with the given |key| exists and is
  // a null value.
  bool IsNullParameter(const std::string& key) const;

  // Returns the command parameter with the given |key| as a UTF-16 string.
  // Returns true on success.
  bool GetStringParameter(const std::string& key, string16* out) const;

  // Provides the command parameter with the given |key| as a UTF-8 string.
  // Returns true on success.
  bool GetStringParameter(const std::string& key, std::string* out) const;

  // Provides the command parameter with the given |key| as a ASCII string.
  // Returns true on success.
  bool GetStringASCIIParameter(const std::string& key, std::string* out) const;

  // Provides the command parameter with the given |key| as a boolean. Returns
  // false if there is no such parameter, or if it is not a boolean.
  bool GetBooleanParameter(const std::string& key, bool* out) const;

  // Provides the command parameter with the given |key| as a int. Returns
  // false if there is no such parameter, or if it is not a int.
  bool GetIntegerParameter(const std::string& key, int* out) const;

  // Provides the command parameter with the given |key| as a double. Returns
  // false if there is no such parameter, or if it is not a dobule.
  bool GetDoubleParameter(const std::string& key, double* out) const;

  // Provides the command parameter with the given |key| as a Dictionary.
  // Returns false if there is no such parameter, or if it is not a Dictionary.
  bool GetDictionaryParameter(const std::string& key,
                              DictionaryValue** out) const;

  // Provides the command parameter with the given |key| as a list. Returns
  // false if there is no such parameter, or if it is not a list.
  bool GetListParameter(const std::string& key, ListValue** out) const;

  const std::vector<std::string> path_segments_;
  const scoped_ptr<const DictionaryValue> parameters_;

 private:
#if defined(OS_MACOSX)
  // An autorelease pool must exist on any thread where Objective C is used,
  // even implicitly. Otherwise the warning:
  //   "Objects autoreleased with no pool in place."
  // is printed for every object deallocated.  Since every incoming command to
  // chrome driver is allocated a new thread, the release pool is declared here.
  base::mac::ScopedNSAutoreleasePool autorelease_pool;
#endif

  DISALLOW_COPY_AND_ASSIGN(Command);
};

}  // namespace webdriver

#endif  // CHROME_TEST_WEBDRIVER_COMMANDS_COMMAND_H_