Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / browsing_data / browsing_data_channel_id_helper.cc
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.
4
5 #include "chrome/browser/browsing_data/browsing_data_channel_id_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/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"
16
17 using content::BrowserThread;
18
19 namespace {
20
21 class BrowsingDataChannelIDHelperImpl
22     : public BrowsingDataChannelIDHelper {
23  public:
24   explicit BrowsingDataChannelIDHelperImpl(Profile* profile);
25
26   // BrowsingDataChannelIDHelper methods.
27   virtual void StartFetching(const FetchResultCallback& callback) OVERRIDE;
28   virtual void DeleteChannelID(const std::string& server_id) OVERRIDE;
29
30  private:
31   virtual ~BrowsingDataChannelIDHelperImpl();
32
33   // Fetch the certs. This must be called in the IO thread.
34   void FetchOnIOThread();
35
36   void OnFetchComplete(
37       const net::ChannelIDStore::ChannelIDList& channel_id_list);
38
39   // Notifies the completion callback. This must be called in the UI thread.
40   void NotifyInUIThread(
41       const net::ChannelIDStore::ChannelIDList& channel_id_list);
42
43   // Delete a single cert. This must be called in IO thread.
44   void DeleteOnIOThread(const std::string& server_id);
45
46   // Called when deletion is done.
47   void DeleteCallback();
48
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.
53   bool is_fetching_;
54
55   scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
56
57   // This member is only mutated on the UI thread.
58   FetchResultCallback completion_callback_;
59
60   DISALLOW_COPY_AND_ASSIGN(BrowsingDataChannelIDHelperImpl);
61 };
62
63 BrowsingDataChannelIDHelperImpl::
64 BrowsingDataChannelIDHelperImpl(Profile* profile)
65     : is_fetching_(false),
66       request_context_getter_(profile->GetRequestContext()) {
67   DCHECK_CURRENTLY_ON(BrowserThread::UI);
68 }
69
70 BrowsingDataChannelIDHelperImpl::
71 ~BrowsingDataChannelIDHelperImpl() {
72 }
73
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());
80   is_fetching_ = true;
81   completion_callback_ = callback;
82   BrowserThread::PostTask(
83       BrowserThread::IO,
84       FROM_HERE,
85       base::Bind(&BrowsingDataChannelIDHelperImpl::FetchOnIOThread, this));
86 }
87
88 void BrowsingDataChannelIDHelperImpl::DeleteChannelID(
89     const std::string& server_id) {
90   DCHECK_CURRENTLY_ON(BrowserThread::UI);
91   BrowserThread::PostTask(
92       BrowserThread::IO,
93       FROM_HERE,
94       base::Bind(
95           &BrowsingDataChannelIDHelperImpl::DeleteOnIOThread, this, server_id));
96 }
97
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();
103   if (cert_store) {
104     cert_store->GetAllChannelIDs(base::Bind(
105         &BrowsingDataChannelIDHelperImpl::OnFetchComplete, this));
106   } else {
107     OnFetchComplete(net::ChannelIDStore::ChannelIDList());
108   }
109 }
110
111 void BrowsingDataChannelIDHelperImpl::OnFetchComplete(
112     const net::ChannelIDStore::ChannelIDList& channel_id_list) {
113   DCHECK_CURRENTLY_ON(BrowserThread::IO);
114   BrowserThread::PostTask(
115       BrowserThread::UI,
116       FROM_HERE,
117       base::Bind(&BrowsingDataChannelIDHelperImpl::NotifyInUIThread,
118                  this,
119                  channel_id_list));
120 }
121
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();
129 }
130
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();
137   if (cert_store) {
138     cert_store->DeleteChannelID(
139         server_id,
140         base::Bind(&BrowsingDataChannelIDHelperImpl::DeleteCallback,
141                    this));
142   }
143 }
144
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
148   // are deleting.
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();
153 }
154
155 }  // namespace
156
157 // static
158 BrowsingDataChannelIDHelper*
159 BrowsingDataChannelIDHelper::Create(Profile* profile) {
160   return new BrowsingDataChannelIDHelperImpl(profile);
161 }
162
163 CannedBrowsingDataChannelIDHelper::
164 CannedBrowsingDataChannelIDHelper() {}
165
166 CannedBrowsingDataChannelIDHelper::
167 ~CannedBrowsingDataChannelIDHelper() {}
168
169 CannedBrowsingDataChannelIDHelper*
170 CannedBrowsingDataChannelIDHelper::Clone() {
171   DCHECK_CURRENTLY_ON(BrowserThread::UI);
172   CannedBrowsingDataChannelIDHelper* clone =
173       new CannedBrowsingDataChannelIDHelper();
174
175   clone->channel_id_map_ = channel_id_map_;
176   return clone;
177 }
178
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()] =
183       channel_id;
184 }
185
186 void CannedBrowsingDataChannelIDHelper::Reset() {
187   channel_id_map_.clear();
188 }
189
190 bool CannedBrowsingDataChannelIDHelper::empty() const {
191   return channel_id_map_.empty();
192 }
193
194 size_t CannedBrowsingDataChannelIDHelper::GetChannelIDCount() const {
195   DCHECK_CURRENTLY_ON(BrowserThread::UI);
196   return channel_id_map_.size();
197 }
198
199 void CannedBrowsingDataChannelIDHelper::StartFetching(
200     const FetchResultCallback& callback) {
201   DCHECK_CURRENTLY_ON(BrowserThread::UI);
202   if (callback.is_null())
203     return;
204   // We post a task to emulate async fetching behavior.
205   completion_callback_ = callback;
206   base::MessageLoop::current()->PostTask(
207       FROM_HERE,
208       base::Bind(&CannedBrowsingDataChannelIDHelper::FinishFetching,
209                  this));
210 }
211
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);
219 }
220
221 void CannedBrowsingDataChannelIDHelper::DeleteChannelID(
222     const std::string& server_id) {
223   NOTREACHED();
224 }