OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/strict_transport_security_persister.h" | 5 #include "chrome/browser/strict_transport_security_persister.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/thread.h" | 11 #include "chrome/browser/chrome_thread.h" |
12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
13 #include "net/base/strict_transport_security_state.h" | 13 #include "net/base/strict_transport_security_state.h" |
14 | 14 |
15 StrictTransportSecurityPersister::StrictTransportSecurityPersister( | 15 StrictTransportSecurityPersister::StrictTransportSecurityPersister( |
16 net::StrictTransportSecurityState* state, | 16 net::StrictTransportSecurityState* state, |
17 base::Thread* file_thread, | |
18 const FilePath& profile_path) | 17 const FilePath& profile_path) |
19 : state_is_dirty_(false), | 18 : state_is_dirty_(false), |
20 strict_transport_security_state_(state), | 19 strict_transport_security_state_(state), |
21 file_thread_(file_thread), | |
22 state_file_(profile_path.Append( | 20 state_file_(profile_path.Append( |
23 FILE_PATH_LITERAL("StrictTransportSecurity"))) { | 21 FILE_PATH_LITERAL("StrictTransportSecurity"))) { |
24 state->SetDelegate(this); | 22 state->SetDelegate(this); |
25 | 23 |
26 Task* task = NewRunnableMethod(this, | 24 Task* task = NewRunnableMethod(this, |
27 &StrictTransportSecurityPersister::LoadState); | 25 &StrictTransportSecurityPersister::LoadState); |
28 file_thread->message_loop()->PostDelayedTask(FROM_HERE, task, | 26 ChromeThread::PostDelayedTask(ChromeThread::FILE, FROM_HERE, task, 1000); |
29 1000 /* 1 second */); | |
30 } | 27 } |
31 | 28 |
32 StrictTransportSecurityPersister::~StrictTransportSecurityPersister() { | 29 StrictTransportSecurityPersister::~StrictTransportSecurityPersister() { |
33 strict_transport_security_state_->SetDelegate(NULL); | 30 strict_transport_security_state_->SetDelegate(NULL); |
34 } | 31 } |
35 | 32 |
36 void StrictTransportSecurityPersister::LoadState() { | 33 void StrictTransportSecurityPersister::LoadState() { |
37 // Runs on |file_thread_| | |
38 AutoLock locked_(lock_); | 34 AutoLock locked_(lock_); |
39 DCHECK(file_thread_->message_loop() == MessageLoop::current()); | 35 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
40 | 36 |
41 std::string state; | 37 std::string state; |
42 if (!file_util::ReadFileToString(state_file_, &state)) | 38 if (!file_util::ReadFileToString(state_file_, &state)) |
43 return; | 39 return; |
44 | 40 |
45 strict_transport_security_state_->Deserialise(state); | 41 strict_transport_security_state_->Deserialise(state); |
46 } | 42 } |
47 | 43 |
48 void StrictTransportSecurityPersister::StateIsDirty( | 44 void StrictTransportSecurityPersister::StateIsDirty( |
49 net::StrictTransportSecurityState* state) { | 45 net::StrictTransportSecurityState* state) { |
50 // Runs on arbitary thread, may not block nor reenter | 46 // Runs on arbitary thread, may not block nor reenter |
51 // |strict_transport_security_state_|. | 47 // |strict_transport_security_state_|. |
52 AutoLock locked_(lock_); | 48 AutoLock locked_(lock_); |
53 DCHECK(state == strict_transport_security_state_); | 49 DCHECK(state == strict_transport_security_state_); |
54 | 50 |
55 if (state_is_dirty_) | 51 if (state_is_dirty_) |
56 return; // we already have a serialisation scheduled | 52 return; // we already have a serialisation scheduled |
57 | 53 |
58 Task* task = NewRunnableMethod(this, | 54 Task* task = NewRunnableMethod(this, |
59 &StrictTransportSecurityPersister::SerialiseState); | 55 &StrictTransportSecurityPersister::SerialiseState); |
60 file_thread_->message_loop()->PostDelayedTask(FROM_HERE, task, | 56 ChromeThread::PostDelayedTask(ChromeThread::FILE, FROM_HERE, task, 1000); |
61 1000 /* 1 second */); | |
62 state_is_dirty_ = true; | 57 state_is_dirty_ = true; |
63 } | 58 } |
64 | 59 |
65 void StrictTransportSecurityPersister::SerialiseState() { | 60 void StrictTransportSecurityPersister::SerialiseState() { |
66 // Runs on |file_thread_| | |
67 AutoLock locked_(lock_); | 61 AutoLock locked_(lock_); |
68 DCHECK(file_thread_->message_loop() == MessageLoop::current()); | 62 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
69 | 63 |
70 DCHECK(state_is_dirty_); | 64 DCHECK(state_is_dirty_); |
71 state_is_dirty_ = false; | 65 state_is_dirty_ = false; |
72 | 66 |
73 std::string state; | 67 std::string state; |
74 if (!strict_transport_security_state_->Serialise(&state)) | 68 if (!strict_transport_security_state_->Serialise(&state)) |
75 return; | 69 return; |
76 | 70 |
77 file_util::WriteFile(state_file_, state.data(), state.size()); | 71 file_util::WriteFile(state_file_, state.data(), state.size()); |
78 } | 72 } |
OLD | NEW |