Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / net / ssl / default_channel_id_store.h
blobdb6ee97641afe51921872bafd81a665e94ba9284
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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"
21 namespace crypto {
22 class ECPrivateKey;
23 } // namespace crypto
25 namespace net {
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 {
33 public:
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;
64 private:
65 class Task;
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
76 // been initialized.
77 // TODO(mattm): since we load asynchronously now, maybe we should start
78 // loading immediately on construction, or provide some method to initiate
79 // loading?
80 void InitIfNecessary() {
81 if (!initialized_) {
82 if (store_.get()) {
83 InitStore();
84 } else {
85 loaded_ = true;
87 initialized_ = true;
91 // Initializes the backing store and reads existing certs from it.
92 // Should only be called by InitIfNecessary().
93 void InitStore();
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
109 // |waiting_tasks_|.
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_|
114 // is not NULL.
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().
123 bool initialized_;
125 // Indicates whether loading from the backend store is completed and
126 // calls may be immediately processed.
127 bool loaded_;
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 {
147 public:
148 typedef base::Callback<void(scoped_ptr<ScopedVector<ChannelID> >)>
149 LoadedCallback;
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
162 // destruction.
163 virtual void SetForceKeepSessionState() = 0;
165 protected:
166 friend class base::RefCountedThreadSafe<PersistentStore>;
168 PersistentStore();
169 virtual ~PersistentStore();
171 private:
172 DISALLOW_COPY_AND_ASSIGN(PersistentStore);
175 } // namespace net
177 #endif // NET_SSL_DEFAULT_CHANNEL_ID_STORE_H_