[widget-viewer-dali] Change the license as Flora
[platform/core/uifw/widget-viewer-dali.git] / internal / widget_view_manager / widget_view_manager_impl.cpp
index 4896691..41092d0 100644 (file)
@@ -1,18 +1,18 @@
 /*
+ * Samsung API
  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
+ * Licensed under the Flora License, Version 1.1 (the License);
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://floralicense.org/license/
  *
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
+ * distributed under the License is distributed on an AS IS BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- *
  */
 
 // CLASS HEADER
 // EXTERNAL INCLUDES
 #include <dali/integration-api/debug.h>
 #include <system_info.h>
+#include <cynara-client.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
 #include <widget_errno.h>
 #include <widget_instance.h>
 
@@ -39,11 +43,13 @@ namespace Internal
 namespace
 {
 
+#define SMACK_LABEL_LENGTH 255
+
 #if defined(DEBUG_ENABLED)
 Integration::Log::Filter* gWidgetViewManagerLogging  = Integration::Log::Filter::New( Debug::Verbose, false, "LOG_WIDGET_VIEW_MANAGER" );
 #endif
 
-static inline bool IsWidgetFeatureEnabled()
+static bool IsWidgetFeatureEnabled()
 {
   static bool feature = false;
   static bool retrieved = false;
@@ -63,6 +69,52 @@ static inline bool IsWidgetFeatureEnabled()
   return feature;
 }
 
+static bool CheckPrivilege( const char* privilege )
+{
+  cynara* cynara;
+  int fd = 0;
+  int ret = 0;
+  char subjectLabel[SMACK_LABEL_LENGTH + 1] = "";
+  char uid[10] = { 0, };
+  const char* clientSession = "";
+
+  ret = cynara_initialize( &cynara, NULL );
+  if( ret != CYNARA_API_SUCCESS )
+  {
+    return false;
+  }
+
+  fd = open( "/proc/self/attr/current", O_RDONLY );
+  if( fd < 0 )
+  {
+    cynara_finish( cynara );
+    return false;
+  }
+
+  ret = read( fd, subjectLabel, SMACK_LABEL_LENGTH );
+  if( ret < 0 )
+  {
+    close( fd );
+    cynara_finish( cynara );
+    return false;
+  }
+
+  close( fd );
+
+  snprintf( uid, 10, "%d", getuid() );
+
+  ret = cynara_check( cynara, subjectLabel, clientSession, uid, privilege );
+  if( ret != CYNARA_API_ACCESS_ALLOWED )
+  {
+    cynara_finish( cynara );
+    return false;
+  }
+
+  cynara_finish( cynara );
+
+  return true;
+}
+
 } // unnamed namespace
 
 WidgetViewManagerPtr WidgetViewManager::New( Application application, const std::string& name )
@@ -85,6 +137,7 @@ WidgetViewManager::WidgetViewManager()
 
 WidgetViewManager::~WidgetViewManager()
 {
+  widget_instance_unlisten_event( WidgetViewManager::WidgetEventCallback );
   widget_instance_fini();
 }
 
@@ -96,6 +149,12 @@ int WidgetViewManager::Initialize( Application application, const std::string& n
     return WIDGET_ERROR_NOT_SUPPORTED;
   }
 
+  if( !CheckPrivilege( "http://tizen.org/privilege/widget.viewer" ) )
+  {
+    DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::Initialize: Privilege error.\n" );
+    return WIDGET_ERROR_PERMISSION_DENIED;
+  }
+
   // create compositor
   mCompositor = Pepper::Compositor::New( application, name );
 
@@ -104,6 +163,7 @@ int WidgetViewManager::Initialize( Application application, const std::string& n
 
   // init widget service
   widget_instance_init( name.c_str() );
+  widget_instance_listen_event( WidgetViewManager::WidgetEventCallback, this );
 
   DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::Initialize: success.\n" );
 
@@ -112,10 +172,10 @@ int WidgetViewManager::Initialize( Application application, const std::string& n
   return WIDGET_ERROR_NONE;
 }
 
-Dali::WidgetView::WidgetView WidgetViewManager::AddWidget( const std::string& widgetId, const std::string& contentInfo, int width, int height, double period )
+Dali::WidgetView::WidgetView WidgetViewManager::AddWidget( const std::string& widgetId, const std::string& contentInfo, int width, int height, double updatePeriod )
 {
   // Add a new widget view
-  Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::New( widgetId, contentInfo, width, height, period );
+  Dali::WidgetView::WidgetView widgetView = Dali::WidgetView::WidgetView::New( widgetId, contentInfo, width, height, updatePeriod );
 
   std::string instanceId = widgetView.GetInstanceId();
 
@@ -169,6 +229,28 @@ void WidgetViewManager::OnObjectViewDeleted( Pepper::Compositor compositor, Pepp
   DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::OnObjectViewDeleted: ObjectView is deleted!\n" );
 }
 
+int WidgetViewManager::WidgetEventCallback( const char* widgetId, const char* instanceId, int event, void* data )
+{
+  WidgetViewManager* widgetViewManager = static_cast< WidgetViewManager* >( data );
+
+  if( widgetViewManager->mWidgetViewContainer.size() > 0)
+  {
+    WidgetViewIter iter = widgetViewManager->mWidgetViewContainer.find( std::string( instanceId ) );
+    if( iter != widgetViewManager->mWidgetViewContainer.end() )
+    {
+      Dali::WidgetView::WidgetView widgetView = iter->second;
+
+      Dali::WidgetView::GetImplementation( widgetView ).SendWidgetEvent( event );
+
+      return 0;
+    }
+  }
+
+  DALI_LOG_INFO( gWidgetViewManagerLogging, Debug::Verbose, "WidgetViewManager::WidgetEventCallback: WidgetView is not found! [%s, %s]\n", widgetId, instanceId );
+
+  return 0;
+}
+
 } // namespace Internal
 
 } // namespace WidgetView