ProfilePolicyConnectorFactory: Refactoring from Profile to BrowserContext.
[chromium-blink-merge.git] / third_party / leveldatabase / env_chromium.h
blob447e1df936965ac491463c5b06218d6b5ffa957b
1 // Copyright (c) 2013 The LevelDB 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. See the AUTHORS file for names of contributors.
5 #ifndef THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_
6 #define THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_
8 #include <deque>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/files/file.h"
14 #include "base/files/file_path.h"
15 #include "base/metrics/histogram.h"
16 #include "leveldb/env.h"
17 #include "port/port_chromium.h"
18 #include "util/mutexlock.h"
20 namespace leveldb_env {
22 // These entries map to values in tools/metrics/histograms/histograms.xml. New
23 // values should be appended at the end.
24 enum MethodID {
25 kSequentialFileRead,
26 kSequentialFileSkip,
27 kRandomAccessFileRead,
28 kWritableFileAppend,
29 kWritableFileClose,
30 kWritableFileFlush,
31 kWritableFileSync,
32 kNewSequentialFile,
33 kNewRandomAccessFile,
34 kNewWritableFile,
35 kDeleteFile,
36 kCreateDir,
37 kDeleteDir,
38 kGetFileSize,
39 kRenameFile,
40 kLockFile,
41 kUnlockFile,
42 kGetTestDirectory,
43 kNewLogger,
44 kSyncParent,
45 kGetChildren,
46 kNewAppendableFile,
47 kNumEntries
50 const char* MethodIDToString(MethodID method);
52 leveldb::Status MakeIOError(leveldb::Slice filename,
53 const std::string& message,
54 MethodID method,
55 base::File::Error error);
56 leveldb::Status MakeIOError(leveldb::Slice filename,
57 const std::string& message,
58 MethodID method);
60 enum ErrorParsingResult {
61 METHOD_ONLY,
62 METHOD_AND_PFE,
63 METHOD_AND_ERRNO,
64 NONE,
67 ErrorParsingResult ParseMethodAndError(const leveldb::Status& status,
68 MethodID* method,
69 int* error);
70 int GetCorruptionCode(const leveldb::Status& status);
71 int GetNumCorruptionCodes();
72 std::string GetCorruptionMessage(const leveldb::Status& status);
73 bool IndicatesDiskFull(const leveldb::Status& status);
75 class UMALogger {
76 public:
77 virtual void RecordErrorAt(MethodID method) const = 0;
78 virtual void RecordOSError(MethodID method,
79 base::File::Error error) const = 0;
80 virtual void RecordBackupResult(bool success) const = 0;
83 class RetrierProvider {
84 public:
85 virtual int MaxRetryTimeMillis() const = 0;
86 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const = 0;
87 virtual base::HistogramBase* GetRecoveredFromErrorHistogram(
88 MethodID method) const = 0;
91 class WriteTracker {
92 public:
93 virtual void DidCreateNewFile(const std::string& fname) = 0;
94 virtual bool DoesDirNeedSync(const std::string& fname) = 0;
95 virtual void DidSyncDir(const std::string& fname) = 0;
98 class ChromiumEnv : public leveldb::Env,
99 public UMALogger,
100 public RetrierProvider,
101 public WriteTracker {
102 public:
103 ChromiumEnv();
105 typedef void(ScheduleFunc)(void*);
107 static bool MakeBackup(const std::string& fname);
108 virtual ~ChromiumEnv();
110 virtual bool FileExists(const std::string& fname);
111 virtual leveldb::Status GetChildren(const std::string& dir,
112 std::vector<std::string>* result);
113 virtual leveldb::Status DeleteFile(const std::string& fname);
114 virtual leveldb::Status CreateDir(const std::string& name);
115 virtual leveldb::Status DeleteDir(const std::string& name);
116 virtual leveldb::Status GetFileSize(const std::string& fname, uint64_t* size);
117 virtual leveldb::Status RenameFile(const std::string& src,
118 const std::string& dst);
119 virtual leveldb::Status LockFile(const std::string& fname,
120 leveldb::FileLock** lock);
121 virtual leveldb::Status UnlockFile(leveldb::FileLock* lock);
122 virtual void Schedule(ScheduleFunc*, void* arg);
123 virtual void StartThread(void (*function)(void* arg), void* arg);
124 virtual leveldb::Status GetTestDirectory(std::string* path);
125 virtual uint64_t NowMicros();
126 virtual void SleepForMicroseconds(int micros);
127 virtual leveldb::Status NewSequentialFile(const std::string& fname,
128 leveldb::SequentialFile** result);
129 virtual leveldb::Status NewRandomAccessFile(
130 const std::string& fname,
131 leveldb::RandomAccessFile** result);
132 virtual leveldb::Status NewWritableFile(const std::string& fname,
133 leveldb::WritableFile** result);
134 virtual leveldb::Status NewAppendableFile(const std::string& fname,
135 leveldb::WritableFile** result);
136 virtual leveldb::Status NewLogger(const std::string& fname,
137 leveldb::Logger** result);
139 protected:
140 virtual void DidSyncDir(const std::string& fname);
142 std::string name_;
143 bool make_backup_;
145 private:
146 static const char* FileErrorString(base::File::Error error);
148 virtual void DidCreateNewFile(const std::string& fname);
149 virtual bool DoesDirNeedSync(const std::string& fname);
150 virtual void RecordErrorAt(MethodID method) const;
151 virtual void RecordOSError(MethodID method,
152 base::File::Error error) const;
153 void RecordOpenFilesLimit(const std::string& type);
154 base::HistogramBase* GetMaxFDHistogram(const std::string& type) const;
155 base::HistogramBase* GetOSErrorHistogram(MethodID method, int limit) const;
157 // File locks may not be exclusive within a process (e.g. on POSIX). Track
158 // locks held by the ChromiumEnv to prevent access within the process.
159 class LockTable {
160 public:
161 bool Insert(const std::string& fname) {
162 leveldb::MutexLock l(&mu_);
163 return locked_files_.insert(fname).second;
165 bool Remove(const std::string& fname) {
166 leveldb::MutexLock l(&mu_);
167 return locked_files_.erase(fname) == 1;
169 private:
170 leveldb::port::Mutex mu_;
171 std::set<std::string> locked_files_;
174 std::set<std::string> directories_needing_sync_;
175 base::Lock directory_sync_lock_;
177 const int kMaxRetryTimeMillis;
178 // BGThread() is the body of the background thread
179 void BGThread();
180 static void BGThreadWrapper(void* arg) {
181 reinterpret_cast<ChromiumEnv*>(arg)->BGThread();
184 virtual void RecordBackupResult(bool result) const;
185 void RestoreIfNecessary(const std::string& dir,
186 std::vector<std::string>* children);
187 base::FilePath RestoreFromBackup(const base::FilePath& base_name);
188 void RecordLockFileAncestors(int num_missing_ancestors) const;
189 base::HistogramBase* GetMethodIOErrorHistogram() const;
190 base::HistogramBase* GetLockFileAncestorHistogram() const;
192 // RetrierProvider implementation.
193 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis; }
194 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const;
195 virtual base::HistogramBase* GetRecoveredFromErrorHistogram(
196 MethodID method) const;
198 base::FilePath test_directory_;
200 base::Lock mu_;
201 base::ConditionVariable bgsignal_;
202 bool started_bgthread_;
204 // Entry per Schedule() call
205 struct BGItem {
206 void* arg;
207 void (*function)(void*);
209 typedef std::deque<BGItem> BGQueue;
210 BGQueue queue_;
211 LockTable locks_;
214 } // namespace leveldb_env
216 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_