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
3 // found in the LICENSE file.
5 #include "chrome/browser/browsing_data/browsing_data_channel_id_helper.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "net/ssl/channel_id_service.h"
14 #include "net/url_request/url_request_context.h"
15 #include "net/url_request/url_request_context_getter.h"
17 using content::BrowserThread;
21 class BrowsingDataChannelIDHelperImpl
22 : public BrowsingDataChannelIDHelper {
24 explicit BrowsingDataChannelIDHelperImpl(Profile* profile);
26 // BrowsingDataChannelIDHelper methods.
27 virtual void StartFetching(const FetchResultCallback& callback) OVERRIDE;
28 virtual void DeleteChannelID(const std::string& server_id) OVERRIDE;
31 virtual ~BrowsingDataChannelIDHelperImpl();
33 // Fetch the certs. This must be called in the IO thread.
34 void FetchOnIOThread();
37 const net::ChannelIDStore::ChannelIDList& channel_id_list);
39 // Notifies the completion callback. This must be called in the UI thread.
40 void NotifyInUIThread(
41 const net::ChannelIDStore::ChannelIDList& channel_id_list);
43 // Delete a single cert. This must be called in IO thread.
44 void DeleteOnIOThread(const std::string& server_id);
46 // Called when deletion is done.
47 void DeleteCallback();
49 // Indicates whether or not we're currently fetching information:
50 // it's true when StartFetching() is called in the UI thread, and it's reset
51 // after we notify the callback in the UI thread.
52 // This member is only mutated on the UI thread.
55 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
57 // This member is only mutated on the UI thread.
58 FetchResultCallback completion_callback_;
60 DISALLOW_COPY_AND_ASSIGN(BrowsingDataChannelIDHelperImpl);
63 BrowsingDataChannelIDHelperImpl::
64 BrowsingDataChannelIDHelperImpl(Profile* profile)
65 : is_fetching_(false),
66 request_context_getter_(profile->GetRequestContext()) {
67 DCHECK_CURRENTLY_ON(BrowserThread::UI);
70 BrowsingDataChannelIDHelperImpl::
71 ~BrowsingDataChannelIDHelperImpl() {
74 void BrowsingDataChannelIDHelperImpl::StartFetching(
75 const FetchResultCallback& callback) {
76 DCHECK_CURRENTLY_ON(BrowserThread::UI);
77 DCHECK(!is_fetching_);
78 DCHECK(!callback.is_null());
79 DCHECK(completion_callback_.is_null());
81 completion_callback_ = callback;
82 BrowserThread::PostTask(
85 base::Bind(&BrowsingDataChannelIDHelperImpl::FetchOnIOThread, this));
88 void BrowsingDataChannelIDHelperImpl::DeleteChannelID(
89 const std::string& server_id) {
90 DCHECK_CURRENTLY_ON(BrowserThread::UI);
91 BrowserThread::PostTask(
95 &BrowsingDataChannelIDHelperImpl::DeleteOnIOThread, this, server_id));
98 void BrowsingDataChannelIDHelperImpl::FetchOnIOThread() {
99 DCHECK_CURRENTLY_ON(BrowserThread::IO);
100 net::ChannelIDStore* cert_store =
101 request_context_getter_->GetURLRequestContext()->
102 channel_id_service()->GetChannelIDStore();
104 cert_store->GetAllChannelIDs(base::Bind(
105 &BrowsingDataChannelIDHelperImpl::OnFetchComplete, this));
107 OnFetchComplete(net::ChannelIDStore::ChannelIDList());
111 void BrowsingDataChannelIDHelperImpl::OnFetchComplete(
112 const net::ChannelIDStore::ChannelIDList& channel_id_list) {
113 DCHECK_CURRENTLY_ON(BrowserThread::IO);
114 BrowserThread::PostTask(
117 base::Bind(&BrowsingDataChannelIDHelperImpl::NotifyInUIThread,
122 void BrowsingDataChannelIDHelperImpl::NotifyInUIThread(
123 const net::ChannelIDStore::ChannelIDList& channel_id_list) {
124 DCHECK_CURRENTLY_ON(BrowserThread::UI);
125 DCHECK(is_fetching_);
126 is_fetching_ = false;
127 completion_callback_.Run(channel_id_list);
128 completion_callback_.Reset();
131 void BrowsingDataChannelIDHelperImpl::DeleteOnIOThread(
132 const std::string& server_id) {
133 DCHECK_CURRENTLY_ON(BrowserThread::IO);
134 net::ChannelIDStore* cert_store =
135 request_context_getter_->GetURLRequestContext()->
136 channel_id_service()->GetChannelIDStore();
138 cert_store->DeleteChannelID(
140 base::Bind(&BrowsingDataChannelIDHelperImpl::DeleteCallback,
145 void BrowsingDataChannelIDHelperImpl::DeleteCallback() {
146 DCHECK_CURRENTLY_ON(BrowserThread::IO);
147 // Need to close open SSL connections which may be using the channel ids we
149 // TODO(mattm): http://crbug.com/166069 Make the server bound cert
150 // service/store have observers that can notify relevant things directly.
151 request_context_getter_->GetURLRequestContext()->ssl_config_service()->
152 NotifySSLConfigChange();
158 BrowsingDataChannelIDHelper*
159 BrowsingDataChannelIDHelper::Create(Profile* profile) {
160 return new BrowsingDataChannelIDHelperImpl(profile);
163 CannedBrowsingDataChannelIDHelper::
164 CannedBrowsingDataChannelIDHelper() {}
166 CannedBrowsingDataChannelIDHelper::
167 ~CannedBrowsingDataChannelIDHelper() {}
169 CannedBrowsingDataChannelIDHelper*
170 CannedBrowsingDataChannelIDHelper::Clone() {
171 DCHECK_CURRENTLY_ON(BrowserThread::UI);
172 CannedBrowsingDataChannelIDHelper* clone =
173 new CannedBrowsingDataChannelIDHelper();
175 clone->channel_id_map_ = channel_id_map_;
179 void CannedBrowsingDataChannelIDHelper::AddChannelID(
180 const net::ChannelIDStore::ChannelID& channel_id) {
181 DCHECK_CURRENTLY_ON(BrowserThread::UI);
182 channel_id_map_[channel_id.server_identifier()] =
186 void CannedBrowsingDataChannelIDHelper::Reset() {
187 channel_id_map_.clear();
190 bool CannedBrowsingDataChannelIDHelper::empty() const {
191 return channel_id_map_.empty();
194 size_t CannedBrowsingDataChannelIDHelper::GetChannelIDCount() const {
195 DCHECK_CURRENTLY_ON(BrowserThread::UI);
196 return channel_id_map_.size();
199 void CannedBrowsingDataChannelIDHelper::StartFetching(
200 const FetchResultCallback& callback) {
201 DCHECK_CURRENTLY_ON(BrowserThread::UI);
202 if (callback.is_null())
204 // We post a task to emulate async fetching behavior.
205 completion_callback_ = callback;
206 base::MessageLoop::current()->PostTask(
208 base::Bind(&CannedBrowsingDataChannelIDHelper::FinishFetching,
212 void CannedBrowsingDataChannelIDHelper::FinishFetching() {
213 DCHECK_CURRENTLY_ON(BrowserThread::UI);
214 net::ChannelIDStore::ChannelIDList channel_id_list;
215 for (ChannelIDMap::iterator i = channel_id_map_.begin();
216 i != channel_id_map_.end(); ++i)
217 channel_id_list.push_back(i->second);
218 completion_callback_.Run(channel_id_list);
221 void CannedBrowsingDataChannelIDHelper::DeleteChannelID(
222 const std::string& server_id) {