diff options
author | nick@chromium.org <nick@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-10 06:05:27 +0000 |
---|---|---|
committer | nick@chromium.org <nick@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-10 06:05:27 +0000 |
commit | 5852edc1b6eab234b9e048c41dd0d664ae7fc747 (patch) | |
tree | 9e5d8eb4833b76cdb11e66fc3607689e0f5e0122 /chrome/browser/sync/engine/net/openssl_init.cc | |
parent | f6059e37f8b8ac335ce18a189a13e702974a1c7e (diff) | |
download | chromium_src-5852edc1b6eab234b9e048c41dd0d664ae7fc747.zip chromium_src-5852edc1b6eab234b9e048c41dd0d664ae7fc747.tar.gz chromium_src-5852edc1b6eab234b9e048c41dd0d664ae7fc747.tar.bz2 |
Initial commit of sync engine code to browser/sync.
The code is not built on any platform yet. That will arrive
as a subsequent checkin.
This is an implementation of the interface exposed earlier
through syncapi.h. It is the client side of a sync
protocol that lets users sync their browser data
(currently, just bookmarks) with their Google Account.
Table of contents:
browser/sync/
protocol - The protocol definition, and
other definitions necessary to connect to
the service.
syncable/ - defines a data model for syncable objects,
and provides a sqlite-based backing store
for this model.
engine/ - includes the core sync logic, including commiting
changes to the server, downloading changes from
the server, resolving conflicts, other parts of
the sync algorithm.
engine/net - parts of the sync engine focused on the
business of talking to the server. Some of
this is binds a generic "server connection"
interface to a concrete implementation
provided by Chromium.
notifier - the part of the syncer focused on the business
of sending and receiving xmpp notifications.
Notifications are used instead of polling to
achieve very low latency change propagation.
util - not necessarily sync specific utility code. Much
of this is scaffolding which should either be
replaced by, or merged with, the utility code
in base/.
BUG=none
TEST=this code includes its own suite of unit tests.
Review URL: http://codereview.chromium.org/194065
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25850 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/sync/engine/net/openssl_init.cc')
-rw-r--r-- | chrome/browser/sync/engine/net/openssl_init.cc | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/chrome/browser/sync/engine/net/openssl_init.cc b/chrome/browser/sync/engine/net/openssl_init.cc new file mode 100644 index 0000000..afaf006 --- /dev/null +++ b/chrome/browser/sync/engine/net/openssl_init.cc @@ -0,0 +1,129 @@ +// Copyright (c) 2009 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. +// +// OpenSSL multi-threading initialization + +#include "chrome/browser/sync/engine/net/openssl_init.h" + +#include <openssl/crypto.h> + +#include "base/logging.h" +#include "chrome/browser/sync/util/compat-pthread.h" +#include "chrome/browser/sync/util/pthread_helpers.h" + +// OpenSSL requires multithreading callbacks to be initialized prior to using +// the library so that it can manage thread locking as necessary. + +// Dynamic lock type +// +// This needs to be a struct and in global scope because OpenSSL relies on some +// macro magic. +struct CRYPTO_dynlock_value { + PThreadMutex mutex; + void Lock() { + mutex.Lock(); + } + void Unlock() { + mutex.Unlock(); + } +}; + +namespace { + +// This array stores all of the mutexes available to OpenSSL +PThreadMutex* mutex_buf = NULL; + +// OpenSSL mutex handling callback functions + +// OpenSSL Callback - Locks/unlocks the specified mutex held by OpenSSL. +void OpenSslMutexLockControl(int mode, int n, const char* file, int line) { + if (mode & CRYPTO_LOCK) { + mutex_buf[n].Lock(); + } else { + mutex_buf[n].Unlock(); + } +} + +// OpenSSL Callback - Returns the thread ID +unsigned long OpenSslGetThreadID(void) { + return GetCurrentThreadId(); +} + +// Dynamic locking functions + +// Allocate a new lock +struct CRYPTO_dynlock_value* dyn_create_function(const char* file, int line) { + return new CRYPTO_dynlock_value; +} + +void dyn_lock_function(int mode, struct CRYPTO_dynlock_value* lock, + const char* file, int line) { + if (mode & CRYPTO_LOCK) { + lock->Lock(); + } else { + lock->Unlock(); + } +} + +void dyn_destroy_function(struct CRYPTO_dynlock_value* lock, + const char* file, int line) { + delete lock; +} + +} // namespace + +// We want to log the version of the OpenSSL library being used, in particular +// for the case where it's dynamically linked. We want the version from the +// library, not from the header files. It seems the OpenSSL folks haven't +// bothered with an accessor for this, so we just pluck it out. +#ifdef OS_WINDOWS +// TODO(sync): Figure out how to get the SSL version string on Windows. +const char* SSL_version_str = "UNKNOWN"; +#else +extern const char* SSL_version_str; +#endif + +namespace browser_sync { + +// Initializes the OpenSSL multithreading callbacks. This isn't thread-safe, +// but it is called early enough that it doesn't matter. +void InitOpenSslMultithreading() { + LOG(INFO) << "Using OpenSSL headers version " << OPENSSL_VERSION_TEXT + << ", lib version " << SSL_version_str; + + if (mutex_buf) + return; + + mutex_buf = new PThreadMutex[CRYPTO_num_locks()]; + CHECK(NULL != mutex_buf); + + // OpenSSL has only one single global set of callbacks, so this + // initialization must be done only once, even though the OpenSSL lib may be + // used by multiple modules (jingle jabber connections and P2P tunnels). + CRYPTO_set_id_callback(OpenSslGetThreadID); + CRYPTO_set_locking_callback(OpenSslMutexLockControl); + + CRYPTO_set_dynlock_create_callback(dyn_create_function); + CRYPTO_set_dynlock_lock_callback(dyn_lock_function); + CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function); +} + +// Cleans up the OpenSSL multithreading callbacks. +void CleanupOpenSslMultithreading() { + if (!mutex_buf) { + return; + } + + CRYPTO_set_dynlock_create_callback(NULL); + CRYPTO_set_dynlock_lock_callback(NULL); + CRYPTO_set_dynlock_destroy_callback(NULL); + + CRYPTO_set_id_callback(NULL); + CRYPTO_set_locking_callback(NULL); + + delete [] mutex_buf; + mutex_buf = NULL; +} + +} // namespace browser_sync |