1 // Copyright (c) 2011 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/transport_security_persister.h"
8 #include "base/file_path.h"
9 #include "base/file_util.h"
10 #include "base/message_loop.h"
11 #include "base/path_service.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "net/base/transport_security_state.h"
16 using content::BrowserThread
;
18 class TransportSecurityPersister::Loader
{
20 Loader(const base::WeakPtr
<TransportSecurityPersister
>& persister
,
22 : persister_(persister
),
28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
29 state_valid_
= file_util::ReadFileToString(path_
, &state_
);
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
35 // Make sure we're deleted.
36 scoped_ptr
<Loader
> deleter(this);
38 if (!persister_
|| !state_valid_
)
40 persister_
->CompleteLoad(state_
);
44 base::WeakPtr
<TransportSecurityPersister
> persister_
;
51 DISALLOW_COPY_AND_ASSIGN(Loader
);
54 TransportSecurityPersister::TransportSecurityPersister(
55 net::TransportSecurityState
* state
,
56 const FilePath
& profile_path
,
58 : transport_security_state_(state
),
59 writer_(profile_path
.AppendASCII("TransportSecurity"),
60 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)),
62 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
65 transport_security_state_
->SetDelegate(this);
67 Loader
* loader
= new Loader(weak_ptr_factory_
.GetWeakPtr(), writer_
.path());
68 BrowserThread::PostTaskAndReply(
69 BrowserThread::FILE, FROM_HERE
,
70 base::Bind(&Loader::Load
, base::Unretained(loader
)),
71 base::Bind(&Loader::CompleteLoad
, base::Unretained(loader
)));
74 TransportSecurityPersister::~TransportSecurityPersister() {
75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
77 if (writer_
.HasPendingWrite())
78 writer_
.DoScheduledWrite();
80 transport_security_state_
->SetDelegate(NULL
);
83 void TransportSecurityPersister::CompleteLoad(const std::string
& state
) {
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
87 if (!transport_security_state_
->LoadEntries(state
, &dirty
)) {
88 LOG(ERROR
) << "Failed to deserialize state: " << state
;
92 StateIsDirty(transport_security_state_
);
95 void TransportSecurityPersister::StateIsDirty(
96 net::TransportSecurityState
* state
) {
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
98 DCHECK_EQ(transport_security_state_
, state
);
101 writer_
.ScheduleWrite(this);
104 bool TransportSecurityPersister::SerializeData(std::string
* data
) {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
106 return transport_security_state_
->Serialise(data
);