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