tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Content / JSAudioContentLyrics.cpp
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 #include <SecurityExceptions.h>
19
20 #include <JSUtil.h>
21 #include <JSWebAPIError.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <MultiCallbackUserData.h>
25 #include <PlatformException.h>
26
27 #include "plugin_config.h"
28
29 #include "JSAudioContentLyrics.h"
30
31 using namespace WrtDeviceApis::Commons;
32
33 namespace DeviceAPI {
34 namespace Content {
35
36 using namespace DeviceAPI::Common;
37
38 struct LyricsHolder{
39     AudioContentLyricsPtr ptr;
40 };
41
42 JSClassDefinition JSAudioContentLyrics::m_classInfo = {
43         0,
44         kJSClassAttributeNone,
45         "AudioContentLyrics",
46         NULL, //ParentClass
47         m_property, //StaticValues
48         NULL, //StaticFunctions
49         initialize, //Initialize
50         finalize, //Finalize
51         NULL, //HasProperty,
52         NULL, //GetProperty,
53         NULL, //SetProperty,
54         NULL, //DeleteProperty,
55         NULL, //GetPropertyNames,
56         NULL, //CallAsFunction,
57         NULL, //CallAsConstructor,
58         NULL, //HasInstance,
59         NULL //ConvertToType
60 };
61
62 JSStaticValue JSAudioContentLyrics::m_property[] = {
63         { AUDIO_CONTENT_LYRICS_TYPE, getType, NULL, kJSPropertyAttributeReadOnly },
64         { AUDIO_CONTENT_LYRICS_TIMESTAMPS, getTimestamps, NULL, kJSPropertyAttributeReadOnly },
65         { AUDIO_CONTENT_LYRICS_TEXTS, getTexts, NULL, kJSPropertyAttributeReadOnly },
66         { 0, 0, 0, 0 }
67 };
68
69 JSClassRef JSAudioContentLyrics::m_jsClassRef = JSClassCreate(JSAudioContentLyrics::getClassInfo());
70
71 const JSClassRef JSAudioContentLyrics::getClassRef()
72 {
73     if (!m_jsClassRef) {
74         m_jsClassRef = JSClassCreate(&m_classInfo);
75     }
76     return m_jsClassRef;
77 }
78
79 const JSClassDefinition* JSAudioContentLyrics::getClassInfo()
80 {
81     return &m_classInfo;
82 }
83
84 void JSAudioContentLyrics::initialize(JSContextRef context, JSObjectRef object)
85 {
86     LOGD("Entered");
87 }
88
89 void JSAudioContentLyrics::finalize(JSObjectRef object)
90 {
91     LOGD("Entered");
92     LyricsHolder* priv = static_cast<LyricsHolder*>(JSObjectGetPrivate(object));
93     if (priv) {
94         JSObjectSetPrivate(object, NULL);
95         delete priv;
96         priv = NULL;
97     }
98 }
99
100 JSValueRef JSAudioContentLyrics::CreateJSObject(JSContextRef context,
101         AudioContentLyricsPtr native)
102 {
103     JSObjectRef obj = JSObjectMake(context, getClassRef(), NULL);
104
105     if (!native) {
106         LOGE("AudioContentLyrics private object is NULL.");
107         throw TypeMismatchException("AudioContentLyrics private object is NULL.");
108     }
109
110     LyricsHolder* priv = new(std::nothrow) LyricsHolder;
111     if (!priv) {
112         LOGE("Can not be allocated memory");
113         throw UnknownException("Can not be allocated memory");
114     }
115
116     priv->ptr = native;
117     JSObjectSetPrivate(obj, static_cast<void*>(priv));
118     return obj;
119 }
120
121 AudioContentLyricsPtr JSAudioContentLyrics::getPrivateObject(JSContextRef context,
122         JSObjectRef object)
123 {
124     if (!JSValueIsObjectOfClass(context, object, getClassRef())) {
125         LOGE("TypeMismatch");
126         throw TypeMismatchException("TypeMismatch");
127     }
128
129     LyricsHolder* priv = static_cast<LyricsHolder*>(JSObjectGetPrivate(object));
130     if (!priv) {
131         LOGE("Priv is null");
132         throw UnknownException("Priv is null");
133     }
134
135     if (!(priv->ptr)) {
136         LOGE("Native is null");
137         throw UnknownException("Native is null");
138     }
139
140     return std::static_pointer_cast<AudioContentLyrics>(priv->ptr);
141 }
142
143 void JSAudioContentLyrics::setPrivateObject(JSObjectRef object,
144         AudioContentLyricsPtr native)
145 {
146     LyricsHolder* priv = static_cast<LyricsHolder*>(JSObjectGetPrivate(object));
147     if (!priv) {
148         LOGE("AudioContentLyrics private object is NULL.");
149         throw TypeMismatchException("AudioContentLyrics private object is NULL.");
150     }
151     priv->ptr = native;
152 }
153
154 JSValueRef JSAudioContentLyrics::getType(JSContextRef context,
155         JSObjectRef object,
156         JSStringRef propertyName,
157         JSValueRef* exception)
158 {
159     LOGD("Entered");
160     try {
161         AudioContentLyricsPtr priv = JSAudioContentLyrics::getPrivateObject(context, object);
162         return JSUtil::toJSValueRef(context, priv->getType());
163     }
164     catch (const BasePlatformException &error) {
165         LOGE("Attribute get failed: %s", error.getMessage().c_str());
166     }
167     catch (...) {
168         LOGE("Attribute get failed");
169     }
170     return JSValueMakeUndefined(context);
171 }
172
173 JSValueRef JSAudioContentLyrics::getTimestamps(JSContextRef context,
174         JSObjectRef object,
175         JSStringRef propertyName,
176         JSValueRef* exception)
177 {
178     LOGD("Entered");
179     try {
180         AudioContentLyricsPtr priv = JSAudioContentLyrics::getPrivateObject(context, object);
181         if ("UNSYNCHRONIZED" != priv->getType()) {
182             return JSUtil::toJSValueRef_(context, priv->getTimestamps());
183         }
184     }
185     catch (const BasePlatformException &error) {
186         LOGE("Attribute get failed: %s", error.getMessage().c_str());
187     }
188     catch (...) {
189         LOGE("Attribute get failed");
190     }
191     return JSValueMakeUndefined(context);
192 }
193
194 JSValueRef JSAudioContentLyrics::getTexts(JSContextRef context,
195         JSObjectRef object,
196         JSStringRef propertyName,
197         JSValueRef* exception)
198 {
199     LOGD("Entered");
200     try {
201         AudioContentLyricsPtr priv = JSAudioContentLyrics::getPrivateObject(context, object);
202         return JSUtil::toJSValueRef(context, priv->getTexts());
203     }
204     catch (const BasePlatformException &error) {
205         LOGE("Attribute get failed: %s", error.getMessage().c_str());
206     }
207     catch (...) {
208         LOGE("Attribute get failed");
209     }
210     return JSValueMakeUndefined(context);
211 }
212
213 } // Content
214 } // DeviceAPI