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/location.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "net/ssl/channel_id_service.h"
15 #include "net/url_request/url_request_context.h"
16 #include "net/url_request/url_request_context_getter.h"
18 using content::BrowserThread
;
22 class BrowsingDataChannelIDHelperImpl
23 : public BrowsingDataChannelIDHelper
{
25 explicit BrowsingDataChannelIDHelperImpl(
26 net::URLRequestContextGetter
* request_context
);
28 // BrowsingDataChannelIDHelper methods.
29 void StartFetching(const FetchResultCallback
& callback
) override
;
30 void DeleteChannelID(const std::string
& server_id
) override
;
33 ~BrowsingDataChannelIDHelperImpl() override
;
35 // Fetch the certs. This must be called in the IO thread.
36 void FetchOnIOThread();
39 const net::ChannelIDStore::ChannelIDList
& channel_id_list
);
41 // Notifies the completion callback. This must be called in the UI thread.
42 void NotifyInUIThread(
43 const net::ChannelIDStore::ChannelIDList
& channel_id_list
);
45 // Delete a single cert. This must be called in IO thread.
46 void DeleteOnIOThread(const std::string
& server_id
);
48 // Called when deletion is done.
49 void DeleteCallback();
51 // Indicates whether or not we're currently fetching information:
52 // it's true when StartFetching() is called in the UI thread, and it's reset
53 // after we notify the callback in the UI thread.
54 // This member is only mutated on the UI thread.
55 bool is_fetching_
= false;
57 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
59 // This member is only mutated on the UI thread.
60 FetchResultCallback completion_callback_
;
62 DISALLOW_COPY_AND_ASSIGN(BrowsingDataChannelIDHelperImpl
);
65 BrowsingDataChannelIDHelperImpl::BrowsingDataChannelIDHelperImpl(
66 net::URLRequestContextGetter
* request_context
)
67 : request_context_getter_(request_context
) {
68 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
71 BrowsingDataChannelIDHelperImpl::
72 ~BrowsingDataChannelIDHelperImpl() {
75 void BrowsingDataChannelIDHelperImpl::StartFetching(
76 const FetchResultCallback
& callback
) {
77 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
78 DCHECK(!is_fetching_
);
79 DCHECK(!callback
.is_null());
80 DCHECK(completion_callback_
.is_null());
82 completion_callback_
= callback
;
83 BrowserThread::PostTask(
86 base::Bind(&BrowsingDataChannelIDHelperImpl::FetchOnIOThread
, this));
89 void BrowsingDataChannelIDHelperImpl::DeleteChannelID(
90 const std::string
& server_id
) {
91 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
92 BrowserThread::PostTask(
96 &BrowsingDataChannelIDHelperImpl::DeleteOnIOThread
, this, server_id
));
99 void BrowsingDataChannelIDHelperImpl::FetchOnIOThread() {
100 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
101 net::ChannelIDStore
* cert_store
=
102 request_context_getter_
->GetURLRequestContext()->
103 channel_id_service()->GetChannelIDStore();
105 cert_store
->GetAllChannelIDs(base::Bind(
106 &BrowsingDataChannelIDHelperImpl::OnFetchComplete
, this));
108 OnFetchComplete(net::ChannelIDStore::ChannelIDList());
112 void BrowsingDataChannelIDHelperImpl::OnFetchComplete(
113 const net::ChannelIDStore::ChannelIDList
& channel_id_list
) {
114 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
115 BrowserThread::PostTask(
118 base::Bind(&BrowsingDataChannelIDHelperImpl::NotifyInUIThread
,
123 void BrowsingDataChannelIDHelperImpl::NotifyInUIThread(
124 const net::ChannelIDStore::ChannelIDList
& channel_id_list
) {
125 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
126 DCHECK(is_fetching_
);
127 is_fetching_
= false;
128 completion_callback_
.Run(channel_id_list
);
129 completion_callback_
.Reset();
132 void BrowsingDataChannelIDHelperImpl::DeleteOnIOThread(
133 const std::string
& server_id
) {
134 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
135 net::ChannelIDStore
* cert_store
=
136 request_context_getter_
->GetURLRequestContext()->
137 channel_id_service()->GetChannelIDStore();
139 cert_store
->DeleteChannelID(
141 base::Bind(&BrowsingDataChannelIDHelperImpl::DeleteCallback
,
146 void BrowsingDataChannelIDHelperImpl::DeleteCallback() {
147 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
148 // Need to close open SSL connections which may be using the channel ids we
150 // TODO(mattm): http://crbug.com/166069 Make the server bound cert
151 // service/store have observers that can notify relevant things directly.
152 request_context_getter_
->GetURLRequestContext()->ssl_config_service()->
153 NotifySSLConfigChange();
159 BrowsingDataChannelIDHelper
* BrowsingDataChannelIDHelper::Create(
160 net::URLRequestContextGetter
* request_context
) {
161 return new BrowsingDataChannelIDHelperImpl(request_context
);
164 CannedBrowsingDataChannelIDHelper::
165 CannedBrowsingDataChannelIDHelper() {}
167 CannedBrowsingDataChannelIDHelper::
168 ~CannedBrowsingDataChannelIDHelper() {}
170 void CannedBrowsingDataChannelIDHelper::AddChannelID(
171 const net::ChannelIDStore::ChannelID
& channel_id
) {
172 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
173 channel_id_map_
[channel_id
.server_identifier()] =
177 void CannedBrowsingDataChannelIDHelper::Reset() {
178 channel_id_map_
.clear();
181 bool CannedBrowsingDataChannelIDHelper::empty() const {
182 return channel_id_map_
.empty();
185 size_t CannedBrowsingDataChannelIDHelper::GetChannelIDCount() const {
186 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
187 return channel_id_map_
.size();
190 void CannedBrowsingDataChannelIDHelper::StartFetching(
191 const FetchResultCallback
& callback
) {
192 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
193 if (callback
.is_null())
195 // We post a task to emulate async fetching behavior.
196 completion_callback_
= callback
;
197 base::ThreadTaskRunnerHandle::Get()->PostTask(
199 base::Bind(&CannedBrowsingDataChannelIDHelper::FinishFetching
, this));
202 void CannedBrowsingDataChannelIDHelper::FinishFetching() {
203 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
204 net::ChannelIDStore::ChannelIDList channel_id_list
;
205 for (const auto& pair
: channel_id_map_
)
206 channel_id_list
.push_back(pair
.second
);
207 completion_callback_
.Run(channel_id_list
);
210 void CannedBrowsingDataChannelIDHelper::DeleteChannelID(
211 const std::string
& server_id
) {