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