[Code format] Fixed formating with auto-format tool
[platform/core/api/webapi-plugins.git] / src / utils / utils_instance.cc
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Copyright (c) 2015 Samsung Electronics Co, Ltd. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include <utility>
9
10 #include "common/logger.h"
11 #include "common/scope_exit.h"
12 #include "common/tools.h"
13
14 #include "utils/utils_instance.h"
15
16 using common::PlatformResult;
17 using common::ErrorCode;
18
19 namespace extension {
20 namespace utils {
21
22 UtilsInstance::UtilsInstance() {
23   ScopeLogger();
24   using std::placeholders::_1;
25   using std::placeholders::_2;
26
27 #define REGISTER_METHOD(M) RegisterSyncHandler(#M, std::bind(&UtilsInstance::M, this, _1, _2))
28   REGISTER_METHOD(UtilsGetPkgApiVersion);
29   REGISTER_METHOD(UtilsCheckPrivilegeAccess);
30   REGISTER_METHOD(UtilsCheckBackwardCompabilityPrivilegeAccess);
31   REGISTER_METHOD(UtilsToLongLong);
32   REGISTER_METHOD(UtilsToUnsignedLongLong);
33   REGISTER_METHOD(UtilsCheckProfile);
34 #undef REGISTER_METHOD
35 }
36
37 void UtilsInstance::UtilsGetPkgApiVersion(const picojson::value& args, picojson::object& out) {
38   ScopeLogger();
39
40   std::string api_version;
41   PlatformResult ret = common::tools::GetPkgApiVersion(&api_version);
42   if (ret.IsError()) {
43     ReportError(ret, &out);
44   }
45   ReportSuccess(picojson::value(api_version), out);
46 }
47
48 void UtilsInstance::UtilsCheckPrivilegeAccess(const picojson::value& args, picojson::object& out) {
49   ScopeLogger();
50   const auto& privilege = args.get("privilege").to_str();
51   CHECK_PRIVILEGE_ACCESS(privilege, &out);
52   ReportSuccess(out);
53 }
54
55 void UtilsInstance::UtilsCheckBackwardCompabilityPrivilegeAccess(const picojson::value& args,
56                                                                  picojson::object& out) {
57   ScopeLogger();
58   const auto& current_priv = args.get("current_privilege").to_str();
59   const auto& prev_priv = args.get("previous_privilege").to_str();
60
61   CHECK_BACKWARD_COMPABILITY_PRIVILEGE_ACCESS(current_priv, prev_priv, &out);
62   ReportSuccess(out);
63 }
64
65 namespace {
66
67 template <typename T>
68 int sgn(T val) {
69   return (T(0) < val) - (val < T(0));
70 }
71
72 const double kTwoPow63 = 9223372036854775808.0;
73 const double kTwoPow64 = 18446744073709551616.0;
74
75 }  // namespace
76
77 void UtilsInstance::UtilsToLongLong(const picojson::value& args, picojson::object& out) {
78   ScopeLogger();
79
80   const auto& n = args.get("n");
81   long long output = 0;
82
83   if (n.is<double>()) {
84     auto d = n.get<double>();
85     d = sgn<double>(d) * std::floor(std::fabs(d));
86     d = std::fmod(d, kTwoPow64);
87     if (d > kTwoPow63) {
88       d -= kTwoPow64;
89     }
90     output = static_cast<long long>(d);
91   }
92
93   ReportSuccess(picojson::value(static_cast<double>(output)), out);
94 }
95
96 void UtilsInstance::UtilsToUnsignedLongLong(const picojson::value& args, picojson::object& out) {
97   ScopeLogger();
98
99   const auto& n = args.get("n");
100   unsigned long long output = 0;
101
102   if (n.is<double>()) {
103     auto d = n.get<double>();
104     d = sgn<double>(d) * std::floor(std::fabs(d));
105     d = std::fmod(d, kTwoPow64);
106     if (d < 0.0) {
107       d += kTwoPow64;
108     }
109     output = static_cast<unsigned long long>(d);
110   }
111
112   ReportSuccess(picojson::value(static_cast<double>(output)), out);
113 }
114
115 void UtilsInstance::UtilsCheckProfile(const picojson::value& args, picojson::object& out) {
116   ScopeLogger();
117
118   std::string profile = "common";
119 #if defined(TIZEN_MOBILE)
120   profile = "mobile";
121 #elif defined(TIZEN_WEARABLE)
122   profile = "wearable";
123 #elif defined(TIZEN_TV) || defined(USBHOST)
124   profile = "tv";
125 #elif defined(TIZEN_IVI)
126   profile = "ivi";
127 #endif
128
129   ReportSuccess(picojson::value(profile), out);
130 }
131
132 }  // namespace utils
133 }  // namespace extension