2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "JSFilestream.h"
20 #include <dpl/scoped_array.h>
21 #include <dpl/log/log.h>
23 #include <Commons/Base64.h>
24 #include <CommonsJavaScript/JSUtils.h>
25 #include <Tizen/Common/JSTizenExceptionFactory.h>
26 #include <Tizen/Common/JSTizenException.h>
27 #include "Converter.h"
28 #include "plugin_config.h"
31 const char* PLUGIN_NAME = "FileStream";
32 const char* PROPERTY_EOF = "eof";
33 const char* PROPERTY_POSITION = "position";
34 const char* PROPERTY_BYTES_AVAILABLE = "bytesAvailable";
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace TizenApis::Commons;
43 JSClassRef JSFilestream::m_classRef = 0;
45 JSClassDefinition JSFilestream::m_classInfo = {
47 kJSClassAttributeNone,
65 JSStaticValue JSFilestream::m_properties[] = {
66 { PROPERTY_EOF, getProperty, NULL, kJSPropertyAttributeReadOnly },
67 { PROPERTY_POSITION, getProperty, setProperty, kJSPropertyAttributeNone },
68 { PROPERTY_BYTES_AVAILABLE, getProperty, NULL, kJSPropertyAttributeReadOnly },
72 JSStaticFunction JSFilestream::m_functions[] = {
73 { "close", close, kJSPropertyAttributeNone },
74 { "read", read, kJSPropertyAttributeNone },
75 { "readBytes", readBytes, kJSPropertyAttributeNone },
76 { "readBase64", readBase64, kJSPropertyAttributeNone },
77 { "write", write, kJSPropertyAttributeNone },
78 { "writeBytes", writeBytes, kJSPropertyAttributeNone },
79 { "writeBase64", writeBase64, kJSPropertyAttributeNone },
83 void JSFilestream::initialize(JSContextRef context,
88 void JSFilestream::finalize(JSObjectRef object)
90 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(object));
94 const JSClassRef JSFilestream::getClassRef()
97 m_classRef = JSClassCreate(&m_classInfo);
102 const JSClassDefinition* JSFilestream::getClassInfo()
107 JSValueRef JSFilestream::getProperty(JSContextRef context,
109 JSStringRef propertyName,
110 JSValueRef* exception)
112 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(object));
113 if (!privateObject) {
114 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
118 PrivateObject::ObjectType stream = privateObject->getObject();
119 Converter converter(context);
121 if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_EOF)) {
122 return converter.toJSValueRef(stream->isEof());
123 } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_POSITION)) {
124 long pos = stream->getPosition();
126 return JSValueMakeUndefined(context);
128 return converter.toJSValueRef(static_cast<unsigned long>(pos));
129 } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_BYTES_AVAILABLE)) {
130 long bytes = stream->getSize() - stream->getPosition();
131 return converter.toJSValueRef(static_cast<unsigned long>(bytes));
133 } catch (const WrtDeviceApis::Commons::ConversionException& ex) {
134 LogWarning("trying to get incorrect value");
137 return JSValueMakeUndefined(context);
140 bool JSFilestream::setProperty(JSContextRef context,
142 JSStringRef propertyName,
144 JSValueRef* exception)
146 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(object));
147 if (!privateObject) {
151 Converter converter(context);
153 if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_POSITION)) {
154 privateObject->getObject()->setPosition(converter.toLong(value));
157 } catch (const WrtDeviceApis::Commons::Exception& ex) {
158 LogWarning("trying to set incorrect value");
164 void JSFilestream::getPropertyNames(JSContextRef context,
166 JSPropertyNameAccumulatorRef propertyNames)
170 bool JSFilestream::hasInstance(JSContextRef context,
171 JSObjectRef constructor,
173 JSValueRef* exception)
175 return JSValueIsObjectOfClass(context, instance, JSFilestream::getClassRef());
178 JSValueRef JSFilestream::close(JSContextRef context,
180 JSObjectRef thisObject,
182 const JSValueRef argv[],
183 JSValueRef* exception)
185 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
186 if (!privateObject) {
187 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
192 privateObject->getObject()->close();
193 } Catch (WrtDeviceApis::Commons::PlatformException) {
194 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
197 return JSValueMakeUndefined(context);
200 JSValueRef JSFilestream::read(JSContextRef context,
202 JSObjectRef thisObject,
204 const JSValueRef argv[],
205 JSValueRef* exception)
207 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
208 if (!privateObject) {
209 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
212 Converter converter(context);
214 JSValueRef undefinedValue = JSValueMakeUndefined(context);
215 unsigned long count = 0;
218 count = converter.toULong(argv[0]);
220 count = converter.toULong(undefinedValue);
223 ThrowMsg(InvalidArgumentException, "Invalid argument");
226 DPL::ScopedArray<char> text(privateObject->getObject()->getChars(count));
227 return converter.toJSValueRef(std::string(text.Get()));
228 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
229 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
230 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
231 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
232 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
233 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
234 } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
235 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
238 return JSValueMakeUndefined(context);
241 JSValueRef JSFilestream::readBytes(JSContextRef context,
243 JSObjectRef thisObject,
245 const JSValueRef argv[],
246 JSValueRef* exception)
248 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
249 if (!privateObject) {
250 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
253 Converter converter(context);
255 JSValueRef undefinedValue = JSValueMakeUndefined(context);
256 unsigned long count = 0;
259 count = converter.toULong(argv[0]);
261 count = converter.toULong(undefinedValue);
264 ThrowMsg(InvalidArgumentException, "Invalid argument");
268 DPL::ScopedArray<unsigned char> data(privateObject->getObject()->getBytes(count));
269 return converter.toJSValueRef(data.Get(), privateObject->getObject()->getCount());
270 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
271 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
272 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
273 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
274 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
275 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
276 } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
277 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
280 return JSValueMakeUndefined(context);
283 JSValueRef JSFilestream::readBase64(JSContextRef context,
285 JSObjectRef thisObject,
287 const JSValueRef argv[],
288 JSValueRef* exception)
290 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
291 if (!privateObject) {
292 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
295 Converter converter(context);
297 JSValueRef undefinedValue = JSValueMakeUndefined(context);
298 unsigned long count = 0;
301 count = converter.toULong(argv[0]);
303 count = converter.toULong(undefinedValue);
306 ThrowMsg(InvalidArgumentException, "Invalid argument");
309 DPL::ScopedArray<unsigned char> data(privateObject->getObject()->getBytes(count));
310 std::string base64 = WrtDeviceApis::Commons::Base64::encode(data.Get(), privateObject->getObject()->getCount());
311 return converter.toJSValueRef(base64);
312 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
313 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
314 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
315 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
316 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
317 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
318 } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
319 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
322 return JSValueMakeUndefined(context);
325 JSValueRef JSFilestream::write(JSContextRef context,
327 JSObjectRef thisObject,
329 const JSValueRef argv[],
330 JSValueRef* exception)
332 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
333 if (!privateObject) {
334 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
337 Converter converter(context);
339 JSValueRef undefinedValue = JSValueMakeUndefined(context);
342 privateObject->getObject()->write(converter.toString(argv[0]));
344 privateObject->getObject()->write(converter.toString(undefinedValue));
347 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
348 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
349 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
350 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
351 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
352 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
353 } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
354 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
357 return JSValueMakeUndefined(context);
360 JSValueRef JSFilestream::writeBytes(JSContextRef context,
362 JSObjectRef thisObject,
364 const JSValueRef argv[],
365 JSValueRef* exception)
367 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
368 if (!privateObject) {
369 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
373 Converter converter(context);
375 if (argc < 1 || !JSIsArrayValue(context, argv[0]))
376 ThrowMsg(ConversionException, "Type mismatch error");
378 PrivateObject::ObjectType stream = privateObject->getObject();
379 std::vector<unsigned char> data = converter.toVectorOfUChars(argv[0]);
380 std::vector<unsigned char>::const_iterator it = data.begin();
381 for (; it != data.end(); ++it) {
384 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
385 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
386 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
387 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
388 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
389 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
390 } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
391 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
394 return JSValueMakeUndefined(context);
397 JSValueRef JSFilestream::writeBase64(JSContextRef context,
399 JSObjectRef thisObject,
401 const JSValueRef argv[],
402 JSValueRef* exception)
404 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
405 if (!privateObject) {
406 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
409 Converter converter(context);
413 JSValueRef undefinedValue = JSValueMakeUndefined(context);
416 base64 = WrtDeviceApis::Commons::Base64::decode(converter.toString(argv[0]));
418 base64 = WrtDeviceApis::Commons::Base64::decode(converter.toString(undefinedValue));
420 privateObject->getObject()->write(base64);
421 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
422 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
423 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
424 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
425 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
426 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
427 } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
428 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
431 return JSValueMakeUndefined(context);