OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/transport_security_persister.h" | 5 #include "chrome/browser/transport_security_persister.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/file_path.h" | 8 #include "base/file_path.h" |
8 #include "base/file_util.h" | 9 #include "base/file_util.h" |
9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
10 #include "base/path_service.h" | 11 #include "base/path_service.h" |
11 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
12 #include "content/browser/browser_thread.h" | 13 #include "content/browser/browser_thread.h" |
13 #include "net/base/transport_security_state.h" | 14 #include "net/base/transport_security_state.h" |
14 | 15 |
| 16 class TransportSecurityPersister::Loader { |
| 17 public: |
| 18 Loader(const base::WeakPtr<TransportSecurityPersister>& persister, |
| 19 const FilePath& path) |
| 20 : persister_(persister), |
| 21 path_(path), |
| 22 state_valid_(false) { |
| 23 } |
| 24 |
| 25 void Load() { |
| 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 27 state_valid_ = file_util::ReadFileToString(path_, &state_); |
| 28 } |
| 29 |
| 30 void CompleteLoad() { |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 32 |
| 33 // Make sure we're deleted. |
| 34 scoped_ptr<Loader> deleter(this); |
| 35 |
| 36 if (!persister_ || !state_valid_) |
| 37 return; |
| 38 persister_->CompleteLoad(state_); |
| 39 } |
| 40 |
| 41 private: |
| 42 base::WeakPtr<TransportSecurityPersister> persister_; |
| 43 |
| 44 FilePath path_; |
| 45 |
| 46 std::string state_; |
| 47 bool state_valid_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(Loader); |
| 50 }; |
| 51 |
15 TransportSecurityPersister::TransportSecurityPersister( | 52 TransportSecurityPersister::TransportSecurityPersister( |
16 net::TransportSecurityState* state, | 53 net::TransportSecurityState* state, |
17 const FilePath& profile_path, | 54 const FilePath& profile_path, |
18 bool readonly) | 55 bool readonly) |
19 : transport_security_state_(state), | 56 : transport_security_state_(state), |
20 writer_(profile_path.AppendASCII("TransportSecurity"), | 57 writer_(profile_path.AppendASCII("TransportSecurity"), |
21 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)), | 58 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)), |
22 readonly_(readonly) { | 59 readonly_(readonly), |
23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 60 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
24 | 62 |
25 transport_security_state_->SetDelegate(this); | 63 transport_security_state_->SetDelegate(this); |
| 64 |
| 65 Loader* loader = new Loader(weak_ptr_factory_.GetWeakPtr(), writer_.path()); |
| 66 BrowserThread::PostTaskAndReply( |
| 67 BrowserThread::FILE, FROM_HERE, |
| 68 base::Bind(&Loader::Load, base::Unretained(loader)), |
| 69 base::Bind(&Loader::CompleteLoad, base::Unretained(loader))); |
26 } | 70 } |
27 | 71 |
28 TransportSecurityPersister::~TransportSecurityPersister() { | 72 TransportSecurityPersister::~TransportSecurityPersister() { |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 74 |
29 if (writer_.HasPendingWrite()) | 75 if (writer_.HasPendingWrite()) |
30 writer_.DoScheduledWrite(); | 76 writer_.DoScheduledWrite(); |
31 | 77 |
32 transport_security_state_->SetDelegate(NULL); | 78 transport_security_state_->SetDelegate(NULL); |
33 } | 79 } |
34 | 80 |
35 void TransportSecurityPersister::Init() { | |
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
37 BrowserThread::PostTask( | |
38 BrowserThread::FILE, FROM_HERE, | |
39 NewRunnableMethod(this, &TransportSecurityPersister::Load)); | |
40 } | |
41 | |
42 void TransportSecurityPersister::Load() { | |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
44 | |
45 std::string state; | |
46 if (!file_util::ReadFileToString(writer_.path(), &state)) | |
47 return; | |
48 | |
49 BrowserThread::PostTask( | |
50 BrowserThread::IO, FROM_HERE, | |
51 NewRunnableMethod(this, | |
52 &TransportSecurityPersister::CompleteLoad, | |
53 state)); | |
54 } | |
55 | |
56 void TransportSecurityPersister::CompleteLoad(const std::string& state) { | 81 void TransportSecurityPersister::CompleteLoad(const std::string& state) { |
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
58 | 83 |
59 bool dirty = false; | 84 bool dirty = false; |
60 if (!transport_security_state_->LoadEntries(state, &dirty)) { | 85 if (!transport_security_state_->LoadEntries(state, &dirty)) { |
61 LOG(ERROR) << "Failed to deserialize state: " << state; | 86 LOG(ERROR) << "Failed to deserialize state: " << state; |
62 return; | 87 return; |
63 } | 88 } |
64 if (dirty) | 89 if (dirty) |
65 StateIsDirty(transport_security_state_); | 90 StateIsDirty(transport_security_state_); |
66 } | 91 } |
67 | 92 |
68 void TransportSecurityPersister::StateIsDirty( | 93 void TransportSecurityPersister::StateIsDirty( |
69 net::TransportSecurityState* state) { | 94 net::TransportSecurityState* state) { |
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
71 DCHECK_EQ(transport_security_state_, state); | 96 DCHECK_EQ(transport_security_state_, state); |
72 | 97 |
73 if (!readonly_) | 98 if (!readonly_) |
74 writer_.ScheduleWrite(this); | 99 writer_.ScheduleWrite(this); |
75 } | 100 } |
76 | 101 |
77 bool TransportSecurityPersister::SerializeData(std::string* data) { | 102 bool TransportSecurityPersister::SerializeData(std::string* data) { |
| 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
78 return transport_security_state_->Serialise(data); | 104 return transport_security_state_->Serialise(data); |
79 } | 105 } |
OLD | NEW |