Merge branch 'tizen_5.0' into tizen_5.5
[platform/core/api/webapi-plugins.git] / src / common / tools.h
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef COMMON_TOOLS_H_
18 #define COMMON_TOOLS_H_
19
20 #include <string>
21 #include <vector>
22
23 #include "common/picojson.h"
24 #include "common/platform_exception.h"
25 #include "common/platform_result.h"
26 #include "common/tizen_result.h"
27
28 namespace common {
29 namespace tools {
30
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);
36
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);
44
45 // it is used for modules which return TizenResult objects to JS layer
46 #define CHECK_PRIVILEGE(privilege)                                                             \
47   do {                                                                                         \
48     auto r = common::tools::CheckAccess(privilege);                                            \
49     if (!r) {                                                                                  \
50       return common::SecurityError("Application does not have privilege to call this method"); \
51     }                                                                                          \
52   } while (0)
53
54 #define CHECK_PRIVILEGE_ACCESS(privilege, out)      \
55   do {                                              \
56     auto r = common::tools::CheckAccess(privilege); \
57     if (!r) {                                       \
58       common::tools::ReportError(r, out);           \
59       return;                                       \
60     }                                               \
61   } while (0)
62
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)                   \
66   do {                                                    \
67     if (common::tools::IsStoragePrivilegeCheckNeeded()) { \
68       auto ret = common::tools::CheckStorageAccess(path); \
69       if (!ret) {                                         \
70         common::tools::ReportError(ret, out);             \
71         return;                                           \
72       }                                                   \
73     }                                                     \
74   } while (0)
75
76 #define CHECK_STORAGE_ACCESS_AND_RETURN(path)                      \
77   do {                                                             \
78     if (common::tools::IsStoragePrivilegeCheckNeeded()) {          \
79       auto ret = common::tools::CheckStorageAccessAndReturn(path); \
80       if (!ret) {                                                  \
81         return ret;                                                \
82       }                                                            \
83     }                                                              \
84   } while (0)
85
86 #define CHECK_BACKWARD_COMPABILITY_PRIVILEGE_ACCESS(current_priv, prev_priv, out) \
87   do {                                                                            \
88     auto ret = common::tools::CheckAccess(current_priv);                          \
89     auto ret2 = common::tools::CheckAccess(prev_priv);                            \
90                                                                                   \
91     if (!ret && ret2) {                                                           \
92       ret = ret2;                                                                 \
93     }                                                                             \
94                                                                                   \
95     if (!ret) {                                                                   \
96       common::tools::ReportError(ret, out);                                       \
97       return;                                                                     \
98     }                                                                             \
99   } while (0)
100
101 /**
102  * @brief Safe wrapper of strerror() function.
103  *
104  * @param[in] error_code - error code to be passed to strerror()
105  *
106  * @return string representation of error_code
107  */
108 std::string GetErrorString(int error_code);
109
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);
113
114 bool IsPathValid(const std::string& path);
115
116 PlatformResult CheckFileStatus(const std::string& path);
117
118 PlatformResult CheckFileAvailability(const std::string& path);
119
120 void PrintDeprecationWarningFor(const char* className, const char* replacement = nullptr);
121 std::string ConvertToLowerCase(const std::string& input_string);
122
123 }  // namespace tools
124 }  // namespace common
125
126 #endif  // COMMON_TOOLS_H_