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"
27 // This class is the system for storing and retrieving Channel IDs. Modeled
28 // after the CookieMonster class, it has an in-memory store and synchronizes
29 // Channel IDs to an optional permanent storage that implements the
30 // PersistentStore interface. The use case is described in
31 // https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
32 class NET_EXPORT DefaultChannelIDStore
: public ChannelIDStore
{
34 class PersistentStore
;
36 // The key for each ChannelID* in ChannelIDMap is the
37 // corresponding server.
38 typedef std::map
<std::string
, ChannelID
*> ChannelIDMap
;
40 // The store passed in should not have had Init() called on it yet. This
41 // class will take care of initializing it. The backing store is NOT owned by
42 // this class, but it must remain valid for the duration of the
43 // DefaultChannelIDStore's existence. If |store| is NULL, then no
44 // backing store will be updated.
45 explicit DefaultChannelIDStore(PersistentStore
* store
);
47 ~DefaultChannelIDStore() override
;
49 // ChannelIDStore implementation.
50 int GetChannelID(const std::string
& server_identifier
,
51 scoped_ptr
<crypto::ECPrivateKey
>* key_result
,
52 const GetChannelIDCallback
& callback
) override
;
53 void SetChannelID(scoped_ptr
<ChannelID
> channel_id
) override
;
54 void DeleteChannelID(const std::string
& server_identifier
,
55 const base::Closure
& callback
) override
;
56 void DeleteAllCreatedBetween(base::Time delete_begin
,
57 base::Time delete_end
,
58 const base::Closure
& callback
) override
;
59 void DeleteAll(const base::Closure
& callback
) override
;
60 void GetAllChannelIDs(const GetChannelIDListCallback
& callback
) override
;
61 int GetChannelIDCount() override
;
62 void SetForceKeepSessionState() override
;
66 class GetChannelIDTask
;
67 class SetChannelIDTask
;
68 class DeleteChannelIDTask
;
69 class DeleteAllCreatedBetweenTask
;
70 class GetAllChannelIDsTask
;
72 // Deletes all of the certs. Does not delete them from |store_|.
73 void DeleteAllInMemory();
75 // Called by all non-static functions to ensure that the cert store has
77 // TODO(mattm): since we load asynchronously now, maybe we should start
78 // loading immediately on construction, or provide some method to initiate
80 void InitIfNecessary() {
91 // Initializes the backing store and reads existing certs from it.
92 // Should only be called by InitIfNecessary().
95 // Callback for backing store loading completion.
96 void OnLoaded(scoped_ptr
<ScopedVector
<ChannelID
> > certs
);
98 // Syncronous methods which do the actual work. Can only be called after
99 // initialization is complete.
100 void SyncSetChannelID(scoped_ptr
<ChannelID
> channel_id
);
101 void SyncDeleteChannelID(const std::string
& server_identifier
);
102 void SyncDeleteAllCreatedBetween(base::Time delete_begin
,
103 base::Time delete_end
);
104 void SyncGetAllChannelIDs(ChannelIDList
* channel_id_list
);
106 // Add |task| to |waiting_tasks_|.
107 void EnqueueTask(scoped_ptr
<Task
> task
);
108 // If already initialized, run |task| immediately. Otherwise add it to
110 void RunOrEnqueueTask(scoped_ptr
<Task
> task
);
112 // Deletes the channel id for the specified server, if such a channel id
113 // exists, from the in-memory store. Deletes it from |store_| if |store_|
115 void InternalDeleteChannelID(const std::string
& server
);
117 // Adds the channel id to the in-memory store and adds it to |store_| if
118 // |store_| is not NULL.
119 void InternalInsertChannelID(scoped_ptr
<ChannelID
> channel_id
);
121 // Indicates whether the channel id store has been initialized. This happens
122 // lazily in InitIfNecessary().
125 // Indicates whether loading from the backend store is completed and
126 // calls may be immediately processed.
129 // Tasks that are waiting to be run once we finish loading.
130 ScopedVector
<Task
> waiting_tasks_
;
131 base::TimeTicks waiting_tasks_start_time_
;
133 scoped_refptr
<PersistentStore
> store_
;
135 ChannelIDMap channel_ids_
;
137 base::WeakPtrFactory
<DefaultChannelIDStore
> weak_ptr_factory_
;
139 DISALLOW_COPY_AND_ASSIGN(DefaultChannelIDStore
);
142 typedef base::RefCountedThreadSafe
<DefaultChannelIDStore::PersistentStore
>
143 RefcountedPersistentStore
;
145 class NET_EXPORT
DefaultChannelIDStore::PersistentStore
146 : public RefcountedPersistentStore
{
148 typedef base::Callback
<void(scoped_ptr
<ScopedVector
<ChannelID
> >)>
151 // Initializes the store and retrieves the existing channel_ids. This will be
152 // called only once at startup. Note that the channel_ids are individually
153 // allocated and that ownership is transferred to the caller upon return.
154 // The |loaded_callback| must not be called synchronously.
155 virtual void Load(const LoadedCallback
& loaded_callback
) = 0;
157 virtual void AddChannelID(const ChannelID
& channel_id
) = 0;
159 virtual void DeleteChannelID(const ChannelID
& channel_id
) = 0;
161 // When invoked, instructs the store to keep session related data on
163 virtual void SetForceKeepSessionState() = 0;
166 friend class base::RefCountedThreadSafe
<PersistentStore
>;
169 virtual ~PersistentStore();
172 DISALLOW_COPY_AND_ASSIGN(PersistentStore
);
177 #endif // NET_SSL_DEFAULT_CHANNEL_ID_STORE_H_