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