summaryrefslogtreecommitdiffstats
path: root/chrome/browser/transport_security_persister.cc
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-21 18:54:32 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-21 18:54:32 +0000
commit02896a8fd669ced03e8a76edfbb423c858eafc53 (patch)
treec75f30c0004063e6e0c9db8109638131c099f7fe /chrome/browser/transport_security_persister.cc
parent111efaf9b1799bcf2326703c2d26bd60f799ab2f (diff)
downloadchromium_src-02896a8fd669ced03e8a76edfbb423c858eafc53.zip
chromium_src-02896a8fd669ced03e8a76edfbb423c858eafc53.tar.gz
chromium_src-02896a8fd669ced03e8a76edfbb423c858eafc53.tar.bz2
Move TransportSecurityPersister completely to IO thread.
This should fix problems caused by calling ImportantFileWriter on wrong threads. BUG=none Review URL: http://codereview.chromium.org/7966005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@102139 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/transport_security_persister.cc')
-rw-r--r--chrome/browser/transport_security_persister.cc72
1 files changed, 49 insertions, 23 deletions
diff --git a/chrome/browser/transport_security_persister.cc b/chrome/browser/transport_security_persister.cc
index 59f093a..21c3e2b 100644
--- a/chrome/browser/transport_security_persister.cc
+++ b/chrome/browser/transport_security_persister.cc
@@ -4,6 +4,7 @@
#include "chrome/browser/transport_security_persister.h"
+#include "base/bind.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/message_loop.h"
@@ -12,6 +13,42 @@
#include "content/browser/browser_thread.h"
#include "net/base/transport_security_state.h"
+class TransportSecurityPersister::Loader {
+ public:
+ Loader(const base::WeakPtr<TransportSecurityPersister>& persister,
+ const FilePath& path)
+ : persister_(persister),
+ path_(path),
+ state_valid_(false) {
+ }
+
+ void Load() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+ state_valid_ = file_util::ReadFileToString(path_, &state_);
+ }
+
+ void CompleteLoad() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+
+ // Make sure we're deleted.
+ scoped_ptr<Loader> deleter(this);
+
+ if (!persister_ || !state_valid_)
+ return;
+ persister_->CompleteLoad(state_);
+ }
+
+ private:
+ base::WeakPtr<TransportSecurityPersister> persister_;
+
+ FilePath path_;
+
+ std::string state_;
+ bool state_valid_;
+
+ DISALLOW_COPY_AND_ASSIGN(Loader);
+};
+
TransportSecurityPersister::TransportSecurityPersister(
net::TransportSecurityState* state,
const FilePath& profile_path,
@@ -19,40 +56,28 @@ TransportSecurityPersister::TransportSecurityPersister(
: transport_security_state_(state),
writer_(profile_path.AppendASCII("TransportSecurity"),
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)),
- readonly_(readonly) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ readonly_(readonly),
+ weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
transport_security_state_->SetDelegate(this);
+
+ Loader* loader = new Loader(weak_ptr_factory_.GetWeakPtr(), writer_.path());
+ BrowserThread::PostTaskAndReply(
+ BrowserThread::FILE, FROM_HERE,
+ base::Bind(&Loader::Load, base::Unretained(loader)),
+ base::Bind(&Loader::CompleteLoad, base::Unretained(loader)));
}
TransportSecurityPersister::~TransportSecurityPersister() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+
if (writer_.HasPendingWrite())
writer_.DoScheduledWrite();
transport_security_state_->SetDelegate(NULL);
}
-void TransportSecurityPersister::Init() {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- BrowserThread::PostTask(
- BrowserThread::FILE, FROM_HERE,
- NewRunnableMethod(this, &TransportSecurityPersister::Load));
-}
-
-void TransportSecurityPersister::Load() {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
-
- std::string state;
- if (!file_util::ReadFileToString(writer_.path(), &state))
- return;
-
- BrowserThread::PostTask(
- BrowserThread::IO, FROM_HERE,
- NewRunnableMethod(this,
- &TransportSecurityPersister::CompleteLoad,
- state));
-}
-
void TransportSecurityPersister::CompleteLoad(const std::string& state) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
@@ -75,5 +100,6 @@ void TransportSecurityPersister::StateIsDirty(
}
bool TransportSecurityPersister::SerializeData(std::string* data) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
return transport_security_state_->Serialise(data);
}