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 15 class GURL; 16 17 namespace base { 18 class FilePath; 19 } // namespace base 20 21 namespace quota { 22 class SpecialStoragePolicy; 23 } // namespace quota 24 25 namespace 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. 30 class 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. 39 WebRTCIdentityStoreBackend(const base::FilePath& path, 40 quota::SpecialStoragePolicy* policy); 41 42 // Finds the identity with |origin|, |identity_name|, and |common_name| from 43 // the DB. 44 // |origin| is the origin of the identity; 45 // |identity_name| is used to identify an identity within an origin; 46 // |common_name| is the common name used to generate the certificate; 47 // |callback| is the callback to return the find result. 48 // Returns true if |callback| will be called. 49 // Should be called on the IO thread. 50 bool FindIdentity(const GURL& origin, 51 const std::string& identity_name, 52 const std::string& common_name, 53 const FindIdentityCallback& callback); 54 55 // Adds the identity to the DB and overwrites any existing identity having the 56 // same origin and identity_name. 57 // |origin| is the origin of the identity; 58 // |identity_name| is used to identify an identity within an origin; 59 // |common_name| is the common name used to generate the certificate; 60 // |certificate| is the DER string of the certificate; 61 // |private_key| is the DER string of the private key. 62 // Should be called on the IO thread. 63 void AddIdentity(const GURL& origin, 64 const std::string& identity_name, 65 const std::string& common_name, 66 const std::string& certificate, 67 const std::string& private_key); 68 69 // Commits all pending DB operations and closes the DB connection. Any API 70 // call after this will fail. 71 // Can be called on any thread. 72 void Close(); 73 74 // Delete the data created between |delete_begin| and |delete_end|. 75 // Should be called on the IO thread. 76 void DeleteBetween(base::Time delete_begin, 77 base::Time delete_end, 78 const base::Closure& callback); 79 80 private: 81 friend class base::RefCountedThreadSafe<WebRTCIdentityStoreBackend>; 82 class SqlLiteStorage; 83 enum LoadingState { 84 NOT_STARTED, 85 LOADING, 86 LOADED, 87 CLOSED, 88 }; 89 struct PendingFindRequest; 90 struct IdentityKey; 91 struct Identity; 92 typedef std::map<IdentityKey, Identity> IdentityMap; 93 94 ~WebRTCIdentityStoreBackend(); 95 96 void OnLoaded(scoped_ptr<IdentityMap> out_map); 97 98 // In-memory copy of the identities. 99 IdentityMap identities_; 100 // "Find identity" requests waiting for the DB to load. 101 std::vector<PendingFindRequest*> pending_find_requests_; 102 // The persistent storage loading state. 103 LoadingState state_; 104 // The persistent storage of identities. 105 scoped_refptr<SqlLiteStorage> sql_lite_storage_; 106 107 DISALLOW_COPY_AND_ASSIGN(WebRTCIdentityStoreBackend); 108 }; 109 } 110 111 #endif // CONTENT_BROWSER_MEDIA_WEBRTC_IDENTITY_STORE_BACKEND_H_ 112