summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authormark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-29 22:18:01 +0000
committermark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-29 22:18:01 +0000
commit05f9b688e319fcb092be0e06f7b72d39dd3113b3 (patch)
tree6d24be7f9b788bbebbff6b2349e51dec49034ce6 /base
parent86ec30d6710923cf1c193eb88b1e6251f831e0ef (diff)
downloadchromium_src-05f9b688e319fcb092be0e06f7b72d39dd3113b3.zip
chromium_src-05f9b688e319fcb092be0e06f7b72d39dd3113b3.tar.gz
chromium_src-05f9b688e319fcb092be0e06f7b72d39dd3113b3.tar.bz2
Refactoring for portability:
- Move chrome/common/env_util to base/sys_info - Move chrome/common/rand_util to base/rand_util (new), simplify its public interface, and fix its implementation Patch by Paweł Hajdan, Jr. <phajdan.jr@gmail.com> http://codereview.chromium.org/4079 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2697 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/SConscript4
-rw-r--r--base/base.xcodeproj/project.pbxproj14
-rw-r--r--base/build/base.vcproj12
-rw-r--r--base/build/base_unittests.vcproj4
-rw-r--r--base/rand_util.cc47
-rw-r--r--base/rand_util.h23
-rw-r--r--base/rand_util_posix.cc27
-rw-r--r--base/rand_util_unittest.cc22
-rw-r--r--base/rand_util_win.cc30
-rw-r--r--base/sys_info.h25
-rw-r--r--base/sys_info_posix.cc59
-rw-r--r--base/sys_info_unittest.cc10
-rw-r--r--base/sys_info_win.cc56
13 files changed, 333 insertions, 0 deletions
diff --git a/base/SConscript b/base/SConscript
index 11858d1..939ab6e 100644
--- a/base/SConscript
+++ b/base/SConscript
@@ -50,6 +50,7 @@ input_files = [
'non_thread_safe.cc',
'path_service.cc',
'pickle.cc',
+ 'rand_util.cc',
'ref_counted.cc',
'revocable_store.cc',
'sha2.cc',
@@ -113,6 +114,7 @@ if env['PLATFORM'] == 'win32':
'pe_image.cc',
'platform_thread_win.cc',
'process_util_win.cc',
+ 'rand_util_win.cc',
'registry.cc',
'shared_memory_win.cc',
'sys_info_win.cc',
@@ -136,6 +138,7 @@ if env['PLATFORM'] in ('darwin', 'posix'):
'message_pump_libevent.cc',
'platform_thread_posix.cc',
'process_util_posix.cc',
+ 'rand_util_posix.cc',
'shared_memory_posix.cc',
'string16.cc',
'sys_info_posix.cc',
@@ -261,6 +264,7 @@ test_files = [
'path_service_unittest.cc',
'pickle_unittest.cc',
'pr_time_unittest.cc',
+ 'rand_util_unittest.cc',
'ref_counted_unittest.cc',
'run_all_unittests.cc',
'scoped_ptr_unittest.cc',
diff --git a/base/base.xcodeproj/project.pbxproj b/base/base.xcodeproj/project.pbxproj
index 4abf9c5..b8db42e 100644
--- a/base/base.xcodeproj/project.pbxproj
+++ b/base/base.xcodeproj/project.pbxproj
@@ -35,6 +35,9 @@
/* End PBXAggregateTarget section */
/* Begin PBXBuildFile section */
+ 4D11B59A0E91730200EF7617 /* rand_util.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D11B5940E9172F800EF7617 /* rand_util.cc */; };
+ 4D11B59B0E91730200EF7617 /* rand_util_posix.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D11B5960E9172F800EF7617 /* rand_util_posix.cc */; };
+ 4D11B59C0E91730500EF7617 /* rand_util_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D11B5970E9172F800EF7617 /* rand_util_unittest.cc */; };
7B26302F0E82F218001CE27F /* message_pump_libevent.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7B26302D0E82F218001CE27F /* message_pump_libevent.cc */; };
7B2630330E82F258001CE27F /* libevent.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7B2630240E82F1E6001CE27F /* libevent.a */; };
7B4C5F4A0E4B6BF900679E8F /* sys_string_conversions_mac.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7B4C5F480E4B6BF900679E8F /* sys_string_conversions_mac.cc */; };
@@ -359,6 +362,10 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
+ 4D11B5940E9172F800EF7617 /* rand_util.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rand_util.cc; sourceTree = "<group>"; };
+ 4D11B5950E9172F800EF7617 /* rand_util.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rand_util.h; sourceTree = "<group>"; };
+ 4D11B5960E9172F800EF7617 /* rand_util_posix.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rand_util_posix.cc; sourceTree = "<group>"; };
+ 4D11B5970E9172F800EF7617 /* rand_util_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rand_util_unittest.cc; sourceTree = "<group>"; };
7B1435DE0E78416400901940 /* skia_utils_mac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = skia_utils_mac.h; sourceTree = "<group>"; };
7B1435DF0E78419700901940 /* native_widget_types.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = native_widget_types.h; sourceTree = "<group>"; };
7B26301F0E82F1E6001CE27F /* libevent.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = libevent.xcodeproj; path = third_party/libevent/libevent.xcodeproj; sourceTree = "<group>"; };
@@ -914,6 +921,10 @@
825403490D92D23C0006B936 /* prtypes.h */,
825403550D92D2580006B936 /* pure.h */,
825403570D92D25E0006B936 /* pure_api.c */,
+ 4D11B5940E9172F800EF7617 /* rand_util.cc */,
+ 4D11B5950E9172F800EF7617 /* rand_util.h */,
+ 4D11B5960E9172F800EF7617 /* rand_util_posix.cc */,
+ 4D11B5970E9172F800EF7617 /* rand_util_unittest.cc */,
7B836C040E55BBB800F6AD31 /* ref_counted.cc */,
825403590D92D27C0006B936 /* ref_counted.h */,
E4AFA4BA0E50DDDF00201347 /* ref_counted_unittest.cc */,
@@ -1346,6 +1357,8 @@
93E703170E5D63E00046259B /* platform_thread_posix.cc in Sources */,
7BD8F4A10E65AA4600034DE9 /* process_util_posix.cc in Sources */,
824654DF0DC26521007C2BAA /* prtime.cc in Sources */,
+ 4D11B59A0E91730200EF7617 /* rand_util.cc in Sources */,
+ 4D11B59B0E91730200EF7617 /* rand_util_posix.cc in Sources */,
7B836C050E55BBB800F6AD31 /* ref_counted.cc in Sources */,
8246548C0DC259DB007C2BAA /* revocable_store.cc in Sources */,
7BA35DD30E8C0D5F0023C8B9 /* scoped_nsautorelease_pool.mm in Sources */,
@@ -1422,6 +1435,7 @@
A5CB82980E5C74E300FD6825 /* platform_test_mac.mm in Sources */,
7B8505D50E5B441000730B43 /* png_codec_unittest.cc in Sources */,
7B78D3980E54FE0100609465 /* pr_time_unittest.cc in Sources */,
+ 4D11B59C0E91730500EF7617 /* rand_util_unittest.cc in Sources */,
7B8505D30E5B43EE00730B43 /* rect_unittest.cc in Sources */,
7B78D3990E54FE0100609465 /* ref_counted_unittest.cc in Sources */,
7B78D39A0E54FE0100609465 /* run_all_unittests.cc in Sources */,
diff --git a/base/build/base.vcproj b/base/build/base.vcproj
index c188a0f..a5cd362 100644
--- a/base/build/base.vcproj
+++ b/base/build/base.vcproj
@@ -558,6 +558,18 @@
>
</File>
<File
+ RelativePath="..\rand_util.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\rand_util.h"
+ >
+ </File>
+ <File
+ RelativePath="..\rand_util_win.cc"
+ >
+ </File>
+ <File
RelativePath="..\ref_counted.cc"
>
</File>
diff --git a/base/build/base_unittests.vcproj b/base/build/base_unittests.vcproj
index 4f1d999..daf0360 100644
--- a/base/build/base_unittests.vcproj
+++ b/base/build/base_unittests.vcproj
@@ -256,6 +256,10 @@
>
</File>
<File
+ RelativePath="..\rand_util_unittest.cc"
+ >
+ </File>
+ <File
RelativePath="..\ref_counted_unittest.cc"
>
</File>
diff --git a/base/rand_util.cc b/base/rand_util.cc
new file mode 100644
index 0000000..89b5b32
--- /dev/null
+++ b/base/rand_util.cc
@@ -0,0 +1,47 @@
+// Copyright (c) 2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/rand_util.h"
+
+#include <math.h>
+
+#include "base/basictypes.h"
+#include "base/logging.h"
+
+namespace {
+
+union uint64_splitter {
+ uint64 normal;
+ uint16 split[4];
+};
+
+} // namespace
+
+namespace base {
+
+int RandInt(int min, int max) {
+ DCHECK(min <= max);
+
+ uint64 range = static_cast<int64>(max) - min + 1;
+ uint64 number = base::RandUInt64();
+ int result = min + static_cast<int>(number % range);
+ DCHECK(result >= min && result <= max);
+ return result;
+}
+
+double RandDouble() {
+ uint64_splitter number;
+ number.normal = base::RandUInt64();
+
+ // Standard code based on drand48 would give only 48 bits of precision.
+ // We try to get maximum precision for IEEE 754 double (52 bits).
+ double result = ldexp(static_cast<double>(number.split[0] & 0xf), -52) +
+ ldexp(static_cast<double>(number.split[1]), -48) +
+ ldexp(static_cast<double>(number.split[2]), -32) +
+ ldexp(static_cast<double>(number.split[3]), -16);
+ DCHECK(result >= 0.0 && result < 1.0);
+ return result;
+}
+
+} // namespace base
diff --git a/base/rand_util.h b/base/rand_util.h
new file mode 100644
index 0000000..cd687dd
--- /dev/null
+++ b/base/rand_util.h
@@ -0,0 +1,23 @@
+// Copyright (c) 2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_RAND_UTIL_H_
+#define BASE_RAND_UTIL_H_
+
+#include "base/basictypes.h"
+
+namespace base {
+
+// Returns a random number in range [0, kuint64max]. Thread-safe.
+uint64 RandUInt64();
+
+// Returns a random number between min and max (inclusive). Thread-safe.
+int RandInt(int min, int max);
+
+// Returns a random double in range [0, 1). Thread-safe.
+double RandDouble();
+
+} // namespace base
+
+#endif // BASE_RAND_UTIL_H_
diff --git a/base/rand_util_posix.cc b/base/rand_util_posix.cc
new file mode 100644
index 0000000..392cde5
--- /dev/null
+++ b/base/rand_util_posix.cc
@@ -0,0 +1,27 @@
+// Copyright (c) 2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/rand_util.h"
+
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "base/logging.h"
+
+namespace base {
+
+uint64 RandUInt64() {
+ uint64 number;
+
+ int urandom_fd = open("/dev/urandom", O_RDONLY);
+ CHECK(urandom_fd >= 0);
+ ssize_t bytes_read = read(urandom_fd, &number, sizeof(number));
+ CHECK(bytes_read == sizeof(number));
+ close(urandom_fd);
+
+ return number;
+}
+
+} // namespace base
diff --git a/base/rand_util_unittest.cc b/base/rand_util_unittest.cc
new file mode 100644
index 0000000..2710627
--- /dev/null
+++ b/base/rand_util_unittest.cc
@@ -0,0 +1,22 @@
+// Copyright (c) 2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/rand_util.h"
+
+#include <limits>
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+const int kIntMin = std::numeric_limits<int>::min();
+const int kIntMax = std::numeric_limits<int>::max();
+
+} // namespace
+
+TEST(RandUtilTest, SameMinAndMax) {
+ EXPECT_EQ(base::RandInt(0, 0), 0);
+ EXPECT_EQ(base::RandInt(kIntMin, kIntMin), kIntMin);
+ EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax);
+}
diff --git a/base/rand_util_win.cc b/base/rand_util_win.cc
new file mode 100644
index 0000000..12cf11e
--- /dev/null
+++ b/base/rand_util_win.cc
@@ -0,0 +1,30 @@
+// Copyright (c) 2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/rand_util.h"
+
+#include <stdlib.h>
+
+#include "base/basictypes.h"
+#include "base/logging.h"
+
+namespace {
+
+uint32 RandUInt32() {
+ uint32 number;
+ CHECK(rand_s(&number) == 0);
+ return number;
+}
+
+} // namespace
+
+namespace base {
+
+uint64 RandUInt64() {
+ uint32 first_half = RandUInt32();
+ uint32 second_half = RandUInt32();
+ return (static_cast<uint64>(first_half) << 32) + second_half;
+}
+
+} // namespace base
diff --git a/base/sys_info.h b/base/sys_info.h
index 3d6a16a6..a70f5b6 100644
--- a/base/sys_info.h
+++ b/base/sys_info.h
@@ -28,6 +28,31 @@ class SysInfo {
// or -1 on failure.
static int64 AmountOfFreeDiskSpace(const std::wstring& path);
+ // Return true if the given environment variable is defined.
+ // TODO: find a better place for HasEnvVar.
+ static bool HasEnvVar(const wchar_t* var);
+
+ // Return the value of the given environment variable
+ // or an empty string if not defined.
+ // TODO: find a better place for GetEnvVar.
+ static std::wstring GetEnvVar(const wchar_t* var);
+
+ // Returns the name of the host operating system.
+ static std::string OperatingSystemName();
+
+ // Returns the version of the host operating system.
+ static std::string OperatingSystemVersion();
+
+ // Returns the CPU architecture of the system. Exact return value may differ
+ // across platforms.
+ static std::string CPUArchitecture();
+
+ // Returns the pixel dimensions of the primary display via the
+ // width and height parameters.
+ static void GetPrimaryDisplayDimensions(int* width, int* height);
+
+ // Return the number of displays.
+ static int DisplayCount();
};
} // namespace base
diff --git a/base/sys_info_posix.cc b/base/sys_info_posix.cc
index 28c8bd6..b39457d 100644
--- a/base/sys_info_posix.cc
+++ b/base/sys_info_posix.cc
@@ -8,6 +8,7 @@
#include <errno.h>
#include <string.h>
#include <sys/statvfs.h>
+#include <sys/utsname.h>
#include <unistd.h>
#if defined(OS_MACOSX)
@@ -70,4 +71,62 @@ int64 SysInfo::AmountOfFreeDiskSpace(const std::wstring& path) {
return static_cast<int64>(stats.f_bavail) * stats.f_frsize;
}
+// static
+bool SysInfo::HasEnvVar(const wchar_t* var) {
+ std::string var_utf8 = WideToUTF8(std::wstring(var));
+ return getenv(var_utf8.c_str()) != NULL;
+}
+
+// static
+std::wstring SysInfo::GetEnvVar(const wchar_t* var) {
+ std::string var_utf8 = WideToUTF8(std::wstring(var));
+ char* value = getenv(var_utf8.c_str());
+ if (!value) {
+ return L"";
+ } else {
+ return UTF8ToWide(value);
+ }
+}
+
+// static
+std::string SysInfo::OperatingSystemName() {
+ utsname info;
+ if (uname(&info) < 0) {
+ NOTREACHED();
+ return "";
+ }
+ return std::string(info.sysname);
+}
+
+// static
+std::string SysInfo::OperatingSystemVersion() {
+ utsname info;
+ if (uname(&info) < 0) {
+ NOTREACHED();
+ return "";
+ }
+ return std::string(info.release);
+}
+
+// static
+std::string SysInfo::CPUArchitecture() {
+ utsname info;
+ if (uname(&info) < 0) {
+ NOTREACHED();
+ return "";
+ }
+ return std::string(info.machine);
+}
+
+// static
+void SysInfo::GetPrimaryDisplayDimensions(int* width, int* height) {
+ NOTIMPLEMENTED();
+}
+
+// static
+int SysInfo::DisplayCount() {
+ NOTIMPLEMENTED();
+ return 1;
+}
+
} // namespace base
diff --git a/base/sys_info_unittest.cc b/base/sys_info_unittest.cc
index 8357c5a..ea67aa4 100644
--- a/base/sys_info_unittest.cc
+++ b/base/sys_info_unittest.cc
@@ -26,3 +26,13 @@ TEST_F(SysInfoTest, AmountOfFreeDiskSpace) {
ASSERT_TRUE(file_util::GetTempDir(&tmp_path));
EXPECT_GT(base::SysInfo::AmountOfFreeDiskSpace(tmp_path), 0) << tmp_path;
}
+
+TEST_F(SysInfoTest, GetEnvVar) {
+ // Every setup should have non-empty PATH...
+ EXPECT_NE(base::SysInfo::GetEnvVar(L"PATH"), L"");
+}
+
+TEST_F(SysInfoTest, HasEnvVar) {
+ // Every setup should have PATH...
+ EXPECT_TRUE(base::SysInfo::HasEnvVar(L"PATH"));
+}
diff --git a/base/sys_info_win.cc b/base/sys_info_win.cc
index 2d3c203..4d52c4a 100644
--- a/base/sys_info_win.cc
+++ b/base/sys_info_win.cc
@@ -7,6 +7,8 @@
#include <windows.h>
#include "base/logging.h"
+#include "base/scoped_ptr.h"
+#include "base/string_util.h"
namespace base {
@@ -44,4 +46,58 @@ int64 SysInfo::AmountOfFreeDiskSpace(const std::wstring& path) {
return rv;
}
+// static
+bool SysInfo::HasEnvVar(const wchar_t* var) {
+ return GetEnvironmentVariable(var, NULL, 0) != 0;
+}
+
+// static
+std::wstring SysInfo::GetEnvVar(const wchar_t* var) {
+ DWORD value_length = GetEnvironmentVariable(var, NULL, 0);
+ if (value_length == 0) {
+ return L"";
+ }
+ scoped_array<wchar_t> value(new wchar_t[value_length]);
+ GetEnvironmentVariable(var, value.get(), value_length);
+ return std::wstring(value.get());
+}
+
+// static
+std::string SysInfo::OperatingSystemName() {
+ return "Windows NT";
+}
+
+// static
+std::string SysInfo::OperatingSystemVersion() {
+ OSVERSIONINFO info = {0};
+ info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+ GetVersionEx(&info);
+
+ return StringPrintf("%lu.%lu", info.dwMajorVersion, info.dwMinorVersion);
+}
+
+// TODO: Implement OperatingSystemVersionComplete, which would include
+// patchlevel/service pack number. See chrome/browser/views/bug_report_view.cc,
+// BugReportView::SetOSVersion.
+
+// static
+std::string SysInfo::CPUArchitecture() {
+ // TODO: Make this vary when we support any other architectures.
+ return "x86";
+}
+
+// static
+void SysInfo::GetPrimaryDisplayDimensions(int* width, int* height) {
+ if (width)
+ *width = GetSystemMetrics(SM_CXSCREEN);
+
+ if (height)
+ *height = GetSystemMetrics(SM_CYSCREEN);
+}
+
+// static
+int SysInfo::DisplayCount() {
+ return GetSystemMetrics(SM_CMONITORS);
+}
+
} // namespace base