2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "Converter.h"
20 #include <dpl/log/log.h>
22 #include <Commons/Exception.h>
23 #include <CommonsJavaScript/Validator.h>
24 #include <CommonsJavaScript/ScopedJSStringRef.h>
25 #include <CommonsJavaScript/JSUtils.h>
26 #include <CommonsJavaScript/JSCallbackManager.h>
27 #include "CallHistoryDefine.h"
28 #include "JSCallHistoryEntry.h"
29 #include "JSRemoteParty.h"
31 using namespace WrtDeviceApis;
32 using namespace WrtDeviceApis::Commons;
33 using namespace WrtDeviceApis::CommonsJavaScript;
36 namespace CallHistory {
38 Converter::Converter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
40 static bool init = initializeAllowedProperties();
44 std::vector<unsigned long> Converter::toVectorOfULongs(const JSValueRef& jsValue)
46 return toVectorOfT_(jsValue, &Converter::toULong);
49 CallHistoryEntryList Converter::toVectorOfCallHistoryEntryProperties(const JSValueRef& jsValue)
51 if (JSValueIsNull(m_context, jsValue) || JSValueIsUndefined(m_context, jsValue)) {
52 ThrowMsg(Commons::ConversionException, "Undefined history entry");
55 if (!JSIsArrayValue(m_context, jsValue)) {
56 ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
60 CallHistoryEntryList result;
61 JSObjectRef objArg = toJSObjectRef(jsValue);
62 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
63 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
64 result.push_back(toCallHistoryEntryProperties(element));
67 } catch (Commons::ConversionException& ex) {
68 ThrowMsg(Commons::ConversionException, ex.GetMessage());
69 } catch (Commons::InvalidArgumentException& ex) {
70 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
71 } catch (Commons::Exception& ex) {
72 ThrowMsg(Commons::Exception, ex.GetMessage());
77 StringArrayPtr Converter::toStringArray(const JSValueRef &jsValue)
79 StringArrayPtr result(new StringArray());
81 if (!JSValueIsNull(m_context, jsValue)) {
82 JSObjectRef jsObject = toJSObjectRef(jsValue);
84 for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
85 JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
86 result->push_back(toString(element));
89 } catch (Commons::ConversionException& ex) {
90 ThrowMsg(Commons::ConversionException, ex.GetMessage());
91 } catch (Commons::InvalidArgumentException& ex) {
92 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
93 } catch (Commons::Exception& ex) {
94 ThrowMsg(Commons::Exception, ex.GetMessage());
99 RemotePartyListPtr Converter::toRemotePartyList(const JSValueRef &jsValue)
101 RemotePartyListPtr result(new RemotePartyList());
103 if (!JSValueIsNull(m_context, jsValue)) {
104 JSObjectRef jsObject = toJSObjectRef(jsValue);
106 for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
107 JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
108 result->push_back(toRemoteParty(element));
111 } catch (Commons::ConversionException& ex) {
112 ThrowMsg(Commons::ConversionException, ex.GetMessage());
113 } catch (Commons::InvalidArgumentException& ex) {
114 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
115 } catch (Commons::Exception& ex) {
116 ThrowMsg(Commons::Exception, ex.GetMessage());
121 RemotePartyPtr Converter::toRemoteParty(const JSValueRef &jsValue)
124 return JSRemoteParty::getRemoteParty(m_context, jsValue);
125 } catch (Commons::ConversionException& ex) {
126 ThrowMsg(Commons::ConversionException, ex.GetMessage());
127 } catch (Commons::Exception& ex) {
128 ThrowMsg(Commons::Exception, ex.GetMessage());
132 CallHistoryEntryPropertiesPtr Converter::toCallHistoryEntryProperties(const JSValueRef &jsValue)
135 return JSCallHistoryEntry::getCallHistoryEntry(m_context, jsValue);
136 } catch (Commons::ConversionException& ex) {
137 ThrowMsg(Commons::ConversionException, ex.GetMessage());
138 } catch (Commons::Exception& ex) {
139 ThrowMsg(Commons::Exception, ex.GetMessage());
143 EventCallHistoryListenerPrivateDataPtr
144 Converter::toEventCallHistoryListenerPrivateData(const JSValueRef &jsValue, JSContextRef context)
149 if (JSValueIsNull(m_context, jsValue) || JSValueIsUndefined(m_context, jsValue)) {
150 ThrowMsg(Commons::ConversionException, "Type missmatch error : Listener callback is null or undefined");
152 if (!JSValueIsObject(m_context, jsValue) || JSObjectIsFunction(m_context, toJSObjectRef(jsValue))) {
153 ThrowMsg(Commons::ConversionException, "Type missmatch error : Listener callback");
156 JSObjectRef objectCallbacks = toJSObjectRef(jsValue);
157 CallHistoryChangeCB result;
159 result.onAdded = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onadded");
160 result.onChanged = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onchanged");
161 result.onRemoved = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onremoved");
163 JSCallbackManagerPtr onAddedCbm = JSCallbackManager::createObject(context);
164 JSCallbackManagerPtr onChangedCbm = JSCallbackManager::createObject(context);
165 JSCallbackManagerPtr onRemovedCbm = JSCallbackManager::createObject(context);
167 if (!JSValueIsNull(m_context, result.onAdded) && !JSValueIsUndefined(m_context, result.onAdded)) {
168 if (JSObjectIsFunction(m_context, toJSObjectRef(result.onAdded))) {
169 onAddedCbm->setOnSuccess(result.onAdded);
171 ThrowMsg(Commons::ConversionException, "Type missmatch error : onadded callback");
175 if (!JSValueIsNull(m_context, result.onChanged) && !JSValueIsUndefined(m_context, result.onChanged)) {
176 if (JSObjectIsFunction(m_context, toJSObjectRef(result.onChanged))) {
177 onChangedCbm->setOnSuccess(result.onChanged);
179 ThrowMsg(Commons::ConversionException, "Type missmatch error : onchanged callback");
183 if (!JSValueIsNull(m_context, result.onRemoved) && !JSValueIsUndefined(m_context, result.onRemoved)) {
184 if (JSObjectIsFunction(m_context, toJSObjectRef(result.onRemoved))) {
185 onRemovedCbm->setOnSuccess(result.onRemoved);
186 LogDebug("onRemoved register");
189 ThrowMsg(Commons::ConversionException, "Type missmatch error : onremoved callback");
194 return EventCallHistoryListenerPrivateDataPtr(new EventCallHistoryListenerPrivateData(onAddedCbm, onChangedCbm, onRemovedCbm));
197 JSValueRef Converter::toJSValueRef(const CallHistoryEntryListPtr& arg, JSContextRef context)
199 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
201 ThrowMsg(Commons::ConversionException, "Could not create js array object");
203 CallHistoryEntryProperties tmpCallEntry;
206 for (size_t i = 0; i < (*arg).size(); i++) {
207 tmpCallEntry = *((*arg)[i]);
208 JSObjectRef jsObject = JSCallHistoryEntry::createJSObject(context, tmpCallEntry);
210 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
213 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
214 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
217 } catch (Commons::ConversionException& ex) {
218 ThrowMsg(Commons::ConversionException, ex.GetMessage());
219 } catch (Commons::InvalidArgumentException& ex) {
220 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
221 } catch (Commons::Exception& ex) {
222 ThrowMsg(Commons::Exception, ex.GetMessage());
227 JSValueRef Converter::toJSValueRef(const StringArrayPtr &arg, JSContextRef context)
229 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
231 ThrowMsg(Commons::ConversionException, "Could not create js array object");
234 for (size_t i = 0; i < (*arg).size(); i++) {
235 if (!JSSetArrayElement(m_context, jsResult, i, toJSValueRef((*arg)[i]))) {
236 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
243 JSValueRef Converter::toJSValueRef(const RemotePartyListPtr& arg, JSContextRef context)
245 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
247 ThrowMsg(Commons::ConversionException, "Could not create js array object");
250 RemoteParty tmpRemoteParty;
253 for (size_t i = 0; i < (*arg).size(); i++) {
254 tmpRemoteParty = *((*arg)[i]);
255 JSObjectRef jsObject = JSRemoteParty::createJSObject(context, tmpRemoteParty);
257 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
260 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
261 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
264 } catch (Commons::ConversionException& ex) {
265 ThrowMsg(Commons::ConversionException, ex.GetMessage());
266 } catch (Commons::InvalidArgumentException& ex) {
267 ThrowMsg(Commons::InvalidArgumentException, ex.GetMessage());
268 } catch (Commons::Exception& ex) {
269 ThrowMsg(Commons::Exception, ex.GetMessage());
274 bool Converter::initializeAllowedProperties()