Modify tizen coding style
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / dotnet / dotnet_launcher.cc
index b3ad787..d3df80e 100644 (file)
@@ -31,311 +31,275 @@ namespace runtime {
 namespace dotnetcore {
 
 CoreRuntime::CoreRuntime() :
-  InitializeClr(nullptr),
-  ExecuteAssembly(nullptr),
-  Shutdown(nullptr),
-  CreateDelegate(nullptr),
-  coreclrLib(nullptr),
-  hostHandle(nullptr),
-  domainId(-1),
-  PreparedFunction(nullptr),
-  LaunchFunction(nullptr)
+       initializeClr(nullptr),
+       executeAssembly(nullptr),
+       shutdown(nullptr),
+       createDelegate(nullptr),
+       __coreclrLib(nullptr),
+       __hostHandle(nullptr),
+       __domainId(-1),
+       preparedFunction(nullptr),
+       launchFunction(nullptr)
 {
 #define __XSTR(x) #x
 #define __STR(x) __XSTR(x)
 
 #ifdef DEVICE_API_DIR
-  DeviceAPIDirectory = __STR(DEVICE_API_DIR);
+       __deviceAPIDirectory = __STR(DEVICE_API_DIR);
 #endif
 #ifdef RUNTIME_DIR
-  RuntimeDirectory = __STR(RUNTIME_DIR);
+       __runtimeDirectory = __STR(RUNTIME_DIR);
 #endif
 #ifdef NATIVE_LIB_DIR
-  NativeLibDirectory = __STR(NATIVE_LIB_DIR);
+       __nativeLibDirectory = __STR(NATIVE_LIB_DIR);
 #endif
 
 #ifdef USE_MANAGED_LAUNCHER
 #ifdef CORECLR_LAUNCHER_ASSEMBLY_PATH
-  LauncherAssembly = __STR(CORECLR_LAUNCHER_ASSEMBLY_PATH);
+       __launcherAssembly = __STR(CORECLR_LAUNCHER_ASSEMBLY_PATH);
 #endif
 #endif
 
 #undef __STR
 #undef __XSTR
 
-  _DBG("Constructor called!!");
+       _DBG("Constructor called!!");
 }
 
 CoreRuntime::~CoreRuntime()
 {
-  Dispose();
+       dispose();
 }
 
-int CoreRuntime::Initialize(bool standalone)
+int CoreRuntime::initialize(bool standalone)
 {
-  if (standalone)
-  {
-    const char *_deviceapi_directory = getenv("DeviceAPIDirectory");
-    const char *_runtime_directory = getenv("RuntimeDirectory");
-    if (_deviceapi_directory != nullptr)
-      DeviceAPIDirectory = _deviceapi_directory;
-    if (_runtime_directory != nullptr)
-      RuntimeDirectory = _runtime_directory;
+       if (standalone) {
+               const char *deviceApiDirectory = getenv("__deviceAPIDirectory");
+               const char *runtimeDirectory = getenv("__runtimeDirectory");
+               if (deviceApiDirectory != nullptr)
+                       __deviceAPIDirectory = deviceApiDirectory;
+               if (runtimeDirectory != nullptr)
+                       __runtimeDirectory = runtimeDirectory;
 
 #ifdef USE_MANAGED_LAUNCHER
-    const char *_launcher_assembly = getenv("LauncherAssembly");
-    if (_launcher_assembly != nullptr)
-      LauncherAssembly = _launcher_assembly;
+               const char *launcherAssembly = getenv("__launcherAssembly");
+               if (launcherAssembly != nullptr)
+                       __launcherAssembly = launcherAssembly;
 #endif
-  }
-
-  if (DeviceAPIDirectory.empty())
-  {
-    _ERR("Empty Device API Directory");
-    return 1;
-  }
-  else
-  {
-    DeviceAPIDirectory = AbsolutePath(DeviceAPIDirectory);
-  }
-
-  if (RuntimeDirectory.empty())
-  {
-    _ERR("Empty Runtime Directory");
-    return 1;
-  }
-  else
-  {
-    RuntimeDirectory = AbsolutePath(RuntimeDirectory);
-  }
+       }
+
+       if (__deviceAPIDirectory.empty()) {
+               _ERR("Empty Device API Directory");
+               return 1;
+       } else {
+               __deviceAPIDirectory = absolutePath(__deviceAPIDirectory);
+       }
+
+       if (__runtimeDirectory.empty()) {
+               _ERR("Empty Runtime Directory");
+               return 1;
+       } else {
+               __runtimeDirectory = absolutePath(__runtimeDirectory);
+       }
 
 #ifdef USE_MANAGED_LAUNCHER
-  if (LauncherAssembly.empty())
-  {
-    _ERR("Empty Launcher Assembly");
-    return 1;
-  }
-  else
-  {
-    LauncherAssembly = AbsolutePath(LauncherAssembly);
-  }
+       if (__launcherAssembly.empty()) {
+               _ERR("Empty Launcher Assembly");
+               return 1;
+       } else {
+               __launcherAssembly = absolutePath(__launcherAssembly);
+       }
 #endif
 
-  std::string libcoreclr(ConcatPath(RuntimeDirectory, "libcoreclr.so"));
+       std::string libCoreclr(concatPath(__runtimeDirectory, "libcoreclr.so"));
 
-  _DBG("libcoreclr : %s", libcoreclr.c_str());
+       _DBG("libcoreclr : %s", libCoreclr.c_str());
 
-  coreclrLib = dlopen(libcoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
-  if (coreclrLib == nullptr)
-  {
-    char *err = dlerror();
-    _ERR("dlopen failed to open libcoreclr.so with error %s", err);
-    return 1;
-  }
+       __coreclrLib = dlopen(libCoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
+       if (__coreclrLib == nullptr) {
+               char *err = dlerror();
+               _ERR("dlopen failed to open libcoreclr.so with error %s", err);
+               return 1;
+       }
 
 #define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
-  do { variable = (type)dlsym(coreclrLib, name); \
-    if (variable == nullptr) { \
-      _ERR(name " is not found in the libcoreclr.so"); \
-      return 1; \
-    }} while(0)
-
-  CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, InitializeClr, "coreclr_initialize");
-  CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, ExecuteAssembly, "coreclr_execute_assembly");
-  CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, Shutdown, "coreclr_shutdown");
-  CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, CreateDelegate, "coreclr_create_delegate");
+       do { \
+               variable = (type)dlsym(__coreclrLib, name); \
+               if (variable == nullptr) { \
+                       _ERR(name " is not found in the libcoreclr.so"); \
+                       return 1; \
+               } \
+       } while (0)
+
+       CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, initializeClr, "coreclr_initialize");
+       CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, executeAssembly, "coreclr_execute_assembly");
+       CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, shutdown, "coreclr_shutdown");
+       CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, createDelegate, "coreclr_create_delegate");
 
 #undef CORELIB_RETURN_IF_NOSYM
 
-  _DBG("libcoreclr dlopen and dlsym success");
-
-  _DBG("this addr : %x", this);
-  _DBG("coreclr_initialize : %x", InitializeClr);
+       _DBG("libcoreclr dlopen and dlsym success");
+       _DBG("this addr : %x", this);
+       _DBG("coreclr_initialize : %x", initializeClr);
 
-  return 0;
+       return 0;
 }
 
-bool CoreRuntime::InitializeCoreClr(const char* app_id,
-                                    const char* assembly_probe_paths,
-                                    const char* pinvoke_probe_paths,
-                                    const char* tpa_list)
+bool CoreRuntime::initializeCoreClr(const char* appId,
+                                                                        const char* assemblyProbePaths,
+                                                                        const char* pinvokeProbePaths,
+                                                                        const char* tpaList)
 {
-  const char *propertyKeys[] =
-  {
-    "TRUSTED_PLATFORM_ASSEMBLIES",
-    "APP_PATHS",
-    "APP_NI_PATHS",
-    "NATIVE_DLL_SEARCH_DIRECTORIES",
-    "AppDomainCompatSwitch"
-  };
-
-  const char *propertyValues[] =
-  {
-    tpa_list,
-    assembly_probe_paths,
-    assembly_probe_paths,
-    pinvoke_probe_paths,
-    "UseLatestBehaviorWhenTFMNotSpecified"
-  };
-
-  std::string selfPath = ReadSelfPath();
-
-  int st = InitializeClr(
-      selfPath.c_str(),
-      app_id,
-      sizeof(propertyKeys) / sizeof(propertyKeys[0]),
-      propertyKeys,
-      propertyValues,
-      &hostHandle,
-      &domainId);
-
-  if (st < 0)
-  {
-    _ERR("initialize core clr fail! (0x%08x)", st);
-    return false;
-  }
-
-  _DBG("Initialize core clr success");
-  return true;
+       const char *propertyKeys[] = {
+               "TRUSTED_PLATFORM_ASSEMBLIES",
+               "APP_PATHS",
+               "APP_NI_PATHS",
+               "NATIVE_DLL_SEARCH_DIRECTORIES",
+               "AppDomainCompatSwitch"
+       };
+
+       const char *propertyValues[] = {
+               tpaList,
+               assemblyProbePaths,
+               assemblyProbePaths,
+               pinvokeProbePaths,
+               "UseLatestBehaviorWhenTFMNotSpecified"
+       };
+
+       std::string selfPath = readSelfPath();
+
+       int st = initializeClr(selfPath.c_str(),
+                                                       appId,
+                                                       sizeof(propertyKeys) / sizeof(propertyKeys[0]),
+                                                       propertyKeys,
+                                                       propertyValues,
+                                                       &__hostHandle,
+                                                       &__domainId);
+
+       if (st < 0) {
+               _ERR("initialize core clr fail! (0x%08x)", st);
+               return false;
+       }
+
+       _DBG("Initialize core clr success");
+       return true;
 }
 
-int CoreRuntime::RunManagedLauncher(const char* app_id, const char* app_base, const char* tpa_list)
+int CoreRuntime::runManagedLauncher(const char* appId, const char* appBase, const char* tpaList)
 {
-  if (FileNotExist(LauncherAssembly))
-  {
-    _ERR("Launcher assembly is not exist in %s", LauncherAssembly.c_str());
-    return 1;
-  }
-
-  if (!InitializeCoreClr(app_id, app_base, app_base, tpa_list))
-  {
-    _ERR("Failed to initialize coreclr");
-    return 1;
-  }
+       if (fileNotExist(__launcherAssembly)) {
+               _ERR("Launcher assembly is not exist in %s", __launcherAssembly.c_str());
+               return 1;
+       }
+
+       if (!initializeCoreClr(appId, appBase, appBase, tpaList)) {
+               _ERR("Failed to initialize coreclr");
+               return 1;
+       }
 
 #ifdef USE_MANAGED_LAUNCHER
-  void *preparedFunctionDelegate;
-  int st = CreateDelegate(hostHandle, domainId,
-      "Tizen.Runtime.Coreclr",
-      "Tizen.Runtime.Coreclr.AssemblyManager",
-      "Prepared", &preparedFunctionDelegate);
-  if (st < 0)
-  {
-    _ERR("Create delegate for Launch prepared function is fail (0x%08x)", st);
-    return 1;
-  }
-  PreparedFunction = reinterpret_cast<PreparedFunctionPtr>(preparedFunctionDelegate);
-
-  if(PreparedFunction != nullptr)
-  {
-    PreparedFunction();
-  }
-
-  void *launchFunctionDelegate;
-  st = CreateDelegate(hostHandle, domainId,
-      "Tizen.Runtime.Coreclr",
-      "Tizen.Runtime.Coreclr.AssemblyManager",
-      "Launch", &launchFunctionDelegate);
-  if (st < 0)
-  {
-    _ERR("Create delegate for Launch managed function is fail! (0x%08x)", st);
-    return 1;
-  }
-  LaunchFunction = reinterpret_cast<LaunchFunctionPtr>(launchFunctionDelegate);
+       void *preparedFunctionDelegate;
+       int st = createDelegate(__hostHandle, __domainId,
+                                                       "Tizen.Runtime",
+                                                       "Tizen.Runtime.Coreclr.AssemblyManager",
+                                                       "Prepared", &preparedFunctionDelegate);
+       if (st < 0) {
+               _ERR("Create delegate for Launch prepared function is fail (0x%08x)", st);
+               return 1;
+       }
+       preparedFunction = reinterpret_cast<PreparedFunctionPtr>(preparedFunctionDelegate);
+
+       if (preparedFunction != nullptr)
+               preparedFunction();
+
+       void *launchFunctionDelegate;
+       st = createDelegate(__hostHandle, __domainId,
+                                               "Tizen.Runtime",
+                                               "Tizen.Runtime.Coreclr.AssemblyManager",
+                                               "Launch", &launchFunctionDelegate);
+       if (st < 0) {
+               _ERR("Create delegate for Launch managed function is fail! (0x%08x)", st);
+               return 1;
+       }
+       launchFunction = reinterpret_cast<LaunchFunctionPtr>(launchFunctionDelegate);
 #endif
-  return 0;
+       return 0;
 }
 
-void CoreRuntime::Dispose()
+void CoreRuntime::dispose()
 {
-  if (hostHandle != nullptr)
-  {
-    int st = Shutdown(hostHandle, domainId);
-    if (st < 0)
-    {
-      _ERR("shutdown core clr fail! (0x%08x)", st);
-    }
-  }
-
-  if (dlclose(coreclrLib) != 0)
-  {
-    _ERR("libcoreclr.so close failed");
-  }
-  coreclrLib = nullptr;
-
-  _DBG("Dotnet runtime disposed");
+       if (__hostHandle != nullptr) {
+               int st = shutdown(__hostHandle, __domainId);
+               if (st < 0)
+                       _ERR("shutdown core clr fail! (0x%08x)", st);
+       }
+
+       if (dlclose(__coreclrLib) != 0)
+               _ERR("libcoreclr.so close failed");
+
+       __coreclrLib = nullptr;
+
+       _DBG("Dotnet runtime disposed");
 }
 
-int CoreRuntime::Launch(const char* app_id, const char* root, const char* path, int argc, char* argv[])
+int CoreRuntime::launch(const char* appId, const char* root, const char* path, int argc, char* argv[])
 {
-  if (path == nullptr)
-  {
-    _ERR("executable path is null");
-    return 1;
-  }
-
-  if (FileNotExist(path))
-  {
-    _ERR("File not exist : %s", path);
-    return 1;
-  }
-
-  std::string tpa;
-  std::string appRoot = root;
-  std::string appBin = ConcatPath(appRoot, "bin");
-  std::string appLib = ConcatPath(appRoot, "lib");
-  std::string probePath = appBin + ":" + appLib + ":" + NativeLibDirectory;
-
-  std::vector<std::string> searchDirectories;
-  searchDirectories.push_back(appBin);
-  searchDirectories.push_back(appLib);
-  searchDirectories.push_back(RuntimeDirectory);
-  searchDirectories.push_back(DeviceAPIDirectory);
+       if (path == nullptr) {
+               _ERR("executable path is null");
+               return 1;
+       }
+
+       if (fileNotExist(path)) {
+               _ERR("File not exist : %s", path);
+               return 1;
+       }
+
+       std::string tpa;
+       std::string appRoot = root;
+       std::string appBin = concatPath(appRoot, "bin");
+       std::string appLib = concatPath(appRoot, "lib");
+       std::string probePath = appBin + ":" + appLib + ":" + __nativeLibDirectory;
+
+       std::vector<std::string> searchDirectories;
+       searchDirectories.push_back(appBin);
+       searchDirectories.push_back(appLib);
+       searchDirectories.push_back(__runtimeDirectory);
+       searchDirectories.push_back(__deviceAPIDirectory);
 #ifdef USE_MANAGED_LAUNCHER
-  searchDirectories.push_back(Basename(LauncherAssembly));
+       searchDirectories.push_back(baseName(__launcherAssembly));
 #endif
 
-  AssembliesInDirectory(searchDirectories, tpa);
+       assembliesInDirectory(searchDirectories, tpa);
 
 #ifdef USE_MANAGED_LAUNCHER
-  RunManagedLauncher(app_id, probePath.c_str(), tpa.c_str());
-
-  bool success = false;
-  if (LaunchFunction != nullptr)
-  {
-    std::string cpppath(path);
-
-    if (IsManagedAssembly(cpppath) && !IsNativeImage(cpppath))
-    {
-      size_t extindex = cpppath.size() - 4;
-      cpppath = cpppath.substr(0, extindex) + ".ni" + cpppath.substr(extindex, 4);
-      if (!FileNotExist(cpppath))
-      {
-        path = cpppath.c_str();
-      }
-    }
-
-    success = LaunchFunction(root, path, argc, argv);
-    if (!success)
-    {
-      _ERR("Failed to launch Application %s", path);
-    }
-    return success ? 0 : 1;
-  }
-  else
-  {
-    _ERR("Failed to find launch function");
-    return 1;
-  }
+       runManagedLauncher(appId, probePath.c_str(), tpa.c_str());
+
+       bool success = false;
+       if (launchFunction != nullptr) {
+               std::string cppPath(path);
+
+               if (isManagedAssembly(cppPath) && !isNativeImage(cppPath)) {
+                       size_t extindex = cppPath.size() - 4;
+                       cppPath = cppPath.substr(0, extindex) + ".ni" + cppPath.substr(extindex, 4);
+                       if (!fileNotExist(cppPath))
+                               path = cppPath.c_str();
+               }
+
+               success = launchFunction(root, path, argc, argv);
+               if (!success)
+                       _ERR("Failed to launch Application %s", path);
+               return success ? 0 : 1;
+       } else {
+               _ERR("Failed to find launch function");
+               return 1;
+       }
 #else
-  int st = InitializeCoreClr(app_id, probePath.c_str(), probePath.c_str(), tpa.c_str());
-  unsigned int ret = 0;
-  st = ExecuteAssembly(hostHandle, domainId, argc, (const char**)argv, path, &ret);
-  if (st < 0)
-  {
-    _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
-  }
-  return ret;
+       int st = initializeCoreClr(appId, probePath.c_str(), probePath.c_str(), tpa.c_str());
+       unsigned int ret = 0;
+       st = executeAssembly(__hostHandle, __domainId, argc, (const char**)argv, path, &ret);
+       if (st < 0)
+               _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
+       return ret;
 #endif
 }