Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: base/linux_util.cc

Issue 1606007: Move EnvironmentVariableGetter from base/linux_util.h to base/env_var.h. Labe... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix windows build for good this time? Created 10 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/linux_util.h ('k') | base/sys_info.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/linux_util.h" 5 #include "base/linux_util.h"
6 6
7 #include <dirent.h> 7 #include <dirent.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <glib.h> 9 #include <glib.h>
10 #include <stdlib.h> 10 #include <stdlib.h>
11 #include <sys/stat.h> 11 #include <sys/stat.h>
12 #include <unistd.h> 12 #include <unistd.h>
13 13
14 #include <vector> 14 #include <vector>
15 15
16 #include "base/command_line.h" 16 #include "base/command_line.h"
17 #include "base/env_var.h"
17 #include "base/lock.h" 18 #include "base/lock.h"
18 #include "base/path_service.h" 19 #include "base/path_service.h"
19 #include "base/process_util.h" 20 #include "base/process_util.h"
20 #include "base/singleton.h" 21 #include "base/singleton.h"
21 #include "base/string_util.h" 22 #include "base/string_util.h"
22 #include "base/third_party/xdg_user_dirs/xdg_user_dir_lookup.h" 23 #include "base/third_party/xdg_user_dirs/xdg_user_dir_lookup.h"
23 24
24 namespace { 25 namespace {
25 26
26 class EnvironmentVariableGetterImpl
27 : public base::EnvironmentVariableGetter {
28 public:
29 virtual bool Getenv(const char* variable_name, std::string* result) {
30 const char* env_value = ::getenv(variable_name);
31 if (env_value) {
32 // Note that the variable may be defined but empty.
33 *result = env_value;
34 return true;
35 }
36 // Some commonly used variable names are uppercase while others
37 // are lowercase, which is inconsistent. Let's try to be helpful
38 // and look for a variable name with the reverse case.
39 char first_char = variable_name[0];
40 std::string alternate_case_var;
41 if (first_char >= 'a' && first_char <= 'z')
42 alternate_case_var = StringToUpperASCII(std::string(variable_name));
43 else if (first_char >= 'A' && first_char <= 'Z')
44 alternate_case_var = StringToLowerASCII(std::string(variable_name));
45 else
46 return false;
47 env_value = ::getenv(alternate_case_var.c_str());
48 if (env_value) {
49 *result = env_value;
50 return true;
51 }
52 return false;
53 }
54 };
55
56 // Not needed for OS_CHROMEOS. 27 // Not needed for OS_CHROMEOS.
57 #if defined(OS_LINUX) 28 #if defined(OS_LINUX)
58 enum LinuxDistroState { 29 enum LinuxDistroState {
59 STATE_DID_NOT_CHECK = 0, 30 STATE_DID_NOT_CHECK = 0,
60 STATE_CHECK_STARTED = 1, 31 STATE_CHECK_STARTED = 1,
61 STATE_CHECK_FINISHED = 2, 32 STATE_CHECK_FINISHED = 2,
62 }; 33 };
63 34
64 // Helper class for GetLinuxDistro(). 35 // Helper class for GetLinuxDistro().
65 class LinuxDistroHelper { 36 class LinuxDistroHelper {
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 LOG(WARNING) << "Failed to parse a socket's inode number: the number was " 112 LOG(WARNING) << "Failed to parse a socket's inode number: the number was "
142 "too large. Please report this bug: " << buf; 113 "too large. Please report this bug: " << buf;
143 } 114 }
144 return false; 115 return false;
145 } 116 }
146 117
147 *inode_out = inode_ul; 118 *inode_out = inode_ul;
148 return true; 119 return true;
149 } 120 }
150 121
151 } // anonymous namespace 122 } // namespace
152 123
153 namespace base { 124 namespace base {
154 125
155 uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride) { 126 uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride) {
156 if (stride == 0) 127 if (stride == 0)
157 stride = width * 4; 128 stride = width * 4;
158 129
159 uint8_t* new_pixels = static_cast<uint8_t*>(malloc(height * stride)); 130 uint8_t* new_pixels = static_cast<uint8_t*>(malloc(height * stride));
160 131
161 // We have to copy the pixels and swap from BGRA to RGBA. 132 // We have to copy the pixels and swap from BGRA to RGBA.
(...skipping 12 matching lines...) Expand all
174 145
175 // We use this static string to hold the Linux distro info. If we 146 // We use this static string to hold the Linux distro info. If we
176 // crash, the crash handler code will send this in the crash dump. 147 // crash, the crash handler code will send this in the crash dump.
177 std::string linux_distro = 148 std::string linux_distro =
178 #if defined(OS_CHROMEOS) 149 #if defined(OS_CHROMEOS)
179 "CrOS"; 150 "CrOS";
180 #else // if defined(OS_LINUX) 151 #else // if defined(OS_LINUX)
181 "Unknown"; 152 "Unknown";
182 #endif 153 #endif
183 154
184 FilePath GetHomeDir(EnvironmentVariableGetter* env) { 155 FilePath GetHomeDir(EnvVarGetter* env) {
185 std::string home_dir; 156 std::string home_dir;
186 if (env->Getenv("HOME", &home_dir) && !home_dir.empty()) 157 if (env->GetEnv("HOME", &home_dir) && !home_dir.empty())
187 return FilePath(home_dir); 158 return FilePath(home_dir);
188 159
189 home_dir = g_get_home_dir(); 160 home_dir = g_get_home_dir();
190 if (!home_dir.empty()) 161 if (!home_dir.empty())
191 return FilePath(home_dir); 162 return FilePath(home_dir);
192 163
193 FilePath rv; 164 FilePath rv;
194 if (PathService::Get(base::DIR_TEMP, &rv)) 165 if (PathService::Get(base::DIR_TEMP, &rv))
195 return rv; 166 return rv;
196 167
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 return "Unknown"; 200 return "Unknown";
230 } else { 201 } else {
231 // In STATE_CHECK_FINISHED, no more writing to |linux_distro|. 202 // In STATE_CHECK_FINISHED, no more writing to |linux_distro|.
232 return linux_distro; 203 return linux_distro;
233 } 204 }
234 #else 205 #else
235 NOTIMPLEMENTED(); 206 NOTIMPLEMENTED();
236 #endif 207 #endif
237 } 208 }
238 209
239 FilePath GetXDGDirectory(EnvironmentVariableGetter* env, 210 FilePath GetXDGDirectory(EnvVarGetter* env, const char* env_name,
240 const char* env_name, const char* fallback_dir) { 211 const char* fallback_dir) {
241 std::string env_value; 212 std::string env_value;
242 if (env->Getenv(env_name, &env_value) && !env_value.empty()) 213 if (env->GetEnv(env_name, &env_value) && !env_value.empty())
243 return FilePath(env_value); 214 return FilePath(env_value);
244 return GetHomeDir(env).Append(fallback_dir); 215 return GetHomeDir(env).Append(fallback_dir);
245 } 216 }
246 217
247 FilePath GetXDGUserDirectory(EnvironmentVariableGetter* env, 218 FilePath GetXDGUserDirectory(EnvVarGetter* env, const char* dir_name,
248 const char* dir_name, const char* fallback_dir) { 219 const char* fallback_dir) {
249 char* xdg_dir = xdg_user_dir_lookup(dir_name); 220 char* xdg_dir = xdg_user_dir_lookup(dir_name);
250 if (xdg_dir) { 221 if (xdg_dir) {
251 FilePath rv(xdg_dir); 222 FilePath rv(xdg_dir);
252 free(xdg_dir); 223 free(xdg_dir);
253 return rv; 224 return rv;
254 } 225 }
255 return GetHomeDir(env).Append(fallback_dir); 226 return GetHomeDir(env).Append(fallback_dir);
256 } 227 }
257 228
258 // static 229 DesktopEnvironment GetDesktopEnvironment(EnvVarGetter* env) {
259 EnvironmentVariableGetter* EnvironmentVariableGetter::Create() {
260 return new EnvironmentVariableGetterImpl();
261 }
262
263 DesktopEnvironment GetDesktopEnvironment(EnvironmentVariableGetter* env) {
264 std::string desktop_session; 230 std::string desktop_session;
265 if (env->Getenv("DESKTOP_SESSION", &desktop_session)) { 231 if (env->GetEnv("DESKTOP_SESSION", &desktop_session)) {
266 if (desktop_session == "gnome") 232 if (desktop_session == "gnome")
267 return DESKTOP_ENVIRONMENT_GNOME; 233 return DESKTOP_ENVIRONMENT_GNOME;
268 else if (desktop_session == "kde4") 234 else if (desktop_session == "kde4")
269 return DESKTOP_ENVIRONMENT_KDE4; 235 return DESKTOP_ENVIRONMENT_KDE4;
270 else if (desktop_session == "kde") { 236 else if (desktop_session == "kde") {
271 // This may mean KDE4 on newer systems, so we have to check. 237 // This may mean KDE4 on newer systems, so we have to check.
272 std::string dummy; 238 if (env->HasEnv("KDE_SESSION_VERSION"))
273 if (env->Getenv("KDE_SESSION_VERSION", &dummy))
274 return DESKTOP_ENVIRONMENT_KDE4; 239 return DESKTOP_ENVIRONMENT_KDE4;
275 return DESKTOP_ENVIRONMENT_KDE3; 240 return DESKTOP_ENVIRONMENT_KDE3;
276 } 241 }
277 else if (desktop_session == "xfce4") 242 else if (desktop_session == "xfce4")
278 return DESKTOP_ENVIRONMENT_XFCE; 243 return DESKTOP_ENVIRONMENT_XFCE;
279 } 244 }
280 245
281 // Fall back on some older environment variables. 246 // Fall back on some older environment variables.
282 // Useful particularly in the DESKTOP_SESSION=default case. 247 // Useful particularly in the DESKTOP_SESSION=default case.
283 std::string dummy; 248 if (env->HasEnv("GNOME_DESKTOP_SESSION_ID")) {
284 if (env->Getenv("GNOME_DESKTOP_SESSION_ID", &dummy)) {
285 return DESKTOP_ENVIRONMENT_GNOME; 249 return DESKTOP_ENVIRONMENT_GNOME;
286 } else if (env->Getenv("KDE_FULL_SESSION", &dummy)) { 250 } else if (env->HasEnv("KDE_FULL_SESSION")) {
287 if (env->Getenv("KDE_SESSION_VERSION", &dummy)) 251 if (env->HasEnv("KDE_SESSION_VERSION"))
288 return DESKTOP_ENVIRONMENT_KDE4; 252 return DESKTOP_ENVIRONMENT_KDE4;
289 return DESKTOP_ENVIRONMENT_KDE3; 253 return DESKTOP_ENVIRONMENT_KDE3;
290 } 254 }
291 255
292 return DESKTOP_ENVIRONMENT_OTHER; 256 return DESKTOP_ENVIRONMENT_OTHER;
293 } 257 }
294 258
295 const char* GetDesktopEnvironmentName(DesktopEnvironment env) { 259 const char* GetDesktopEnvironmentName(DesktopEnvironment env) {
296 switch (env) { 260 switch (env) {
297 case DESKTOP_ENVIRONMENT_OTHER: 261 case DESKTOP_ENVIRONMENT_OTHER:
298 return NULL; 262 return NULL;
299 case DESKTOP_ENVIRONMENT_GNOME: 263 case DESKTOP_ENVIRONMENT_GNOME:
300 return "GNOME"; 264 return "GNOME";
301 case DESKTOP_ENVIRONMENT_KDE3: 265 case DESKTOP_ENVIRONMENT_KDE3:
302 return "KDE3"; 266 return "KDE3";
303 case DESKTOP_ENVIRONMENT_KDE4: 267 case DESKTOP_ENVIRONMENT_KDE4:
304 return "KDE4"; 268 return "KDE4";
305 case DESKTOP_ENVIRONMENT_XFCE: 269 case DESKTOP_ENVIRONMENT_XFCE:
306 return "XFCE"; 270 return "XFCE";
307 } 271 }
308 return NULL; 272 return NULL;
309 } 273 }
310 274
311 const char* GetDesktopEnvironmentName(EnvironmentVariableGetter* env) { 275 const char* GetDesktopEnvironmentName(EnvVarGetter* env) {
312 return GetDesktopEnvironmentName(GetDesktopEnvironment(env)); 276 return GetDesktopEnvironmentName(GetDesktopEnvironment(env));
313 } 277 }
314 278
315 bool FileDescriptorGetInode(ino_t* inode_out, int fd) { 279 bool FileDescriptorGetInode(ino_t* inode_out, int fd) {
316 DCHECK(inode_out); 280 DCHECK(inode_out);
317 281
318 struct stat buf; 282 struct stat buf;
319 if (fstat(fd, &buf) < 0) 283 if (fstat(fd, &buf) < 0)
320 return false; 284 return false;
321 285
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 } 342 }
379 } 343 }
380 344
381 closedir(fd); 345 closedir(fd);
382 } 346 }
383 347
384 return already_found; 348 return already_found;
385 } 349 }
386 350
387 } // namespace base 351 } // namespace base
OLDNEW
« no previous file with comments | « base/linux_util.h ('k') | base/sys_info.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698