b9f2cb88d9529de21eae4d5f109bd71f349038b5
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / shared / base-wrapped-object.h
1 #ifndef __DALI_V8PLUGIN_BASE_WRAPPED_OBJECT_H__
2 #define __DALI_V8PLUGIN_BASE_WRAPPED_OBJECT_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <v8.h>
23
24 // INTERNAL INCLUDES
25 #include <interfaces/garbage-collector-interface.h>
26
27
28 namespace Dali
29 {
30
31 namespace V8Plugin
32 {
33
34 class SignalManager;
35
36 /**
37  * @brief Used as a base class for all wrapped objects.
38  *
39  *  Dali JavaScript objects look like this
40  *
41  * \code
42  *  _______________________________________         ________________________________________
43  * | JavaScript Object                     |       |  C++ WrappedObject  (e.g. ImageWrapper)|
44  * |---------------------------------------|       |----------------------------------------|
45  * | Hidden internal fields                |       | Handle to a Dali::Image object         |
46  * | *Pointer to a BaseWrappedObject       | ----> |________________________________________|
47  * | Type of wrapped object (e.g. Image)   |
48  * -----------------------------------------
49  *
50  * \endcode
51  *
52  * Whenever we want to access functions / properties of that wrapped object, we unwrap it
53  * to get access to the Dali object.
54  *
55  * Each wrapped object registers with Dali garbage collector so they can be deleted
56  * when Dali shuts down
57  */
58 class BaseWrappedObject
59 {
60
61 public:
62
63   /*
64    * A type enum for the wrapped object
65    */
66   enum Type
67     {
68       UNKNOWN              = -1,
69       HANDLE               = 0 ,
70       HANDLE_SIGNAL,
71       CONNECTION,
72       ANIMATION,
73       PATH,
74       PATH_CONSTRAINER,
75       LINEAR_CONSTRAINER,
76       BUILDER,
77       STAGE,
78       FONT,
79       IMAGE,
80       IMAGE_ATTRIBUTES,
81       ACTOR,
82       ACTOR_PROPERTY,
83       RENDER_TASK,
84       RENDER_TASK_LIST,
85       TIMER,
86       TAP_GESTURE,
87       SHADER_EFFECT,
88       PAN_GESTURE,
89       LONGPRESS_GESTURE,
90       PINCH_GESTURE,
91       KEYBOARD_FOCUS_MANAGER,
92       PAN_GESTURE_DETECTOR,
93       ////////////////////////////////////////////////////////
94       PROPERTY_VALUE_START_RANGE,  // start of property values
95       VECTOR2,
96       VECTOR3,
97       VECTOR4,
98       MATRIX,
99       MATRIX3,
100       RECTANGLE,
101       ROTATION,
102       PROPERTY_VALUE_END_RANGE,  // end of property values
103       ///////////////////////////////////////////////////////
104     };
105
106   /*
107    * A type enum for the wrapped object internal field
108    */
109   enum Field
110     {
111       FIELD_POINTER   = 0,
112       FIELD_TYPE      = 1,
113       FIELD_COUNT     = 2 // increase if more fields are added
114     };
115
116    /**
117    * @brief virtual destructor
118    */
119   virtual ~BaseWrappedObject();
120
121   /**
122    * @return true if nothing else is referencing this object
123    */
124   bool IsReferenced();
125
126   /**
127    * @brief Called when the v8 garbage collector decides the JavaScript object (which contains the Dali wrapped object)
128    * is no longer used / reachable.
129    */
130   static void WeakCallback( const v8::WeakCallbackData<v8::Object,BaseWrappedObject >& data);
131
132   /**
133    * @brief create and set the internal fields of a JavaScript object.
134    * Currently has 2 internal fields, 1 is a pointer to dali wrapped object, 2nd is the type.
135    */
136   void SetJavascriptObject( v8::Isolate* isolate, v8::Local<v8::Object>& object );
137
138   /**
139    * @return true if the object is of a certain wrapped type (e.g. ImageActor )
140    */
141   static bool IsWrappedType( v8::Isolate* isolate, const v8::Local<v8::Object>& object,  BaseWrappedObject::Type type);
142
143   /**
144    * @return true if the object is a wrapped property value
145    */
146   static bool IsWrappedTypeAPropertyValue(const v8::Local<v8::Object>& object);
147
148   /**
149    * @brief Extracts the Dali wrapped object from the javascript object
150    */
151   static BaseWrappedObject* UnWrap( v8::Isolate* isolate, const v8::Local<v8::Object>& object);
152
153   /**
154    * @return the wrapped type
155    */
156   Type GetType();
157
158   virtual SignalManager* GetSignalManager();
159
160 protected:
161
162   /**
163    * Constructor
164    * @param type wrapped type
165    * @param gc garbage collector interface
166    */
167   BaseWrappedObject( Type type, GarbageCollectorInterface& gc );
168
169 private:
170
171   BaseWrappedObject();
172
173   /**
174    * We store a  persistent handle to the JavaScript object and then set it to weak.
175    * This means we be notified when v8 decides it's no longer required and we can delete
176    * the associated Dali object
177    */
178   v8::Persistent<v8::Object > mWeakPersistentHandle;
179   Type mWrappedType;                                    ///< wrapped type
180   GarbageCollectorInterface& mGarbageCollector;         ///< Dali garbage collector
181 };
182
183 } // V8Plugin
184
185 } // Dali
186
187 #endif // header __DALI_V8PLUGIN_DALI_OBJECT_LIFETIME_MANAGER_H__