• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_LINUX_UTIL_H__
6 #define BASE_LINUX_UTIL_H__
7 
8 #include <stdint.h>
9 #include <sys/types.h>
10 
11 #include <string>
12 
13 class FilePath;
14 
15 namespace base {
16 
17 static const char kFindInodeSwitch[] = "--find-inode";
18 
19 // Makes a copy of |pixels| with the ordering changed from BGRA to RGBA.
20 // The caller is responsible for free()ing the data. If |stride| is 0,
21 // it's assumed to be 4 * |width|.
22 uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride);
23 
24 // Get the Linux Distro if we can, or return "Unknown", similar to
25 // GetWinVersion() in base/win_util.h.
26 std::string GetLinuxDistro();
27 
28 // These are used to derive mocks for unittests.
29 class EnvironmentVariableGetter {
30  public:
~EnvironmentVariableGetter()31   virtual ~EnvironmentVariableGetter() {}
32   // Gets an environment variable's value and stores it in
33   // result. Returns false if the key is unset.
34   virtual bool Getenv(const char* variable_name, std::string* result) = 0;
35 
36   // Create an instance of EnvironmentVariableGetter
37   static EnvironmentVariableGetter* Create();
38 };
39 
40 // Get the home directory.
41 FilePath GetHomeDir(EnvironmentVariableGetter* env);
42 
43 // Utility function for getting XDG directories.
44 // |env_name| is the name of an environment variable that we want to use to get
45 // a directory path. |fallback_dir| is the directory relative to $HOME that we
46 // use if |env_name| cannot be found or is empty. |fallback_dir| may be NULL.
47 // Examples of |env_name| are XDG_CONFIG_HOME and XDG_DATA_HOME.
48 FilePath GetXDGDirectory(EnvironmentVariableGetter* env,
49                          const char* env_name, const char* fallback_dir);
50 
51 // Wrapper around xdg_user_dir_lookup() from src/base/third_party/xdg-user-dirs
52 // This looks up "well known" user directories like the desktop and music
53 // folder. Examples of |dir_name| are DESKTOP and MUSIC.
54 FilePath GetXDGUserDirectory(EnvironmentVariableGetter* env,
55                              const char* dir_name, const char* fallback_dir);
56 
57 enum DesktopEnvironment {
58   DESKTOP_ENVIRONMENT_OTHER,
59   DESKTOP_ENVIRONMENT_GNOME,
60   // KDE3 and KDE4 are sufficiently different that we count
61   // them as two different desktop environments here.
62   DESKTOP_ENVIRONMENT_KDE3,
63   DESKTOP_ENVIRONMENT_KDE4,
64 };
65 
66 // Return an entry from the DesktopEnvironment enum with a best guess
67 // of which desktop environment we're using.  We use this to know when
68 // to attempt to use preferences from the desktop environment --
69 // proxy settings, password manager, etc.
70 DesktopEnvironment GetDesktopEnvironment(EnvironmentVariableGetter* env);
71 
72 // Return a string representation of the given desktop environment.
73 // May return NULL in the case of DESKTOP_ENVIRONMENT_OTHER.
74 const char* GetDesktopEnvironmentName(DesktopEnvironment env);
75 // Convenience wrapper that calls GetDesktopEnvironment() first.
76 const char* GetDesktopEnvironmentName(EnvironmentVariableGetter* env);
77 
78 // Return the inode number for the UNIX domain socket |fd|.
79 bool FileDescriptorGetInode(ino_t* inode_out, int fd);
80 
81 // Find the process which holds the given socket, named by inode number. If
82 // multiple processes hold the socket, this function returns false.
83 bool FindProcessHoldingSocket(pid_t* pid_out, ino_t socket_inode);
84 
85 }  // namespace base
86 
87 #endif  // BASE_LINUX_UTIL_H__
88