Modified internal api for fault widget case
[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/public-api/object/type-registry.h>
26 #include <dali/public-api/object/type-registry-helper.h>
27 #include <dali/integration-api/debug.h>
28 #include <system_info.h>
29 #include <cynara-client.h>
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <widget_errno.h>
34 #include <widget_instance.h>
35 #include <screen_connector_toolkit.h>
36 #include <libintl.h>
37
38 namespace Dali
39 {
40
41 namespace WidgetView
42 {
43
44 namespace Internal
45 {
46
47 namespace
48 {
49
50 #define SMACK_LABEL_LENGTH 255
51
52 #if defined(DEBUG_ENABLED)
53 Integration::Log::Filter* gWidgetViewManagerLogging  = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_WIDGET_VIEW_MANAGER" );
54 #endif
55
56 BaseHandle Create()
57 {
58   return Dali::BaseHandle();
59 }
60
61 DALI_TYPE_REGISTRATION_BEGIN( Dali::WidgetView::WidgetViewManager, Dali::BaseHandle, Create );
62
63 static bool IsWidgetFeatureEnabled()
64 {
65   static bool feature = false;
66   static bool retrieved = false;
67   int ret;
68
69   if( retrieved == true )
70     return feature;
71
72   ret = system_info_get_platform_bool( "http://tizen.org/feature/shell.appwidget", &feature );
73   if( ret != SYSTEM_INFO_ERROR_NONE )
74   {
75     return false;
76   }
77
78   retrieved = true;
79
80   return feature;
81 }
82
83 static bool CheckPrivilege( const char* privilege )
84 {
85   cynara* cynara;
86   int fd = 0;
87   int ret = 0;
88   char subjectLabel[SMACK_LABEL_LENGTH + 1] = "";
89   char uid[10] = { 0, };
90   const char* clientSession = "";
91
92   ret = cynara_initialize( &cynara, NULL );
93   if( ret != CYNARA_API_SUCCESS )
94   {
95     return false;
96   }
97
98   fd = open( "/proc/self/attr/current", O_RDONLY );
99   if( fd < 0 )
100   {
101     cynara_finish( cynara );
102     return false;
103   }
104
105   ret = read( fd, subjectLabel, SMACK_LABEL_LENGTH );
106   if( ret < 0 )
107   {
108     close( fd );
109     cynara_finish( cynara );
110     return false;
111   }
112
113   close( fd );
114
115   snprintf( uid, 10, "%d", getuid() );
116
117   ret = cynara_check( cynara, subjectLabel, clientSession, uid, privilege );
118   if( ret != CYNARA_API_ACCESS_ALLOWED )
119   {
120     cynara_finish( cynara );
121     return false;
122   }
123
124   cynara_finish( cynara );
125
126   return true;
127 }
128
129 } // unnamed namespace
130
131 WidgetViewManagerPtr WidgetViewManager::New( Application application, const std::string& name )
132 {
133   WidgetViewManagerPtr impl = new WidgetViewManager();
134
135   // Second-phase init of the implementation
136   if( impl->Initialize( application, name ) != WIDGET_ERROR_NONE )
137   {
138     DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::New: Fail to create WidgetViewManager.\n" );
139     return NULL;
140   }
141
142   return impl;
143 }
144
145 WidgetViewManager::WidgetViewManager()
146 {
147 }
148
149 WidgetViewManager::~WidgetViewManager()
150 {
151   screen_connector_toolkit_fini(SCREEN_CONNECTOR_SCREEEN_TYPE_WIDGET);
152   widget_instance_unlisten_event( WidgetViewManager::WidgetEventCallback );
153   widget_instance_fini();
154 }
155
156 int WidgetViewManager::Initialize( Application application, const std::string& name )
157 {
158   if( !IsWidgetFeatureEnabled() )
159   {
160     DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::Initialize: Widget feature is not enabled.\n" );
161     return WIDGET_ERROR_NOT_SUPPORTED;
162   }
163
164   if( !CheckPrivilege( "http://tizen.org/privilege/widget.viewer" ) )
165   {
166     DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::Initialize: Privilege error.\n" );
167     return WIDGET_ERROR_PERMISSION_DENIED;
168   }
169
170   if( !bindtextdomain( PKGNAME, WIDGET_VIEWER_DALI_LOCALE ) )
171   {
172     DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::Initialize: bindtextdomain error.\n" );
173   }
174
175   // Binds tizen remote surface manager & connects callback
176   if( screen_connector_toolkit_init(SCREEN_CONNECTOR_SCREEEN_TYPE_WIDGET) < 0 )
177   {
178     return WIDGET_ERROR_FAULT;
179   }
180
181   // init widget service
182   widget_instance_init( name.c_str() );
183   widget_instance_listen_event( WidgetViewManager::WidgetEventCallback, this );
184
185   DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::Initialize: success.\n" );
186
187   return WIDGET_ERROR_NONE;
188 }
189
190 Dali::WidgetView::WidgetView WidgetViewManager::AddWidget( const std::string& widgetId, const std::string& contentInfo, int width, int height, float updatePeriod )
191 {
192   // Add a new widget view
193   Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::New( widgetId, contentInfo, width, height, updatePeriod );
194
195   std::string instanceId;
196   Property::Value value = widgetView.GetProperty( Dali::WidgetView::WidgetView::Property::INSTANCE_ID );
197   value.Get( instanceId );
198
199   if( !instanceId.empty() )
200   {
201     Dali::WidgetView::GetImplementation( widgetView ).WidgetTerminatedSignal().Connect( this, &WidgetViewManager::OnTerminatedWidget );
202
203     // Add to map
204     mWidgetViewContainer.insert( std::pair<std::string, Dali::WidgetView::WidgetView>( instanceId, widgetView ) );
205   }
206
207   DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::AddWidget: success [%s]\n", widgetId.c_str() );
208
209   return widgetView;
210 }
211
212 void WidgetViewManager::OnTerminatedWidget( Dali::WidgetView::WidgetView widgetView )
213 {
214   std::string instanceId;
215   Dali::Property::Value value = widgetView.GetProperty( Dali::WidgetView::WidgetView::Property::INSTANCE_ID );
216
217   if( value.Get( instanceId ) && !instanceId.empty() && mWidgetViewContainer.size() > 0 )
218   {
219     WidgetViewIter iter = mWidgetViewContainer.find( instanceId );
220     if( iter != mWidgetViewContainer.end() )
221     {
222       mWidgetViewContainer.erase( iter );
223     }
224   }
225
226   DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::OnTerminatedWidget: Widget is deleted!\n" );
227 }
228
229 int WidgetViewManager::WidgetEventCallback( const char* widgetId, const char* instanceId, int event, void* data )
230 {
231   WidgetViewManager* widgetViewManager = static_cast< WidgetViewManager* >( data );
232
233   if( widgetViewManager->mWidgetViewContainer.size() > 0)
234   {
235     WidgetViewIter iter = widgetViewManager->mWidgetViewContainer.find( std::string( instanceId ) );
236     if( iter != widgetViewManager->mWidgetViewContainer.end() )
237     {
238       Dali::WidgetView::WidgetView widgetView = iter->second;
239
240       Dali::WidgetView::GetImplementation( widgetView ).SendWidgetEvent( event );
241
242       return 0;
243     }
244   }
245
246   DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::WidgetEventCallback: WidgetView is not found! [%s, %s]\n", widgetId, instanceId );
247
248   return 0;
249 }
250
251 } // namespace Internal
252
253 } // namespace WidgetView
254
255 } // namespace Dali