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_WEBDRIVER_UTIL_H_ 6 #define CHROME_TEST_WEBDRIVER_WEBDRIVER_UTIL_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/compiler_specific.h" 13 #include "base/values.h" 14 #include "chrome/test/automation/value_conversion_traits.h" 15 #include "chrome/test/webdriver/webdriver_error.h" 16 17 class AutomationId; 18 class WebViewId; 19 20 namespace base { 21 class FilePath; 22 } 23 24 namespace webdriver { 25 26 // Generates a random, 32-character hexidecimal ID. 27 std::string GenerateRandomID(); 28 29 // Decodes the given base64-encoded string, after removing any newlines, 30 // which are required in some base64 standards. 31 // Returns true on success. 32 bool Base64Decode(const std::string& base64, std::string* bytes); 33 34 // Unzip the given zip archive, after base64 decoding, into the given directory. 35 // Returns true on success. 36 bool Base64DecodeAndUnzip(const base::FilePath& unzip_dir, 37 const std::string& base64, 38 std::string* error_msg); 39 40 // Unzips the sole file contained in the given zip data |bytes| into 41 // |unzip_dir|. The zip data may be a normal zip archive or a single zip file 42 // entry. If the unzip successfully produced one file, returns true and sets 43 // |file| to the unzipped file. 44 // TODO(kkania): Remove the ability to parse single zip file entries when 45 // the current versions of all WebDriver clients send actual zip files. 46 bool UnzipSoleFile(const base::FilePath& unzip_dir, 47 const std::string& bytes, 48 base::FilePath* file, 49 std::string* error_msg); 50 51 // Returns the equivalent JSON string for the given value. 52 std::string JsonStringify(const base::Value* value); 53 54 // Returns the JSON string for the given value, with the exception that 55 // long strings are shortened for easier display. 56 std::string JsonStringifyForDisplay(const base::Value* value); 57 58 // Returns the string representation of the given type, for display purposes. 59 const char* GetJsonTypeName(base::Value::Type type); 60 61 // Converts the automation ID to a string. 62 std::string AutomationIdToString(const AutomationId& id); 63 64 // Converts the string to an automation ID and returns true on success. 65 bool StringToAutomationId(const std::string& string_id, AutomationId* id); 66 67 // Converts the web view ID to a string. 68 std::string WebViewIdToString(const WebViewId& view_id); 69 70 // Converts the string to a web view ID and returns true on success. 71 bool StringToWebViewId(const std::string& string_id, WebViewId* view_id); 72 73 // Flattens the given list of strings into one. 74 Error* FlattenStringArray(const ListValue* src, string16* dest); 75 76 #if defined(OS_MACOSX) 77 // Gets the paths to the user and local application directory. 78 void GetApplicationDirs(std::vector<base::FilePath>* app_dirs); 79 #endif 80 81 // Parses a given value. 82 class ValueParser { 83 public: 84 virtual ~ValueParser(); 85 virtual bool Parse(base::Value* value) const = 0; 86 87 protected: 88 ValueParser(); 89 90 private: 91 DISALLOW_COPY_AND_ASSIGN(ValueParser); 92 }; 93 94 // Define a special type and constant that allows users to skip parsing a value. 95 // Useful when wanting to skip parsing for one value out of many in a list. 96 enum SkipParsing { }; 97 extern SkipParsing* kSkipParsing; 98 99 // Parses a given value using the |ValueConversionTraits| to the appropriate 100 // type. This assumes that a direct conversion can be performed without 101 // pulling the value out of a dictionary or list. 102 template <typename T> 103 class DirectValueParser : public ValueParser { 104 public: 105 explicit DirectValueParser(T* t) : t_(t) { } 106 107 virtual ~DirectValueParser() { } 108 109 virtual bool Parse(base::Value* value) const OVERRIDE { 110 return ValueConversionTraits<T>::SetFromValue(value, t_); 111 } 112 113 private: 114 T* t_; 115 DISALLOW_COPY_AND_ASSIGN(DirectValueParser); 116 }; 117 118 // Convenience function for creating a DirectValueParser. 119 template <typename T> 120 DirectValueParser<T>* CreateDirectValueParser(T* t) { 121 return new DirectValueParser<T>(t); 122 } 123 124 } // namespace webdriver 125 126 // Value conversion traits for SkipParsing, which just return true. 127 template <> 128 struct ValueConversionTraits<webdriver::SkipParsing> { 129 static bool SetFromValue(const base::Value* value, 130 const webdriver::SkipParsing* t); 131 static bool CanConvert(const base::Value* value); 132 }; 133 134 #endif // CHROME_TEST_WEBDRIVER_WEBDRIVER_UTIL_H_ 135