blob: 1606fb5396fe6222674b518355ee4ede8460b37c [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepez19922bb2015-05-28 13:23:12 -07007#ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_VALUE_H_
8#define FPDFSDK_INCLUDE_JAVASCRIPT_JS_VALUE_H_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
Tom Sepez9a3f8122015-04-07 15:35:48 -070010#include "../../../core/include/fxcrt/fx_basic.h"
Tom Sepez19922bb2015-05-28 13:23:12 -070011#include "../jsapi/fxjs_v8.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070012
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013class CJS_Array;
14class CJS_Date;
Tom Sepezf79a69c2014-10-30 13:23:42 -070015class CJS_Document;
16class CJS_Object;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017
Nico Weber9d8ec5a2015-08-04 13:00:21 -070018class CJS_Value {
19 public:
20 CJS_Value(v8::Isolate* isolate);
21 CJS_Value(v8::Isolate* isolate, v8::Local<v8::Value> pValue, FXJSVALUETYPE t);
22 CJS_Value(v8::Isolate* isolate, const int& iValue);
23 CJS_Value(v8::Isolate* isolate, const double& dValue);
24 CJS_Value(v8::Isolate* isolate, const float& fValue);
25 CJS_Value(v8::Isolate* isolate, const bool& bValue);
26 CJS_Value(v8::Isolate* isolate, JSFXObject);
27 CJS_Value(v8::Isolate* isolate, CJS_Object*);
28 CJS_Value(v8::Isolate* isolate, CJS_Document*);
29 CJS_Value(v8::Isolate* isolate, const FX_CHAR* pStr);
30 CJS_Value(v8::Isolate* isolate, const FX_WCHAR* pWstr);
31 CJS_Value(v8::Isolate* isolate, CJS_Array& array);
Tom Sepezf79a69c2014-10-30 13:23:42 -070032
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 ~CJS_Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035 void SetNull();
36 void Attach(v8::Local<v8::Value> pValue, FXJSVALUETYPE t);
37 void Attach(CJS_Value* pValue);
38 void Detach();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040 int ToInt() const;
41 bool ToBool() const;
42 double ToDouble() const;
43 float ToFloat() const;
44 CJS_Object* ToCJSObject() const;
45 CFX_WideString ToCFXWideString() const;
46 CFX_ByteString ToCFXByteString() const;
47 v8::Local<v8::Object> ToV8Object() const;
48 v8::Local<v8::Array> ToV8Array() const;
49 v8::Local<v8::Value> ToV8Value() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 void operator=(int iValue);
52 void operator=(bool bValue);
53 void operator=(double);
54 void operator=(float);
55 void operator=(CJS_Object*);
56 void operator=(CJS_Document*);
57 void operator=(v8::Local<v8::Object>);
58 void operator=(CJS_Array&);
59 void operator=(CJS_Date&);
60 void operator=(const FX_WCHAR* pWstr);
61 void operator=(const FX_CHAR* pStr);
62 void operator=(CJS_Value value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 FX_BOOL IsArrayObject() const;
65 FX_BOOL IsDateObject() const;
66 FXJSVALUETYPE GetType() const;
Tom Sepezf79a69c2014-10-30 13:23:42 -070067
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 FX_BOOL ConvertToArray(CJS_Array&) const;
69 FX_BOOL ConvertToDate(CJS_Date&) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 v8::Isolate* GetIsolate() { return m_isolate; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070072
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 protected:
74 v8::Local<v8::Value> m_pValue;
75 FXJSVALUETYPE m_eType;
76 v8::Isolate* m_isolate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077};
78
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079class CJS_Parameters : public CFX_ArrayTemplate<CJS_Value> {
80 public:
81 void push_back(const CJS_Value& newElement) {
82 CFX_ArrayTemplate<CJS_Value>::Add(newElement);
83 }
84 int size() const { return CFX_ArrayTemplate<CJS_Value>::GetSize(); }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087class CJS_PropValue : public CJS_Value {
88 public:
89 CJS_PropValue(const CJS_Value&);
90 CJS_PropValue(v8::Isolate* isolate);
91 ~CJS_PropValue();
92
93 public:
94 FX_BOOL IsSetting();
95 FX_BOOL IsGetting();
96 void operator<<(int);
97 void operator>>(int&) const;
98 void operator<<(bool);
99 void operator>>(bool&) const;
100 void operator<<(double);
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<<(JSFXObject);
112 void operator>>(JSFXObject&) 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 operator v8::Local<v8::Value>() const;
118 void StartSetting();
119 void StartGetting();
120
121 private:
122 FX_BOOL m_bIsSetting;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700123};
124
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125class CJS_Array {
126 public:
127 CJS_Array(v8::Isolate* isolate);
128 virtual ~CJS_Array();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 void Attach(v8::Local<v8::Array> pArray);
131 void GetElement(unsigned index, CJS_Value& value);
132 void SetElement(unsigned index, CJS_Value value);
133 int GetLength();
134 FX_BOOL IsAttached();
135 operator v8::Local<v8::Array>();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700136
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 v8::Isolate* GetIsolate() { return m_isolate; }
138
139 private:
140 v8::Local<v8::Array> m_pArray;
141 v8::Isolate* m_isolate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142};
143
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144class CJS_Date {
145 friend class CJS_Value;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 public:
148 CJS_Date(v8::Isolate* isolate);
149 CJS_Date(v8::Isolate* isolate, double dMsec_time);
150 CJS_Date(v8::Isolate* isolate,
151 int year,
152 int mon,
153 int day,
154 int hour,
155 int min,
156 int sec);
157 virtual ~CJS_Date();
158 void Attach(v8::Local<v8::Value> pDate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 int GetYear();
161 void SetYear(int iYear);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 int GetMonth();
164 void SetMonth(int iMonth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700165
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 int GetDay();
167 void SetDay(int iDay);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 int GetHours();
170 void SetHours(int iHours);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 int GetMinutes();
173 void SetMinutes(int minutes);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 int GetSeconds();
176 void SetSeconds(int seconds);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700177
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 operator v8::Local<v8::Value>();
179 operator double() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181 CFX_WideString ToString() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700182
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 static double
184 MakeDate(int year, int mon, int mday, int hour, int min, int sec, int ms);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700185
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 FX_BOOL IsValidDate();
187
188 protected:
189 v8::Local<v8::Value> m_pDate;
190 v8::Isolate* m_isolate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191};
192
Tom Sepez19922bb2015-05-28 13:23:12 -0700193#endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_VALUE_H_