Home | History | Annotate | Download | only in browser
      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
      3 // found in the LICENSE file.
      4 
      5 // TransportSecurityState maintains an in memory database containing the
      6 // list of hosts that currently have transport security enabled. This
      7 // singleton object deals with writing that data out to disk as needed and
      8 // loading it at startup.
      9 
     10 // At startup we need to load the transport security state from the
     11 // disk. For the moment, we don't want to delay startup for this load, so we
     12 // let the TransportSecurityState run for a while without being loaded.
     13 // This means that it's possible for pages opened very quickly not to get the
     14 // correct transport security information.
     15 //
     16 // To load the state, we schedule a Task on the file thread which loads,
     17 // deserialises and configures the TransportSecurityState.
     18 //
     19 // The TransportSecurityState object supports running a callback function
     20 // when it changes. This object registers the callback, pointing at itself.
     21 //
     22 // TransportSecurityState calls...
     23 // TransportSecurityPersister::StateIsDirty
     24 //   since the callback isn't allowed to block or reenter, we schedule a Task
     25 //   on the file thread after some small amount of time
     26 //
     27 // ...
     28 //
     29 // TransportSecurityPersister::SerialiseState
     30 //   copies the current state of the TransportSecurityState, serialises
     31 //   and writes to disk.
     32 
     33 #ifndef CHROME_BROWSER_TRANSPORT_SECURITY_PERSISTER_H_
     34 #define CHROME_BROWSER_TRANSPORT_SECURITY_PERSISTER_H_
     35 #pragma once
     36 
     37 #include "base/file_path.h"
     38 #include "base/memory/ref_counted.h"
     39 #include "base/task.h"
     40 #include "net/base/transport_security_state.h"
     41 
     42 class TransportSecurityPersister
     43     : public base::RefCountedThreadSafe<TransportSecurityPersister>,
     44       public net::TransportSecurityState::Delegate {
     45  public:
     46   explicit TransportSecurityPersister(bool readonly);
     47   void Initialize(net::TransportSecurityState* state,
     48                   const FilePath& profile_path);
     49 
     50   // Called by the TransportSecurityState when it changes its state.
     51   virtual void StateIsDirty(net::TransportSecurityState*);
     52 
     53  private:
     54   friend class base::RefCountedThreadSafe<TransportSecurityPersister>;
     55 
     56   ~TransportSecurityPersister();
     57 
     58   void Load();
     59   void CompleteLoad(const std::string& state);
     60 
     61   void Save();
     62   void CompleteSave(const std::string& state);
     63 
     64   // Used on the IO thread to coalesce writes to disk.
     65   ScopedRunnableMethodFactory<TransportSecurityPersister> save_coalescer_;
     66 
     67   scoped_refptr<net::TransportSecurityState>
     68       transport_security_state_;  // IO thread only.
     69 
     70   // The path to the file in which we store the serialised state.
     71   FilePath state_file_;
     72 
     73   // Whether or not we're in read-only mode.
     74   bool readonly_;
     75 };
     76 
     77 #endif  // CHROME_BROWSER_TRANSPORT_SECURITY_PERSISTER_H_
     78