This repository has been archived by the owner on Sep 25, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving GUID generation from base to chrome/browser/guid*
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
Showing
11 changed files
with
152 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters