Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / media / webrtc_identity_store_backend.h
blob57c16ab39b51d6bf8a821a613de35751e22d079d
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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"
17 class GURL;
19 namespace base {
20 class FilePath;
21 } // namespace base
23 namespace storage {
24 class SpecialStoragePolicy;
25 } // namespace storage
27 namespace content {
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> {
34 public:
35 typedef base::Callback<void(int error,
36 const std::string& certificate,
37 const std::string& private_key)>
38 FindIdentityCallback;
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
47 // the DB.
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.
76 void Close();
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);
88 private:
89 friend class base::RefCountedThreadSafe<WebRTCIdentityStoreBackend>;
90 class SqlLiteStorage;
91 enum LoadingState {
92 NOT_STARTED,
93 LOADING,
94 LOADED,
95 CLOSED,
97 struct PendingFindRequest;
98 struct IdentityKey;
99 struct Identity;
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.
114 LoadingState 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_