2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef COMMON_TOOLS_H_
18 #define COMMON_TOOLS_H_
23 #include "common/picojson.h"
24 #include "common/platform_exception.h"
25 #include "common/platform_result.h"
26 #include "common/tizen_result.h"
31 void ReportSuccess(picojson::object& out);
32 void ReportSuccess(const picojson::value& result, picojson::object& out);
33 void ReportError(picojson::object& out);
34 void ReportError(const PlatformException& ex, picojson::object& out);
35 void ReportError(const PlatformResult& error, picojson::object* out);
37 common::PlatformResult CheckAccess(const std::string& privilege);
38 common::PlatformResult CheckAccess(const std::vector<std::string>& privileges);
39 common::PlatformResult CheckStorageAccess(const std::string& path);
40 common::TizenResult CheckStorageAccessAndReturn(const std::string& path);
41 common::PlatformResult GetPkgApiVersion(std::string* api_version);
42 bool IsStoragePrivilegeCheckNeeded();
43 bool IsAppVersionEarlierThan(const std::string& ver);
45 // it is used for modules which return TizenResult objects to JS layer
46 #define CHECK_PRIVILEGE(privilege) \
48 auto r = common::tools::CheckAccess(privilege); \
50 return common::SecurityError("Application does not have privilege to call this method"); \
54 #define CHECK_PRIVILEGE_ACCESS(privilege, out) \
56 auto r = common::tools::CheckAccess(privilege); \
58 common::tools::ReportError(r, out); \
63 // The below macro is designed to check, whether the application has necessary privilege in order
64 // to access resource on internal or external memory.
65 #define CHECK_STORAGE_ACCESS(path, out) \
67 if (common::tools::IsStoragePrivilegeCheckNeeded()) { \
68 auto ret = common::tools::CheckStorageAccess(path); \
70 common::tools::ReportError(ret, out); \
76 #define CHECK_STORAGE_ACCESS_AND_RETURN(path) \
78 if (common::tools::IsStoragePrivilegeCheckNeeded()) { \
79 auto ret = common::tools::CheckStorageAccessAndReturn(path); \
86 #define CHECK_BACKWARD_COMPABILITY_PRIVILEGE_ACCESS(current_priv, prev_priv, out) \
88 auto ret = common::tools::CheckAccess(current_priv); \
89 auto ret2 = common::tools::CheckAccess(prev_priv); \
96 common::tools::ReportError(ret, out); \
102 * @brief Safe wrapper of strerror() function.
104 * @param[in] error_code - error code to be passed to strerror()
106 * @return string representation of error_code
108 std::string GetErrorString(int error_code);
110 int HexToInt(char c);
111 unsigned char* HexToBin(const char* hex, int size, unsigned char* bin, int bin_size);
112 char* BinToHex(const unsigned char* bin, int size, char* hex, int hex_size);
114 bool IsPathValid(const std::string& path);
116 PlatformResult CheckFileStatus(const std::string& path);
118 PlatformResult CheckFileAvailability(const std::string& path);
120 void PrintDeprecationWarningFor(const char* className, const char* replacement = nullptr);
121 std::string ConvertToLowerCase(const std::string& input_string);
124 } // namespace common
126 #endif // COMMON_TOOLS_H_