1 // Copyright 2014 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.
5 #ifndef NET_SSL_DEFAULT_CHANNEL_ID_STORE_H_
6 #define NET_SSL_DEFAULT_CHANNEL_ID_STORE_H_
12 #include "base/callback_forward.h"
13 #include "base/compiler_specific.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"
18 #include "net/base/net_export.h"
19 #include "net/ssl/channel_id_store.h"
23 // This class is the system for storing and retrieving server bound certs.
24 // Modeled after the CookieMonster class, it has an in-memory cert store,
25 // and synchronizes server bound certs to an optional permanent storage that
26 // implements the PersistentStore interface. The use case is described in
27 // http://balfanz.github.com/tls-obc-spec/draft-balfanz-tls-obc-00.html
28 // TODO(wtc): Update this comment.
29 class NET_EXPORT DefaultChannelIDStore
: public ChannelIDStore
{
31 class PersistentStore
;
33 // The key for each ChannelID* in ChannelIDMap is the
34 // corresponding server.
35 typedef std::map
<std::string
, ChannelID
*> ChannelIDMap
;
37 // The store passed in should not have had Init() called on it yet. This
38 // class will take care of initializing it. The backing store is NOT owned by
39 // this class, but it must remain valid for the duration of the
40 // DefaultChannelIDStore's existence. If |store| is NULL, then no
41 // backing store will be updated.
42 explicit DefaultChannelIDStore(PersistentStore
* store
);
44 ~DefaultChannelIDStore() override
;
46 // ChannelIDStore implementation.
47 int GetChannelID(const std::string
& server_identifier
,
48 base::Time
* expiration_time
,
49 std::string
* private_key_result
,
50 std::string
* cert_result
,
51 const GetChannelIDCallback
& callback
) override
;
52 void SetChannelID(const std::string
& server_identifier
,
53 base::Time creation_time
,
54 base::Time expiration_time
,
55 const std::string
& private_key
,
56 const std::string
& cert
) override
;
57 void DeleteChannelID(const std::string
& server_identifier
,
58 const base::Closure
& callback
) override
;
59 void DeleteAllCreatedBetween(base::Time delete_begin
,
60 base::Time delete_end
,
61 const base::Closure
& callback
) override
;
62 void DeleteAll(const base::Closure
& callback
) override
;
63 void GetAllChannelIDs(const GetChannelIDListCallback
& callback
) override
;
64 int GetChannelIDCount() override
;
65 void SetForceKeepSessionState() override
;
69 class GetChannelIDTask
;
70 class SetChannelIDTask
;
71 class DeleteChannelIDTask
;
72 class DeleteAllCreatedBetweenTask
;
73 class GetAllChannelIDsTask
;
75 // Deletes all of the certs. Does not delete them from |store_|.
76 void DeleteAllInMemory();
78 // Called by all non-static functions to ensure that the cert store has
80 // TODO(mattm): since we load asynchronously now, maybe we should start
81 // loading immediately on construction, or provide some method to initiate
83 void InitIfNecessary() {
94 // Initializes the backing store and reads existing certs from it.
95 // Should only be called by InitIfNecessary().
98 // Callback for backing store loading completion.
99 void OnLoaded(scoped_ptr
<ScopedVector
<ChannelID
> > certs
);
101 // Syncronous methods which do the actual work. Can only be called after
102 // initialization is complete.
103 void SyncSetChannelID(
104 const std::string
& server_identifier
,
105 base::Time creation_time
,
106 base::Time expiration_time
,
107 const std::string
& private_key
,
108 const std::string
& cert
);
109 void SyncDeleteChannelID(const std::string
& server_identifier
);
110 void SyncDeleteAllCreatedBetween(base::Time delete_begin
,
111 base::Time delete_end
);
112 void SyncGetAllChannelIDs(ChannelIDList
* channel_id_list
);
114 // Add |task| to |waiting_tasks_|.
115 void EnqueueTask(scoped_ptr
<Task
> task
);
116 // If already initialized, run |task| immediately. Otherwise add it to
118 void RunOrEnqueueTask(scoped_ptr
<Task
> task
);
120 // Deletes the channel id for the specified server, if such a channel id
121 // exists, from the in-memory store. Deletes it from |store_| if |store_|
123 void InternalDeleteChannelID(const std::string
& server
);
125 // Takes ownership of *channel_id.
126 // Adds the channel id for the specified server to the in-memory store.
127 // Deletes it from |store_| if |store_| is not NULL.
128 void InternalInsertChannelID(const std::string
& server_identifier
,
129 ChannelID
* channel_id
);
131 // Indicates whether the channel id store has been initialized. This happens
132 // lazily in InitIfNecessary().
135 // Indicates whether loading from the backend store is completed and
136 // calls may be immediately processed.
139 // Tasks that are waiting to be run once we finish loading.
140 ScopedVector
<Task
> waiting_tasks_
;
141 base::TimeTicks waiting_tasks_start_time_
;
143 scoped_refptr
<PersistentStore
> store_
;
145 ChannelIDMap channel_ids_
;
147 base::WeakPtrFactory
<DefaultChannelIDStore
> weak_ptr_factory_
;
149 DISALLOW_COPY_AND_ASSIGN(DefaultChannelIDStore
);
152 typedef base::RefCountedThreadSafe
<DefaultChannelIDStore::PersistentStore
>
153 RefcountedPersistentStore
;
155 class NET_EXPORT
DefaultChannelIDStore::PersistentStore
156 : public RefcountedPersistentStore
{
158 typedef base::Callback
<void(scoped_ptr
<ScopedVector
<ChannelID
> >)>
161 // Initializes the store and retrieves the existing channel_ids. This will be
162 // called only once at startup. Note that the channel_ids are individually
163 // allocated and that ownership is transferred to the caller upon return.
164 // The |loaded_callback| must not be called synchronously.
165 virtual void Load(const LoadedCallback
& loaded_callback
) = 0;
167 virtual void AddChannelID(const ChannelID
& channel_id
) = 0;
169 virtual void DeleteChannelID(const ChannelID
& channel_id
) = 0;
171 // When invoked, instructs the store to keep session related data on
173 virtual void SetForceKeepSessionState() = 0;
176 friend class base::RefCountedThreadSafe
<PersistentStore
>;
179 virtual ~PersistentStore();
182 DISALLOW_COPY_AND_ASSIGN(PersistentStore
);
187 #endif // NET_SSL_DEFAULT_CHANNEL_ID_STORE_H_