Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / 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/secure_log.h>
25
26 using namespace PluginModule;
27
28 const char *NAVIGATOR_PROPERTY_NAME = "navigator";
29 const char *SCALE_PROPERTY_NAME = "scale";
30 const char *BUNDLE_PROPERTY_NAME = "__bundle";
31 const char *THEME_PROPERTY_NAME = "theme";
32
33 WindowPropertySupport::WindowPropertySupport(
34     JSGlobalContextRef context) :
35     m_scale(0),
36     m_bundle(""),
37     m_theme(""),
38     m_context(context)
39 {}
40
41 WindowPropertySupport::~WindowPropertySupport()
42 {}
43
44 float WindowPropertySupport::getScale() const
45 {
46     return m_scale;
47 }
48
49 std::string WindowPropertySupport::getBundle() const
50 {
51     return m_bundle;
52 }
53
54 std::string WindowPropertySupport::getTheme() const
55 {
56     return m_theme;
57 }
58
59 JSGlobalContextRef WindowPropertySupport::getJSContext() const
60 {
61     return m_context;
62 }
63
64 void WindowPropertySupport::setScaleToNavigatorProperty(const double scale)
65 {
66     _D("set window.navigator.scale: %ld", scale);
67     m_scale = scale;
68     setPropertyToNavigator(SCALE_PROPERTY_NAME,
69                            JSValueMakeNumber(m_context, scale));
70 }
71
72 void WindowPropertySupport::setBundleToWindowProperty(const char* bundle)
73 {
74     _D("set window.__bundle: %s", bundle);
75     if (bundle) {
76         m_bundle = bundle;
77         JSStringRef bundleString = JSStringCreateWithUTF8CString(bundle);
78         setPropertyToWindow(BUNDLE_PROPERTY_NAME,
79                             JSValueMakeString(m_context, bundleString));
80         JSStringRelease(bundleString);
81     }
82 }
83
84 void WindowPropertySupport::setThemeToNavigatorProperty(const char* theme)
85 {
86     _D("set window.navigator.__theme: %s", theme);
87     if (theme) {
88         m_theme = theme;
89         JSStringRef themeString = JSStringCreateWithUTF8CString(theme);
90         setPropertyToNavigator(THEME_PROPERTY_NAME,
91                                JSValueMakeString(m_context, themeString));
92         JSStringRelease(themeString);
93     }
94 }
95
96 void WindowPropertySupport::setPropertyToWindow(const char* propertyName,
97                                                 JSValueRef jsValue)
98 {
99     _D("et property to window : %s", propertyName);
100     if (propertyName) {
101         JSObjectRef globalObject = JSContextGetGlobalObject(m_context);
102         JSStringRef propertyNameString =
103             JSStringCreateWithUTF8CString(propertyName);
104         JSObjectSetProperty(m_context,
105                             globalObject,
106                             propertyNameString,
107                             jsValue,
108                             kJSPropertyAttributeReadOnly,
109                             NULL);
110         JSStringRelease(propertyNameString);
111     }
112 }
113
114 void WindowPropertySupport::setPropertyToNavigator(const char* propertyName,
115                                                    JSValueRef jsValue)
116 {
117     _D("set property to navigator : %s", propertyName);
118     if (propertyName) {
119         JSObjectRef globalObject = JSContextGetGlobalObject(m_context);
120
121         JSStringRef navigatorString =
122             JSStringCreateWithUTF8CString(NAVIGATOR_PROPERTY_NAME);
123         JSValueRef navigatorValue = JSObjectGetProperty(m_context,
124                                                         globalObject,
125                                                         navigatorString,
126                                                         NULL);
127
128         JSStringRef propertyNameString =
129             JSStringCreateWithUTF8CString(propertyName);
130         JSObjectSetProperty(m_context,
131                             JSValueToObject(m_context, navigatorValue, NULL),
132                             propertyNameString,
133                             jsValue,
134                             kJSPropertyAttributeReadOnly,
135                             NULL);
136
137         JSStringRelease(propertyNameString);
138         JSStringRelease(navigatorString);
139     }
140 }
141