OLD | NEW |
| (Empty) |
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 #ifndef CHROME_TEST_WEBDRIVER_COMMANDS_COMMAND_H_ | |
6 #define CHROME_TEST_WEBDRIVER_COMMANDS_COMMAND_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/values.h" | |
14 #include "build/build_config.h" | |
15 | |
16 #if defined(OS_MACOSX) | |
17 #include "base/mac/scoped_nsautorelease_pool.h" | |
18 #endif | |
19 | |
20 namespace webdriver { | |
21 | |
22 class Error; | |
23 class Response; | |
24 | |
25 // Base class for a command mapped to a URL in the WebDriver REST API. Each | |
26 // URL may respond to commands sent with a DELETE, GET/HEAD, or POST HTTP | |
27 // request. For more information on the WebDriver REST API, see | |
28 // http://code.google.com/p/selenium/wiki/JsonWireProtocol | |
29 class Command { | |
30 public: | |
31 Command(const std::vector<std::string>& path_segments, | |
32 const DictionaryValue* const parameters); | |
33 virtual ~Command(); | |
34 | |
35 // Indicates which HTTP methods this command URL responds to. | |
36 virtual bool DoesDelete(); | |
37 virtual bool DoesGet(); | |
38 virtual bool DoesPost(); | |
39 | |
40 // Initializes this command for execution. If initialization fails, will | |
41 // return |false| and populate the |response| with the necessary information | |
42 // to return to the client. | |
43 virtual bool Init(Response* const response); | |
44 | |
45 // Called after this command is executed. Returns NULL if no error occurs. | |
46 // This is only called if |Init| is successful and regardless of whether | |
47 // the execution results in a |Error|. | |
48 virtual void Finish(Response* const response); | |
49 | |
50 // Executes the corresponding variant of this command URL. | |
51 // Always called after |Init()| and called from the Execute function. | |
52 // Any failure is handled as a return code found in Response. | |
53 virtual void ExecuteDelete(Response* const response) {} | |
54 virtual void ExecuteGet(Response* const response) {} | |
55 virtual void ExecutePost(Response* const response) {} | |
56 | |
57 protected: | |
58 | |
59 // Returns the path variable encoded at the |i|th index (0-based) in the | |
60 // request URL for this command. If the index is out of bounds, an empty | |
61 // string will be returned. | |
62 std::string GetPathVariable(const size_t i) const; | |
63 | |
64 // Returns whether the command has a parameter with the given |key|. | |
65 bool HasParameter(const std::string& key) const; | |
66 | |
67 // Returns true if the command parameter with the given |key| exists and is | |
68 // a null value. | |
69 bool IsNullParameter(const std::string& key) const; | |
70 | |
71 // Returns the command parameter with the given |key| as a UTF-16 string. | |
72 // Returns true on success. | |
73 bool GetStringParameter(const std::string& key, string16* out) const; | |
74 | |
75 // Provides the command parameter with the given |key| as a UTF-8 string. | |
76 // Returns true on success. | |
77 bool GetStringParameter(const std::string& key, std::string* out) const; | |
78 | |
79 // Provides the command parameter with the given |key| as a ASCII string. | |
80 // Returns true on success. | |
81 bool GetStringASCIIParameter(const std::string& key, std::string* out) const; | |
82 | |
83 // Provides the command parameter with the given |key| as a boolean. Returns | |
84 // false if there is no such parameter, or if it is not a boolean. | |
85 bool GetBooleanParameter(const std::string& key, bool* out) const; | |
86 | |
87 // Provides the command parameter with the given |key| as a int. Returns | |
88 // false if there is no such parameter, or if it is not a int. | |
89 bool GetIntegerParameter(const std::string& key, int* out) const; | |
90 | |
91 // Provides the command parameter with the given |key| as a double. Returns | |
92 // false if there is no such parameter, or if it is not a dobule. | |
93 bool GetDoubleParameter(const std::string& key, double* out) const; | |
94 | |
95 // Provides the command parameter with the given |key| as a Dictionary. | |
96 // Returns false if there is no such parameter, or if it is not a Dictionary. | |
97 bool GetDictionaryParameter(const std::string& key, | |
98 const DictionaryValue** out) const; | |
99 | |
100 // Provides the command parameter with the given |key| as a list. Returns | |
101 // false if there is no such parameter, or if it is not a list. | |
102 bool GetListParameter(const std::string& key, const ListValue** out) const; | |
103 | |
104 const std::vector<std::string> path_segments_; | |
105 const scoped_ptr<const DictionaryValue> parameters_; | |
106 | |
107 private: | |
108 #if defined(OS_MACOSX) | |
109 // An autorelease pool must exist on any thread where Objective C is used, | |
110 // even implicitly. Otherwise the warning: | |
111 // "Objects autoreleased with no pool in place." | |
112 // is printed for every object deallocated. Since every incoming command to | |
113 // chrome driver is allocated a new thread, the release pool is declared here. | |
114 base::mac::ScopedNSAutoreleasePool autorelease_pool; | |
115 #endif | |
116 | |
117 DISALLOW_COPY_AND_ASSIGN(Command); | |
118 }; | |
119 | |
120 } // namespace webdriver | |
121 | |
122 #endif // CHROME_TEST_WEBDRIVER_COMMANDS_COMMAND_H_ | |
OLD | NEW |