| OLD | NEW |
| (Empty) |
| 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_WEBRTC_IDENTITY_STORE_H_ | |
| 6 #define CONTENT_BROWSER_MEDIA_WEBRTC_WEBRTC_IDENTITY_STORE_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/time/time.h" | |
| 14 #include "content/common/content_export.h" | |
| 15 | |
| 16 class GURL; | |
| 17 | |
| 18 namespace base { | |
| 19 class FilePath; | |
| 20 class TaskRunner; | |
| 21 } // namespace base | |
| 22 | |
| 23 namespace storage { | |
| 24 class SpecialStoragePolicy; | |
| 25 } // namespace storage | |
| 26 | |
| 27 namespace content { | |
| 28 class WebRTCIdentityRequest; | |
| 29 struct WebRTCIdentityRequestResult; | |
| 30 class WebRTCIdentityStoreBackend; | |
| 31 class WebRTCIdentityStoreTest; | |
| 32 | |
| 33 // A class for creating and fetching DTLS identities, i.e. the private key and | |
| 34 // the self-signed certificate. | |
| 35 // It can be created/destroyed on any thread, but the public methods must be | |
| 36 // called on the IO thread. | |
| 37 class CONTENT_EXPORT WebRTCIdentityStore | |
| 38 : public base::RefCountedThreadSafe<WebRTCIdentityStore> { | |
| 39 public: | |
| 40 typedef base::Callback<void(int error, | |
| 41 const std::string& certificate, | |
| 42 const std::string& private_key)> | |
| 43 CompletionCallback; | |
| 44 | |
| 45 // If |path| is empty, nothing will be saved to disk. | |
| 46 WebRTCIdentityStore(const base::FilePath& path, | |
| 47 storage::SpecialStoragePolicy* policy); | |
| 48 | |
| 49 // Retrieve the cached DTLS private key and certificate, i.e. identity, for | |
| 50 // the |origin| and |identity_name| pair if such an identity exists and | |
| 51 // |enable_cache| is true. Otherwise, generate a new identity using | |
| 52 // |common_name|. | |
| 53 // If the given |common_name| is different from the common name in the cached | |
| 54 // identity that has the same origin and identity_name, a new private key and | |
| 55 // a new certificate will be generated, overwriting the old one. | |
| 56 // | |
| 57 // |origin| is the origin of the DTLS connection; | |
| 58 // |identity_name| is used to identify an identity within an origin; it is | |
| 59 // opaque to WebRTCIdentityStore and remains private to the caller, i.e. not | |
| 60 // present in the certificate; | |
| 61 // |common_name| is the common name used to generate the certificate and will | |
| 62 // be shared with the peer of the DTLS connection. Identities created for | |
| 63 // different origins or different identity names may have the same common | |
| 64 // name. | |
| 65 // |callback| is the callback to return the result as DER strings. | |
| 66 // |enable_cache| is true if the persistent cache should be used to return the | |
| 67 // certificate. If a new identity is generated, it will be not saved in the | |
| 68 // cache if |enable_cache| is false. | |
| 69 // Returns the Closure used to cancel the request if the request is accepted. | |
| 70 // The Closure can only be called before the request completes. | |
| 71 virtual base::Closure RequestIdentity(const GURL& origin, | |
| 72 const std::string& identity_name, | |
| 73 const std::string& common_name, | |
| 74 const CompletionCallback& callback, | |
| 75 bool enable_cache); | |
| 76 | |
| 77 // Delete the identities created between |delete_begin| and |delete_end|. | |
| 78 // |callback| will be called when the operation is done. | |
| 79 void DeleteBetween(base::Time delete_begin, | |
| 80 base::Time delete_end, | |
| 81 const base::Closure& callback); | |
| 82 | |
| 83 protected: | |
| 84 // Only virtual to allow subclassing for test mock. | |
| 85 virtual ~WebRTCIdentityStore(); | |
| 86 | |
| 87 private: | |
| 88 friend class base::RefCountedThreadSafe<WebRTCIdentityStore>; | |
| 89 friend class WebRtcIdentityStoreTest; | |
| 90 | |
| 91 void SetValidityPeriodForTesting(base::TimeDelta validity_period); | |
| 92 void SetTaskRunnerForTesting( | |
| 93 const scoped_refptr<base::TaskRunner>& task_runner); | |
| 94 | |
| 95 void BackendFindCallback(WebRTCIdentityRequest* request, | |
| 96 int error, | |
| 97 const std::string& certificate, | |
| 98 const std::string& private_key); | |
| 99 void GenerateIdentityCallback(WebRTCIdentityRequest* request, | |
| 100 WebRTCIdentityRequestResult* result); | |
| 101 WebRTCIdentityRequest* FindRequest(const GURL& origin, | |
| 102 const std::string& identity_name, | |
| 103 const std::string& common_name); | |
| 104 void PostRequestResult(WebRTCIdentityRequest* request, | |
| 105 const WebRTCIdentityRequestResult& result); | |
| 106 | |
| 107 void GenerateNewIdentity(WebRTCIdentityRequest* request); | |
| 108 | |
| 109 // The validity period of the certificates. | |
| 110 base::TimeDelta validity_period_; | |
| 111 | |
| 112 // The TaskRunner for doing work on a worker thread. | |
| 113 scoped_refptr<base::TaskRunner> task_runner_; | |
| 114 | |
| 115 // Weak references of the in flight requests. Used to join identical external | |
| 116 // requests. | |
| 117 std::vector<WebRTCIdentityRequest*> in_flight_requests_; | |
| 118 | |
| 119 scoped_refptr<WebRTCIdentityStoreBackend> backend_; | |
| 120 | |
| 121 DISALLOW_COPY_AND_ASSIGN(WebRTCIdentityStore); | |
| 122 }; | |
| 123 | |
| 124 } // namespace content | |
| 125 | |
| 126 #endif // CONTENT_BROWSER_MEDIA_WEBRTC_WEBRTC_IDENTITY_STORE_H_ | |
| OLD | NEW |