| OLD | NEW | 
| (Empty) |  | 
 |    1 // Copyright 2016 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 #ifndef COMPONENTS_FILESYSTEM_PUBLIC_CPP_PREFS_FILESYSTEM_JSON_PREF_STORE_H_ | 
 |    6 #define COMPONENTS_FILESYSTEM_PUBLIC_CPP_PREFS_FILESYSTEM_JSON_PREF_STORE_H_ | 
 |    7  | 
 |    8 #include <stdint.h> | 
 |    9  | 
 |   10 #include <set> | 
 |   11 #include <string> | 
 |   12  | 
 |   13 #include "base/callback_forward.h" | 
 |   14 #include "base/compiler_specific.h" | 
 |   15 #include "base/files/file_path.h" | 
 |   16 #include "base/gtest_prod_util.h" | 
 |   17 #include "base/macros.h" | 
 |   18 #include "base/memory/scoped_ptr.h" | 
 |   19 #include "base/memory/weak_ptr.h" | 
 |   20 #include "base/observer_list.h" | 
 |   21 #include "base/prefs/base_prefs_export.h" | 
 |   22 #include "base/prefs/persistent_pref_store.h" | 
 |   23 #include "base/prefs/pref_filter.h" | 
 |   24 #include "base/threading/non_thread_safe.h" | 
 |   25 #include "components/filesystem/public/interfaces/directory.mojom.h" | 
 |   26 #include "components/filesystem/public/interfaces/file.mojom.h" | 
 |   27 #include "components/filesystem/public/interfaces/file_system.mojom.h" | 
 |   28 #include "components/filesystem/public/interfaces/types.mojom.h" | 
 |   29 #include "mojo/public/cpp/bindings/binding.h" | 
 |   30  | 
 |   31 class PrefFilter; | 
 |   32  | 
 |   33 namespace base { | 
 |   34 class Clock; | 
 |   35 class DictionaryValue; | 
 |   36 class FilePath; | 
 |   37 class JsonPrefStoreLossyWriteTest; | 
 |   38 class Value; | 
 |   39 } | 
 |   40  | 
 |   41 namespace filesystem { | 
 |   42  | 
 |   43 // A forked, hack'n'slashed copy of base::JsonPrefStore which writes its | 
 |   44 // preference data to the mojo filesystem instead of the real | 
 |   45 // filesystem. Unlike base::JsonPrefStore, this class can safely be used inside | 
 |   46 // a sandboxed process. | 
 |   47 // | 
 |   48 // In the long run, we'll want to replace the current base::PrefService code | 
 |   49 // with something very different, especially since this component hard punts on | 
 |   50 // all the hard things that the preference service does (enterprise management, | 
 |   51 // parental controls, extension integration, etc.) and its interface is really | 
 |   52 // not optimal for a mojoified world--there are a few places where we assume | 
 |   53 // that writing to disk is synchronous...but it no longer is! | 
 |   54 // | 
 |   55 // Removing this class is a part of crbug.com/580652. | 
 |   56 class FilesystemJsonPrefStore | 
 |   57     : public PersistentPrefStore, | 
 |   58       public filesystem::FileSystemClient, | 
 |   59       public base::SupportsWeakPtr<FilesystemJsonPrefStore>, | 
 |   60       public base::NonThreadSafe { | 
 |   61  public: | 
 |   62   struct ReadResult; | 
 |   63  | 
 |   64   FilesystemJsonPrefStore(const std::string& pref_filename, | 
 |   65                           filesystem::FileSystemPtr filesystem, | 
 |   66                           scoped_ptr<PrefFilter> pref_filter); | 
 |   67  | 
 |   68   // PrefStore overrides: | 
 |   69   bool GetValue(const std::string& key, | 
 |   70                 const base::Value** result) const override; | 
 |   71   void AddObserver(PrefStore::Observer* observer) override; | 
 |   72   void RemoveObserver(PrefStore::Observer* observer) override; | 
 |   73   bool HasObservers() const override; | 
 |   74   bool IsInitializationComplete() const override; | 
 |   75  | 
 |   76   // PersistentPrefStore overrides: | 
 |   77   bool GetMutableValue(const std::string& key, base::Value** result) override; | 
 |   78   void SetValue(const std::string& key, | 
 |   79                 scoped_ptr<base::Value> value, | 
 |   80                 uint32_t flags) override; | 
 |   81   void SetValueSilently(const std::string& key, | 
 |   82                         scoped_ptr<base::Value> value, | 
 |   83                         uint32_t flags) override; | 
 |   84   void RemoveValue(const std::string& key, uint32_t flags) override; | 
 |   85   bool ReadOnly() const override; | 
 |   86   PrefReadError GetReadError() const override; | 
 |   87   // Note this method may be asynchronous if this instance has a |pref_filter_| | 
 |   88   // in which case it will return PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE. | 
 |   89   // See details in pref_filter.h. | 
 |   90   PrefReadError ReadPrefs() override; | 
 |   91   void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override; | 
 |   92   void CommitPendingWrite() override; | 
 |   93   void SchedulePendingLossyWrites() override; | 
 |   94   void ReportValueChanged(const std::string& key, uint32_t flags) override; | 
 |   95  | 
 |   96   // FileSystemClient overrides: | 
 |   97   void OnFileSystemShutdown() override; | 
 |   98  | 
 |   99   // Just like RemoveValue(), but doesn't notify observers. Used when doing some | 
 |  100   // cleanup that shouldn't otherwise alert observers. | 
 |  101   void RemoveValueSilently(const std::string& key, uint32_t flags); | 
 |  102  | 
 |  103  private: | 
 |  104   friend class base::JsonPrefStoreLossyWriteTest; | 
 |  105  | 
 |  106   ~FilesystemJsonPrefStore() override; | 
 |  107  | 
 |  108   // This method is called after the JSON file has been read.  It then hands | 
 |  109   // |value| (or an empty dictionary in some read error cases) to the | 
 |  110   // |pref_filter| if one is set. It also gives a callback pointing at | 
 |  111   // FinalizeFileRead() to that |pref_filter_| which is then responsible for | 
 |  112   // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead() | 
 |  113   // is invoked directly. | 
 |  114   void OnFileRead(scoped_ptr<ReadResult> read_result); | 
 |  115  | 
 |  116   // This method is called after the JSON file has been read and the result has | 
 |  117   // potentially been intercepted and modified by |pref_filter_|. | 
 |  118   // |schedule_write| indicates whether a write should be immediately scheduled | 
 |  119   // (typically because the |pref_filter_| has already altered the |prefs|) -- | 
 |  120   // this will be ignored if this store is read-only. | 
 |  121   void FinalizeFileRead(scoped_ptr<base::DictionaryValue> prefs, | 
 |  122                         bool schedule_write); | 
 |  123  | 
 |  124   // Schedule a write with the file writer as long as |flags| doesn't contain | 
 |  125   // WriteablePrefStore::LOSSY_PREF_WRITE_FLAG. | 
 |  126   void ScheduleWrite(uint32_t flags); | 
 |  127  | 
 |  128   // Actually performs a write. Unlike the //base version of this class, we | 
 |  129   // don't use the ImportantFileWriter and instead write using the mojo | 
 |  130   // filesystem API. | 
 |  131   void PerformWrite(); | 
 |  132  | 
 |  133   // Opens the filesystem and calls |callback| when completed, whether | 
 |  134   // successfully or unsuccessfully. | 
 |  135   void OpenFilesystem(base::Closure callback); | 
 |  136  | 
 |  137   // Callback method which verifies that there were no errors on opening the | 
 |  138   // filesystem, and if there aren't, invokes the passed in callback. | 
 |  139   void OnOpenFilesystem(base::Closure callback, FileError err); | 
 |  140  | 
 |  141   // Asynchronous implementation details of PerformWrite(). | 
 |  142   void OnTempFileWriteStart(); | 
 |  143   void OnTempFileOpened(FileError err); | 
 |  144   void OnTempFileWrite(FileError err, uint32_t num_bytes_written); | 
 |  145   void OnTempFileClosed(FileError err); | 
 |  146   void OnTempFileRenamed(FileError err); | 
 |  147  | 
 |  148   // Asynchronous implementation details of ReadPrefsAsync(). | 
 |  149   void OnPreferencesReadStart(); | 
 |  150   void OnPreferencesFileOpened(FileError err); | 
 |  151   void OnPreferencesFileRead(FileError err, mojo::Array<uint8_t> contents); | 
 |  152  | 
 |  153   const std::string path_; | 
 |  154   mojo::Binding<filesystem::FileSystemClient> binding_; | 
 |  155   filesystem::FileSystemPtr filesystem_; | 
 |  156  | 
 |  157   // |directory_| is only bound after the first attempt to access the | 
 |  158   // |filesystem. See OpenFilesystem(). | 
 |  159   DirectoryPtr directory_; | 
 |  160  | 
 |  161   FilePtr preferences_file_; | 
 |  162   FilePtr temporary_file_; | 
 |  163  | 
 |  164   scoped_ptr<base::DictionaryValue> prefs_; | 
 |  165  | 
 |  166   bool read_only_; | 
 |  167  | 
 |  168   scoped_ptr<PrefFilter> pref_filter_; | 
 |  169   base::ObserverList<PrefStore::Observer, true> observers_; | 
 |  170  | 
 |  171   scoped_ptr<ReadErrorDelegate> error_delegate_; | 
 |  172  | 
 |  173   bool initialized_; | 
 |  174   bool filtering_in_progress_; | 
 |  175   bool pending_lossy_write_; | 
 |  176   PrefReadError read_error_; | 
 |  177  | 
 |  178   DISALLOW_COPY_AND_ASSIGN(FilesystemJsonPrefStore); | 
 |  179 }; | 
 |  180  | 
 |  181 }  // namespace filesystem | 
 |  182  | 
 |  183 #endif  // COMPONENTS_FILESYSTEM_PUBLIC_CPP_PREFS_FILESYSTEM_JSON_PREF_STORE_H_ | 
| OLD | NEW |