OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_TEST_WEBDRIVER_WEBDRIVER_UTIL_H_ |
| 6 #define CHROME_TEST_WEBDRIVER_WEBDRIVER_UTIL_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/compiler_specific.h" |
| 13 #include "chrome/test/automation/value_conversion_traits.h" |
| 14 |
| 15 namespace base { |
| 16 class Value; |
| 17 } |
| 18 |
| 19 namespace webdriver { |
| 20 |
| 21 // Generates a random, 32-character hexidecimal ID. |
| 22 std::string GenerateRandomID(); |
| 23 |
| 24 // Returns the equivalent JSON string for the given value. |
| 25 std::string JsonStringify(const base::Value* value); |
| 26 |
| 27 // Parses a given value. |
| 28 class ValueParser { |
| 29 public: |
| 30 virtual ~ValueParser(); |
| 31 virtual bool Parse(base::Value* value) const = 0; |
| 32 |
| 33 protected: |
| 34 ValueParser(); |
| 35 |
| 36 private: |
| 37 DISALLOW_COPY_AND_ASSIGN(ValueParser); |
| 38 }; |
| 39 |
| 40 // Define a special type and constant that allows users to skip parsing a value. |
| 41 // Useful when wanting to skip parsing for one value out of many in a list. |
| 42 enum SkipParsing { }; |
| 43 extern SkipParsing* kSkipParsing; |
| 44 |
| 45 // Parses a given value using the |ValueConversionTraits| to the appropriate |
| 46 // type. This assumes that a direct conversion can be performed without |
| 47 // pulling the value out of a dictionary or list. |
| 48 template <typename T> |
| 49 class DirectValueParser : public ValueParser { |
| 50 public: |
| 51 explicit DirectValueParser(T* t) : t_(t) { } |
| 52 |
| 53 virtual ~DirectValueParser() { } |
| 54 |
| 55 virtual bool Parse(base::Value* value) const OVERRIDE { |
| 56 return ValueConversionTraits<T>::SetFromValue(value, t_); |
| 57 } |
| 58 |
| 59 private: |
| 60 T* t_; |
| 61 DISALLOW_COPY_AND_ASSIGN(DirectValueParser); |
| 62 }; |
| 63 |
| 64 // Convenience function for creating a DirectValueParser. |
| 65 template <typename T> |
| 66 DirectValueParser<T>* CreateDirectValueParser(T* t) { |
| 67 return new DirectValueParser<T>(t); |
| 68 } |
| 69 |
| 70 } // namespace webdriver |
| 71 |
| 72 // Value conversion traits for SkipParsing, which just return true. |
| 73 template <> |
| 74 struct ValueConversionTraits<webdriver::SkipParsing> { |
| 75 static bool SetFromValue(const base::Value* value, |
| 76 const webdriver::SkipParsing* t); |
| 77 static bool CanConvert(const base::Value* value); |
| 78 }; |
| 79 |
| 80 #endif // CHROME_TEST_WEBDRIVER_WEBDRIVER_UTIL_H_ |
OLD | NEW |