2005-11-21 21:01:45 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 50; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-11-21 21:01:45 +00:00
|
|
|
|
2006-04-06 21:48:47 +00:00
|
|
|
#if defined(XP_WIN)
|
2005-11-21 21:01:45 +00:00
|
|
|
#include <windows.h>
|
2006-02-19 07:43:19 +00:00
|
|
|
#include <objbase.h>
|
2006-04-06 21:48:47 +00:00
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
2005-11-21 21:01:45 +00:00
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
2006-04-06 21:48:47 +00:00
|
|
|
#include "prrng.h"
|
2005-11-21 21:01:45 +00:00
|
|
|
#endif
|
|
|
|
|
2006-04-06 21:48:47 +00:00
|
|
|
#include "nsMemory.h"
|
2005-11-21 21:01:45 +00:00
|
|
|
|
2011-03-31 23:57:38 +00:00
|
|
|
#include "nsUUIDGenerator.h"
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 19:51:19 +00:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2006-05-14 22:41:47 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsUUIDGenerator, nsIUUIDGenerator)
|
2005-11-21 21:01:45 +00:00
|
|
|
|
|
|
|
nsUUIDGenerator::nsUUIDGenerator()
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
: mLock("nsUUIDGenerator.mLock")
|
2005-11-21 21:01:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-05-14 22:41:47 +00:00
|
|
|
nsUUIDGenerator::~nsUUIDGenerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-11-21 21:01:45 +00:00
|
|
|
nsresult
|
|
|
|
nsUUIDGenerator::Init()
|
|
|
|
{
|
2006-05-14 22:41:47 +00:00
|
|
|
// We're a service, so we're guaranteed that Init() is not going
|
|
|
|
// to be reentered while we're inside Init().
|
|
|
|
|
2010-06-01 19:02:42 +00:00
|
|
|
#if !defined(XP_WIN) && !defined(XP_MACOSX) && !defined(ANDROID)
|
2005-11-21 21:01:45 +00:00
|
|
|
/* initialize random number generator using NSPR random noise */
|
|
|
|
unsigned int seed;
|
|
|
|
|
2012-08-09 07:10:11 +00:00
|
|
|
size_t bytes = 0;
|
2005-11-21 21:01:45 +00:00
|
|
|
while (bytes < sizeof(seed)) {
|
2012-08-09 07:10:11 +00:00
|
|
|
size_t nbytes = PR_GetRandomNoise(((unsigned char *)&seed)+bytes,
|
2005-11-21 21:01:45 +00:00
|
|
|
sizeof(seed)-bytes);
|
|
|
|
if (nbytes == 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
bytes += nbytes;
|
|
|
|
}
|
|
|
|
|
2006-08-14 18:53:34 +00:00
|
|
|
/* Initialize a new RNG state, and immediately switch
|
|
|
|
* back to the previous one -- we want to use mState
|
|
|
|
* only for our own calls to random().
|
|
|
|
*/
|
2006-08-15 00:52:21 +00:00
|
|
|
mSavedState = initstate(seed, mState, sizeof(mState));
|
|
|
|
setstate(mSavedState);
|
2005-11-21 21:01:45 +00:00
|
|
|
|
|
|
|
mRBytes = 4;
|
|
|
|
#ifdef RAND_MAX
|
|
|
|
if ((unsigned long) RAND_MAX < (unsigned long)0xffffffff)
|
|
|
|
mRBytes = 3;
|
|
|
|
if ((unsigned long) RAND_MAX < (unsigned long)0x00ffffff)
|
|
|
|
mRBytes = 2;
|
|
|
|
if ((unsigned long) RAND_MAX < (unsigned long)0x0000ffff)
|
|
|
|
mRBytes = 1;
|
|
|
|
if ((unsigned long) RAND_MAX < (unsigned long)0x000000ff)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
|
|
|
|
2006-04-06 21:48:47 +00:00
|
|
|
#endif /* non XP_WIN and non XP_MACOSX */
|
2005-11-21 21:01:45 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUUIDGenerator::GenerateUUID(nsID** ret)
|
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
nsID *id = static_cast<nsID*>(NS_Alloc(sizeof(nsID)));
|
2012-07-30 14:20:58 +00:00
|
|
|
if (id == nullptr)
|
2005-11-21 21:01:45 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv = GenerateUUIDInPlace(id);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_Free(id);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret = id;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUUIDGenerator::GenerateUUIDInPlace(nsID* id)
|
|
|
|
{
|
2006-05-14 22:41:47 +00:00
|
|
|
// The various code in this method is probably not threadsafe, so lock
|
|
|
|
// across the whole method.
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2009-02-10 23:10:07 +00:00
|
|
|
|
2011-05-01 18:59:24 +00:00
|
|
|
#if defined(XP_WIN)
|
2005-11-21 21:01:45 +00:00
|
|
|
HRESULT hr = CoCreateGuid((GUID*)id);
|
2012-08-16 11:58:11 +00:00
|
|
|
if (FAILED(hr))
|
2005-11-21 21:01:45 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2006-04-06 21:48:47 +00:00
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
|
|
|
|
if (!uuid)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuid);
|
|
|
|
memcpy(id, &bytes, sizeof(nsID));
|
|
|
|
|
|
|
|
CFRelease(uuid);
|
|
|
|
#else /* not windows or OS X; generate randomness using random(). */
|
2006-08-15 00:52:21 +00:00
|
|
|
/* XXX we should be saving the return of setstate here and switching
|
|
|
|
* back to it; instead, we use the value returned when we called
|
|
|
|
* initstate, since older glibc's have broken setstate() return values
|
|
|
|
*/
|
2010-06-01 19:02:42 +00:00
|
|
|
#ifndef ANDROID
|
2006-08-15 00:52:21 +00:00
|
|
|
setstate(mState);
|
2010-06-01 19:02:42 +00:00
|
|
|
#endif
|
2006-08-14 18:53:34 +00:00
|
|
|
|
2012-08-09 07:10:11 +00:00
|
|
|
size_t bytesLeft = sizeof(nsID);
|
2005-11-21 21:01:45 +00:00
|
|
|
while (bytesLeft > 0) {
|
2010-06-01 19:02:42 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
long rval = arc4random();
|
|
|
|
const int mRBytes = 4;
|
|
|
|
#else
|
2005-11-21 21:01:45 +00:00
|
|
|
long rval = random();
|
2010-06-01 19:02:42 +00:00
|
|
|
#endif
|
|
|
|
|
2005-11-21 21:01:45 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint8_t *src = (uint8_t*)&rval;
|
2006-06-15 04:40:21 +00:00
|
|
|
// We want to grab the mRBytes least significant bytes of rval, since
|
|
|
|
// mRBytes less than sizeof(rval) means the high bytes are 0.
|
|
|
|
#ifdef IS_BIG_ENDIAN
|
2005-11-21 21:01:45 +00:00
|
|
|
src += sizeof(rval) - mRBytes;
|
|
|
|
#endif
|
2012-08-22 15:56:38 +00:00
|
|
|
uint8_t *dst = ((uint8_t*) id) + (sizeof(nsID) - bytesLeft);
|
2012-08-09 07:10:11 +00:00
|
|
|
size_t toWrite = (bytesLeft < mRBytes ? bytesLeft : mRBytes);
|
|
|
|
for (size_t i = 0; i < toWrite; i++)
|
2005-11-21 21:01:45 +00:00
|
|
|
dst[i] = src[i];
|
|
|
|
|
|
|
|
bytesLeft -= toWrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put in the version */
|
|
|
|
id->m2 &= 0x0fff;
|
|
|
|
id->m2 |= 0x4000;
|
|
|
|
|
|
|
|
/* Put in the variant */
|
|
|
|
id->m3[0] &= 0x3f;
|
|
|
|
id->m3[0] |= 0x80;
|
2006-08-14 18:53:34 +00:00
|
|
|
|
2010-06-01 19:02:42 +00:00
|
|
|
#ifndef ANDROID
|
2006-08-14 18:53:34 +00:00
|
|
|
/* Restore the previous RNG state */
|
2006-08-15 00:52:21 +00:00
|
|
|
setstate(mSavedState);
|
2010-06-01 19:02:42 +00:00
|
|
|
#endif
|
2005-11-21 21:01:45 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|