OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef NET_BASE_DEFAULT_SERVER_BOUND_CERT_STORE_H_ | 5 #ifndef NET_BASE_DEFAULT_SERVER_BOUND_CERT_STORE_H_ |
6 #define NET_BASE_DEFAULT_SERVER_BOUND_CERT_STORE_H_ | 6 #define NET_BASE_DEFAULT_SERVER_BOUND_CERT_STORE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/memory/scoped_vector.h" | |
17 #include "base/memory/weak_ptr.h" | |
15 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
16 #include "net/base/server_bound_cert_store.h" | 19 #include "net/base/server_bound_cert_store.h" |
17 | 20 |
18 class Task; | |
19 | |
20 namespace net { | 21 namespace net { |
21 | 22 |
22 // This class is the system for storing and retrieving server bound certs. | 23 // This class is the system for storing and retrieving server bound certs. |
23 // Modeled after the CookieMonster class, it has an in-memory cert store, | 24 // Modeled after the CookieMonster class, it has an in-memory cert store, |
24 // and synchronizes server bound certs to an optional permanent storage that | 25 // and synchronizes server bound certs to an optional permanent storage that |
25 // implements the PersistentStore interface. The use case is described in | 26 // implements the PersistentStore interface. The use case is described in |
26 // http://balfanz.github.com/tls-obc-spec/draft-balfanz-tls-obc-00.html | 27 // http://balfanz.github.com/tls-obc-spec/draft-balfanz-tls-obc-00.html |
27 class NET_EXPORT DefaultServerBoundCertStore : public ServerBoundCertStore { | 28 class NET_EXPORT DefaultServerBoundCertStore : public ServerBoundCertStore { |
28 public: | 29 public: |
29 class PersistentStore; | 30 class PersistentStore; |
(...skipping 11 matching lines...) Expand all Loading... | |
41 | 42 |
42 virtual ~DefaultServerBoundCertStore(); | 43 virtual ~DefaultServerBoundCertStore(); |
43 | 44 |
44 // Flush the backing store (if any) to disk and post the given task when done. | 45 // Flush the backing store (if any) to disk and post the given task when done. |
45 // WARNING: THE CALLBACK WILL RUN ON A RANDOM THREAD. IT MUST BE THREAD SAFE. | 46 // WARNING: THE CALLBACK WILL RUN ON A RANDOM THREAD. IT MUST BE THREAD SAFE. |
46 // It may be posted to the current thread, or it may run on the thread that | 47 // It may be posted to the current thread, or it may run on the thread that |
47 // actually does the flushing. Your Task should generally post a notification | 48 // actually does the flushing. Your Task should generally post a notification |
48 // to the thread you actually want to be notified on. | 49 // to the thread you actually want to be notified on. |
49 void FlushStore(const base::Closure& completion_task); | 50 void FlushStore(const base::Closure& completion_task); |
50 | 51 |
51 // ServerBoundCertStore implementation. | 52 // ServerBoundCertStore implementation. All the callbacks for the following |
53 // methods will be run on the calling thread. | |
erikwright (departed)
2013/01/04 19:20:10
This class is no longer threadsafe, right? If so t
mattm
2013/01/08 04:53:21
Well, I wanted to make the distinction from FlushS
erikwright (departed)
2013/01/10 22:03:30
I see. OK, the comment does make sense in that con
| |
52 virtual bool GetServerBoundCert( | 54 virtual bool GetServerBoundCert( |
53 const std::string& server_identifier, | 55 const std::string& server_identifier, |
54 SSLClientCertType* type, | 56 SSLClientCertType* type, |
55 base::Time* creation_time, | |
56 base::Time* expiration_time, | 57 base::Time* expiration_time, |
57 std::string* private_key_result, | 58 std::string* private_key_result, |
58 std::string* cert_result) OVERRIDE; | 59 std::string* cert_result, |
60 const GetCertCallback& callback) OVERRIDE; | |
59 virtual void SetServerBoundCert( | 61 virtual void SetServerBoundCert( |
60 const std::string& server_identifier, | 62 const std::string& server_identifier, |
61 SSLClientCertType type, | 63 SSLClientCertType type, |
62 base::Time creation_time, | 64 base::Time creation_time, |
63 base::Time expiration_time, | 65 base::Time expiration_time, |
64 const std::string& private_key, | 66 const std::string& private_key, |
65 const std::string& cert) OVERRIDE; | 67 const std::string& cert) OVERRIDE; |
66 virtual void DeleteServerBoundCert(const std::string& server_identifier) | 68 virtual void DeleteServerBoundCert( |
67 OVERRIDE; | 69 const std::string& server_identifier, |
68 virtual void DeleteAllCreatedBetween(base::Time delete_begin, | 70 const base::Closure& callback) OVERRIDE; |
69 base::Time delete_end) OVERRIDE; | 71 virtual void DeleteAllCreatedBetween( |
70 virtual void DeleteAll() OVERRIDE; | 72 base::Time delete_begin, |
73 base::Time delete_end, | |
74 const base::Closure& callback) OVERRIDE; | |
75 virtual void DeleteAll(const base::Closure& callback) OVERRIDE; | |
71 virtual void GetAllServerBoundCerts( | 76 virtual void GetAllServerBoundCerts( |
72 ServerBoundCertList* server_bound_certs) OVERRIDE; | 77 const GetCertListCallback& callback) OVERRIDE; |
73 virtual int GetCertCount() OVERRIDE; | 78 virtual int GetCertCount() OVERRIDE; |
74 virtual void SetForceKeepSessionState() OVERRIDE; | 79 virtual void SetForceKeepSessionState() OVERRIDE; |
75 | 80 |
76 private: | 81 private: |
82 class Task; | |
83 class GetServerBoundCertTask; | |
84 class SetServerBoundCertTask; | |
85 class DeleteServerBoundCertTask; | |
86 class DeleteAllCreatedBetweenTask; | |
87 class GetAllServerBoundCertsTask; | |
88 | |
77 static const size_t kMaxCerts; | 89 static const size_t kMaxCerts; |
78 | 90 |
79 // Deletes all of the certs. Does not delete them from |store_|. | 91 // Deletes all of the certs. Does not delete them from |store_|. |
80 void DeleteAllInMemory(); | 92 void DeleteAllInMemory(); |
81 | 93 |
82 // Called by all non-static functions to ensure that the cert store has | 94 // Called by all non-static functions to ensure that the cert store has |
83 // been initialized. This is not done during creating so it doesn't block | 95 // been initialized. |
84 // the window showing. | 96 // TODO(mattm): since we load asynchronously now, maybe we should start |
97 // loading immediately on construction, or provide some method to initiate | |
98 // loading? | |
85 void InitIfNecessary() { | 99 void InitIfNecessary() { |
86 if (!initialized_) { | 100 if (!initialized_) { |
87 if (store_) | 101 if (store_) { |
88 InitStore(); | 102 InitStore(); |
103 } else { | |
104 loaded_ = true; | |
105 } | |
89 initialized_ = true; | 106 initialized_ = true; |
90 } | 107 } |
91 } | 108 } |
92 | 109 |
93 // Initializes the backing store and reads existing certs from it. | 110 // Initializes the backing store and reads existing certs from it. |
94 // Should only be called by InitIfNecessary(). | 111 // Should only be called by InitIfNecessary(). |
95 void InitStore(); | 112 void InitStore(); |
96 | 113 |
114 // Callback for backing store loading completion. | |
115 void OnLoaded(scoped_ptr<ScopedVector<ServerBoundCert> > certs); | |
116 | |
117 // Syncronous methods which do the actual work. Can only be called after | |
118 // initialization is complete. | |
119 void SyncSetServerBoundCert( | |
120 const std::string& server_identifier, | |
121 SSLClientCertType type, | |
122 base::Time creation_time, | |
123 base::Time expiration_time, | |
124 const std::string& private_key, | |
125 const std::string& cert); | |
126 void SyncDeleteServerBoundCert(const std::string& server_identifier); | |
127 void SyncDeleteAllCreatedBetween(base::Time delete_begin, | |
128 base::Time delete_end); | |
129 void SyncGetAllServerBoundCerts(ServerBoundCertList* cert_list); | |
130 | |
131 // Add |task| to |waiting_tasks_|. Takes ownership. | |
132 void EnqueueTask(Task* task); | |
erikwright (departed)
2013/01/04 19:20:10
"Takes ownership" should be reflected by a scoped_
mattm
2013/01/08 04:53:21
Done.
| |
133 // If already initialized, run |task| immediately. Otherwise add it to | |
134 // |waiting_tasks_|. Takes ownership. | |
135 void RunOrEnqueueTask(Task* task); | |
erikwright (departed)
2013/01/04 19:20:10
ditto.
mattm
2013/01/08 04:53:21
Done.
| |
136 | |
97 // Deletes the cert for the specified server, if such a cert exists, from the | 137 // Deletes the cert for the specified server, if such a cert exists, from the |
98 // in-memory store. Deletes it from |store_| if |store_| is not NULL. | 138 // in-memory store. Deletes it from |store_| if |store_| is not NULL. |
99 void InternalDeleteServerBoundCert(const std::string& server); | 139 void InternalDeleteServerBoundCert(const std::string& server); |
100 | 140 |
101 // Takes ownership of *cert. | 141 // Takes ownership of *cert. |
102 // Adds the cert for the specified server to the in-memory store. Deletes it | 142 // Adds the cert for the specified server to the in-memory store. Deletes it |
103 // from |store_| if |store_| is not NULL. | 143 // from |store_| if |store_| is not NULL. |
104 void InternalInsertServerBoundCert(const std::string& server_identifier, | 144 void InternalInsertServerBoundCert(const std::string& server_identifier, |
105 ServerBoundCert* cert); | 145 ServerBoundCert* cert); |
106 | 146 |
107 // Indicates whether the cert store has been initialized. This happens | 147 // Indicates whether the cert store has been initialized. This happens |
108 // Lazily in InitStoreIfNecessary(). | 148 // lazily in InitIfNecessary(). |
109 bool initialized_; | 149 bool initialized_; |
110 | 150 |
151 // Indicates whether loading from the backend store is completed and | |
152 // calls may be immediately processed. | |
153 bool loaded_; | |
154 | |
155 // Tasks that are waiting to be run once we finish loading. | |
156 ScopedVector<Task> waiting_tasks_; | |
157 base::TimeTicks waiting_tasks_start_time_; | |
158 | |
111 scoped_refptr<PersistentStore> store_; | 159 scoped_refptr<PersistentStore> store_; |
112 | 160 |
113 ServerBoundCertMap server_bound_certs_; | 161 ServerBoundCertMap server_bound_certs_; |
114 | 162 |
163 base::WeakPtrFactory<DefaultServerBoundCertStore> weak_ptr_factory_; | |
164 | |
115 DISALLOW_COPY_AND_ASSIGN(DefaultServerBoundCertStore); | 165 DISALLOW_COPY_AND_ASSIGN(DefaultServerBoundCertStore); |
116 }; | 166 }; |
117 | 167 |
118 typedef base::RefCountedThreadSafe<DefaultServerBoundCertStore::PersistentStore> | 168 typedef base::RefCountedThreadSafe<DefaultServerBoundCertStore::PersistentStore> |
119 RefcountedPersistentStore; | 169 RefcountedPersistentStore; |
120 | 170 |
121 class NET_EXPORT DefaultServerBoundCertStore::PersistentStore | 171 class NET_EXPORT DefaultServerBoundCertStore::PersistentStore |
122 : public RefcountedPersistentStore { | 172 : public RefcountedPersistentStore { |
123 public: | 173 public: |
174 typedef base::Callback<void(scoped_ptr<ScopedVector<ServerBoundCert> >)> | |
175 LoadedCallback; | |
176 | |
124 // Initializes the store and retrieves the existing certs. This will be | 177 // Initializes the store and retrieves the existing certs. This will be |
125 // called only once at startup. Note that the certs are individually allocated | 178 // called only once at startup. Note that the certs are individually allocated |
126 // and that ownership is transferred to the caller upon return. | 179 // and that ownership is transferred to the caller upon return. |
127 virtual bool Load( | 180 // The |loaded_callback| must not be called synchronously. |
128 std::vector<ServerBoundCert*>* certs) = 0; | 181 virtual void Load(const LoadedCallback& loaded_callback) = 0; |
129 | 182 |
130 virtual void AddServerBoundCert(const ServerBoundCert& cert) = 0; | 183 virtual void AddServerBoundCert(const ServerBoundCert& cert) = 0; |
131 | 184 |
132 virtual void DeleteServerBoundCert(const ServerBoundCert& cert) = 0; | 185 virtual void DeleteServerBoundCert(const ServerBoundCert& cert) = 0; |
133 | 186 |
134 // When invoked, instructs the store to keep session related data on | 187 // When invoked, instructs the store to keep session related data on |
135 // destruction. | 188 // destruction. |
136 virtual void SetForceKeepSessionState() = 0; | 189 virtual void SetForceKeepSessionState() = 0; |
137 | 190 |
138 // Flush the store and post the given Task when complete. | 191 // Flush the store and post the given Task when complete. |
139 virtual void Flush(const base::Closure& completion_task) = 0; | 192 virtual void Flush(const base::Closure& completion_task) = 0; |
140 | 193 |
141 protected: | 194 protected: |
142 friend class base::RefCountedThreadSafe<PersistentStore>; | 195 friend class base::RefCountedThreadSafe<PersistentStore>; |
143 | 196 |
144 PersistentStore(); | 197 PersistentStore(); |
145 virtual ~PersistentStore(); | 198 virtual ~PersistentStore(); |
146 | 199 |
147 private: | 200 private: |
148 DISALLOW_COPY_AND_ASSIGN(PersistentStore); | 201 DISALLOW_COPY_AND_ASSIGN(PersistentStore); |
149 }; | 202 }; |
150 | 203 |
151 } // namespace net | 204 } // namespace net |
152 | 205 |
153 #endif // NET_DEFAULT_ORIGIN_BOUND_CERT_STORE_H_ | 206 #endif // NET_DEFAULT_ORIGIN_BOUND_CERT_STORE_H_ |
OLD | NEW |