Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: net/base/openssl_util.h

Issue 4963002: Refactor EnsureOpenSSLInit and openssl_util into base (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review comments Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/cert_test_util.cc ('k') | net/base/openssl_util.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <openssl/ssl.h>
6
7 #include "base/lock.h"
8 #include "base/scoped_vector.h"
9 #include "base/singleton.h"
10
11 namespace net {
12
13 // A helper class that takes care of destroying OpenSSL objects when it goes out
14 // of scope.
15 template <typename T, void (*destructor)(T*)>
16 class ScopedSSL {
17 public:
18 explicit ScopedSSL(T* ptr_) : ptr_(ptr_) { }
19 ~ScopedSSL() { if (ptr_) (*destructor)(ptr_); }
20
21 T* get() const { return ptr_; }
22
23 private:
24 T* ptr_;
25 };
26
27 // Singleton for initializing / cleaning up OpenSSL and holding a X509 store.
28 // Access it via GetOpenSSLInitSingleton().
29 class OpenSSLInitSingleton {
30 public:
31 SSL_CTX* ssl_ctx() const { return ssl_ctx_.get(); }
32 X509_STORE* x509_store() const { return store_.get(); }
33
34 private:
35 friend struct DefaultSingletonTraits<OpenSSLInitSingleton>;
36 OpenSSLInitSingleton();
37 ~OpenSSLInitSingleton();
38
39 static void LockingCallback(int mode, int n, const char* file, int line);
40 void OnLockingCallback(int mode, int n, const char* file, int line);
41
42 ScopedSSL<SSL_CTX, SSL_CTX_free> ssl_ctx_;
43 ScopedSSL<X509_STORE, X509_STORE_free> store_;
44 // These locks are used and managed by OpenSSL via LockingCallback().
45 ScopedVector<Lock> locks_;
46
47 DISALLOW_COPY_AND_ASSIGN(OpenSSLInitSingleton);
48 };
49
50 OpenSSLInitSingleton* GetOpenSSLInitSingleton();
51
52 // Initialize OpenSSL if it isn't already initialized. This must be called
53 // before any other OpenSSL functions (except GetOpenSSLInitSingleton above).
54 // This function is thread-safe, and OpenSSL will only ever be initialized once.
55 // OpenSSL will be properly shut down on program exit.
56 void EnsureOpenSSLInit();
57
58 } // namespace net
59
OLDNEW
« no previous file with comments | « net/base/cert_test_util.cc ('k') | net/base/openssl_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698