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.
5 #ifndef CONTENT_BROWSER_MEDIA_WEBRTC_IDENTITY_STORE_BACKEND_H_
6 #define CONTENT_BROWSER_MEDIA_WEBRTC_IDENTITY_STORE_BACKEND_H_
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/time/time.h"
24 class SpecialStoragePolicy
;
25 } // namespace storage
29 // This class represents a persistent cache of WebRTC identities.
30 // It can be created/destroyed/Close() on any thread. All other members should
31 // be accessed on the IO thread.
32 class WebRTCIdentityStoreBackend
33 : public base::RefCountedThreadSafe
<WebRTCIdentityStoreBackend
> {
35 typedef base::Callback
<void(int error
,
36 const std::string
& certificate
,
37 const std::string
& private_key
)>
40 // No data is saved on disk if |path| is empty. Identites older than
41 // |validity_period| will be removed lazily.
42 WebRTCIdentityStoreBackend(const base::FilePath
& path
,
43 storage::SpecialStoragePolicy
* policy
,
44 base::TimeDelta validity_period
);
46 // Finds the identity with |origin|, |identity_name|, and |common_name| from
48 // |origin| is the origin of the identity;
49 // |identity_name| is used to identify an identity within an origin;
50 // |common_name| is the common name used to generate the certificate;
51 // |callback| is the callback to return the find result.
52 // Returns true if |callback| will be called.
53 // Should be called on the IO thread.
54 bool FindIdentity(const GURL
& origin
,
55 const std::string
& identity_name
,
56 const std::string
& common_name
,
57 const FindIdentityCallback
& callback
);
59 // Adds the identity to the DB and overwrites any existing identity having the
60 // same origin and identity_name.
61 // |origin| is the origin of the identity;
62 // |identity_name| is used to identify an identity within an origin;
63 // |common_name| is the common name used to generate the certificate;
64 // |certificate| is the DER string of the certificate;
65 // |private_key| is the DER string of the private key.
66 // Should be called on the IO thread.
67 void AddIdentity(const GURL
& origin
,
68 const std::string
& identity_name
,
69 const std::string
& common_name
,
70 const std::string
& certificate
,
71 const std::string
& private_key
);
73 // Commits all pending DB operations and closes the DB connection. Any API
74 // call after this will fail.
75 // Can be called on any thread.
78 // Delete the data created between |delete_begin| and |delete_end|.
79 // Should be called on the IO thread.
80 void DeleteBetween(base::Time delete_begin
,
81 base::Time delete_end
,
82 const base::Closure
& callback
);
84 // Changes the validity period. Should be called before the database is
85 // loaded into memory.
86 void SetValidityPeriodForTesting(base::TimeDelta validity_period
);
89 friend class base::RefCountedThreadSafe
<WebRTCIdentityStoreBackend
>;
97 struct PendingFindRequest
;
100 typedef std::map
<IdentityKey
, Identity
> IdentityMap
;
102 ~WebRTCIdentityStoreBackend();
104 void OnLoaded(scoped_ptr
<IdentityMap
> out_map
);
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.
115 // The persistent storage of identities.
116 scoped_refptr
<SqlLiteStorage
> sql_lite_storage_
;
118 DISALLOW_COPY_AND_ASSIGN(WebRTCIdentityStoreBackend
);
122 #endif // CONTENT_BROWSER_MEDIA_WEBRTC_IDENTITY_STORE_BACKEND_H_