Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules / utils / src / wrt_global_settings.cpp
1 /*
2  * Copyright (c) 2011 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  * @file        wrt_global_settings.cpp
18  * @version     1.0
19  * @author      Pawel Sikorski(p.sikorski@samsung.com)
20  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
21  * @brief       runtime
22  */
23 #include <stddef.h>
24 #include <cstdlib>
25 #include <cstring>
26 #include <string>
27 #include <sstream>
28 #include <sys/utsname.h>
29 #include <dpl/utils/wrt_global_settings.h>
30
31 namespace GlobalSettings {
32 namespace {
33 const int ROAMING_TEST = 0x00000001;
34 const int POPUPS_TEST = 0x00000002;
35 const int OCSP_TEST = 0x00000004;
36 const int WARP_TEST = 0x00000008;
37 const int CRL_TEST = 0x00000010;
38 const int SCREEN_SHOT_TEST = 0x00000020;
39 const int ALL_TEST = (ROAMING_TEST | POPUPS_TEST | OCSP_TEST | WARP_TEST
40                       | CRL_TEST | SCREEN_SHOT_TEST);
41 const char* WRT_TEST_MODE = "WRT_TEST_MODE";
42 const char* MACHINE_NAME_EMUL = "emulated"; // "arch_emulated"
43 enum MachineType
44 {
45     MACHINE_TYPE_TARGET,
46     MACHINE_TYPE_EMULATOR,
47     MACHINE_TYPE_UNKNOWN
48 };
49
50 struct Settings {
51     bool isEmulator;
52     int testModes;
53
54     Settings() :
55         isEmulator(false), testModes(0)
56     {}
57 };
58
59 Settings gSettings;
60
61 bool initializeGlobalSettings();
62 bool initHelper = initializeGlobalSettings();
63
64 MachineType getMachineType()
65 {
66     // get current machine name
67     struct utsname u;
68     if (0 == uname(&u)) {
69         if (0 == strlen(u.machine)) {
70             return MACHINE_TYPE_UNKNOWN;
71         } else {
72             // If current machine is emul,
73             // machine name include "<arch>_emulated"
74             std::string machine = u.machine;
75             // find "emulated" string in the u.machine
76             if (std::string::npos != machine.find(MACHINE_NAME_EMUL)) {
77                 return MACHINE_TYPE_EMULATOR;
78             } else {
79                 return MACHINE_TYPE_TARGET;
80             }
81         }
82     }
83
84     return MACHINE_TYPE_UNKNOWN;
85 }
86
87 bool initializeGlobalSettings()
88 {
89     (void)initHelper;
90
91     // ignore environment variables if this flag is not set
92 #ifdef GLOBAL_SETTINGS_CONTROL
93     char * envStr = getenv(WRT_TEST_MODE);
94     if (NULL != envStr) {
95         std::string env = envStr;
96         int testMode = 0;
97         if ("1" == env) {
98             testMode = ALL_TEST;
99         } else {
100             std::istringstream str(envStr);
101             while (std::getline(str, env, '|')) {
102                 if ("popups" == env) {
103                     testMode |= POPUPS_TEST;
104                 } else if ("roaming" == env) {
105                     testMode |= ROAMING_TEST;
106                 } else if ("ocsp" == env) {
107                     testMode |= OCSP_TEST;
108                 } else if ("warp" == env) {
109                     testMode |= WARP_TEST;
110                 } else if ("crl" == env) {
111                     testMode |= CRL_TEST;
112                 } else if ("screen" == env) {
113                     testMode |= SCREEN_SHOT_TEST;
114                 }
115             }
116         }
117         gSettings.testModes = testMode;
118     }
119     // TODO other settings initialization
120
121 #endif
122     gSettings.isEmulator = (MACHINE_TYPE_EMULATOR == getMachineType());
123     return false;
124 }
125 } // namespace
126
127 bool TestModeEnabled()
128 {
129     return ((gSettings.testModes & ALL_TEST) == ALL_TEST);
130 }
131
132 bool PopupsTestModeEnabled()
133 {
134     return (gSettings.testModes & POPUPS_TEST);
135 }
136 bool WarpTestModeEnabled()
137 {
138     return (gSettings.testModes & WARP_TEST);
139 }
140 bool RoamingTestModeEnabled()
141 {
142     return (gSettings.testModes & ROAMING_TEST);
143 }
144 bool OCSPTestModeEnabled()
145 {
146     return (gSettings.testModes & OCSP_TEST);
147 }
148 bool CrlTestModeEnabled()
149 {
150     return (gSettings.testModes & CRL_TEST);
151 }
152 bool MakeScreenTestModeEnabled()
153 {
154     return (gSettings.testModes & SCREEN_SHOT_TEST);
155 }
156
157 bool IsEmulator()
158 {
159     return gSettings.isEmulator;
160 }
161 } // GlobalSettings