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 #ifndef CHROME_TEST_WEBDRIVER_COMMANDS_RESPONSE_H_ |
| 6 #define CHROME_TEST_WEBDRIVER_COMMANDS_RESPONSE_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/test/webdriver/error_codes.h" |
| 13 |
| 14 namespace webdriver { |
| 15 |
| 16 // A simple class that encapsulates the information describing the response to |
| 17 // a |Command|. In Webdriver all responses must be sent back as a JSON value, |
| 18 // conforming to the spec found at: |
| 19 // http://code.google.com/p/selenium/wiki/JsonWireProtocol#Messages |
| 20 class Response { |
| 21 public: |
| 22 inline Response() : status_(kSuccess) { |
| 23 set_value(Value::CreateNullValue()); |
| 24 } |
| 25 |
| 26 inline ErrorCode status() const { return status_; } |
| 27 inline void set_status(const ErrorCode status) { |
| 28 status_ = status; |
| 29 data_.SetInteger(kStatusKey, status_); |
| 30 } |
| 31 |
| 32 // Ownership of the returned pointer is kept by the class and held in |
| 33 // the Dictiionary Value data_. |
| 34 inline const Value* value() const { |
| 35 Value* out = NULL; |
| 36 LOG_IF(WARNING, !data_.Get(kValueKey, &out)) |
| 37 << "Accessing unset response value."; // Should never happen. |
| 38 return out; |
| 39 } |
| 40 |
| 41 // Sets the |value| of this response, assuming ownership of the object in the |
| 42 // process. |
| 43 inline void set_value(Value* value) { |
| 44 data_.Set(kValueKey, value); |
| 45 } |
| 46 |
| 47 // Sets a JSON field in this response. The |key| may be a "." delimitted |
| 48 // string to indicate the value should be set in a nested object. Any |
| 49 // previously set value for the |key| will be deleted. |
| 50 // This object assumes ownership of |in_value|. |
| 51 inline void SetField(const std::string& key, Value* value) { |
| 52 data_.Set(key, value); |
| 53 } |
| 54 |
| 55 // Returns this response as a JSON string. |
| 56 std::string ToJSON() const { |
| 57 std::string json; |
| 58 base::JSONWriter::Write(static_cast<const Value*>(&data_), false, &json); |
| 59 return json; |
| 60 } |
| 61 |
| 62 private: |
| 63 // The hard coded values for the keys below are set in the command.cc file. |
| 64 static const char* const kStatusKey; |
| 65 static const char* const kValueKey; |
| 66 |
| 67 // The response status code. Stored outside of |data_| since it is |
| 68 // likely to be queried often. |
| 69 ErrorCode status_; |
| 70 DictionaryValue data_; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(Response); |
| 73 }; |
| 74 } // namespace webdriver |
| 75 #endif // CHROME_TEST_WEBDRIVER_COMMANDS_RESPONSE_H_ |
| 76 |
OLD | NEW |