chromeos: bluetooth: tie Proxy lifetime to object, not observer
[chromium-blink-merge.git] / chrome / browser / transport_security_persister.cc
blob36ccbffd5ba57e6ba042258146d354e25121e58d
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"
7 #include "base/bind.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 {
19 public:
20 Loader(const base::WeakPtr<TransportSecurityPersister>& persister,
21 const FilePath& path)
22 : persister_(persister),
23 path_(path),
24 state_valid_(false) {
27 void Load() {
28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
29 state_valid_ = file_util::ReadFileToString(path_, &state_);
32 void CompleteLoad() {
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
35 // Make sure we're deleted.
36 scoped_ptr<Loader> deleter(this);
38 if (!persister_ || !state_valid_)
39 return;
40 persister_->CompleteLoad(state_);
43 private:
44 base::WeakPtr<TransportSecurityPersister> persister_;
46 FilePath path_;
48 std::string state_;
49 bool state_valid_;
51 DISALLOW_COPY_AND_ASSIGN(Loader);
54 TransportSecurityPersister::TransportSecurityPersister(
55 net::TransportSecurityState* state,
56 const FilePath& profile_path,
57 bool readonly)
58 : transport_security_state_(state),
59 writer_(profile_path.AppendASCII("TransportSecurity"),
60 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)),
61 readonly_(readonly),
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));
86 bool dirty = false;
87 if (!transport_security_state_->LoadEntries(state, &dirty)) {
88 LOG(ERROR) << "Failed to deserialize state: " << state;
89 return;
91 if (dirty)
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);
100 if (!readonly_)
101 writer_.ScheduleWrite(this);
104 bool TransportSecurityPersister::SerializeData(std::string* data) {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
106 return transport_security_state_->Serialise(data);