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