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(TransportSecurityPersister* persister, const FilePath& path) |
| 19 : persister_(persister->AsWeakPtr()), |
| 20 path_(path), |
| 21 state_valid_(false) { |
| 22 } |
| 23 |
| 24 void Load() { |
| 25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 26 state_valid_ = file_util::ReadFileToString(path_, &state_); |
| 27 } |
| 28 |
| 29 void CompleteLoad() { |
| 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 31 |
| 32 // Make sure we're deleted. |
| 33 scoped_ptr<Loader> deleter(this); |
| 34 |
| 35 if (!persister_ || !state_valid_) |
| 36 return; |
| 37 persister_->CompleteLoad(state_); |
| 38 } |
| 39 |
| 40 private: |
| 41 base::WeakPtr<TransportSecurityPersister> persister_; |
| 42 |
| 43 FilePath path_; |
| 44 |
| 45 std::string state_; |
| 46 bool state_valid_; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(Loader); |
| 49 }; |
| 50 |
15 TransportSecurityPersister::TransportSecurityPersister( | 51 TransportSecurityPersister::TransportSecurityPersister( |
16 net::TransportSecurityState* state, | 52 net::TransportSecurityState* state, |
17 const FilePath& profile_path, | 53 const FilePath& profile_path, |
18 bool readonly) | 54 bool readonly) |
19 : transport_security_state_(state), | 55 : transport_security_state_(state), |
20 writer_(profile_path.AppendASCII("TransportSecurity"), | 56 writer_(profile_path.AppendASCII("TransportSecurity"), |
21 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)), | 57 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)), |
22 readonly_(readonly) { | 58 readonly_(readonly) { |
23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
24 | 60 |
25 transport_security_state_->SetDelegate(this); | 61 transport_security_state_->SetDelegate(this); |
| 62 |
| 63 Loader* loader = new Loader(this, writer_.path()); |
| 64 BrowserThread::PostTaskAndReply( |
| 65 BrowserThread::FILE, FROM_HERE, |
| 66 base::Bind(&Loader::Load, base::Unretained(loader)), |
| 67 base::Bind(&Loader::CompleteLoad, base::Unretained(loader))); |
26 } | 68 } |
27 | 69 |
28 TransportSecurityPersister::~TransportSecurityPersister() { | 70 TransportSecurityPersister::~TransportSecurityPersister() { |
| 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 72 |
29 if (writer_.HasPendingWrite()) | 73 if (writer_.HasPendingWrite()) |
30 writer_.DoScheduledWrite(); | 74 writer_.DoScheduledWrite(); |
31 | 75 |
32 transport_security_state_->SetDelegate(NULL); | 76 transport_security_state_->SetDelegate(NULL); |
33 } | 77 } |
34 | 78 |
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) { | 79 void TransportSecurityPersister::CompleteLoad(const std::string& state) { |
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
58 | 81 |
59 bool dirty = false; | 82 bool dirty = false; |
60 if (!transport_security_state_->LoadEntries(state, &dirty)) { | 83 if (!transport_security_state_->LoadEntries(state, &dirty)) { |
61 LOG(ERROR) << "Failed to deserialize state: " << state; | 84 LOG(ERROR) << "Failed to deserialize state: " << state; |
62 return; | 85 return; |
63 } | 86 } |
64 if (dirty) | 87 if (dirty) |
65 StateIsDirty(transport_security_state_); | 88 StateIsDirty(transport_security_state_); |
66 } | 89 } |
67 | 90 |
68 void TransportSecurityPersister::StateIsDirty( | 91 void TransportSecurityPersister::StateIsDirty( |
69 net::TransportSecurityState* state) { | 92 net::TransportSecurityState* state) { |
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
71 DCHECK_EQ(transport_security_state_, state); | 94 DCHECK_EQ(transport_security_state_, state); |
72 | 95 |
73 if (!readonly_) | 96 if (!readonly_) |
74 writer_.ScheduleWrite(this); | 97 writer_.ScheduleWrite(this); |
75 } | 98 } |
76 | 99 |
77 bool TransportSecurityPersister::SerializeData(std::string* data) { | 100 bool TransportSecurityPersister::SerializeData(std::string* data) { |
| 101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
78 return transport_security_state_->Serialise(data); | 102 return transport_security_state_->Serialise(data); |
79 } | 103 } |
OLD | NEW |