Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: net/cert_cache/disk_based_cert_cache.cc

Issue 329733002: Disk Based Certificate Cache Implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improved and fixed first test. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2014 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 #include "net/cert_cache/disk_based_cert_cache.h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/pickle.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "net/base/io_buffer.h"
16 #include "net/base/net_errors.h"
17 #include "net/cert/x509_certificate.h"
18 #include "net/disk_cache/disk_cache.h"
19
20 namespace net {
21
22 DiskBasedCertCache::DiskBasedCertCache(disk_cache::Backend* backend)
23 : backend_(backend),
24 active_entry_(NULL),
25 weak_factory_(this),
26 io_callback_(base::Bind(&DiskBasedCertCache::OnIOComplete,
27 weak_factory_.GetWeakPtr())),
28 state_(NONE) {
29 DCHECK(backend_);
30 }
31
32 DiskBasedCertCache::~DiskBasedCertCache() {
33 weak_factory_.InvalidateWeakPtrs();
34 }
35
36 void DiskBasedCertCache::Get(
37 std::string& key,
38 base::Callback<void(X509Certificate::OSCertHandle cert_handle)> cb) {
39 user_read_callback_ = cb;
40
41 state_ = START_READ;
42
43 int rv = backend_->OpenEntry(key, &active_entry_, io_callback_);
44
45 // todo: implement other error codes.
46 if (rv >= 0) {
47 DoStartRead();
48 } else if (rv == ERR_IO_PENDING) {
49 } else {
50 }
Ryan Sleevi 2014/06/11 22:00:08 A couple common coding conventions: Handle errors
51 }
52
53 void DiskBasedCertCache::DoStartRead() {
54 active_entry_size_ = active_entry_->GetDataSize(0 /* index */);
55 buffer = (new IOBuffer(active_entry_size_));
Ryan Sleevi 2014/06/11 22:00:08 No need for the extra () around new.
56
57 state_ = FINISH_READ;
58
59 int rv = active_entry_->ReadData(
60 0 /* index */, 0 /* offset */, buffer, active_entry_size_, io_callback_);
61
62 // todo: implement other error codes.
63 if (rv >= 0) {
64 DoFinishRead();
65 } else if (rv == ERR_IO_PENDING) {
66 } else {
67 }
68 }
69
70 void DiskBasedCertCache::DoFinishRead() {
71 if (!user_read_callback_.is_null()) {
Ryan Sleevi 2014/06/11 22:00:08 if (user_read_callback_.is_null()) return; acti
72 active_cert_handle_ = X509Certificate::CreateOSCertHandleFromBytes(
73 buffer->data(), active_entry_size_);
74
75 CHECK(active_cert_handle_ != NULL);
Ryan Sleevi 2014/06/11 22:00:08 CHECK(active_cert_handle_);
76
77 base::Callback<void(X509Certificate::OSCertHandle cert_handle)> callback =
78 user_read_callback_;
79 callback.Run(active_cert_handle_);
Ryan Sleevi 2014/06/11 22:00:08 base::ResetAndReturn(&user_read_callback_).Run(act
80 reset_state();
Ryan Sleevi 2014/06/11 22:00:08 It looks like your state machine breaks if user_re
81 }
82 }
83
84 void DiskBasedCertCache::Set(const X509Certificate::OSCertHandle cert_handle,
85 base::Callback<void(const std::string&)> cb) {
86 DCHECK(!cb.is_null());
87 CHECK(active_entry_ == NULL);
Ryan Sleevi 2014/06/11 22:00:08 CHECK(active_entry_)
88
89 active_cert_handle_ = cert_handle;
90
91 state_ = START_WRITE;
92 user_write_callback_ = cb;
93
94 if (backend_->CreateEntry(Key(), &active_entry_, io_callback_) !=
95 ERR_IO_PENDING) {
96 DoStartWrite();
Ryan Sleevi 2014/06/11 22:00:08 what about errors?
97 }
98 }
99
100 std::string DiskBasedCertCache::Key() {
101 CHECK(active_cert_handle_ != NULL);
Ryan Sleevi 2014/06/11 22:00:08 CHECK(active_cert_handle_);
102
103 SHA1HashValue fingerprint =
104 X509Certificate::CalculateFingerprint(active_cert_handle_);
105
106 // should update to store the key so the data doesn't have to be encoded
107 // multiple times.
108
109 return "cert:" + base::HexEncode(fingerprint.data, 20);
110 }
111
112 void DiskBasedCertCache::OnIOComplete(int rv) {
113 switch (state_) {
114 case START_WRITE:
115 DoStartWrite();
116 break;
117 case FINISH_WRITE:
118 DoFinishWrite();
119 break;
120 case START_READ:
121 DoStartRead();
122 break;
123 case FINISH_READ:
124 break;
125 DoFinishRead();
126 case NONE:
127 break;
128 }
129
130 state_ = NONE;
131 }
132
133 void DiskBasedCertCache::DoStartWrite() {
134 std::string write_data;
135
136 // encode data
137 CHECK(X509Certificate::GetDEREncoded(active_cert_handle_, &write_data));
Ryan Sleevi 2014/06/11 22:00:08 DANGER: You never want to put function calls in CH
138
139 buffer = new IOBuffer(write_data.size());
140 memcpy(buffer->data(), write_data.data(), write_data.size());
141
142 state_ = FINISH_WRITE;
143
144 if (active_entry_->WriteData(0 /* index */,
145 0 /* offset */,
146 buffer,
147 write_data.size(),
148 io_callback_,
149 true /* truncate */) != ERR_IO_PENDING)
150 DoFinishWrite();
151 }
152
153 void DiskBasedCertCache::DoFinishWrite() {
154 if (!user_write_callback_.is_null()) {
Ryan Sleevi 2014/06/11 22:00:08 if (user_write_callback_.is_null()) return; ...
155 base::Callback<void(const std::string&)> callback = user_write_callback_;
156 callback.Run(Key());
157 reset_state();
158 }
159 }
160
161 void DiskBasedCertCache::reset_state() {
Ryan Sleevi 2014/06/11 22:00:08 Naming: This would be ResetState(), not reset_stat
162 state_ = NONE;
163 active_entry_->Close();
164 active_entry_ = NULL;
165 active_cert_handle_ = NULL;
166 user_write_callback_.Reset();
167 user_read_callback_.Reset();
168 }
169
170 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698