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