summaryrefslogtreecommitdiffstats
path: root/chrome/browser/transport_security_persister.cc
blob: 98f4580b13eed853f8a7f56c12a7fa89d45428e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/transport_security_persister.h"

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "chrome/common/chrome_paths.h"
#include "content/browser/browser_thread.h"
#include "net/base/transport_security_state.h"

TransportSecurityPersister::TransportSecurityPersister(bool readonly)
  : ALLOW_THIS_IN_INITIALIZER_LIST(save_coalescer_(this)),
    readonly_(readonly) {
}

TransportSecurityPersister::~TransportSecurityPersister() {
  transport_security_state_->SetDelegate(NULL);
}

void TransportSecurityPersister::Initialize(
    net::TransportSecurityState* state, const FilePath& profile_path) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  transport_security_state_ = state;
  state_file_ =
      profile_path.Append(FILE_PATH_LITERAL("TransportSecurity"));
  state->SetDelegate(this);

  Task* task = NewRunnableMethod(this,
      &TransportSecurityPersister::Load);
  BrowserThread::PostDelayedTask(BrowserThread::FILE, FROM_HERE, task, 1000);
}

void TransportSecurityPersister::Load() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));

  std::string state;
  if (!file_util::ReadFileToString(state_file_, &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));

  bool dirty = false;
  if (!transport_security_state_->LoadEntries(state, &dirty)) {
    LOG(ERROR) << "Failed to deserialize state: " << state;
    return;
  }
  if (dirty)
    StateIsDirty(transport_security_state_);
}

void TransportSecurityPersister::StateIsDirty(
    net::TransportSecurityState* state) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  DCHECK(state == transport_security_state_);

  if (readonly_)
    return;

  if (!save_coalescer_.empty())
    return;

  Task* task = save_coalescer_.NewRunnableMethod(
      &TransportSecurityPersister::Save);
  MessageLoop::current()->PostDelayedTask(FROM_HERE, task, 1000);
}

void TransportSecurityPersister::Save() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));

  std::string state;
  if (!transport_security_state_->Serialise(&state))
    return;

  BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
      NewRunnableMethod(this,
                        &TransportSecurityPersister::CompleteSave,
                        state));
}

void TransportSecurityPersister::CompleteSave(const std::string& state) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  DCHECK(!readonly_);

  file_util::WriteFile(state_file_, state.data(), state.size());
}