OLD | NEW |
| (Empty) |
1 // Copyright 2013 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. | |
4 | |
5 #ifndef CONTENT_BROWSER_MEDIA_WEBRTC_WEBRTC_IDENTITY_STORE_BACKEND_H_ | |
6 #define CONTENT_BROWSER_MEDIA_WEBRTC_WEBRTC_IDENTITY_STORE_BACKEND_H_ | |
7 | |
8 #include <map> | |
9 #include <memory> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/callback.h" | |
14 #include "base/macros.h" | |
15 #include "base/memory/ref_counted.h" | |
16 #include "base/time/time.h" | |
17 | |
18 class GURL; | |
19 | |
20 namespace base { | |
21 class FilePath; | |
22 } // namespace base | |
23 | |
24 namespace storage { | |
25 class SpecialStoragePolicy; | |
26 } // namespace storage | |
27 | |
28 namespace content { | |
29 | |
30 // This class represents a persistent cache of WebRTC identities. | |
31 // It can be created/destroyed/Close() on any thread. All other members should | |
32 // be accessed on the IO thread. | |
33 class WebRTCIdentityStoreBackend | |
34 : public base::RefCountedThreadSafe<WebRTCIdentityStoreBackend> { | |
35 public: | |
36 typedef base::Callback<void(int error, | |
37 const std::string& certificate, | |
38 const std::string& private_key)> | |
39 FindIdentityCallback; | |
40 | |
41 // No data is saved on disk if |path| is empty. Identites older than | |
42 // |validity_period| will be removed lazily. | |
43 WebRTCIdentityStoreBackend(const base::FilePath& path, | |
44 storage::SpecialStoragePolicy* policy, | |
45 base::TimeDelta validity_period); | |
46 | |
47 // Finds the identity with |origin|, |identity_name|, and |common_name| from | |
48 // the DB. | |
49 // |origin| is the origin of the identity; | |
50 // |identity_name| is used to identify an identity within an origin; | |
51 // |common_name| is the common name used to generate the certificate; | |
52 // |callback| is the callback to return the find result. | |
53 // Returns true if |callback| will be called. | |
54 // Should be called on the IO thread. | |
55 bool FindIdentity(const GURL& origin, | |
56 const std::string& identity_name, | |
57 const std::string& common_name, | |
58 const FindIdentityCallback& callback); | |
59 | |
60 // Adds the identity to the DB and overwrites any existing identity having the | |
61 // same origin and identity_name. | |
62 // |origin| is the origin of the identity; | |
63 // |identity_name| is used to identify an identity within an origin; | |
64 // |common_name| is the common name used to generate the certificate; | |
65 // |certificate| is the DER string of the certificate; | |
66 // |private_key| is the DER string of the private key. | |
67 // Should be called on the IO thread. | |
68 void AddIdentity(const GURL& origin, | |
69 const std::string& identity_name, | |
70 const std::string& common_name, | |
71 const std::string& certificate, | |
72 const std::string& private_key); | |
73 | |
74 // Commits all pending DB operations and closes the DB connection. Any API | |
75 // call after this will fail. | |
76 // Can be called on any thread. | |
77 void Close(); | |
78 | |
79 // Delete the data created between |delete_begin| and |delete_end|. | |
80 // Should be called on the IO thread. | |
81 void DeleteBetween(base::Time delete_begin, | |
82 base::Time delete_end, | |
83 const base::Closure& callback); | |
84 | |
85 // Changes the validity period. Should be called before the database is | |
86 // loaded into memory. | |
87 void SetValidityPeriodForTesting(base::TimeDelta validity_period); | |
88 | |
89 private: | |
90 friend class base::RefCountedThreadSafe<WebRTCIdentityStoreBackend>; | |
91 class SqlLiteStorage; | |
92 enum LoadingState { | |
93 NOT_STARTED, | |
94 LOADING, | |
95 LOADED, | |
96 CLOSED, | |
97 }; | |
98 struct PendingFindRequest; | |
99 struct IdentityKey; | |
100 struct Identity; | |
101 typedef std::map<IdentityKey, Identity> IdentityMap; | |
102 | |
103 ~WebRTCIdentityStoreBackend(); | |
104 | |
105 void OnLoaded(std::unique_ptr<IdentityMap> out_map); | |
106 | |
107 // Identities expires after |validity_period_|. | |
108 base::TimeDelta validity_period_; | |
109 // In-memory copy of the identities. | |
110 IdentityMap identities_; | |
111 // "Find identity" requests waiting for the DB to load. | |
112 std::vector<PendingFindRequest*> pending_find_requests_; | |
113 // The persistent storage loading state. | |
114 LoadingState state_; | |
115 // The persistent storage of identities. | |
116 scoped_refptr<SqlLiteStorage> sql_lite_storage_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(WebRTCIdentityStoreBackend); | |
119 }; | |
120 } | |
121 | |
122 #endif // CONTENT_BROWSER_MEDIA_WEBRTC_WEBRTC_IDENTITY_STORE_BACKEND_H_ | |
OLD | NEW |