OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "chrome/browser/browsing_data_server_bound_cert_helper.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/message_loop.h" | |
11 #include "chrome/browser/profiles/profile.h" | |
12 #include "content/public/browser/browser_thread.h" | |
13 #include "net/base/server_bound_cert_service.h" | |
14 #include "net/url_request/url_request_context.h" | |
15 #include "net/url_request/url_request_context_getter.h" | |
16 | |
17 namespace { | |
18 | |
19 class BrowsingDataServerBoundCertHelperImpl | |
20 : public BrowsingDataServerBoundCertHelper { | |
21 public: | |
22 explicit BrowsingDataServerBoundCertHelperImpl(Profile* profile); | |
23 | |
24 // BrowsingDataServerBoundCertHelper methods. | |
25 virtual void StartFetching(const FetchResultCallback& callback) OVERRIDE; | |
26 virtual void DeleteServerBoundCert(const std::string& server_id) OVERRIDE; | |
27 | |
28 private: | |
29 virtual ~BrowsingDataServerBoundCertHelperImpl(); | |
30 | |
31 // Fetch the certs. This must be called in the IO thread. | |
32 void FetchOnIOThread(); | |
33 | |
34 // Notifies the completion callback. This must be called in the UI thread. | |
35 void NotifyInUIThread(); | |
36 | |
37 // Delete a single cert. This must be called in IO thread. | |
38 void DeleteOnIOThread(const std::string& server_id); | |
39 | |
40 // Access to |server_bound_cert_list_| is triggered indirectly via the UI | |
41 // thread and guarded by |is_fetching_|. This means |server_bound_cert_list_| | |
42 // is only accessed while |is_fetching_| is true. The flag |is_fetching_| is | |
43 // only accessed on the UI thread. | |
44 net::ServerBoundCertStore::ServerBoundCertList server_bound_cert_list_; | |
45 | |
46 // Indicates whether or not we're currently fetching information: | |
47 // it's true when StartFetching() is called in the UI thread, and it's reset | |
48 // after we notify the callback in the UI thread. | |
49 // This only mutates on the UI thread. | |
50 bool is_fetching_; | |
51 | |
52 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
53 | |
54 // This only mutates on the UI thread. | |
55 FetchResultCallback completion_callback_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(BrowsingDataServerBoundCertHelperImpl); | |
58 }; | |
59 | |
60 BrowsingDataServerBoundCertHelperImpl:: | |
61 BrowsingDataServerBoundCertHelperImpl(Profile* profile) | |
62 : is_fetching_(false), | |
63 request_context_getter_(profile->GetRequestContext()) { | |
64 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
65 } | |
66 | |
67 BrowsingDataServerBoundCertHelperImpl:: | |
68 ~BrowsingDataServerBoundCertHelperImpl() { | |
69 } | |
70 | |
71 void BrowsingDataServerBoundCertHelperImpl::StartFetching( | |
72 const FetchResultCallback& callback) { | |
73 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
74 DCHECK(!is_fetching_); | |
75 DCHECK(!callback.is_null()); | |
76 DCHECK(completion_callback_.is_null()); | |
77 is_fetching_ = true; | |
78 completion_callback_ = callback; | |
79 content::BrowserThread::PostTask( | |
80 content::BrowserThread::IO, FROM_HERE, | |
81 base::Bind(&BrowsingDataServerBoundCertHelperImpl::FetchOnIOThread, | |
82 this)); | |
83 } | |
84 | |
85 void BrowsingDataServerBoundCertHelperImpl::DeleteServerBoundCert( | |
86 const std::string& server_id) { | |
87 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
88 content::BrowserThread::PostTask( | |
89 content::BrowserThread::IO, FROM_HERE, | |
90 base::Bind(&BrowsingDataServerBoundCertHelperImpl::DeleteOnIOThread, | |
91 this, server_id)); | |
92 } | |
93 | |
94 void BrowsingDataServerBoundCertHelperImpl::FetchOnIOThread() { | |
95 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
96 net::ServerBoundCertStore* cert_store = | |
97 request_context_getter_->GetURLRequestContext()-> | |
98 server_bound_cert_service()->GetCertStore(); | |
99 if (cert_store) { | |
100 server_bound_cert_list_.clear(); | |
101 cert_store->GetAllServerBoundCerts(&server_bound_cert_list_); | |
102 content::BrowserThread::PostTask( | |
103 content::BrowserThread::UI, FROM_HERE, | |
104 base::Bind(&BrowsingDataServerBoundCertHelperImpl::NotifyInUIThread, | |
105 this)); | |
106 } | |
107 } | |
108 | |
109 void BrowsingDataServerBoundCertHelperImpl::NotifyInUIThread() { | |
110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
111 DCHECK(is_fetching_); | |
112 is_fetching_ = false; | |
113 completion_callback_.Run(server_bound_cert_list_); | |
114 completion_callback_.Reset(); | |
115 } | |
116 | |
117 void BrowsingDataServerBoundCertHelperImpl::DeleteOnIOThread( | |
118 const std::string& server_id) { | |
119 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
120 net::ServerBoundCertStore* cert_store = | |
121 request_context_getter_->GetURLRequestContext()-> | |
122 server_bound_cert_service()->GetCertStore(); | |
123 if (cert_store) | |
124 cert_store->DeleteServerBoundCert(server_id); | |
125 } | |
126 | |
127 } // namespace | |
128 | |
129 // static | |
130 BrowsingDataServerBoundCertHelper* | |
131 BrowsingDataServerBoundCertHelper::Create(Profile* profile) { | |
132 return new BrowsingDataServerBoundCertHelperImpl(profile); | |
133 } | |
134 | |
135 CannedBrowsingDataServerBoundCertHelper:: | |
136 CannedBrowsingDataServerBoundCertHelper() {} | |
137 | |
138 CannedBrowsingDataServerBoundCertHelper:: | |
139 ~CannedBrowsingDataServerBoundCertHelper() {} | |
140 | |
141 CannedBrowsingDataServerBoundCertHelper* | |
142 CannedBrowsingDataServerBoundCertHelper::Clone() { | |
143 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
144 CannedBrowsingDataServerBoundCertHelper* clone = | |
145 new CannedBrowsingDataServerBoundCertHelper(); | |
146 | |
147 clone->server_bound_cert_map_ = server_bound_cert_map_; | |
148 return clone; | |
149 } | |
150 | |
151 void CannedBrowsingDataServerBoundCertHelper::AddServerBoundCert( | |
152 const net::ServerBoundCertStore::ServerBoundCert& server_bound_cert) { | |
153 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
154 server_bound_cert_map_[server_bound_cert.server_identifier()] = | |
155 server_bound_cert; | |
156 } | |
157 | |
158 void CannedBrowsingDataServerBoundCertHelper::Reset() { | |
159 server_bound_cert_map_.clear(); | |
160 } | |
161 | |
162 bool CannedBrowsingDataServerBoundCertHelper::empty() const { | |
163 return server_bound_cert_map_.empty(); | |
164 } | |
165 | |
166 size_t CannedBrowsingDataServerBoundCertHelper::GetCertCount() const { | |
167 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
168 return server_bound_cert_map_.size(); | |
169 } | |
170 | |
171 void CannedBrowsingDataServerBoundCertHelper::StartFetching( | |
172 const FetchResultCallback& callback) { | |
173 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
174 if (callback.is_null()) | |
175 return; | |
176 // We post a task to emulate async fetching behavior. | |
177 completion_callback_ = callback; | |
178 MessageLoop::current()->PostTask( | |
179 FROM_HERE, | |
180 base::Bind(&CannedBrowsingDataServerBoundCertHelper::FinishFetching, | |
181 this)); | |
182 } | |
183 | |
184 void CannedBrowsingDataServerBoundCertHelper::FinishFetching() { | |
185 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
186 net::ServerBoundCertStore::ServerBoundCertList cert_list; | |
187 for (ServerBoundCertMap::iterator i = server_bound_cert_map_.begin(); | |
188 i != server_bound_cert_map_.end(); ++i) | |
189 cert_list.push_back(i->second); | |
190 completion_callback_.Run(cert_list); | |
191 } | |
192 | |
193 void CannedBrowsingDataServerBoundCertHelper::DeleteServerBoundCert( | |
194 const std::string& server_id) { | |
195 NOTREACHED(); | |
196 } | |
OLD | NEW |