3 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
9 * http://floralicense.org/license/
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.
19 #include <internal/widget_view_manager/widget_view_manager_impl.h>
22 #include <internal/widget_view/widget_view_impl.h>
25 #include <dali/integration-api/debug.h>
26 #include <system_info.h>
27 #include <cynara-client.h>
31 #include <widget_errno.h>
32 #include <widget_instance.h>
33 #include <aul_rsm_viewer.h>
47 #define SMACK_LABEL_LENGTH 255
49 #if defined(DEBUG_ENABLED)
50 Integration::Log::Filter* gWidgetViewManagerLogging = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_WIDGET_VIEW_MANAGER" );
53 static bool IsWidgetFeatureEnabled()
55 static bool feature = false;
56 static bool retrieved = false;
59 if( retrieved == true )
62 ret = system_info_get_platform_bool( "http://tizen.org/feature/shell.appwidget", &feature );
63 if( ret != SYSTEM_INFO_ERROR_NONE )
73 static bool CheckPrivilege( const char* privilege )
78 char subjectLabel[SMACK_LABEL_LENGTH + 1] = "";
79 char uid[10] = { 0, };
80 const char* clientSession = "";
82 ret = cynara_initialize( &cynara, NULL );
83 if( ret != CYNARA_API_SUCCESS )
88 fd = open( "/proc/self/attr/current", O_RDONLY );
91 cynara_finish( cynara );
95 ret = read( fd, subjectLabel, SMACK_LABEL_LENGTH );
99 cynara_finish( cynara );
105 snprintf( uid, 10, "%d", getuid() );
107 ret = cynara_check( cynara, subjectLabel, clientSession, uid, privilege );
108 if( ret != CYNARA_API_ACCESS_ALLOWED )
110 cynara_finish( cynara );
114 cynara_finish( cynara );
119 } // unnamed namespace
121 WidgetViewManagerPtr WidgetViewManager::New( Application application, const std::string& name )
123 WidgetViewManagerPtr impl = new WidgetViewManager();
125 // Second-phase init of the implementation
126 if( impl->Initialize( application, name ) != WIDGET_ERROR_NONE )
128 DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::New: Fail to create WidgetViewManager.\n" );
135 WidgetViewManager::WidgetViewManager()
139 WidgetViewManager::~WidgetViewManager()
141 aul_rsm_viewer_fini();
142 widget_instance_unlisten_event( WidgetViewManager::WidgetEventCallback );
143 widget_instance_fini();
146 int WidgetViewManager::Initialize( Application application, const std::string& name )
148 if( !IsWidgetFeatureEnabled() )
150 DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::Initialize: Widget feature is not enabled.\n" );
151 return WIDGET_ERROR_NOT_SUPPORTED;
154 if( !CheckPrivilege( "http://tizen.org/privilege/widget.viewer" ) )
156 DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::Initialize: Privilege error.\n" );
157 return WIDGET_ERROR_PERMISSION_DENIED;
160 // Binds tizen remote surface manager & connects callback
161 if( aul_rsm_viewer_init() < 0 )
163 return WIDGET_ERROR_FAULT;
166 // init widget service
167 widget_instance_init( name.c_str() );
168 widget_instance_listen_event( WidgetViewManager::WidgetEventCallback, this );
170 DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::Initialize: success.\n" );
172 return WIDGET_ERROR_NONE;
175 Dali::WidgetView::WidgetView WidgetViewManager::AddWidget( const std::string& widgetId, const std::string& contentInfo, int width, int height, double updatePeriod )
177 // Add a new widget view
178 Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::New( widgetId, contentInfo, width, height, updatePeriod );
180 std::string instanceId = widgetView.GetInstanceId();
182 if( !instanceId.empty() )
185 mWidgetViewContainer.insert( std::pair<std::string, Dali::WidgetView::WidgetView>( instanceId, widgetView ) );
188 DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::AddWidget: success [%s]\n", widgetId.c_str() );
193 int WidgetViewManager::WidgetEventCallback( const char* widgetId, const char* instanceId, int event, void* data )
195 WidgetViewManager* widgetViewManager = static_cast< WidgetViewManager* >( data );
197 if( widgetViewManager->mWidgetViewContainer.size() > 0)
199 WidgetViewIter iter = widgetViewManager->mWidgetViewContainer.find( std::string( instanceId ) );
200 if( iter != widgetViewManager->mWidgetViewContainer.end() )
202 Dali::WidgetView::WidgetView widgetView = iter->second;
204 Dali::WidgetView::GetImplementation( widgetView ).SendWidgetEvent( event );
210 DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::WidgetEventCallback: WidgetView is not found! [%s, %s]\n", widgetId, instanceId );
215 } // namespace Internal
217 } // namespace WidgetView