| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_SSL_CHANNEL_ID_SERVICE_H_ | 5 #ifndef NET_SSL_CHANNEL_ID_SERVICE_H_ |
| 6 #define NET_SSL_CHANNEL_ID_SERVICE_H_ | 6 #define NET_SSL_CHANNEL_ID_SERVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 class TaskRunner; | 22 class TaskRunner; |
| 23 } // namespace base | 23 } // namespace base |
| 24 | 24 |
| 25 namespace crypto { | 25 namespace crypto { |
| 26 class ECPrivateKey; | 26 class ECPrivateKey; |
| 27 } // namespace crypto | 27 } // namespace crypto |
| 28 | 28 |
| 29 namespace net { | 29 namespace net { |
| 30 | 30 |
| 31 class ChannelIDServiceJob; | 31 class ChannelIDServiceJob; |
| 32 class ChannelIDServiceRequest; | |
| 33 class ChannelIDServiceWorker; | 32 class ChannelIDServiceWorker; |
| 34 | 33 |
| 35 // A class for creating and fetching Channel IDs. | 34 // A class for creating and fetching Channel IDs. |
| 36 | 35 |
| 37 // Inherits from NonThreadSafe in order to use the function | 36 // Inherits from NonThreadSafe in order to use the function |
| 38 // |CalledOnValidThread|. | 37 // |CalledOnValidThread|. |
| 39 class NET_EXPORT ChannelIDService | 38 class NET_EXPORT ChannelIDService |
| 40 : NON_EXPORTED_BASE(public base::NonThreadSafe) { | 39 : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
| 41 public: | 40 public: |
| 42 class NET_EXPORT RequestHandle { | 41 class NET_EXPORT Request { |
| 43 public: | 42 public: |
| 44 RequestHandle(); | 43 Request(); |
| 45 ~RequestHandle(); | 44 ~Request(); |
| 46 | 45 |
| 47 // Cancel the request. Does nothing if the request finished or was already | 46 // Cancel the request. Does nothing if the request finished or was already |
| 48 // cancelled. | 47 // cancelled. |
| 49 void Cancel(); | 48 void Cancel(); |
| 50 | 49 |
| 51 bool is_active() const { return request_ != NULL; } | 50 bool is_active() const { return !callback_.is_null(); } |
| 52 | 51 |
| 53 private: | 52 private: |
| 54 friend class ChannelIDService; | 53 friend class ChannelIDService; |
| 54 friend class ChannelIDServiceJob; |
| 55 | 55 |
| 56 void RequestStarted(ChannelIDService* service, | 56 void RequestStarted(ChannelIDService* service, |
| 57 ChannelIDServiceRequest* request, | 57 base::TimeTicks request_start, |
| 58 const CompletionCallback& callback); | 58 const CompletionCallback& callback, |
| 59 scoped_ptr<crypto::ECPrivateKey>* key, |
| 60 ChannelIDServiceJob* job); |
| 59 | 61 |
| 60 void OnRequestComplete(int result); | 62 void Post(int error, scoped_ptr<crypto::ECPrivateKey> key); |
| 61 | 63 |
| 62 ChannelIDService* service_; | 64 ChannelIDService* service_; |
| 63 ChannelIDServiceRequest* request_; | 65 base::TimeTicks request_start_; |
| 64 CompletionCallback callback_; | 66 CompletionCallback callback_; |
| 67 scoped_ptr<crypto::ECPrivateKey>* key_; |
| 68 ChannelIDServiceJob* job_; |
| 65 }; | 69 }; |
| 66 | 70 |
| 67 // Password used on EncryptedPrivateKeyInfo data stored in EC private_key | 71 // Password used on EncryptedPrivateKeyInfo data stored in EC private_key |
| 68 // values. (This is not used to provide any security, but to workaround NSS | 72 // values. (This is not used to provide any security, but to workaround NSS |
| 69 // being unable to import unencrypted PrivateKeyInfo for EC keys.) | 73 // being unable to import unencrypted PrivateKeyInfo for EC keys.) |
| 70 static const char kEPKIPassword[]; | 74 static const char kEPKIPassword[]; |
| 71 | 75 |
| 72 // This object owns |channel_id_store|. |task_runner| will | 76 // This object owns |channel_id_store|. |task_runner| will |
| 73 // be used to post channel ID generation worker tasks. The tasks are | 77 // be used to post channel ID generation worker tasks. The tasks are |
| 74 // safe for use with WorkerPool and SequencedWorkerPool::CONTINUE_ON_SHUTDOWN. | 78 // safe for use with WorkerPool and SequencedWorkerPool::CONTINUE_ON_SHUTDOWN. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 87 // creates one otherwise. Returns OK if successful or an error code upon | 91 // creates one otherwise. Returns OK if successful or an error code upon |
| 88 // failure. | 92 // failure. |
| 89 // | 93 // |
| 90 // On successful completion, |key| holds the ECDSA keypair used for this | 94 // On successful completion, |key| holds the ECDSA keypair used for this |
| 91 // channel ID. | 95 // channel ID. |
| 92 // | 96 // |
| 93 // |callback| must not be null. ERR_IO_PENDING is returned if the operation | 97 // |callback| must not be null. ERR_IO_PENDING is returned if the operation |
| 94 // could not be completed immediately, in which case the result code will | 98 // could not be completed immediately, in which case the result code will |
| 95 // be passed to the callback when available. | 99 // be passed to the callback when available. |
| 96 // | 100 // |
| 97 // |*out_req| will be initialized with a handle to the async request. This | 101 // |*out_req| will be initialized with a handle to the async request. |
| 98 // RequestHandle object must be cancelled or destroyed before the | |
| 99 // ChannelIDService is destroyed. | |
| 100 int GetOrCreateChannelID(const std::string& host, | 102 int GetOrCreateChannelID(const std::string& host, |
| 101 scoped_ptr<crypto::ECPrivateKey>* key, | 103 scoped_ptr<crypto::ECPrivateKey>* key, |
| 102 const CompletionCallback& callback, | 104 const CompletionCallback& callback, |
| 103 RequestHandle* out_req); | 105 Request* out_req); |
| 104 | 106 |
| 105 // Fetches the channel ID for the specified host if one exists. | 107 // Fetches the channel ID for the specified host if one exists. |
| 106 // Returns OK if successful, ERR_FILE_NOT_FOUND if none exists, or an error | 108 // Returns OK if successful, ERR_FILE_NOT_FOUND if none exists, or an error |
| 107 // code upon failure. | 109 // code upon failure. |
| 108 // | 110 // |
| 109 // On successful completion, |key| holds the ECDSA keypair used for this | 111 // On successful completion, |key| holds the ECDSA keypair used for this |
| 110 // channel ID. | 112 // channel ID. |
| 111 // | 113 // |
| 112 // |callback| must not be null. ERR_IO_PENDING is returned if the operation | 114 // |callback| must not be null. ERR_IO_PENDING is returned if the operation |
| 113 // could not be completed immediately, in which case the result code will | 115 // could not be completed immediately, in which case the result code will |
| 114 // be passed to the callback when available. If an in-flight | 116 // be passed to the callback when available. If an in-flight |
| 115 // GetChannelID is pending, and a new GetOrCreateChannelID | 117 // GetChannelID is pending, and a new GetOrCreateChannelID |
| 116 // request arrives for the same domain, the GetChannelID request will | 118 // request arrives for the same domain, the GetChannelID request will |
| 117 // not complete until a new channel ID is created. | 119 // not complete until a new channel ID is created. |
| 118 // | 120 // |
| 119 // |*out_req| will be initialized with a handle to the async request. This | 121 // |*out_req| will be initialized with a handle to the async request. |
| 120 // RequestHandle object must be cancelled or destroyed before the | |
| 121 // ChannelIDService is destroyed. | |
| 122 int GetChannelID(const std::string& host, | 122 int GetChannelID(const std::string& host, |
| 123 scoped_ptr<crypto::ECPrivateKey>* key, | 123 scoped_ptr<crypto::ECPrivateKey>* key, |
| 124 const CompletionCallback& callback, | 124 const CompletionCallback& callback, |
| 125 RequestHandle* out_req); | 125 Request* out_req); |
| 126 | 126 |
| 127 // Returns the backing ChannelIDStore. | 127 // Returns the backing ChannelIDStore. |
| 128 ChannelIDStore* GetChannelIDStore(); | 128 ChannelIDStore* GetChannelIDStore(); |
| 129 | 129 |
| 130 // Public only for unit testing. | 130 // Public only for unit testing. |
| 131 int channel_id_count(); | 131 int channel_id_count(); |
| 132 uint64 requests() const { return requests_; } | 132 uint64 requests() const { return requests_; } |
| 133 uint64 key_store_hits() const { return key_store_hits_; } | 133 uint64 key_store_hits() const { return key_store_hits_; } |
| 134 uint64 inflight_joins() const { return inflight_joins_; } | 134 uint64 inflight_joins() const { return inflight_joins_; } |
| 135 uint64 workers_created() const { return workers_created_; } | 135 uint64 workers_created() const { return workers_created_; } |
| 136 | 136 |
| 137 private: | 137 private: |
| 138 // Cancels the specified request. |req| is the handle stored by | |
| 139 // GetChannelID(). After a request is canceled, its completion | |
| 140 // callback will not be called. | |
| 141 void CancelRequest(ChannelIDServiceRequest* req); | |
| 142 | |
| 143 void GotChannelID(int err, | 138 void GotChannelID(int err, |
| 144 const std::string& server_identifier, | 139 const std::string& server_identifier, |
| 145 scoped_ptr<crypto::ECPrivateKey> key); | 140 scoped_ptr<crypto::ECPrivateKey> key); |
| 146 void GeneratedChannelID( | 141 void GeneratedChannelID( |
| 147 const std::string& server_identifier, | 142 const std::string& server_identifier, |
| 148 int error, | 143 int error, |
| 149 scoped_ptr<ChannelIDStore::ChannelID> channel_id); | 144 scoped_ptr<ChannelIDStore::ChannelID> channel_id); |
| 150 void HandleResult(int error, | 145 void HandleResult(int error, |
| 151 const std::string& server_identifier, | 146 const std::string& server_identifier, |
| 152 scoped_ptr<crypto::ECPrivateKey> key); | 147 scoped_ptr<crypto::ECPrivateKey> key); |
| 153 | 148 |
| 154 // Searches for an in-flight request for the same domain. If found, | 149 // Searches for an in-flight request for the same domain. If found, |
| 155 // attaches to the request and returns true. Returns false if no in-flight | 150 // attaches to the request and returns true. Returns false if no in-flight |
| 156 // request is found. | 151 // request is found. |
| 157 bool JoinToInFlightRequest(const base::TimeTicks& request_start, | 152 bool JoinToInFlightRequest(const base::TimeTicks& request_start, |
| 158 const std::string& domain, | 153 const std::string& domain, |
| 159 scoped_ptr<crypto::ECPrivateKey>* key, | 154 scoped_ptr<crypto::ECPrivateKey>* key, |
| 160 bool create_if_missing, | 155 bool create_if_missing, |
| 161 const CompletionCallback& callback, | 156 const CompletionCallback& callback, |
| 162 RequestHandle* out_req); | 157 Request* out_req); |
| 163 | 158 |
| 164 // Looks for the channel ID for |domain| in this service's store. | 159 // Looks for the channel ID for |domain| in this service's store. |
| 165 // Returns OK if it can be found synchronously, ERR_IO_PENDING if the | 160 // Returns OK if it can be found synchronously, ERR_IO_PENDING if the |
| 166 // result cannot be obtained synchronously, or a network error code on | 161 // result cannot be obtained synchronously, or a network error code on |
| 167 // failure (including failure to find a channel ID of |domain|). | 162 // failure (including failure to find a channel ID of |domain|). |
| 168 int LookupChannelID(const base::TimeTicks& request_start, | 163 int LookupChannelID(const base::TimeTicks& request_start, |
| 169 const std::string& domain, | 164 const std::string& domain, |
| 170 scoped_ptr<crypto::ECPrivateKey>* key, | 165 scoped_ptr<crypto::ECPrivateKey>* key, |
| 171 bool create_if_missing, | 166 bool create_if_missing, |
| 172 const CompletionCallback& callback, | 167 const CompletionCallback& callback, |
| 173 RequestHandle* out_req); | 168 Request* out_req); |
| 174 | 169 |
| 175 scoped_ptr<ChannelIDStore> channel_id_store_; | 170 scoped_ptr<ChannelIDStore> channel_id_store_; |
| 176 scoped_refptr<base::TaskRunner> task_runner_; | 171 scoped_refptr<base::TaskRunner> task_runner_; |
| 177 | 172 |
| 178 // inflight_ maps from a server to an active generation which is taking | 173 // inflight_ maps from a server to an active generation which is taking |
| 179 // place. | 174 // place. |
| 180 std::map<std::string, ChannelIDServiceJob*> inflight_; | 175 std::map<std::string, ChannelIDServiceJob*> inflight_; |
| 181 | 176 |
| 182 uint64 requests_; | 177 uint64 requests_; |
| 183 uint64 key_store_hits_; | 178 uint64 key_store_hits_; |
| 184 uint64 inflight_joins_; | 179 uint64 inflight_joins_; |
| 185 uint64 workers_created_; | 180 uint64 workers_created_; |
| 186 | 181 |
| 187 base::WeakPtrFactory<ChannelIDService> weak_ptr_factory_; | 182 base::WeakPtrFactory<ChannelIDService> weak_ptr_factory_; |
| 188 | 183 |
| 189 DISALLOW_COPY_AND_ASSIGN(ChannelIDService); | 184 DISALLOW_COPY_AND_ASSIGN(ChannelIDService); |
| 190 }; | 185 }; |
| 191 | 186 |
| 192 } // namespace net | 187 } // namespace net |
| 193 | 188 |
| 194 #endif // NET_SSL_CHANNEL_ID_SERVICE_H_ | 189 #endif // NET_SSL_CHANNEL_ID_SERVICE_H_ |
| OLD | NEW |