Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_channel_id_helper.cc
blob2f76bebef07d0ee422b127c39117c50b70da5060
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"
7 #include "base/bind.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;
20 namespace {
22 class BrowsingDataChannelIDHelperImpl
23 : public BrowsingDataChannelIDHelper {
24 public:
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;
32 private:
33 ~BrowsingDataChannelIDHelperImpl() override;
35 // Fetch the certs. This must be called in the IO thread.
36 void FetchOnIOThread(const FetchResultCallback& callback);
38 void OnFetchComplete(
39 const FetchResultCallback& callback,
40 const net::ChannelIDStore::ChannelIDList& channel_id_list);
42 // Delete a single cert. This must be called in IO thread.
43 void DeleteOnIOThread(const std::string& server_id);
45 // Called when deletion is done.
46 void DeleteCallback();
48 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
50 DISALLOW_COPY_AND_ASSIGN(BrowsingDataChannelIDHelperImpl);
53 BrowsingDataChannelIDHelperImpl::BrowsingDataChannelIDHelperImpl(
54 net::URLRequestContextGetter* request_context)
55 : request_context_getter_(request_context) {
56 DCHECK_CURRENTLY_ON(BrowserThread::UI);
59 BrowsingDataChannelIDHelperImpl::
60 ~BrowsingDataChannelIDHelperImpl() {
63 void BrowsingDataChannelIDHelperImpl::StartFetching(
64 const FetchResultCallback& callback) {
65 DCHECK_CURRENTLY_ON(BrowserThread::UI);
66 DCHECK(!callback.is_null());
67 BrowserThread::PostTask(
68 BrowserThread::IO, FROM_HERE,
69 base::Bind(&BrowsingDataChannelIDHelperImpl::FetchOnIOThread, this,
70 callback));
73 void BrowsingDataChannelIDHelperImpl::DeleteChannelID(
74 const std::string& server_id) {
75 DCHECK_CURRENTLY_ON(BrowserThread::UI);
76 BrowserThread::PostTask(
77 BrowserThread::IO,
78 FROM_HERE,
79 base::Bind(
80 &BrowsingDataChannelIDHelperImpl::DeleteOnIOThread, this, server_id));
83 void BrowsingDataChannelIDHelperImpl::FetchOnIOThread(
84 const FetchResultCallback& callback) {
85 DCHECK_CURRENTLY_ON(BrowserThread::IO);
86 DCHECK(!callback.is_null());
88 net::ChannelIDStore* cert_store =
89 request_context_getter_->GetURLRequestContext()->
90 channel_id_service()->GetChannelIDStore();
91 if (cert_store) {
92 cert_store->GetAllChannelIDs(base::Bind(
93 &BrowsingDataChannelIDHelperImpl::OnFetchComplete, this, callback));
94 } else {
95 OnFetchComplete(callback, net::ChannelIDStore::ChannelIDList());
99 void BrowsingDataChannelIDHelperImpl::OnFetchComplete(
100 const FetchResultCallback& callback,
101 const net::ChannelIDStore::ChannelIDList& channel_id_list) {
102 DCHECK_CURRENTLY_ON(BrowserThread::IO);
103 DCHECK(!callback.is_null());
104 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
105 base::Bind(callback, channel_id_list));
108 void BrowsingDataChannelIDHelperImpl::DeleteOnIOThread(
109 const std::string& server_id) {
110 DCHECK_CURRENTLY_ON(BrowserThread::IO);
111 net::ChannelIDStore* cert_store =
112 request_context_getter_->GetURLRequestContext()->
113 channel_id_service()->GetChannelIDStore();
114 if (cert_store) {
115 cert_store->DeleteChannelID(
116 server_id,
117 base::Bind(&BrowsingDataChannelIDHelperImpl::DeleteCallback,
118 this));
122 void BrowsingDataChannelIDHelperImpl::DeleteCallback() {
123 DCHECK_CURRENTLY_ON(BrowserThread::IO);
124 // Need to close open SSL connections which may be using the channel ids we
125 // are deleting.
126 // TODO(mattm): http://crbug.com/166069 Make the server bound cert
127 // service/store have observers that can notify relevant things directly.
128 request_context_getter_->GetURLRequestContext()->ssl_config_service()->
129 NotifySSLConfigChange();
132 } // namespace
134 // static
135 BrowsingDataChannelIDHelper* BrowsingDataChannelIDHelper::Create(
136 net::URLRequestContextGetter* request_context) {
137 return new BrowsingDataChannelIDHelperImpl(request_context);
140 CannedBrowsingDataChannelIDHelper::
141 CannedBrowsingDataChannelIDHelper() {}
143 CannedBrowsingDataChannelIDHelper::
144 ~CannedBrowsingDataChannelIDHelper() {}
146 void CannedBrowsingDataChannelIDHelper::AddChannelID(
147 const net::ChannelIDStore::ChannelID& channel_id) {
148 DCHECK_CURRENTLY_ON(BrowserThread::UI);
149 channel_id_map_[channel_id.server_identifier()] =
150 channel_id;
153 void CannedBrowsingDataChannelIDHelper::Reset() {
154 channel_id_map_.clear();
157 bool CannedBrowsingDataChannelIDHelper::empty() const {
158 return channel_id_map_.empty();
161 size_t CannedBrowsingDataChannelIDHelper::GetChannelIDCount() const {
162 DCHECK_CURRENTLY_ON(BrowserThread::UI);
163 return channel_id_map_.size();
166 void CannedBrowsingDataChannelIDHelper::StartFetching(
167 const FetchResultCallback& callback) {
168 DCHECK_CURRENTLY_ON(BrowserThread::UI);
169 if (callback.is_null())
170 return;
171 // We post a task to emulate async fetching behavior.
172 base::ThreadTaskRunnerHandle::Get()->PostTask(
173 FROM_HERE, base::Bind(&CannedBrowsingDataChannelIDHelper::FinishFetching,
174 this, callback));
177 void CannedBrowsingDataChannelIDHelper::FinishFetching(
178 const FetchResultCallback& callback) {
179 DCHECK_CURRENTLY_ON(BrowserThread::UI);
180 DCHECK(!callback.is_null());
181 net::ChannelIDStore::ChannelIDList channel_id_list;
182 for (const auto& pair : channel_id_map_)
183 channel_id_list.push_back(pair.second);
184 callback.Run(channel_id_list);
187 void CannedBrowsingDataChannelIDHelper::DeleteChannelID(
188 const std::string& server_id) {
189 NOTREACHED();