Skip to content
This repository has been archived by the owner on Sep 25, 2019. It is now read-only.

Commit

Permalink
Moving GUID generation from base to chrome/browser/guid*
Browse files Browse the repository at this point in the history
Moves GUID generation into chrome/browser/guid*.  GUID generation is used only within chrome/browser.  So am moving it there.

BUG=58813
TEST=GUIDTest.GUIDGeneratesAllZeroes, GUIDTest.GUIDGeneratesCorrectly, GUIDTest.GUIDCorrectlyFormatted, MetricsServiceTest.ClientIdCorrectlyFormatted

Review URL: http://codereview.chromium.org/3800003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@62639 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
dhollowa@chromium.org committed Oct 14, 2010
1 parent 3f92417 commit a269701
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 91 deletions.
5 changes: 0 additions & 5 deletions base/rand_util.h
Expand Up @@ -6,8 +6,6 @@
#define BASE_RAND_UTIL_H_
#pragma once

#include <string>

#include "base/basictypes.h"

namespace base {
Expand All @@ -28,9 +26,6 @@ uint64 RandGenerator(uint64 max);
// Returns a random double in range [0, 1). Thread-safe.
double RandDouble();

// Generate a 128-bit random GUID of the form: "%08X-%04X-%04X-%04X-%012llX".
std::string GenerateGUID();

} // namespace base

#endif // BASE_RAND_UTIL_H_
17 changes: 0 additions & 17 deletions base/rand_util_posix.cc
Expand Up @@ -12,7 +12,6 @@
#include "base/file_util.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/stringprintf.h"

namespace {

Expand Down Expand Up @@ -55,22 +54,6 @@ uint64 RandUint64() {
return number;
}

// TODO(cmasone): Once we're comfortable this works, migrate Windows code to
// use this as well.
std::string RandomBytesToGUIDString(const uint64 bytes[2]) {
return StringPrintf("%08X-%04X-%04X-%04X-%012llX",
static_cast<unsigned int>(bytes[0] >> 32),
static_cast<unsigned int>((bytes[0] >> 16) & 0x0000ffff),
static_cast<unsigned int>(bytes[0] & 0x0000ffff),
static_cast<unsigned int>(bytes[1] >> 48),
bytes[1] & 0x0000ffffffffffffULL);
}

std::string GenerateGUID() {
uint64 sixteen_bytes[2] = { base::RandUint64(), base::RandUint64() };
return RandomBytesToGUIDString(sixteen_bytes);
}

} // namespace base

int GetUrandomFD(void) {
Expand Down
47 changes: 0 additions & 47 deletions base/rand_util_unittest.cc
Expand Up @@ -35,50 +35,3 @@ TEST(RandUtilTest, RandGeneratorForRandomShuffle) {
EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(),
std::numeric_limits<int64>::max());
}

#if defined(OS_POSIX)
// For unit testing purposes only. Do not use outside of tests.
namespace base {
extern std::string RandomBytesToGUIDString(const uint64 bytes[2]);
} // base

TEST(RandUtilTest, GUIDGeneratesAllZeroes) {
uint64 bytes[] = { 0, 0 };
std::string clientid = base::RandomBytesToGUIDString(bytes);
EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid);
}

TEST(RandUtilTest, GUIDGeneratesCorrectly) {
uint64 bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL };
std::string clientid = base::RandomBytesToGUIDString(bytes);
EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid);
}
#endif

TEST(RandUtilTest, GUIDCorrectlyFormatted) {
const int kIterations = 10;
for (int it = 0; it < kIterations; ++it) {
std::string guid = base::GenerateGUID();
EXPECT_EQ(36U, guid.length());
std::string hexchars = "0123456789ABCDEF";
for (uint32 i = 0; i < guid.length(); ++i) {
char current = guid.at(i);
if (i == 8 || i == 13 || i == 18 || i == 23) {
EXPECT_EQ('-', current);
} else {
EXPECT_TRUE(std::string::npos != hexchars.find(current));
}
}
}
}

TEST(RandUtilTest, GUIDBasicUniqueness) {
const int kIterations = 10;
for (int it = 0; it < kIterations; ++it) {
std::string guid1 = base::GenerateGUID();
std::string guid2 = base::GenerateGUID();
EXPECT_EQ(36U, guid1.length());
EXPECT_EQ(36U, guid2.length());
EXPECT_NE(guid1, guid2);
}
}
20 changes: 0 additions & 20 deletions base/rand_util_win.cc
Expand Up @@ -6,13 +6,8 @@

#include <stdlib.h>

#include <objbase.h>
#include <windows.h>

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"

namespace {

Expand All @@ -32,19 +27,4 @@ uint64 RandUint64() {
return (static_cast<uint64>(first_half) << 32) + second_half;
}

std::string GenerateGUID() {
const int kGUIDSize = 39;

GUID guid;
HRESULT guid_result = CoCreateGuid(&guid);
DCHECK(SUCCEEDED(guid_result));

std::wstring guid_string;
int result = StringFromGUID2(guid,
WriteInto(&guid_string, kGUIDSize), kGUIDSize);
DCHECK(result == kGUIDSize);

return WideToUTF8(guid_string.substr(1, guid_string.length() - 2));
}

} // namespace base
29 changes: 29 additions & 0 deletions chrome/browser/guid.h
@@ -0,0 +1,29 @@
// Copyright (c) 2010 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_BROWSER_GUID_H_
#define CHROME_BROWSER_GUID_H_
#pragma once

#include <string>

#include "base/basictypes.h"
#include "build/build_config.h"

namespace guid {

// Generate a 128-bit random GUID of the form: "%08X-%04X-%04X-%04X-%012llX".
// If GUID generation fails an empty string is returned.
// The POSIX implementation uses psuedo random number generation to create
// the GUID. The Windows implementation uses system services.
std::string GenerateGUID();

#if defined(OS_POSIX)
// For unit testing purposes only. Do not use outside of tests.
std::string RandomDataToGUIDString(const uint64 bytes[2]);
#endif

} // namespace guid

#endif // CHROME_BROWSER_GUID_H_
28 changes: 28 additions & 0 deletions chrome/browser/guid_posix.cc
@@ -0,0 +1,28 @@
// 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 "chrome/browser/guid.h"

#include "base/rand_util.h"
#include "base/stringprintf.h"

namespace guid {

std::string GenerateGUID() {
uint64 sixteen_bytes[2] = { base::RandUint64(), base::RandUint64() };
return RandomDataToGUIDString(sixteen_bytes);
}

// TODO(cmasone): Once we're comfortable this works, migrate Windows code to
// use this as well.
std::string RandomDataToGUIDString(const uint64 bytes[2]) {
return StringPrintf("%08X-%04X-%04X-%04X-%012llX",
static_cast<unsigned int>(bytes[0] >> 32),
static_cast<unsigned int>((bytes[0] >> 16) & 0x0000ffff),
static_cast<unsigned int>(bytes[0] & 0x0000ffff),
static_cast<unsigned int>(bytes[1] >> 48),
bytes[1] & 0x0000ffffffffffffULL);
}

} // namespace guid
51 changes: 51 additions & 0 deletions chrome/browser/guid_unittest.cc
@@ -0,0 +1,51 @@
// 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 "chrome/browser/guid.h"

#include <limits>

#include "testing/gtest/include/gtest/gtest.h"

#if defined(OS_POSIX)
TEST(GUIDTest, GUIDGeneratesAllZeroes) {
uint64 bytes[] = { 0, 0 };
std::string clientid = guid::RandomDataToGUIDString(bytes);
EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid);
}

TEST(GUIDTest, GUIDGeneratesCorrectly) {
uint64 bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL };
std::string clientid = guid::RandomDataToGUIDString(bytes);
EXPECT_EQ("01234567-89AB-CDEF-FEDC-BA9876543210", clientid);
}
#endif

TEST(GUIDTest, GUIDCorrectlyFormatted) {
const int kIterations = 10;
for (int it = 0; it < kIterations; ++it) {
std::string guid = guid::GenerateGUID();
EXPECT_EQ(36U, guid.length());
std::string hexchars = "0123456789ABCDEF";
for (uint32 i = 0; i < guid.length(); ++i) {
char current = guid.at(i);
if (i == 8 || i == 13 || i == 18 || i == 23) {
EXPECT_EQ('-', current);
} else {
EXPECT_TRUE(std::string::npos != hexchars.find(current));
}
}
}
}

TEST(GUIDTest, GUIDBasicUniqueness) {
const int kIterations = 10;
for (int it = 0; it < kIterations; ++it) {
std::string guid1 = guid::GenerateGUID();
std::string guid2 = guid::GenerateGUID();
EXPECT_EQ(36U, guid1.length());
EXPECT_EQ(36U, guid2.length());
EXPECT_NE(guid1, guid2);
}
}
38 changes: 38 additions & 0 deletions chrome/browser/guid_win.cc
@@ -0,0 +1,38 @@
// 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 "chrome/browser/guid.h"

#include <stdlib.h>

#include <objbase.h>
#include <windows.h>

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"

namespace guid {

std::string GenerateGUID() {
const int kGUIDSize = 39;

GUID guid;
HRESULT guid_result = CoCreateGuid(&guid);
DCHECK(SUCCEEDED(guid_result));
if (!SUCCEEDED(guid_result))
return std::string();

std::wstring guid_string;
int result = StringFromGUID2(guid,
WriteInto(&guid_string, kGUIDSize), kGUIDSize);
DCHECK(result == kGUIDSize);
if (result != kGUIDSize)
return std::string();

return WideToUTF8(guid_string.substr(1, guid_string.length() - 2));
}

} // namespace guid
4 changes: 2 additions & 2 deletions chrome/browser/metrics/metrics_service.cc
Expand Up @@ -162,14 +162,14 @@
#include "base/command_line.h"
#include "base/md5.h"
#include "base/metrics/histogram.h"
#include "base/rand_util.h"
#include "base/string_number_conversions.h"
#include "base/thread.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/guid.h"
#include "chrome/browser/load_notification_details.h"
#include "chrome/browser/memory_details.h"
#include "chrome/browser/metrics/histogram_synchronizer.h"
Expand Down Expand Up @@ -808,7 +808,7 @@ void MetricsService::OnInitTaskComplete(
}

std::string MetricsService::GenerateClientID() {
return base::GenerateGUID();
return guid::GenerateGUID();
}

//------------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions chrome/chrome_browser.gypi
Expand Up @@ -1947,6 +1947,9 @@
'browser/gtk/update_recommended_dialog.h',
'browser/gtk/view_id_util.cc',
'browser/gtk/view_id_util.h',
'browser/guid.h',
'browser/guid_posix.cc',
'browser/guid_win.cc',
'browser/hang_monitor/hung_plugin_action.cc',
'browser/hang_monitor/hung_plugin_action.h',
'browser/hang_monitor/hung_window_detector.cc',
Expand Down
1 change: 1 addition & 0 deletions chrome/chrome_tests.gypi
Expand Up @@ -1298,6 +1298,7 @@
'browser/gtk/reload_button_gtk_unittest.cc',
'browser/gtk/status_icons/status_tray_gtk_unittest.cc',
'browser/gtk/tabs/tab_renderer_gtk_unittest.cc',
'browser/guid_unittest.cc',
'browser/history/expire_history_backend_unittest.cc',
'browser/history/history_backend_unittest.cc',
'browser/history/history_querying_unittest.cc',
Expand Down

0 comments on commit a269701

Please sign in to comment.