1 // Copyright 2014 PDFium 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef FPDFSDK_JAVASCRIPT_JS_VALUE_H_ 8 #define FPDFSDK_JAVASCRIPT_JS_VALUE_H_ 9 10 #include <vector> 11 12 #include "core/fxcrt/fx_basic.h" 13 #include "fxjs/fxjs_v8.h" 14 15 class CJS_Array; 16 class CJS_Date; 17 class CJS_Document; 18 class CJS_Object; 19 class CJS_Runtime; 20 21 class CJS_Value { 22 public: 23 enum Type { 24 VT_unknown, 25 VT_string, 26 VT_number, 27 VT_boolean, 28 VT_date, 29 VT_object, 30 VT_null, 31 VT_undefined 32 }; 33 34 explicit CJS_Value(CJS_Runtime* pRuntime); 35 CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue); 36 CJS_Value(CJS_Runtime* pRuntime, const int& iValue); 37 CJS_Value(CJS_Runtime* pRuntime, const double& dValue); 38 CJS_Value(CJS_Runtime* pRuntime, const float& fValue); 39 CJS_Value(CJS_Runtime* pRuntime, const bool& bValue); 40 CJS_Value(CJS_Runtime* pRuntime, CJS_Object* pObj); 41 CJS_Value(CJS_Runtime* pRuntime, const FX_CHAR* pStr); 42 CJS_Value(CJS_Runtime* pRuntime, const FX_WCHAR* pWstr); 43 CJS_Value(CJS_Runtime* pRuntime, const CJS_Array& array); 44 CJS_Value(CJS_Runtime* pRuntime, const CJS_Date& date); 45 CJS_Value(CJS_Runtime* pRuntime, const CJS_Object* object); 46 CJS_Value(const CJS_Value& other); 47 48 ~CJS_Value(); 49 50 void SetNull(CJS_Runtime* pRuntime); 51 void SetValue(const CJS_Value& other); 52 void Attach(v8::Local<v8::Value> pValue); 53 void Detach(); 54 55 static Type GetValueType(v8::Local<v8::Value> value); GetType()56 Type GetType() const { return GetValueType(m_pValue); } 57 58 int ToInt(CJS_Runtime* pRuntime) const; 59 bool ToBool(CJS_Runtime* pRuntime) const; 60 double ToDouble(CJS_Runtime* pRuntime) const; 61 float ToFloat(CJS_Runtime* pRuntime) const; 62 CJS_Object* ToCJSObject(CJS_Runtime* pRuntime) const; 63 CFX_WideString ToCFXWideString(CJS_Runtime* pRuntime) const; 64 CFX_ByteString ToCFXByteString(CJS_Runtime* pRuntime) const; 65 v8::Local<v8::Object> ToV8Object(CJS_Runtime* pRuntime) const; 66 v8::Local<v8::Array> ToV8Array(CJS_Runtime* pRuntime) const; 67 v8::Local<v8::Value> ToV8Value(CJS_Runtime* pRuntime) const; 68 69 // Replace the current |m_pValue| with a v8::Number if possible 70 // to make one from the current |m_pValue|. 71 void MaybeCoerceToNumber(CJS_Runtime* pRuntime); 72 73 bool IsArrayObject() const; 74 bool IsDateObject() const; 75 bool ConvertToArray(CJS_Runtime* pRuntime, CJS_Array&) const; 76 bool ConvertToDate(CJS_Runtime* pRuntime, CJS_Date&) const; 77 78 protected: 79 v8::Local<v8::Value> m_pValue; 80 }; 81 82 class CJS_PropValue { 83 public: 84 explicit CJS_PropValue(CJS_Runtime* pRuntime); 85 CJS_PropValue(CJS_Runtime* pRuntime, const CJS_Value&); 86 ~CJS_PropValue(); 87 StartSetting()88 void StartSetting() { m_bIsSetting = true; } StartGetting()89 void StartGetting() { m_bIsSetting = false; } IsSetting()90 bool IsSetting() const { return m_bIsSetting; } IsGetting()91 bool IsGetting() const { return !m_bIsSetting; } GetJSRuntime()92 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; } GetJSValue()93 CJS_Value* GetJSValue() { return &m_Value; } 94 95 // These calls may re-enter JS (and hence invalidate objects). 96 void operator<<(int val); 97 void operator>>(int&) const; 98 void operator<<(bool val); 99 void operator>>(bool&) const; 100 void operator<<(double val); 101 void operator>>(double&) const; 102 void operator<<(CJS_Object* pObj); 103 void operator>>(CJS_Object*& ppObj) const; 104 void operator<<(CJS_Document* pJsDoc); 105 void operator>>(CJS_Document*& ppJsDoc) const; 106 void operator<<(CFX_ByteString); 107 void operator>>(CFX_ByteString&) const; 108 void operator<<(CFX_WideString); 109 void operator>>(CFX_WideString&) const; 110 void operator<<(const FX_WCHAR* c_string); 111 void operator<<(v8::Local<v8::Object>); 112 void operator>>(v8::Local<v8::Object>&) const; 113 void operator>>(CJS_Array& array) const; 114 void operator<<(CJS_Array& array); 115 void operator<<(CJS_Date& date); 116 void operator>>(CJS_Date& date) const; 117 118 private: 119 bool m_bIsSetting; 120 CJS_Value m_Value; 121 CJS_Runtime* const m_pJSRuntime; 122 }; 123 124 class CJS_Array { 125 public: 126 CJS_Array(); 127 CJS_Array(const CJS_Array& other); 128 virtual ~CJS_Array(); 129 130 void Attach(v8::Local<v8::Array> pArray); 131 int GetLength(CJS_Runtime* pRuntime) const; 132 133 // These two calls may re-enter JS (and hence invalidate objects). 134 void GetElement(CJS_Runtime* pRuntime, 135 unsigned index, 136 CJS_Value& value) const; 137 void SetElement(CJS_Runtime* pRuntime, 138 unsigned index, 139 const CJS_Value& value); 140 141 v8::Local<v8::Array> ToV8Array(CJS_Runtime* pRuntime) const; 142 143 private: 144 mutable v8::Local<v8::Array> m_pArray; 145 }; 146 147 class CJS_Date { 148 public: 149 CJS_Date(); 150 CJS_Date(CJS_Runtime* pRuntime, double dMsec_time); 151 CJS_Date(CJS_Runtime* pRuntime, 152 int year, 153 int mon, 154 int day, 155 int hour, 156 int min, 157 int sec); 158 virtual ~CJS_Date(); 159 160 void Attach(v8::Local<v8::Date> pDate); 161 bool IsValidDate(CJS_Runtime* pRuntime) const; 162 163 int GetYear(CJS_Runtime* pRuntime) const; 164 void SetYear(CJS_Runtime* pRuntime, int iYear); 165 166 int GetMonth(CJS_Runtime* pRuntime) const; 167 void SetMonth(CJS_Runtime* pRuntime, int iMonth); 168 169 int GetDay(CJS_Runtime* pRuntime) const; 170 void SetDay(CJS_Runtime* pRuntime, int iDay); 171 172 int GetHours(CJS_Runtime* pRuntime) const; 173 void SetHours(CJS_Runtime* pRuntime, int iHours); 174 175 int GetMinutes(CJS_Runtime* pRuntime) const; 176 void SetMinutes(CJS_Runtime* pRuntime, int minutes); 177 178 int GetSeconds(CJS_Runtime* pRuntime) const; 179 void SetSeconds(CJS_Runtime* pRuntime, int seconds); 180 181 v8::Local<v8::Date> ToV8Date(CJS_Runtime* pRuntime) const; 182 double ToDouble(CJS_Runtime* pRuntime) const; 183 CFX_WideString ToString(CJS_Runtime* pRuntime) const; 184 185 protected: 186 v8::Local<v8::Date> m_pDate; 187 }; 188 189 double JS_GetDateTime(); 190 int JS_GetYearFromTime(double dt); 191 int JS_GetMonthFromTime(double dt); 192 int JS_GetDayFromTime(double dt); 193 int JS_GetHourFromTime(double dt); 194 int JS_GetMinFromTime(double dt); 195 int JS_GetSecFromTime(double dt); 196 double JS_DateParse(const CFX_WideString& str); 197 double JS_MakeDay(int nYear, int nMonth, int nDay); 198 double JS_MakeTime(int nHour, int nMin, int nSec, int nMs); 199 double JS_MakeDate(double day, double time); 200 bool JS_PortIsNan(double d); 201 double JS_LocalTime(double d); 202 203 // Some JS methods have the bizarre convention that they may also be called 204 // with a single argument which is an object containing the actual arguments 205 // as its properties. The varying arguments to this method are the property 206 // names as wchar_t string literals corresponding to each positional argument. 207 // The result will always contain |nKeywords| value, with unspecified ones 208 // being set to type VT_unknown. 209 std::vector<CJS_Value> JS_ExpandKeywordParams( 210 CJS_Runtime* pRuntime, 211 const std::vector<CJS_Value>& originals, 212 size_t nKeywords, 213 ...); 214 215 #endif // FPDFSDK_JAVASCRIPT_JS_VALUE_H_ 216