162a7bbc2e398e5a04de15a216f4d1b38d137721
[platform/framework/web/wrt-plugins-tizen.git] / src / Filesystem / JSFile.h
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18  
19 #ifndef TIZENAPIS_TIZEN_JSFILE_H_
20 #define TIZENAPIS_TIZEN_JSFILE_H_
21
22 #include <JavaScriptCore/JavaScript.h>
23 #include <Commons/IEvent.h>
24 #include <CommonsJavaScript/PrivateObject.h>
25 #include <CommonsJavaScript/JSCallbackManager.h>
26 #include "INode.h"
27
28 namespace DeviceAPI {
29 namespace Filesystem {
30 class JSFile
31 {
32 public:
33
34     class PrivateObjectDef
35     {
36     public:
37
38         typedef std::vector<int> PermissionList;
39
40         PrivateObjectDef(const INodePtr &node,
41                          const PermissionList &parentPermissions) :
42             m_node(node),
43             m_parentPerms(parentPermissions)
44         {
45         }
46
47         virtual ~PrivateObjectDef()
48         {
49         }
50
51         INodePtr getNode() const
52         {
53             return m_node;
54         }
55
56         PermissionList getParentPermissions() const
57         {
58             return m_parentPerms;
59         }
60
61         void setParentPermissions(const PermissionList &permissions)
62         {
63             m_parentPerms = permissions;
64         }
65
66         void pushParentPermissions(int permissions)
67         {
68             m_parentPerms.push_back(permissions);
69         }
70
71     private:
72         INodePtr m_node;
73         PermissionList m_parentPerms;
74     };
75
76     class ListFilesPrivateData : public WrtDeviceApis::Commons::IEventPrivateData
77     {
78     public:
79         ListFilesPrivateData(
80                 const WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr &callbackManager,
81                 const PrivateObjectDef::PermissionList parentPermissions) :
82             m_callbackManager(callbackManager),
83             m_parentPermissions(parentPermissions)
84         {
85         }
86
87         virtual ~ListFilesPrivateData()
88         {
89         }
90
91         WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr getCallbackManager() const
92         {
93             return m_callbackManager;
94         }
95
96         PrivateObjectDef::PermissionList getParentPermissions() const
97         {
98             return m_parentPermissions;
99         }
100
101     private:
102         WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr m_callbackManager;
103         PrivateObjectDef::PermissionList m_parentPermissions;
104     };
105
106     typedef DPL::SharedPtr<ListFilesPrivateData> ListFilesPrivateDataPtr;
107     typedef DPL::SharedPtr<PrivateObjectDef> PrivateObjectDefPtr;
108     typedef WrtDeviceApis::CommonsJavaScript::PrivateObjectT<PrivateObjectDefPtr>::Type PrivateObject;
109
110 public:
111         static const JSClassDefinition* getClassInfo();
112
113         static const JSClassRef getClassRef();
114
115 private:
116
117         /**
118         * The callback invoked when an object is first created.
119         */
120         static void initialize(JSContextRef context,
121                 JSObjectRef object);
122
123         /**
124         * The callback invoked when an object is finalized.
125         */
126         static void finalize(JSObjectRef object);
127
128         /**
129         * The callback invoked when getting a property's value.
130         */
131         static JSValueRef getProperty(JSContextRef context,
132                 JSObjectRef object,
133                 JSStringRef propertyName,
134                 JSValueRef* exception);
135
136         /**
137         * The callback invoked when collecting the names of an object's properties.
138         */
139         static void getPropertyNames(JSContextRef context,
140                 JSObjectRef object,
141                 JSPropertyNameAccumulatorRef propertyNames);
142
143         /**
144         * The callback invoked when an object is used as the target of an 'instanceof' expression.
145         */
146         static bool hasInstance(JSContextRef context,
147                 JSObjectRef constructor,
148                 JSValueRef possibleInstance,
149                 JSValueRef* exception);
150
151         /**
152         * Returns a URI for the file.
153         */
154         static JSValueRef toUri(JSContextRef context,
155                 JSObjectRef object,
156                 JSObjectRef thisObject,
157                 size_t argumentCount,
158                 const JSValueRef arguments[],
159                 JSValueRef* exception);
160
161         /**
162         * Returns list of all files of this directory.
163         */
164         static JSValueRef listFiles(JSContextRef context,
165                 JSObjectRef object,
166                 JSObjectRef thisObject,
167                 size_t argumentCount,
168                 const JSValueRef arguments[],
169                 JSValueRef* exception);
170
171         /**
172         * WRT_GeneralError exception
173         */
174         static JSValueRef openStream(JSContextRef context,
175                 JSObjectRef object,
176                 JSObjectRef thisObject,
177                 size_t argumentCount,
178                 const JSValueRef arguments[],
179                 JSValueRef* exception);
180
181         /**
182         * WRT_GeneralError exception
183         */
184         static JSValueRef readAsText(JSContextRef context,
185                 JSObjectRef object,
186                 JSObjectRef thisObject,
187                 size_t argumentCount,
188                 const JSValueRef arguments[],
189                 JSValueRef* exception);
190
191         /**
192         * The operation is only for file except for directory.
193         * Make sure the dest directory already exists.
194         */
195         static JSValueRef copyTo(JSContextRef context,
196                 JSObjectRef object,
197                 JSObjectRef thisObject,
198                 size_t argumentCount,
199                 const JSValueRef arguments[],
200                 JSValueRef* exception);
201
202         /**
203         * This operation is only for file not directory
204         * The dest directory should exists in local file system, or the operation fails.
205         */
206         static JSValueRef moveTo(JSContextRef context,
207                 JSObjectRef object,
208                 JSObjectRef thisObject,
209                 size_t argumentCount,
210                 const JSValueRef arguments[],
211                 JSValueRef* exception);
212
213         /**
214         * create directory even through the parent directories do not  exist in local file system.
215         */
216         static JSValueRef createDirectory(JSContextRef context,
217                 JSObjectRef object,
218                 JSObjectRef thisObject,
219                 size_t argumentCount,
220                 const JSValueRef arguments[],
221                 JSValueRef* exception);
222
223         /**
224         * Creates a new empty file in a specified location.
225         */
226         static JSValueRef createFile(JSContextRef context,
227                 JSObjectRef object,
228                 JSObjectRef thisObject,
229                 size_t argumentCount,
230                 const JSValueRef arguments[],
231                 JSValueRef* exception);
232
233         /**
234         * Resolves an existing file or directory relative to
235         * the current directory this operation is performed on; and
236         * returns a file handle for it.
237         */
238         static JSValueRef resolve(JSContextRef context,
239                 JSObjectRef object,
240                 JSObjectRef thisObject,
241                 size_t argumentCount,
242                 const JSValueRef arguments[],
243                 JSValueRef* exception);
244
245         /**
246          * Removes all files from specified directory if recursive is true, 
247          * or just remove the directory itself when there is no files or directories underneath it
248          */
249         static JSValueRef deleteDirectory(JSContextRef context,
250                 JSObjectRef object,
251                 JSObjectRef thisObject,
252                 size_t argumentCount,
253                 const JSValueRef arguments[],
254                 JSValueRef* exception);
255
256         /**
257         * Deletes a specified file.
258         */
259         static JSValueRef deleteFile(JSContextRef context,
260                 JSObjectRef object,
261                 JSObjectRef thisObject,
262                 size_t argumentCount,
263                 const JSValueRef arguments[],
264                 JSValueRef* exception);
265
266         /**
267         * This structure describes a statically declared function property.
268         */
269         static JSStaticFunction m_functions[];
270
271         /**
272         * This structure describes a statically declared value property.
273         */
274         static JSStaticValue m_properties[];
275
276         /**
277         * This structure contains properties and callbacks that define a type of object.
278         */
279         static JSClassDefinition m_classInfo;
280
281         static JSClassRef m_classRef;
282 };
283 }
284 }
285
286 #endif