[widget-viewer-dali] Change the license as Flora
[platform/core/uifw/widget-viewer-dali.git] / internal / widget_view_manager / widget_view_manager_impl.cpp
1 /*
2  * Samsung API
3  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 // CLASS HEADER
19 #include <internal/widget_view_manager/widget_view_manager_impl.h>
20
21 // INTERNAL INCLUDES
22 #include <internal/widget_view/widget_view_impl.h>
23
24 // EXTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26 #include <system_info.h>
27 #include <cynara-client.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <widget_errno.h>
32 #include <widget_instance.h>
33
34 namespace Dali
35 {
36
37 namespace WidgetView
38 {
39
40 namespace Internal
41 {
42
43 namespace
44 {
45
46 #define SMACK_LABEL_LENGTH 255
47
48 #if defined(DEBUG_ENABLED)
49 Integration::Log::Filter* gWidgetViewManagerLogging  = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_WIDGET_VIEW_MANAGER" );
50 #endif
51
52 static bool IsWidgetFeatureEnabled()
53 {
54   static bool feature = false;
55   static bool retrieved = false;
56   int ret;
57
58   if( retrieved == true )
59     return feature;
60
61   ret = system_info_get_platform_bool( "http://tizen.org/feature/shell.appwidget", &feature );
62   if( ret != SYSTEM_INFO_ERROR_NONE )
63   {
64     return false;
65   }
66
67   retrieved = true;
68
69   return feature;
70 }
71
72 static bool CheckPrivilege( const char* privilege )
73 {
74   cynara* cynara;
75   int fd = 0;
76   int ret = 0;
77   char subjectLabel[SMACK_LABEL_LENGTH + 1] = "";
78   char uid[10] = { 0, };
79   const char* clientSession = "";
80
81   ret = cynara_initialize( &cynara, NULL );
82   if( ret != CYNARA_API_SUCCESS )
83   {
84     return false;
85   }
86
87   fd = open( "/proc/self/attr/current", O_RDONLY );
88   if( fd < 0 )
89   {
90     cynara_finish( cynara );
91     return false;
92   }
93
94   ret = read( fd, subjectLabel, SMACK_LABEL_LENGTH );
95   if( ret < 0 )
96   {
97     close( fd );
98     cynara_finish( cynara );
99     return false;
100   }
101
102   close( fd );
103
104   snprintf( uid, 10, "%d", getuid() );
105
106   ret = cynara_check( cynara, subjectLabel, clientSession, uid, privilege );
107   if( ret != CYNARA_API_ACCESS_ALLOWED )
108   {
109     cynara_finish( cynara );
110     return false;
111   }
112
113   cynara_finish( cynara );
114
115   return true;
116 }
117
118 } // unnamed namespace
119
120 WidgetViewManagerPtr WidgetViewManager::New( Application application, const std::string& name )
121 {
122   WidgetViewManagerPtr impl = new WidgetViewManager();
123
124   // Second-phase init of the implementation
125   if( impl->Initialize( application, name ) != WIDGET_ERROR_NONE )
126   {
127     DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::New: Fail to create WidgetViewManager.\n" );
128     return NULL;
129   }
130
131   return impl;
132 }
133
134 WidgetViewManager::WidgetViewManager()
135 {
136 }
137
138 WidgetViewManager::~WidgetViewManager()
139 {
140   widget_instance_unlisten_event( WidgetViewManager::WidgetEventCallback );
141   widget_instance_fini();
142 }
143
144 int WidgetViewManager::Initialize( Application application, const std::string& name )
145 {
146   if( !IsWidgetFeatureEnabled() )
147   {
148     DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::Initialize: Widget feature is not enabled.\n" );
149     return WIDGET_ERROR_NOT_SUPPORTED;
150   }
151
152   if( !CheckPrivilege( "http://tizen.org/privilege/widget.viewer" ) )
153   {
154     DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::Initialize: Privilege error.\n" );
155     return WIDGET_ERROR_PERMISSION_DENIED;
156   }
157
158   // create compositor
159   mCompositor = Pepper::Compositor::New( application, name );
160
161   mCompositor.ObjectViewAddedSignal().Connect( this, &WidgetViewManager::OnObjectViewAdded );
162   mCompositor.ObjectViewDeletedSignal().Connect( this, &WidgetViewManager::OnObjectViewDeleted );
163
164   // init widget service
165   widget_instance_init( name.c_str() );
166   widget_instance_listen_event( WidgetViewManager::WidgetEventCallback, this );
167
168   DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::Initialize: success.\n" );
169
170   setenv("WAYLAND_DISPLAY", mCompositor.GetName().c_str(), 1);
171
172   return WIDGET_ERROR_NONE;
173 }
174
175 Dali::WidgetView::WidgetView WidgetViewManager::AddWidget( const std::string& widgetId, const std::string& contentInfo, int width, int height, double updatePeriod )
176 {
177   // Add a new widget view
178   Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::New( widgetId, contentInfo, width, height, updatePeriod );
179
180   std::string instanceId = widgetView.GetInstanceId();
181
182   if( !instanceId.empty() )
183   {
184     // Add to map
185     mWidgetViewContainer.insert( std::pair<std::string, Dali::WidgetView::WidgetView>( instanceId, widgetView ) );
186   }
187
188   DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::AddWidget: success [%s]\n", widgetId.c_str() );
189
190   return widgetView;
191 }
192
193 void WidgetViewManager::OnObjectViewAdded( Pepper::Compositor compositor, Pepper::ObjectView objectView )
194 {
195   std::string appId = objectView.GetAppId();  // widget instance id
196
197   if( mWidgetViewContainer.size() > 0)
198   {
199     WidgetViewIter iter = mWidgetViewContainer.find( appId );
200     if( iter != mWidgetViewContainer.end() )
201     {
202       Dali::WidgetView::WidgetView widgetView = iter->second;
203
204       Dali::WidgetView::GetImplementation( widgetView ).AddObjectView( objectView );
205     }
206   }
207
208   DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::OnObjectViewAdded: ObjectView is added!\n" );
209 }
210
211 void WidgetViewManager::OnObjectViewDeleted( Pepper::Compositor compositor, Pepper::ObjectView objectView )
212 {
213   std::string appId = objectView.GetAppId();  // widget instance id
214
215   // Remove from map
216   if( mWidgetViewContainer.size() > 0)
217   {
218     WidgetViewIter iter = mWidgetViewContainer.find( appId );
219     if( iter != mWidgetViewContainer.end() )
220     {
221       Dali::WidgetView::WidgetView widgetView = iter->second;
222
223       Dali::WidgetView::GetImplementation( widgetView ).RemoveObjectView();
224
225       mWidgetViewContainer.erase( iter );
226     }
227   }
228
229   DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::OnObjectViewDeleted: ObjectView is deleted!\n" );
230 }
231
232 int WidgetViewManager::WidgetEventCallback( const char* widgetId, const char* instanceId, int event, void* data )
233 {
234   WidgetViewManager* widgetViewManager = static_cast< WidgetViewManager* >( data );
235
236   if( widgetViewManager->mWidgetViewContainer.size() > 0)
237   {
238     WidgetViewIter iter = widgetViewManager->mWidgetViewContainer.find( std::string( instanceId ) );
239     if( iter != widgetViewManager->mWidgetViewContainer.end() )
240     {
241       Dali::WidgetView::WidgetView widgetView = iter->second;
242
243       Dali::WidgetView::GetImplementation( widgetView ).SendWidgetEvent( event );
244
245       return 0;
246     }
247   }
248
249   DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::WidgetEventCallback: WidgetView is not found! [%s, %s]\n", widgetId, instanceId );
250
251   return 0;
252 }
253
254 } // namespace Internal
255
256 } // namespace WidgetView
257
258 } // namespace Dali