Update wrt-plugins-common_0.3.53
[framework/web/wrt-plugins-common.git] / src / plugin-loading / plugin_property_support.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file    plugin_property_support.cpp
18  * @author  Yunchan Cho (yunchan.cho@samsung.com)
19  * @version 1.0
20  * @brief
21  */
22
23 #include "plugin_property_support.h"
24 #include <dpl/log/log.h>
25
26 using namespace PluginModule;
27
28
29 const char *NAVIGATOR_PROPERTY_NAME  =  "navigator";
30 const char *SCALE_PROPERTY_NAME = "scale";
31 const char *BUNDLE_PROPERTY_NAME  =  "__bundle";
32 const char *THEME_PROPERTY_NAME  =  "theme";
33
34 WindowPropertySupport::WindowPropertySupport(
35         JSGlobalContextRef context):
36     m_scale(0),
37     m_bundle(""),
38     m_theme(""),
39     m_context(context)
40 {
41 }
42
43 WindowPropertySupport::~WindowPropertySupport()
44 {
45 }
46
47 float WindowPropertySupport::getScale() const
48 {
49     return m_scale;
50 }
51
52 std::string WindowPropertySupport::getBundle() const
53 {
54     return m_bundle;
55 }
56
57 std::string WindowPropertySupport::getTheme() const
58 {
59     return m_theme;
60 }
61
62 JSGlobalContextRef WindowPropertySupport::getJSContext() const
63 {
64     return m_context;
65 }
66
67 void WindowPropertySupport::setScaleToNavigatorProperty(const double scale)
68 {
69     LogInfo("set window.navigator.scale: " << scale);
70
71     m_scale = scale;
72     
73     setPropertyToNavigator(SCALE_PROPERTY_NAME,  JSValueMakeNumber(m_context, scale));
74 }
75
76 void WindowPropertySupport::setBundleToWindowProperty(const char* bundle)
77 {
78     LogInfo("set window.__bundle: " << bundle);
79
80     if(bundle)
81     {
82         m_bundle = bundle;
83
84         JSStringRef bundleString = JSStringCreateWithUTF8CString(bundle);
85
86         setPropertyToWindow(BUNDLE_PROPERTY_NAME,
87                             JSValueMakeString(m_context, bundleString));
88
89         JSStringRelease(bundleString);
90     }
91 }
92
93
94 void WindowPropertySupport::setBundleToNavigatorProperty(const char* bundle)
95 {
96     LogInfo("set window.navigator.__bundle: " << bundle);
97
98     if(bundle)
99     {
100         m_bundle = bundle;
101
102         JSStringRef bundleString = JSStringCreateWithUTF8CString(bundle);
103
104         setPropertyToNavigator(BUNDLE_PROPERTY_NAME,
105                                JSValueMakeString(m_context, bundleString));
106
107         JSStringRelease(bundleString);
108     }
109 }
110
111 void WindowPropertySupport::setThemeToNavigatorProperty(const char* theme)
112 {
113     LogInfo("set window.navigator.__theme: " << theme);
114
115     if(theme)
116     {
117         m_theme = theme;
118
119         JSStringRef themeString = JSStringCreateWithUTF8CString(theme);
120
121         setPropertyToNavigator(THEME_PROPERTY_NAME,
122                                JSValueMakeString(m_context, themeString));
123
124         JSStringRelease(themeString);
125     }
126 }
127
128 void WindowPropertySupport::setPropertyToWindow(const char* propertyName,
129                                                              JSValueRef jsValue)
130 {
131     if(propertyName)
132     {
133         JSObjectRef globalObject = JSContextGetGlobalObject(m_context);
134
135         JSStringRef propertyNameString =
136             JSStringCreateWithUTF8CString(propertyName);
137         JSObjectSetProperty(m_context,
138                             globalObject,
139                             propertyNameString,
140                             jsValue,
141                             kJSPropertyAttributeReadOnly,
142                             NULL);
143
144         JSStringRelease(propertyNameString);
145     }
146 }
147
148 void WindowPropertySupport::setPropertyToNavigator(const char* propertyName,
149                                                              JSValueRef jsValue)
150 {
151     if(propertyName)
152     {
153         JSObjectRef globalObject = JSContextGetGlobalObject(m_context);
154
155         JSStringRef navigatorString =
156             JSStringCreateWithUTF8CString(NAVIGATOR_PROPERTY_NAME);
157         JSValueRef navigatorValue = JSObjectGetProperty(m_context,
158                                                         globalObject,
159                                                         navigatorString,
160                                                         NULL);
161
162         JSStringRef propertyNameString =
163             JSStringCreateWithUTF8CString(propertyName);
164         JSObjectSetProperty(m_context,
165                             JSValueToObject(m_context, navigatorValue, NULL),
166                             propertyNameString,
167                             jsValue,
168                             kJSPropertyAttributeReadOnly,
169                             NULL);
170
171         JSStringRelease(propertyNameString);
172         JSStringRelease(navigatorString);
173     }
174 }
175