| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_TEST_WEBDRIVER_COMMANDS_RESPONSE_H_ | 5 #ifndef CHROME_TEST_WEBDRIVER_COMMANDS_RESPONSE_H_ |
| 6 #define CHROME_TEST_WEBDRIVER_COMMANDS_RESPONSE_H_ | 6 #define CHROME_TEST_WEBDRIVER_COMMANDS_RESPONSE_H_ |
| 7 | 7 |
| 8 #include <sstream> | 8 #include <sstream> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/test/webdriver/error_codes.h" | 12 #include "chrome/test/webdriver/error_codes.h" |
| 13 | 13 |
| 14 namespace webdriver { | 14 namespace webdriver { |
| 15 | 15 |
| 16 // All errors in webdriver must use this macro in order to send back | 16 // All errors in webdriver must use this macro in order to send back |
| 17 // a proper stack trace to the client | 17 // a proper stack trace to the client. |
| 18 #define SET_WEBDRIVER_ERROR(response, msg, err) \ | 18 #define SET_WEBDRIVER_ERROR(response, msg, err) \ |
| 19 response->SetError(err, msg, __FILE__, __LINE__); \ | 19 response->SetError(err, msg, __FILE__, __LINE__); \ |
| 20 LOG(ERROR) << msg | 20 LOG(ERROR) << msg |
| 21 | 21 |
| 22 // Error which need to send back a screenshot should use this macro. |
| 23 // |png| needs to be a string which contains the raw binary data of |
| 24 // the PNG file. |
| 25 #define SET_WEBDRIVER_ERROR_WITH_SCREENSHOT(response, msg, err , png) \ |
| 26 response->SetError(err, msg, __FILE__, __LINE__, png); \ |
| 27 LOG(ERROR) << msg |
| 28 |
| 22 // A simple class that encapsulates the information describing the response to | 29 // A simple class that encapsulates the information describing the response to |
| 23 // a |Command|. In Webdriver all responses must be sent back as a JSON value, | 30 // a |Command|. In Webdriver all responses must be sent back as a JSON value, |
| 24 // conforming to the spec found at: | 31 // conforming to the spec found at: |
| 25 // http://code.google.com/p/selenium/wiki/JsonWireProtocol#Messages | 32 // http://code.google.com/p/selenium/wiki/JsonWireProtocol#Messages |
| 26 class Response { | 33 class Response { |
| 27 public: | 34 public: |
| 28 // Creates a new |Response| with a default status of |kSuccess| and a | 35 // Creates a new |Response| with a default status of |kSuccess| and a |
| 29 // |NullValue|. | 36 // |NullValue|. |
| 30 Response(); | 37 Response(); |
| 31 ~Response(); | 38 ~Response(); |
| 32 | 39 |
| 33 ErrorCode GetStatus() const; | 40 ErrorCode GetStatus() const; |
| 34 void SetStatus(ErrorCode status); | 41 void SetStatus(ErrorCode status); |
| 35 | 42 |
| 36 // Ownership of the returned pointer is kept by this object. | 43 // Ownership of the returned pointer is kept by this object. |
| 37 const Value* GetValue() const; | 44 const Value* GetValue() const; |
| 38 | 45 |
| 39 // Sets the |value| of this response, assuming ownership of the object in the | 46 // Sets the |value| of this response, assuming ownership of the object in the |
| 40 // process. | 47 // process. |
| 41 void SetValue(Value* value); | 48 void SetValue(Value* value); |
| 42 | 49 |
| 43 // Configures this response to report an error. The |file| and |line| | 50 // Configures this response to report an error. The |file| and |line| |
| 44 // parameters, which identify where in the source the error occurred, can be | 51 // parameters, which identify where in the source the error occurred, can be |
| 45 // set using the |SET_WEBDRIVER_ERROR| macro above. | 52 // set using the |SET_WEBDRIVER_ERROR| macro above. |
| 46 void SetError(ErrorCode error, const std::string& message, | 53 void SetError(ErrorCode error, const std::string& message, |
| 47 const std::string& file, int line); | 54 const std::string& file, int line); |
| 48 | 55 |
| 56 // Configures this response to report an error. The |file| and |line| |
| 57 // parameters, which identify where in the source the error occurred, can be |
| 58 // set using the |SET_WEBDRIVER_ERROR_WITH_SCREENSHOT| macro above. Includes |
| 59 // a screen shot of the tab which caused the error. |
| 60 void SetError(ErrorCode error, |
| 61 const std::string& message, |
| 62 const std::string& file, |
| 63 int line, |
| 64 const std::string& png); |
| 65 |
| 49 // Sets a JSON field in this response. The |key| may be a "." delimitted | 66 // Sets a JSON field in this response. The |key| may be a "." delimitted |
| 50 // string to indicate the value should be set in a nested object. Any | 67 // string to indicate the value should be set in a nested object. Any |
| 51 // previously set value for the |key| will be deleted. | 68 // previously set value for the |key| will be deleted. |
| 52 // This object assumes ownership of |value|. | 69 // This object assumes ownership of |value|. |
| 53 void SetField(const std::string& key, Value* value); | 70 void SetField(const std::string& key, Value* value); |
| 54 | 71 |
| 55 // Returns this response as a JSON string. | 72 // Returns this response as a JSON string. |
| 56 std::string ToJSON() const; | 73 std::string ToJSON() const; |
| 57 | 74 |
| 58 private: | 75 private: |
| 59 DictionaryValue data_; | 76 DictionaryValue data_; |
| 60 | 77 |
| 61 DISALLOW_COPY_AND_ASSIGN(Response); | 78 DISALLOW_COPY_AND_ASSIGN(Response); |
| 62 }; | 79 }; |
| 63 | 80 |
| 64 } // namespace webdriver | 81 } // namespace webdriver |
| 65 | 82 |
| 66 #endif // CHROME_TEST_WEBDRIVER_COMMANDS_RESPONSE_H_ | 83 #endif // CHROME_TEST_WEBDRIVER_COMMANDS_RESPONSE_H_ |
| 67 | 84 |
| OLD | NEW |