summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authormarja@chromium.org <marja@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-14 14:22:07 +0000
committermarja@chromium.org <marja@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-14 14:22:07 +0000
commit7e49ad360632304e1890082e1605b2239956afdb (patch)
tree1e3451b1a1af73cc5996c218a2888e35f1337709 /base
parentfacd6e7bd32c811fa8be9a065505f465396cd92e (diff)
downloadchromium_src-7e49ad360632304e1890082e1605b2239956afdb.zip
chromium_src-7e49ad360632304e1890082e1605b2239956afdb.tar.gz
chromium_src-7e49ad360632304e1890082e1605b2239956afdb.tar.bz2
Move guid generation from chrome/common/ to base/.
It will be needed in webkit/dom_storage/. BUG=NONE TEST=Existing tests. Review URL: https://chromiumcodereview.appspot.com/10540003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@142142 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/base.gyp1
-rw-r--r--base/base.gypi4
-rw-r--r--base/guid.cc32
-rw-r--r--base/guid.h33
-rw-r--r--base/guid_posix.cc28
-rw-r--r--base/guid_unittest.cc42
-rw-r--r--base/guid_win.cc38
7 files changed, 178 insertions, 0 deletions
diff --git a/base/base.gyp b/base/base.gyp
index 08630a8..2a55f17 100644
--- a/base/base.gyp
+++ b/base/base.gyp
@@ -382,6 +382,7 @@
'file_util_unittest.cc',
'file_version_info_unittest.cc',
'gmock_unittest.cc',
+ 'guid_unittest.cc',
'hi_res_timer_manager_unittest.cc',
'id_map_unittest.cc',
'i18n/break_iterator_unittest.cc',
diff --git a/base/base.gypi b/base/base.gypi
index c1962e1..2f01f53 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -139,6 +139,10 @@
'global_descriptors_posix.cc',
'global_descriptors_posix.h',
'gtest_prod_util.h',
+ 'guid.cc',
+ 'guid.h',
+ 'guid_posix.cc',
+ 'guid_win.cc',
'hash_tables.h',
'hi_res_timer_manager_posix.cc',
'hi_res_timer_manager_win.cc',
diff --git a/base/guid.cc b/base/guid.cc
new file mode 100644
index 0000000..920dae5
--- /dev/null
+++ b/base/guid.cc
@@ -0,0 +1,32 @@
+// Copyright (c) 2012 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/guid.h"
+
+#include "base/rand_util.h"
+#include "base/stringprintf.h"
+
+namespace base {
+
+bool IsValidGUID(const std::string& guid) {
+ const size_t kGUIDLength = 36U;
+ if (guid.length() != kGUIDLength)
+ return false;
+
+ std::string hexchars = "0123456789ABCDEF";
+ for (uint32 i = 0; i < guid.length(); ++i) {
+ char current = guid[i];
+ if (i == 8 || i == 13 || i == 18 || i == 23) {
+ if (current != '-')
+ return false;
+ } else {
+ if (hexchars.find(current) == std::string::npos)
+ return false;
+ }
+ }
+
+ return true;
+}
+
+} // namespace guid
diff --git a/base/guid.h b/base/guid.h
new file mode 100644
index 0000000..20f7706
--- /dev/null
+++ b/base/guid.h
@@ -0,0 +1,33 @@
+// Copyright (c) 2012 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_GUID_H_
+#define BASE_GUID_H_
+#pragma once
+
+#include <string>
+
+#include "base/base_export.h"
+#include "base/basictypes.h"
+#include "build/build_config.h"
+
+namespace base {
+
+// 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.
+BASE_EXPORT std::string GenerateGUID();
+
+// Returns true if the input string conforms to the GUID format.
+BASE_EXPORT bool IsValidGUID(const std::string& guid);
+
+#if defined(OS_POSIX)
+// For unit testing purposes only. Do not use outside of tests.
+BASE_EXPORT std::string RandomDataToGUIDString(const uint64 bytes[2]);
+#endif
+
+} // namespace guid
+
+#endif // BASE_GUID_H_
diff --git a/base/guid_posix.cc b/base/guid_posix.cc
new file mode 100644
index 0000000..89811d0
--- /dev/null
+++ b/base/guid_posix.cc
@@ -0,0 +1,28 @@
+// Copyright (c) 2012 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/guid.h"
+
+#include "base/rand_util.h"
+#include "base/stringprintf.h"
+
+namespace base {
+
+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
diff --git a/base/guid_unittest.cc b/base/guid_unittest.cc
new file mode 100644
index 0000000..18c04a9
--- /dev/null
+++ b/base/guid_unittest.cc
@@ -0,0 +1,42 @@
+// Copyright (c) 2012 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/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 = base::RandomDataToGUIDString(bytes);
+ EXPECT_EQ("00000000-0000-0000-0000-000000000000", clientid);
+}
+
+TEST(GUIDTest, GUIDGeneratesCorrectly) {
+ uint64 bytes[] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL };
+ std::string clientid = base::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 = base::GenerateGUID();
+ EXPECT_TRUE(base::IsValidGUID(guid));
+ }
+}
+
+TEST(GUIDTest, 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);
+ }
+}
diff --git a/base/guid_win.cc b/base/guid_win.cc
new file mode 100644
index 0000000..1cddff8
--- /dev/null
+++ b/base/guid_win.cc
@@ -0,0 +1,38 @@
+// Copyright (c) 2012 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/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 base {
+
+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