aw: Move SharedRendererState out of AwContents
[chromium-blink-merge.git] / third_party / leveldatabase / env_chromium.h
blobb340c26051aedc7fa3be55c0dc028fb7a07c1438
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 enum MethodID {
23 kSequentialFileRead,
24 kSequentialFileSkip,
25 kRandomAccessFileRead,
26 kWritableFileAppend,
27 kWritableFileClose,
28 kWritableFileFlush,
29 kWritableFileSync,
30 kNewSequentialFile,
31 kNewRandomAccessFile,
32 kNewWritableFile,
33 kDeleteFile,
34 kCreateDir,
35 kDeleteDir,
36 kGetFileSize,
37 kRenameFile,
38 kLockFile,
39 kUnlockFile,
40 kGetTestDirectory,
41 kNewLogger,
42 kSyncParent,
43 kGetChildren,
44 kNumEntries
47 const char* MethodIDToString(MethodID method);
49 leveldb::Status MakeIOError(leveldb::Slice filename,
50 const char* message,
51 MethodID method,
52 int saved_errno);
53 leveldb::Status MakeIOError(leveldb::Slice filename,
54 const char* message,
55 MethodID method,
56 base::File::Error error);
57 leveldb::Status MakeIOError(leveldb::Slice filename,
58 const char* message,
59 MethodID method);
61 enum ErrorParsingResult {
62 METHOD_ONLY,
63 METHOD_AND_PFE,
64 METHOD_AND_ERRNO,
65 NONE,
68 ErrorParsingResult ParseMethodAndError(const char* string,
69 MethodID* method,
70 int* error);
71 int GetCorruptionCode(const leveldb::Status& status);
72 int GetNumCorruptionCodes();
73 std::string GetCorruptionMessage(const leveldb::Status& status);
74 bool IndicatesDiskFull(const leveldb::Status& status);
75 bool IsIOError(const leveldb::Status& status);
76 bool IsCorruption(const leveldb::Status& status);
77 std::string FilePathToString(const base::FilePath& file_path);
79 class UMALogger {
80 public:
81 virtual void RecordErrorAt(MethodID method) const = 0;
82 virtual void RecordOSError(MethodID method, int saved_errno) const = 0;
83 virtual void RecordOSError(MethodID method,
84 base::File::Error error) const = 0;
85 virtual void RecordBackupResult(bool success) const = 0;
88 class RetrierProvider {
89 public:
90 virtual int MaxRetryTimeMillis() const = 0;
91 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const = 0;
92 virtual base::HistogramBase* GetRecoveredFromErrorHistogram(
93 MethodID method) const = 0;
96 class WriteTracker {
97 public:
98 virtual void DidCreateNewFile(const std::string& fname) = 0;
99 virtual bool DoesDirNeedSync(const std::string& fname) = 0;
100 virtual void DidSyncDir(const std::string& fname) = 0;
103 class ChromiumEnv : public leveldb::Env,
104 public UMALogger,
105 public RetrierProvider,
106 public WriteTracker {
107 public:
108 typedef void(ScheduleFunc)(void*);
110 static bool MakeBackup(const std::string& fname);
111 static base::FilePath CreateFilePath(const std::string& file_path);
112 static const char* FileErrorString(::base::File::Error error);
113 static bool HasTableExtension(const base::FilePath& path);
114 virtual ~ChromiumEnv();
116 virtual bool FileExists(const std::string& fname);
117 virtual leveldb::Status GetChildren(const std::string& dir,
118 std::vector<std::string>* result);
119 virtual leveldb::Status DeleteFile(const std::string& fname);
120 virtual leveldb::Status CreateDir(const std::string& name);
121 virtual leveldb::Status DeleteDir(const std::string& name);
122 virtual leveldb::Status GetFileSize(const std::string& fname, uint64_t* size);
123 virtual leveldb::Status RenameFile(const std::string& src,
124 const std::string& dst);
125 virtual leveldb::Status LockFile(const std::string& fname,
126 leveldb::FileLock** lock);
127 virtual leveldb::Status UnlockFile(leveldb::FileLock* lock);
128 virtual void Schedule(ScheduleFunc*, void* arg);
129 virtual void StartThread(void (*function)(void* arg), void* arg);
130 virtual leveldb::Status GetTestDirectory(std::string* path);
131 virtual uint64_t NowMicros();
132 virtual void SleepForMicroseconds(int micros);
134 protected:
135 ChromiumEnv();
137 virtual void DidCreateNewFile(const std::string& fname);
138 virtual bool DoesDirNeedSync(const std::string& fname);
139 virtual void DidSyncDir(const std::string& fname);
140 virtual base::File::Error GetDirectoryEntries(
141 const base::FilePath& dir_param,
142 std::vector<base::FilePath>* result) const = 0;
143 virtual void RecordErrorAt(MethodID method) const;
144 virtual void RecordOSError(MethodID method, int saved_errno) const;
145 virtual void RecordOSError(MethodID method,
146 base::File::Error error) const;
147 base::HistogramBase* GetMaxFDHistogram(const std::string& type) const;
148 base::HistogramBase* GetOSErrorHistogram(MethodID method, int limit) const;
150 std::string name_;
151 bool make_backup_;
153 private:
154 // File locks may not be exclusive within a process (e.g. on POSIX). Track
155 // locks held by the ChromiumEnv to prevent access within the process.
156 class LockTable {
157 public:
158 bool Insert(const std::string& fname) {
159 leveldb::MutexLock l(&mu_);
160 return locked_files_.insert(fname).second;
162 bool Remove(const std::string& fname) {
163 leveldb::MutexLock l(&mu_);
164 return locked_files_.erase(fname) == 1;
166 private:
167 leveldb::port::Mutex mu_;
168 std::set<std::string> locked_files_;
171 std::set<std::string> directories_needing_sync_;
172 base::Lock directory_sync_lock_;
174 const int kMaxRetryTimeMillis;
175 // BGThread() is the body of the background thread
176 void BGThread();
177 static void BGThreadWrapper(void* arg) {
178 reinterpret_cast<ChromiumEnv*>(arg)->BGThread();
181 virtual void RecordBackupResult(bool result) const;
182 void RestoreIfNecessary(const std::string& dir,
183 std::vector<std::string>* children);
184 base::FilePath RestoreFromBackup(const base::FilePath& base_name);
185 void RecordLockFileAncestors(int num_missing_ancestors) const;
186 base::HistogramBase* GetMethodIOErrorHistogram() const;
187 base::HistogramBase* GetLockFileAncestorHistogram() const;
189 // RetrierProvider implementation.
190 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis; }
191 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const;
192 virtual base::HistogramBase* GetRecoveredFromErrorHistogram(
193 MethodID method) const;
195 base::FilePath test_directory_;
197 ::base::Lock mu_;
198 ::base::ConditionVariable bgsignal_;
199 bool started_bgthread_;
201 // Entry per Schedule() call
202 struct BGItem {
203 void* arg;
204 void (*function)(void*);
206 typedef std::deque<BGItem> BGQueue;
207 BGQueue queue_;
208 LockTable locks_;
211 } // namespace leveldb_env
213 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_