summaryrefslogtreecommitdiff
path: root/chromium/content/browser/media/webrtc_identity_store_backend.h
blob: ab4e1ed7e1ebddaed355df10f1bbc3775a12f158 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_MEDIA_WEBRTC_IDENTITY_STORE_BACKEND_H_
#define CONTENT_BROWSER_MEDIA_WEBRTC_IDENTITY_STORE_BACKEND_H_

#include <map>
#include <string>

#include "base/time/time.h"
#include "sql/connection.h"
#include "sql/meta_table.h"

class GURL;

namespace base {
class FilePath;
}  // namespace base

namespace quota {
class SpecialStoragePolicy;
}  // namespace quota

namespace content {

// This class represents a persistent cache of WebRTC identities.
// It can be created/destroyed/Close() on any thread. All other members should
// be accessed on the IO thread.
class WebRTCIdentityStoreBackend
    : public base::RefCountedThreadSafe<WebRTCIdentityStoreBackend> {
 public:
  typedef base::Callback<void(int error,
                              const std::string& certificate,
                              const std::string& private_key)>
      FindIdentityCallback;

  // No data is saved on disk if |path| is empty.
  WebRTCIdentityStoreBackend(const base::FilePath& path,
                             quota::SpecialStoragePolicy* policy);

  // Finds the identity with |origin|, |identity_name|, and |common_name| from
  // the DB.
  // |origin| is the origin of the identity;
  // |identity_name| is used to identify an identity within an origin;
  // |common_name| is the common name used to generate the certificate;
  // |callback| is the callback to return the find result.
  // Returns true if |callback| will be called.
  // Should be called on the IO thread.
  bool FindIdentity(const GURL& origin,
                    const std::string& identity_name,
                    const std::string& common_name,
                    const FindIdentityCallback& callback);

  // Adds the identity to the DB and overwrites any existing identity having the
  // same origin and identity_name.
  // |origin| is the origin of the identity;
  // |identity_name| is used to identify an identity within an origin;
  // |common_name| is the common name used to generate the certificate;
  // |certificate| is the DER string of the certificate;
  // |private_key| is the DER string of the private key.
  // Should be called on the IO thread.
  void AddIdentity(const GURL& origin,
                   const std::string& identity_name,
                   const std::string& common_name,
                   const std::string& certificate,
                   const std::string& private_key);

  // Commits all pending DB operations and closes the DB connection. Any API
  // call after this will fail.
  // Can be called on any thread.
  void Close();

  // Delete the data created between |delete_begin| and |delete_end|.
  // Should be called on the IO thread.
  void DeleteBetween(base::Time delete_begin,
                     base::Time delete_end,
                     const base::Closure& callback);

 private:
  friend class base::RefCountedThreadSafe<WebRTCIdentityStoreBackend>;
  class SqlLiteStorage;
  enum LoadingState {
    NOT_STARTED,
    LOADING,
    LOADED,
    CLOSED,
  };
  struct PendingFindRequest;
  struct IdentityKey;
  struct Identity;
  typedef std::map<IdentityKey, Identity> IdentityMap;

  ~WebRTCIdentityStoreBackend();

  void OnLoaded(scoped_ptr<IdentityMap> out_map);

  // In-memory copy of the identities.
  IdentityMap identities_;
  // "Find identity" requests waiting for the DB to load.
  std::vector<PendingFindRequest*> pending_find_requests_;
  // The persistent storage loading state.
  LoadingState state_;
  // The persistent storage of identities.
  scoped_refptr<SqlLiteStorage> sql_lite_storage_;

  DISALLOW_COPY_AND_ASSIGN(WebRTCIdentityStoreBackend);
};
}

#endif  // CONTENT_BROWSER_MEDIA_WEBRTC_IDENTITY_STORE_BACKEND_H_