diff options
author | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-29 22:18:01 +0000 |
---|---|---|
committer | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-29 22:18:01 +0000 |
commit | 05f9b688e319fcb092be0e06f7b72d39dd3113b3 (patch) | |
tree | 6d24be7f9b788bbebbff6b2349e51dec49034ce6 | |
parent | 86ec30d6710923cf1c193eb88b1e6251f831e0ef (diff) | |
download | chromium_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
33 files changed, 370 insertions, 200 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 diff --git a/chrome/browser/browser_main.cc b/chrome/browser/browser_main.cc index fbb216a9..3cb98f1 100644 --- a/chrome/browser/browser_main.cc +++ b/chrome/browser/browser_main.cc @@ -41,7 +41,6 @@ #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_switches.h" -#include "chrome/common/env_util.h" #include "chrome/common/env_vars.h" #include "chrome/common/jstemplate_builder.h" #include "chrome/common/l10n_util.h" diff --git a/chrome/browser/cache_manager_host.cc b/chrome/browser/cache_manager_host.cc index 13fc171..be6faf3 100644 --- a/chrome/browser/cache_manager_host.cc +++ b/chrome/browser/cache_manager_host.cc @@ -11,7 +11,6 @@ #include "chrome/browser/browser_process.h" #include "chrome/browser/render_process_host.h" #include "chrome/common/chrome_constants.h" -#include "chrome/common/env_util.h" #include "chrome/common/pref_names.h" #include "chrome/common/pref_service.h" #include "chrome/common/notification_service.h" diff --git a/chrome/browser/importer/importer_unittest.cc b/chrome/browser/importer/importer_unittest.cc index 3d39db1..4a0a8df 100644 --- a/chrome/browser/importer/importer_unittest.cc +++ b/chrome/browser/importer/importer_unittest.cc @@ -19,7 +19,6 @@ #include "chrome/browser/importer/importer.h" #include "chrome/browser/profile.h" #include "chrome/common/chrome_paths.h" -#include "chrome/common/env_util.h" #include "chrome/common/win_util.h" class ImporterTest : public testing::Test { @@ -274,10 +273,6 @@ void WritePStore(IPStore* pstore, const GUID* type, const GUID* subtype) { } TEST_F(ImporterTest, IEImporter) { - // Skips in Win2000 for the running environment can not be set up. - if (env_util::GetOperatingSystemVersion() == "5.0") - return; - // Sets up a favorites folder. win_util::ScopedCOMInitializer com_init; std::wstring path = test_path_; diff --git a/chrome/browser/metrics_log.cc b/chrome/browser/metrics_log.cc index bb2c2bb..a1d8f67 100644 --- a/chrome/browser/metrics_log.cc +++ b/chrome/browser/metrics_log.cc @@ -12,7 +12,6 @@ #include "base/sys_info.h" #include "chrome/browser/autocomplete/autocomplete.h" #include "chrome/browser/browser_process.h" -#include "chrome/common/env_util.h" #include "chrome/common/logging_chrome.h" #include "chrome/common/pref_names.h" #include "chrome/common/pref_service.h" @@ -447,7 +446,7 @@ void MetricsLog::RecordEnvironment( { OPEN_ELEMENT_FOR_SCOPE("cpu"); - WriteAttribute("arch", env_util::GetCPUArchitecture()); + WriteAttribute("arch", base::SysInfo::CPUArchitecture()); } { @@ -469,19 +468,19 @@ void MetricsLog::RecordEnvironment( { OPEN_ELEMENT_FOR_SCOPE("os"); WriteAttribute("name", - env_util::GetOperatingSystemName()); + base::SysInfo::OperatingSystemName()); WriteAttribute("version", - env_util::GetOperatingSystemVersion()); + base::SysInfo::OperatingSystemVersion()); } { OPEN_ELEMENT_FOR_SCOPE("display"); int width = 0; int height = 0; - env_util::GetPrimaryDisplayDimensions(&width, &height); + base::SysInfo::GetPrimaryDisplayDimensions(&width, &height); WriteIntAttribute("xsize", width); WriteIntAttribute("ysize", height); - WriteIntAttribute("screens", env_util::GetDisplayCount()); + WriteIntAttribute("screens", base::SysInfo::DisplayCount()); } { diff --git a/chrome/browser/render_process_host.cc b/chrome/browser/render_process_host.cc index a1244ed..ed969a6 100644 --- a/chrome/browser/render_process_host.cc +++ b/chrome/browser/render_process_host.cc @@ -19,6 +19,7 @@ #include "base/logging.h" #include "base/path_service.h" #include "base/process_util.h" +#include "base/rand_util.h" #include "base/shared_event.h" #include "base/shared_memory.h" #include "base/string_util.h" @@ -43,7 +44,6 @@ #include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/debug_flags.h" -#include "chrome/common/env_util.h" #include "chrome/common/l10n_util.h" #include "chrome/common/logging_chrome.h" #include "chrome/common/pref_names.h" @@ -675,7 +675,7 @@ RenderProcessHost* RenderProcessHost::GetExistingProcessHost(Profile* profile) { // Now pick a random suitable renderer, if we have any if (!suitable_renderers.empty()) { int suitable_count = static_cast<int>(suitable_renderers.size()); - int random_index = rand_util::RandInt(0, suitable_count - 1); + int random_index = base::RandInt(0, suitable_count - 1); return suitable_renderers[random_index]; } diff --git a/chrome/browser/render_process_host.h b/chrome/browser/render_process_host.h index fd76ad7..0e6a2cc 100644 --- a/chrome/browser/render_process_host.h +++ b/chrome/browser/render_process_host.h @@ -5,6 +5,7 @@ #ifndef CHROME_BROWSER_RENDER_PROCESS_HOST_H_ #define CHROME_BROWSER_RENDER_PROCESS_HOST_H_ +#include <limits> #include <set> #include <vector> #include <windows.h> @@ -12,12 +13,12 @@ #include "base/id_map.h" #include "base/object_watcher.h" #include "base/process.h" +#include "base/rand_util.h" #include "base/ref_counted.h" #include "base/scoped_handle.h" #include "base/scoped_ptr.h" #include "chrome/common/ipc_sync_channel.h" #include "chrome/common/notification_service.h" -#include "chrome/common/rand_util.h" #include "chrome/common/render_messages.h" class PrefService; @@ -271,7 +272,7 @@ inline std::wstring GenerateRandomChannelID(void* instance) { // to fail. return StringPrintf(L"%d.%x.%d", GetCurrentProcessId(), instance, - rand_util::RandIntSecure(0, kint32max)); + base::RandInt(0, std::numeric_limits<int>::max())); } diff --git a/chrome/browser/safe_browsing/bloom_filter_unittest.cc b/chrome/browser/safe_browsing/bloom_filter_unittest.cc index 1612084..c0ac6cb 100644 --- a/chrome/browser/safe_browsing/bloom_filter_unittest.cc +++ b/chrome/browser/safe_browsing/bloom_filter_unittest.cc @@ -5,18 +5,20 @@ #include "chrome/browser/safe_browsing/bloom_filter.h" +#include <limits.h> + #include <set> #include "base/logging.h" +#include "base/rand_util.h" #include "base/string_util.h" #include "base/win_util.h" -#include "chrome/common/rand_util.h" #include "testing/gtest/include/gtest/gtest.h" namespace { uint32 GenHash() { - return static_cast<uint32>(rand_util::RandIntSecure(0, kint32max)); + return static_cast<uint32>(base::RandInt(INT_MIN, INT_MAX)); } } diff --git a/chrome/browser/safe_browsing/protocol_manager.cc b/chrome/browser/safe_browsing/protocol_manager.cc index 3e3c12b..e3a91ad 100644 --- a/chrome/browser/safe_browsing/protocol_manager.cc +++ b/chrome/browser/safe_browsing/protocol_manager.cc @@ -7,22 +7,22 @@ #include "base/histogram.h" #include "base/logging.h" #include "base/message_loop.h" +#include "base/rand_util.h" #include "base/string_util.h" +#include "base/sys_info.h" #include "base/task.h" #include "base/timer.h" #include "chrome/browser/profile.h" #include "chrome/browser/safe_browsing/protocol_parser.h" #include "chrome/browser/safe_browsing/safe_browsing_service.h" -#include "chrome/common/env_util.h" #include "chrome/common/env_vars.h" -#include "chrome/common/rand_util.h" #include "chrome/common/stl_util-inl.h" #include "net/base/base64.h" #include "net/base/load_flags.h" // Maximum time, in seconds, from start up before we must issue an update query. -static const int kSbTimerStartIntervalSec = 300; +static const int kSbTimerStartIntervalSec = 5 * 60; // Update URL for querying about the latest set of chunk updates. static const char* const kSbUpdateUrl = @@ -69,10 +69,10 @@ SafeBrowsingProtocolManager::SafeBrowsingProtocolManager( client_key_(client_key), wrapped_key_(wrapped_key) { // Set the backoff multiplier fuzz to a random value between 0 and 1. - back_off_fuzz_ = static_cast<float>(rand_util::RandInt(1, INT_MAX)) / INT_MAX; + back_off_fuzz_ = static_cast<float>(base::RandDouble()); // The first update must happen between 0-5 minutes of start up. - next_update_sec_ = rand_util::RandInt(60, kSbTimerStartIntervalSec); + next_update_sec_ = base::RandInt(60, kSbTimerStartIntervalSec); } SafeBrowsingProtocolManager::~SafeBrowsingProtocolManager() { @@ -276,7 +276,7 @@ bool SafeBrowsingProtocolManager::HandleServiceResponse(const GURL& url, if (next_update_sec > 0) { next_update_sec_ = next_update_sec; } else if (update_state_ == SECOND_REQUEST) { - next_update_sec_ = rand_util::RandInt(15, 45) * 60; + next_update_sec_ = base::RandInt(15 * 60, 45 * 60); } // We need to request a new set of keys for MAC. @@ -372,7 +372,7 @@ bool SafeBrowsingProtocolManager::HandleServiceResponse(const GURL& url, void SafeBrowsingProtocolManager::Initialize() { // Don't want to hit the safe browsing servers on build/chrome bots. - if (env_util::HasEnvironmentVariable(env_vars::kHeadless)) + if (base::SysInfo::HasEnvVar(env_vars::kHeadless)) return; ScheduleNextUpdate(false /* no back off */); diff --git a/chrome/browser/views/bug_report_view.cc b/chrome/browser/views/bug_report_view.cc index b1dfacb..77a0d31 100644 --- a/chrome/browser/views/bug_report_view.cc +++ b/chrome/browser/views/bug_report_view.cc @@ -330,7 +330,7 @@ void BugReportView::SetUrl(const GURL& url) { // win_util::GetWinVersion returns WinVersion, which is just // an enum of 2000, XP, 2003, or VISTA. Not enough detail for // bug reports -// env_util::GetOperatingSystemVersion returns an std::string +// base::SysInfo::OperatingSystemVersion returns an std::string // but doesn't include the build or service pack. That function // is probably the right one to extend, but will require changing // all the call sites or making it a wrapper around another util. diff --git a/chrome/common/SConscript b/chrome/common/SConscript index 7cc8c4d..24bf254 100644 --- a/chrome/common/SConscript +++ b/chrome/common/SConscript @@ -87,7 +87,6 @@ if env['PLATFORM'] == 'win32': 'clipboard_service.cc', 'common_glue.cc', 'drag_drop_types.cc', - 'env_util.cc', 'gfx/chrome_canvas.cc', 'gfx/chrome_font.cc', 'gfx/emf.cc', @@ -110,7 +109,6 @@ if env['PLATFORM'] == 'win32': 'plugin_messages.cc', 'pref_service.cc', 'process_watcher.cc', - 'rand_util.cc', 'render_messages.cc', 'resource_bundle.cc', 'resource_dispatcher.cc', diff --git a/chrome/common/common.vcproj b/chrome/common/common.vcproj index 203aa3f..771ff8c 100644 --- a/chrome/common/common.vcproj +++ b/chrome/common/common.vcproj @@ -422,14 +422,6 @@ > </File> <File - RelativePath=".\env_util.cc" - > - </File> - <File - RelativePath=".\env_util.h" - > - </File> - <File RelativePath=".\env_vars.cc" > </File> @@ -598,14 +590,6 @@ > </File> <File - RelativePath=".\rand_util.cc" - > - </File> - <File - RelativePath=".\rand_util.h" - > - </File> - <File RelativePath=".\ref_counted_util.h" > </File> diff --git a/chrome/common/env_util.cc b/chrome/common/env_util.cc deleted file mode 100644 index 3af22a6..0000000 --- a/chrome/common/env_util.cc +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (c) 2006-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 "chrome/common/env_util.h" - -#include "base/basictypes.h" -#include "base/logging.h" - -namespace env_util { - -std::string GetOperatingSystemName() { - return "Windows"; -} - -std::string GetOperatingSystemVersion() { - OSVERSIONINFO info = {0}; - info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - GetVersionEx(&info); - - char result[20]; - memset(result, 0, arraysize(result)); - _snprintf_s(result, arraysize(result), - "%lu.%lu", info.dwMajorVersion, info.dwMinorVersion); - return std::string(result); -} - -std::string GetCPUArchitecture() { - // TODO: Make this vary when we support any other architectures. - return "x86"; -} - -void GetPrimaryDisplayDimensions(int* width, int* height) { - if (width) - *width = GetSystemMetrics(SM_CXSCREEN); - - if (height) - *height = GetSystemMetrics(SM_CYSCREEN); -} - -int GetDisplayCount() { - return GetSystemMetrics(SM_CMONITORS); -} - -} // namespace env_util - diff --git a/chrome/common/env_util.h b/chrome/common/env_util.h deleted file mode 100644 index bb37935..0000000 --- a/chrome/common/env_util.h +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2006-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. - -// This file defines utility functions that can report details about the -// host operating environment. - -#ifndef CHROME_COMMON_ENV_UTIL_H__ -#define CHROME_COMMON_ENV_UTIL_H__ - -#include <windows.h> -#include <string> - -namespace env_util { - -// Test if the given environment variable is defined. -inline bool HasEnvironmentVariable(const wchar_t* var) { - return GetEnvironmentVariable(var, NULL, 0) != 0; -} - -// Returns the name of the host operating system. -std::string GetOperatingSystemName(); - -// Returns the version of the host operating system. -std::string GetOperatingSystemVersion(); - -// Returns the CPU architecture of the system. -std::string GetCPUArchitecture(); - -// Returns the pixel dimensions of the primary display via the -// width and height parameters. -void GetPrimaryDisplayDimensions(int* width, int* height); - -// Return the number of displays. -int GetDisplayCount(); - -} // namespace env_util - -#endif // CHROME_COMMON_ENV_UTIL_H__ - diff --git a/chrome/common/logging_chrome.cc b/chrome/common/logging_chrome.cc index 232e500..af6b647 100644 --- a/chrome/common/logging_chrome.cc +++ b/chrome/common/logging_chrome.cc @@ -14,9 +14,9 @@ #include "base/logging.h" #include "base/path_service.h" #include "base/string_util.h" +#include "base/sys_info.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_switches.h" -#include "chrome/common/env_util.h" #include "chrome/common/env_vars.h" // When true, this means that error dialogs should not be shown. @@ -96,7 +96,7 @@ void InitChromeLogging(const CommandLine& command_line, // headless mode to be configured either by the Environment // Variable or by the Command Line Switch. This is for // automated test purposes. - if (env_util::HasEnvironmentVariable(env_vars::kHeadless) || + if (base::SysInfo::HasEnvVar(env_vars::kHeadless) || command_line.HasSwitch(switches::kNoErrorDialogs)) SuppressDialogs(); diff --git a/chrome/common/process_watcher.cc b/chrome/common/process_watcher.cc index b8d78bd2..c189c2d 100644 --- a/chrome/common/process_watcher.cc +++ b/chrome/common/process_watcher.cc @@ -6,8 +6,8 @@ #include "base/message_loop.h" #include "base/object_watcher.h" +#include "base/sys_info.h" #include "chrome/app/result_codes.h" -#include "chrome/common/env_util.h" #include "chrome/common/env_vars.h" // Maximum amount of time (in milliseconds) to wait for the process to exit. @@ -48,9 +48,9 @@ class TimerExpiredTask : public Task, public base::ObjectWatcher::Delegate { private: void KillProcess() { - if (env_util::HasEnvironmentVariable(env_vars::kHeadless)) { - // If running the distributed tests, give the renderer a little time to figure out - // that the channel is shutdown and unwind. + if (base::SysInfo::HasEnvVar(env_vars::kHeadless)) { + // If running the distributed tests, give the renderer a little time + // to figure out that the channel is shutdown and unwind. if (WaitForSingleObject(process_, kWaitInterval) == WAIT_OBJECT_0) { OnObjectSignaled(process_); return; diff --git a/chrome/common/rand_util.cc b/chrome/common/rand_util.cc deleted file mode 100644 index 52492a0..0000000 --- a/chrome/common/rand_util.cc +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2006-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 "chrome/common/rand_util.h" - -#include <stdlib.h> - -#include "base/logging.h" - -namespace rand_util { - -int RandInt(int min, int max) { - return RandIntSecure(min, max); -} - -int RandIntSecure(int min, int max) { - unsigned int number; - // This code will not work on win2k, which we do not support. - errno_t rv = rand_s(&number); - DCHECK(rv == 0) << "rand_s failed with error " << rv; - - // From the rand man page, use this instead of just rand() % max, so that the - // higher bits are used. - return min + static_cast<int>(static_cast<double>(max - min + 1.0) * - (number / (UINT_MAX + 1.0))); -} - -} // namespace rand_util diff --git a/chrome/common/rand_util.h b/chrome/common/rand_util.h deleted file mode 100644 index 08e735f..0000000 --- a/chrome/common/rand_util.h +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2006-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 CHROME_COMMON_RAND_UTIL_H__ -#define CHROME_COMMON_RAND_UTIL_H__ - -#include "base/basictypes.h" - -namespace rand_util { - -// Returns a random number between min and max (inclusive). This is a -// non-cryptographic random number generator, using rand() from the CRT. -int RandInt(int min, int max); - -// Returns a random number between min and max (inclusive). This is a (slower) -// cryptographic random number generator using rand_s() from the CRT. Note -// that it does not work in Win2K, so it degrades to RandInt. -int RandIntSecure(int min, int max); - -} // namespace rand_util - -#endif // CHROME_COMMON_RAND_UTIL_H__ - diff --git a/chrome/renderer/renderer_main.cc b/chrome/renderer/renderer_main.cc index d54a4bc..2553a65 100644 --- a/chrome/renderer/renderer_main.cc +++ b/chrome/renderer/renderer_main.cc @@ -10,7 +10,6 @@ #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_counters.h" #include "chrome/common/chrome_switches.h" -#include "chrome/common/env_util.h" #include "chrome/common/l10n_util.h" #include "chrome/common/logging_chrome.h" #include "chrome/common/resource_bundle.h" diff --git a/chrome/test/automated_ui_tests/automated_ui_tests.cc b/chrome/test/automated_ui_tests/automated_ui_tests.cc index 694ad4e..7372030 100644 --- a/chrome/test/automated_ui_tests/automated_ui_tests.cc +++ b/chrome/test/automated_ui_tests/automated_ui_tests.cc @@ -7,13 +7,13 @@ #include "base/command_line.h" #include "base/file_util.h" #include "base/path_service.h" +#include "base/rand_util.h" #include "base/string_util.h" #include "chrome/app/chrome_dll_resource.h" #include "chrome/browser/character_encoding.h" #include "chrome/browser/view_ids.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/libxml_utils.h" -#include "chrome/common/rand_util.h" #include "chrome/common/win_util.h" #include "chrome/test/automated_ui_tests/automated_ui_tests.h" #include "chrome/test/automation/browser_proxy.h" @@ -421,9 +421,9 @@ bool AutomatedUITest::ChangeEncoding() { // The vector will contain mostly IDC values for encoding commands plus a few // menu separators (0 values). If we hit a separator we just retry. - int index = rand_util::RandInt(0, len); + int index = base::RandInt(0, len); while ((*encoding_ids)[index] == 0) { - index = rand_util::RandInt(0, len); + index = base::RandInt(0, len); } return RunCommand((*encoding_ids)[index]); @@ -690,7 +690,7 @@ bool AutomatedUITest::TestViewPasswords() { } bool AutomatedUITest::ExerciseDialog() { - int index = rand_util::RandInt(0, arraysize(kDialogs) - 1); + int index = base::RandInt(0, arraysize(kDialogs) - 1); return DoAction(kDialogs[index]) && FuzzyTestDialog(kTestDialogActionsToRun); } @@ -703,9 +703,9 @@ bool AutomatedUITest::FuzzyTestDialog(int num_actions) { // and Enter would close the dialog without performing more actions. We // rely on the fact that those two actions are first in the array and set // the lower bound to 2 if i == 0 to skip those two actions. - int action_index = rand_util::RandInt(i == 0 ? 2 : 0, - arraysize(kTestDialogPossibleActions) - - 1); + int action_index = base::RandInt(i == 0 ? 2 : 0, + arraysize(kTestDialogPossibleActions) + - 1); return_value = return_value && DoAction(kTestDialogPossibleActions[action_index]); if (DidCrash(false)) diff --git a/chrome/test/selenium/selenium_test.cc b/chrome/test/selenium/selenium_test.cc index 6bfc72f..a1d8a09 100644 --- a/chrome/test/selenium/selenium_test.cc +++ b/chrome/test/selenium/selenium_test.cc @@ -14,9 +14,9 @@ #include "base/file_util.h" #include "base/path_service.h" +#include "base/rand_util.h" #include "base/string_util.h" #include "chrome/common/chrome_paths.h" -#include "chrome/common/rand_util.h" #include "chrome/test/automation/tab_proxy.h" #include "chrome/test/automation/window_proxy.h" #include "chrome/test/ui/ui_test.h" @@ -88,7 +88,7 @@ class SeleniumTest : public UITest { L"5.selectFrame,6.click,13.verifyLocation,13.verifyLocation,13.click,24.selectAndWait,24.verifyTitle", L"5.selectFrame,6.click,24.selectAndWait" }; - *failed = kBogusFailures[rand_util::RandInt(0, 2)]; + *failed = kBogusFailures[base::RandInt(0, 2)]; #else std::wstring test_path; PathService::Get(chrome::DIR_TEST_DATA, &test_path); |