Tizen 2.1 base
[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 void WindowPropertySupport::setThemeToNavigatorProperty(const char* theme)
94 {
95     LogInfo("set window.navigator.__theme: " << theme);
96
97     if(theme)
98     {
99         m_theme = theme;
100
101         JSStringRef themeString = JSStringCreateWithUTF8CString(theme);
102
103         setPropertyToNavigator(THEME_PROPERTY_NAME,
104                                JSValueMakeString(m_context, themeString));
105
106         JSStringRelease(themeString);
107     }
108 }
109
110 void WindowPropertySupport::setPropertyToWindow(const char* propertyName,
111                                                              JSValueRef jsValue)
112 {
113     if(propertyName)
114     {
115         JSObjectRef globalObject = JSContextGetGlobalObject(m_context);
116
117         JSStringRef propertyNameString =
118             JSStringCreateWithUTF8CString(propertyName);
119         JSObjectSetProperty(m_context,
120                             globalObject,
121                             propertyNameString,
122                             jsValue,
123                             kJSPropertyAttributeReadOnly,
124                             NULL);
125
126         JSStringRelease(propertyNameString);
127     }
128 }
129
130 void WindowPropertySupport::setPropertyToNavigator(const char* propertyName,
131                                                              JSValueRef jsValue)
132 {
133     if(propertyName)
134     {
135         JSObjectRef globalObject = JSContextGetGlobalObject(m_context);
136
137         JSStringRef navigatorString =
138             JSStringCreateWithUTF8CString(NAVIGATOR_PROPERTY_NAME);
139         JSValueRef navigatorValue = JSObjectGetProperty(m_context,
140                                                         globalObject,
141                                                         navigatorString,
142                                                         NULL);
143
144         JSStringRef propertyNameString =
145             JSStringCreateWithUTF8CString(propertyName);
146         JSObjectSetProperty(m_context,
147                             JSValueToObject(m_context, navigatorValue, NULL),
148                             propertyNameString,
149                             jsValue,
150                             kJSPropertyAttributeReadOnly,
151                             NULL);
152
153         JSStringRelease(propertyNameString);
154         JSStringRelease(navigatorString);
155     }
156 }
157