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

Side by Side Diff: base/crypto/cssm_init.cc

Issue 5527004: Access singletons with a new GetInstance() method instead of Singleton<T>. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 10 years 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
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/crypto/cssm_init.h" 5 #include "base/crypto/cssm_init.h"
6 6
7 #include <Security/SecBase.h> 7 #include <Security/SecBase.h>
8 8
9 #include "base/lock.h" 9 #include "base/lock.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/singleton.h" 11 #include "base/singleton.h"
12 #include "base/sys_string_conversions.h" 12 #include "base/sys_string_conversions.h"
13 13
14 // When writing crypto code for Mac OS X, you may find the following 14 // When writing crypto code for Mac OS X, you may find the following
15 // documentation useful: 15 // documentation useful:
16 // - Common Security: CDSA and CSSM, Version 2 (with corrigenda) 16 // - Common Security: CDSA and CSSM, Version 2 (with corrigenda)
17 // http://www.opengroup.org/security/cdsa.htm 17 // http://www.opengroup.org/security/cdsa.htm
18 // - Apple Cryptographic Service Provider Functional Specification 18 // - Apple Cryptographic Service Provider Functional Specification
19 // - CryptoSample: http://developer.apple.com/SampleCode/CryptoSample/ 19 // - CryptoSample: http://developer.apple.com/SampleCode/CryptoSample/
20 20
21 namespace { 21 namespace {
22 22
23 class CSSMInitSingleton { 23 class CSSMInitSingleton {
24 public: 24 public:
25 static CSSMInitSingleton* GetInstance() {
26 return Singleton<CSSMInitSingleton>::get();
27 }
28
29 CSSM_CSP_HANDLE csp_handle() const {return csp_handle_;}
30
31 private:
25 CSSMInitSingleton() : inited_(false), loaded_(false), csp_handle_(NULL) { 32 CSSMInitSingleton() : inited_(false), loaded_(false), csp_handle_(NULL) {
26 static CSSM_VERSION version = {2, 0}; 33 static CSSM_VERSION version = {2, 0};
27 // TODO(wtc): what should our caller GUID be? 34 // TODO(wtc): what should our caller GUID be?
28 static const CSSM_GUID test_guid = { 35 static const CSSM_GUID test_guid = {
29 0xFADE, 0, 0, { 1, 2, 3, 4, 5, 6, 7, 0 } 36 0xFADE, 0, 0, { 1, 2, 3, 4, 5, 6, 7, 0 }
30 }; 37 };
31 CSSM_RETURN crtn; 38 CSSM_RETURN crtn;
32 CSSM_PVC_MODE pvc_policy = CSSM_PVC_NONE; 39 CSSM_PVC_MODE pvc_policy = CSSM_PVC_NONE;
33 crtn = CSSM_Init(&version, CSSM_PRIVILEGE_SCOPE_NONE, &test_guid, 40 crtn = CSSM_Init(&version, CSSM_PRIVILEGE_SCOPE_NONE, &test_guid,
34 CSSM_KEY_HIERARCHY_NONE, &pvc_policy, NULL); 41 CSSM_KEY_HIERARCHY_NONE, &pvc_policy, NULL);
(...skipping 26 matching lines...) Expand all
61 if (loaded_) { 68 if (loaded_) {
62 crtn = CSSM_ModuleUnload(&gGuidAppleCSP, NULL, NULL); 69 crtn = CSSM_ModuleUnload(&gGuidAppleCSP, NULL, NULL);
63 DCHECK(crtn == CSSM_OK); 70 DCHECK(crtn == CSSM_OK);
64 } 71 }
65 if (inited_) { 72 if (inited_) {
66 crtn = CSSM_Terminate(); 73 crtn = CSSM_Terminate();
67 DCHECK(crtn == CSSM_OK); 74 DCHECK(crtn == CSSM_OK);
68 } 75 }
69 } 76 }
70 77
71 CSSM_CSP_HANDLE csp_handle() const {return csp_handle_;}
72
73 private:
74 bool inited_; // True if CSSM_Init has been called successfully. 78 bool inited_; // True if CSSM_Init has been called successfully.
75 bool loaded_; // True if CSSM_ModuleLoad has been called successfully. 79 bool loaded_; // True if CSSM_ModuleLoad has been called successfully.
76 CSSM_CSP_HANDLE csp_handle_; 80 CSSM_CSP_HANDLE csp_handle_;
81
82 friend struct DefaultSingletonTraits<CSSMInitSingleton>;
77 }; 83 };
78 84
79 // This singleton is separate as it pertains to Apple's wrappers over 85 // This singleton is separate as it pertains to Apple's wrappers over
80 // their own CSSM handles, as opposed to our own CSSM_CSP_HANDLE. 86 // their own CSSM handles, as opposed to our own CSSM_CSP_HANDLE.
81 class SecurityServicesSingleton { 87 class SecurityServicesSingleton {
82 public: 88 public:
89 static SecurityServicesSingleton* GetInstance() {
90 return Singleton<SecurityServicesSingleton>::get();
91 }
92
83 ~SecurityServicesSingleton() {} 93 ~SecurityServicesSingleton() {}
84 94
85 Lock& lock() { return lock_; } 95 Lock& lock() { return lock_; }
86 96
87 private: 97 private:
88 friend class Singleton<SecurityServicesSingleton>; 98 friend class Singleton<SecurityServicesSingleton>;
89 friend struct DefaultSingletonTraits<SecurityServicesSingleton>; 99 friend struct DefaultSingletonTraits<SecurityServicesSingleton>;
90 100
91 SecurityServicesSingleton() {} 101 SecurityServicesSingleton() {}
92 102
93 Lock lock_; 103 Lock lock_;
94 104
95 DISALLOW_COPY_AND_ASSIGN(SecurityServicesSingleton); 105 DISALLOW_COPY_AND_ASSIGN(SecurityServicesSingleton);
96 }; 106 };
97 107
98 } // namespace 108 } // namespace
99 109
100 namespace base { 110 namespace base {
101 111
102 void EnsureCSSMInit() { 112 void EnsureCSSMInit() {
103 Singleton<CSSMInitSingleton>::get(); 113 CSSMInitSingleton::GetInstance();
104 } 114 }
105 115
106 CSSM_CSP_HANDLE GetSharedCSPHandle() { 116 CSSM_CSP_HANDLE GetSharedCSPHandle() {
107 return Singleton<CSSMInitSingleton>::get()->csp_handle(); 117 return CSSMInitSingleton::GetInstance()->csp_handle();
108 } 118 }
109 119
110 void* CSSMMalloc(CSSM_SIZE size, void *alloc_ref) { 120 void* CSSMMalloc(CSSM_SIZE size, void *alloc_ref) {
111 return malloc(size); 121 return malloc(size);
112 } 122 }
113 123
114 void CSSMFree(void* mem_ptr, void* alloc_ref) { 124 void CSSMFree(void* mem_ptr, void* alloc_ref) {
115 free(mem_ptr); 125 free(mem_ptr);
116 } 126 }
117 127
(...skipping 20 matching lines...) Expand all
138 if (cfstr) { 148 if (cfstr) {
139 std::string err_name = SysCFStringRefToUTF8(cfstr); 149 std::string err_name = SysCFStringRefToUTF8(cfstr);
140 CFRelease(cfstr); 150 CFRelease(cfstr);
141 LOG(ERROR) << fn_name << " returned " << err << " (" << err_name << ")"; 151 LOG(ERROR) << fn_name << " returned " << err << " (" << err_name << ")";
142 } else { 152 } else {
143 LOG(ERROR) << fn_name << " returned " << err; 153 LOG(ERROR) << fn_name << " returned " << err;
144 } 154 }
145 } 155 }
146 156
147 Lock& GetMacSecurityServicesLock() { 157 Lock& GetMacSecurityServicesLock() {
148 return Singleton<SecurityServicesSingleton>::get()->lock(); 158 return SecurityServicesSingleton::GetInstance()->lock();
149 } 159 }
150 160
151 } // namespace base 161 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698