Fix alarm defect for applaunch based logic.
[platform/framework/native/appfw.git] / src / system / FSys_RuntimeInfoImpl.cpp
index 2137e7c..b74a1c4 100644 (file)
@@ -1,5 +1,4 @@
 //
-// Open Service Platform
 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
@@ -35,6 +34,7 @@
 #include <runtime_info.h>
 #include <vconf.h>
 
+#include <FIo.h>
 #include <FAppApp.h>
 #include <FBaseRtEvent.h>
 #include <FBaseRtIEventArg.h>
 #include <FBaseSysLog.h>
 #include <FApp_AppInfo.h>
 #include <FBase_StringConverter.h>
-#include <FSys_EnvironmentImpl.h>
-#include <FSys_RuntimeInfoImpl.h>
+
+#include "FSys_EnvironmentImpl.h"
+#include "FSys_RuntimeInfoImpl.h"
+#include "FSys_RuntimeClient.h"
 
 using namespace Tizen::App;
 using namespace Tizen::Base;
@@ -55,6 +57,10 @@ using namespace std;
 namespace Tizen { namespace System
 {
 
+#ifndef VCONFKEY_SERVICE_READY
+#define VCONFKEY_SERVICE_READY "memory/deviced/boot_power_on"
+#endif
+
 static const wchar_t* _ALLOCATED_MEMORY = L"AllocatedMemory";
 static const wchar_t* _MEMORY_ALLOCATED = L"http://tizen.org/runtime/memory.allocated";
 static const wchar_t* _MEMORY_ALLOCATED_SELF = L"http://tizen.org/runtime/memory.allocated.self";
@@ -92,6 +98,11 @@ static const wchar_t* _STORAGE_ALLOCATED_EXTERNAL_VIDEO = L"http://tizen.org/run
 static const wchar_t* _STORAGE_ALLOCATED_EXTERNAL_IMAGE = L"http://tizen.org/runtime/storage.allocated.external.image";
 static const wchar_t* _STORAGE_ALLOCATED_EXTERNAL_DOWNLOAD = L"http://tizen.org/runtime/storage.allocated.external.download";
 
+static const wchar_t _BOOT_STATUS_KEY[] = L"http://tizen.org/runtime/system.status";
+static const wchar_t _BOOT_STATUS_INITIALIZING[] = L"initializing";
+static const wchar_t _BOOT_STATUS_READY[]  = L"ready";
+
+
 static const int _RUNTIME_INFO_EVENT_TYPE_INT = 101;
 static const int _RUNTIME_INFO_EVENT_TYPE_LONGLONG = 102;
 
@@ -255,7 +266,23 @@ _RuntimeInfoImpl::~_RuntimeInfoImpl(void)
 result
 _RuntimeInfoImpl::GetValue(const String& key, String& value)
 {
-       return E_OBJ_NOT_FOUND;
+       if (key == _BOOT_STATUS_KEY)
+       {
+               int bootReady = -1;
+               const int errorCode = vconf_get_int(VCONFKEY_SERVICE_READY, &bootReady);
+               if (errorCode != 0)
+               {
+                       value = (bootReady == 1 ) ? _BOOT_STATUS_READY : _BOOT_STATUS_INITIALIZING;
+
+                       return E_SUCCESS;
+               }
+
+               return E_SYSTEM;
+       }
+       else
+       {
+               return E_OBJ_NOT_FOUND;
+       }
 }
 
 result
@@ -351,10 +378,13 @@ _RuntimeInfoImpl::GetDirectorySize(const char* path)
                return 0;
        }
 
-       char command[512] = {0,};
-       char fileName[512] = {0,};
+       char* pCommand = null;
+       char* pFileName = null;
+       int fileLength = 0;
+       int commandLength = 0;
        long long size = 0;
        int ret = 0;
+       FILE* pFile = null;
 
        Tizen::App::App* pApp = Tizen::App::App::GetInstance();
        if(pApp == null)
@@ -363,80 +393,65 @@ _RuntimeInfoImpl::GetDirectorySize(const char* path)
        }
 
        String appId(pApp->GetAppId());
-       unique_ptr <char> appIdPath(_StringConverter::CopyToCharArrayN(appId));
+       unique_ptr <char[]> appIdPath(_StringConverter::CopyToCharArrayN(appId));
 
-       ret = sprintf(fileName, "/tmp/size_of_directory_%s.tmp", appIdPath.get());
-       if(ret < 1)
-       {
-               SysLogException(NID_SYS, E_SYSTEM, "It is failed to write file path.");
-               return 0;
-       }
+       fileLength = strlen(appIdPath.get()) + 29;
+       pFileName = (char*)malloc(fileLength);
+       SysTryCatch(NID_SYS, pFileName, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "It is not enough memory.");
 
-       ret = sprintf(command, "rm -rf %s", fileName);
-       if(ret < 1)
-       {
-               SysLogException(NID_SYS, E_SYSTEM, "It is failed to write remove command.");
-               return 0;
-       }
+       ret = sprintf(pFileName, "/tmp/size_of_directory_%s.tmp", appIdPath.get());
+       SysTryCatch(NID_SYS, ret > 0, E_SYSTEM, E_SYSTEM, "It is failed to write file path.");
 
-       ret = system(command);
+       commandLength = strlen(pFileName) + 8;
+       pCommand = (char*) malloc(commandLength);
+       SysTryCatch(NID_SYS, pCommand, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "It is not enough memory.");
 
+       ret = sprintf(pCommand, "rm -rf %s", pFileName);
+       SysTryCatch(NID_SYS, ret > 0, E_SYSTEM, E_SYSTEM, "It is failed to write remove pCommand.");
+
+       ret = system(pCommand);
        if(ret == -1)
        {
-               SysLogException(NID_SYS, E_SYSTEM, "It is failed to execute command[%s].", command);
+               SysLogException(NID_SYS, E_SYSTEM, "It is failed to execute pCommand[%s].", pCommand);
        }
+       free(pCommand);
+       pCommand = null;
 
-       ret = sprintf(command, "du -skb -P %s >> %s", path, fileName);
-       if(ret < 1)
-       {
-               SysLogException(NID_SYS, E_SYSTEM, "It is failed to write du command.");
-               return 0;
-       }
+       commandLength = strlen(pFileName) + strlen(path) + 16;
+       pCommand = (char*)malloc(commandLength);
+       SysTryCatch(NID_SYS, pCommand, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "It is not enough memory.");
 
-       ret = system(command);
-       if(ret == -1)
-       {
-               SysLogException(NID_SYS, E_SYSTEM, "It is failed to execute command[%s].", command);
-               return 0;
-       }
+       ret = sprintf(pCommand, "du -skb -P %s >> %s", path, pFileName);
+       SysTryCatch(NID_SYS, ret > 0, E_SYSTEM, E_SYSTEM, "It is failed to write du pCommand.");
 
-       FILE* pFile = null;
-       pFile = fopen(fileName, "r");
-       if(pFile == null)
-       {
-               return 0;
-       }
+       ret = system(pCommand);
+       SysTryCatch(NID_SYS, ret != -1, E_SYSTEM, E_SYSTEM, "It is failed to execute pCommand[%s].", pCommand);
+
+       pFile = fopen(pFileName, "r");
+       SysTryCatch(NID_SYS, pFile != null, E_SYSTEM, E_SYSTEM, "It is failed to read file [%s].", pFileName);
 
        ret = fscanf(pFile, "%lld", &size);
-       if(ret < 1)
-       {
-               if(pFile != null)
-               {
-                       fclose(pFile);
-               }
+       SysTryCatch(NID_SYS, ret > 0, E_SYSTEM, E_SYSTEM, "It is failed to read file [%s].", pFileName);
 
-               return 0;
-       }
+       ret = sprintf(pCommand, "rm -rf %s", pFileName);
+       SysTryCatch(NID_SYS, ret > 0, E_SYSTEM, E_SYSTEM, "It is failed to write remove pCommand.");
 
-       if(pFile != null)
+       ret = system(pCommand);
+       SysTryCatch(NID_SYS, ret != -1, E_SYSTEM, E_SYSTEM, "It is failed to execute pCommand[%s].", pCommand);
+
+CATCH:
+       if (pFileName)
        {
-               fclose(pFile);
+               free(pFileName);
        }
-
-       ret = sprintf(command, "rm -rf %s", fileName);
-       if(ret < 1)
+       if (pCommand)
        {
-               SysLogException(NID_SYS, E_SYSTEM, "It is failed to write remove command.");
-               return 0;
+               free(pCommand);
        }
-
-       ret = system(command);
-       if(ret == -1)
+       if (pFile)
        {
-               SysLogException(NID_SYS, E_SYSTEM, "It is failed to execute command[%s].", command);
+               fclose(pFile);
        }
-
-
        return size;
 }
 
@@ -711,50 +726,49 @@ _RuntimeInfoImpl::GetValueAsync(const String& key, IRuntimeInfoGetLonglongAsyncR
        int thr_id = 0;
        pthread_t p_thread;
        result r = E_SUCCESS;
-       char* directoryPath = null;
+       String directoryPath;
 
        SysTryReturnResult(NID_SYS, listener != null, E_INVALID_ARG, "listener is null");
 
        if(key == _STORAGE_ALLOCATED_INTERNAL_APPLICATION)
        {
-               directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_APPLICATIONS));
+               directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_APPLICATIONS));
        }       
        else if (key == _STORAGE_ALLOCATED_INTERNAL_AUDIO)
        {
-               directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_SOUNDS));
+               directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_SOUNDS));
        }
        else if (key == _STORAGE_ALLOCATED_INTERNAL_VIDEO)
        {
-               directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_VIDEOS));
+               directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_VIDEOS));
        }
        else if (key == _STORAGE_ALLOCATED_INTERNAL_IMAGE)
        {
-               directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_IMAGES));
+               directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_IMAGES));
        }
        else if (key == _STORAGE_ALLOCATED_INTERNAL_DOWNLOAD)
        {
-               directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_DOWNLOADS));
+               directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_DOWNLOADS));
        }
        else if (key == _STORAGE_ALLOCATED_EXTERNAL_APPLICATION)
        {
-               directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_APPLICATIONS));
+               directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_APPLICATIONS));
        }
        else if (key == _STORAGE_ALLOCATED_EXTERNAL_AUDIO)
        {
-               directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_SOUNDS));
+               directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_SOUNDS));
        }
        else if (key == _STORAGE_ALLOCATED_EXTERNAL_VIDEO)
        {
-               directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_VIDEOS));
+               directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_VIDEOS));
        }
        else if (key == _STORAGE_ALLOCATED_EXTERNAL_IMAGE)
        {
-               directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_IMAGES));
+               directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_IMAGES));
        }
-
        else if (key == _STORAGE_ALLOCATED_EXTERNAL_DOWNLOAD)
        {
-               directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_DOWNLOADS));
+               directoryPath.Append(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_DOWNLOADS));
        }
        else
        {
@@ -763,22 +777,11 @@ _RuntimeInfoImpl::GetValueAsync(const String& key, IRuntimeInfoGetLonglongAsyncR
        }
 
        SysLog(NID_SYS, "%s", GetErrorMessage(r));
-       SysLog(NID_SYS, "%s", directoryPath);
+       SysLog(NID_SYS, "%ls", directoryPath.GetPointer());
        if(r == E_SUCCESS)
        {
-               if(listener != null)
-               {
-                       SysLog(NID_SYS, "listener instance is %x.", listener);
-                       runtimeInfoEvent.AddListener(*listener);
-               }
-
-               _RuntimeInfoEventArg* pEventArg = new (std::nothrow) _RuntimeInfoEventArg();
-               pEventArg->pListener = listener;
-               pEventArg->DirectoryPath = directoryPath;
-
-               thr_id = pthread_create(&p_thread, null, GetDirectorySizeAsync, pEventArg);
-               SysLog(NID_SYS, "Thread id is %d.", thr_id);
-               pthread_detach(p_thread);
+               _RuntimeClient* pRuntimeClient = _RuntimeClient::GetInstance();
+               pRuntimeClient->GetDirectorySizeValueAsync(directoryPath, listener);
        }
 
        return r;