OLD | NEW |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #ifndef CRASHPAD_CLIENT_SETTINGS_H_ | 15 #ifndef CRASHPAD_CLIENT_SETTINGS_H_ |
16 #define CRASHPAD_CLIENT_SETTINGS_H_ | 16 #define CRASHPAD_CLIENT_SETTINGS_H_ |
17 | 17 |
18 #include <time.h> | 18 #include <time.h> |
19 | 19 |
20 #include <string> | 20 #include <string> |
21 | 21 |
22 #include "base/basictypes.h" | 22 #include "base/basictypes.h" |
23 #include "base/files/file_path.h" | 23 #include "base/files/file_path.h" |
| 24 #include "base/scoped_generic.h" |
24 #include "util/file/file_io.h" | 25 #include "util/file/file_io.h" |
25 #include "util/misc/initialization_state_dcheck.h" | 26 #include "util/misc/initialization_state_dcheck.h" |
26 #include "util/misc/uuid.h" | 27 #include "util/misc/uuid.h" |
27 | 28 |
28 namespace crashpad { | 29 namespace crashpad { |
29 | 30 |
| 31 namespace internal { |
| 32 |
| 33 struct ScopedLockedFileHandleTraits { |
| 34 static FileHandle InvalidValue(); |
| 35 static void Free(FileHandle handle); |
| 36 }; |
| 37 |
| 38 } // namespace internal |
| 39 |
30 //! \brief An interface for accessing and modifying the settings of a | 40 //! \brief An interface for accessing and modifying the settings of a |
31 //! CrashReportDatabase. | 41 //! CrashReportDatabase. |
32 //! | 42 //! |
33 //! This class must not be instantiated directly, but rather an instance of it | 43 //! This class must not be instantiated directly, but rather an instance of it |
34 //! should be retrieved via CrashReportDatabase::GetSettings(). | 44 //! should be retrieved via CrashReportDatabase::GetSettings(). |
35 class Settings { | 45 class Settings { |
36 public: | 46 public: |
37 explicit Settings(const base::FilePath& file_path); | 47 explicit Settings(const base::FilePath& file_path); |
38 ~Settings(); | 48 ~Settings(); |
39 | 49 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 //! | 98 //! |
89 //! \param[in] time The last time at which a report was uploaded. | 99 //! \param[in] time The last time at which a report was uploaded. |
90 //! | 100 //! |
91 //! \return On success, returns `true`, otherwise returns `false` with an | 101 //! \return On success, returns `true`, otherwise returns `false` with an |
92 //! error logged. | 102 //! error logged. |
93 bool SetLastUploadAttemptTime(time_t time); | 103 bool SetLastUploadAttemptTime(time_t time); |
94 | 104 |
95 private: | 105 private: |
96 struct Data; | 106 struct Data; |
97 | 107 |
| 108 // This must be constructed with MakeScopedLockedFileHandle(). It both unlocks |
| 109 // and closes the file on destruction. |
| 110 using ScopedLockedFileHandle = |
| 111 base::ScopedGeneric<FileHandle, |
| 112 internal::ScopedLockedFileHandleTraits>; |
| 113 static ScopedLockedFileHandle MakeScopedLockedFileHandle(FileHandle file, |
| 114 FileLocking locking); |
| 115 |
98 // Opens the settings file for reading. On error, logs a message and returns | 116 // Opens the settings file for reading. On error, logs a message and returns |
99 // the invalid handle. | 117 // the invalid handle. |
100 ScopedFileHandle OpenForReading(); | 118 ScopedLockedFileHandle OpenForReading(); |
101 | 119 |
102 // Opens the settings file for reading and writing. On error, logs a message | 120 // Opens the settings file for reading and writing. On error, logs a message |
103 // and returns the invalid handle. | 121 // and returns the invalid handle. |
104 ScopedFileHandle OpenForReadingAndWriting(); | 122 ScopedLockedFileHandle OpenForReadingAndWriting(); |
105 | 123 |
106 // Opens the settings file and reads the data. If that fails, an error will | 124 // Opens the settings file and reads the data. If that fails, an error will |
107 // be logged and the settings will be recovered and re-initialized. If that | 125 // be logged and the settings will be recovered and re-initialized. If that |
108 // also fails, returns false with additional log data from recovery. | 126 // also fails, returns false with additional log data from recovery. |
109 bool OpenAndReadSettings(Data* out_data); | 127 bool OpenAndReadSettings(Data* out_data); |
110 | 128 |
111 // Opens the settings file for writing and reads the data. If reading fails, | 129 // Opens the settings file for writing and reads the data. If reading fails, |
112 // recovery is attempted. Returns the opened file handle on success, or the | 130 // recovery is attempted. Returns the opened file handle on success, or the |
113 // invalid file handle on failure, with an error logged. | 131 // invalid file handle on failure, with an error logged. |
114 ScopedFileHandle OpenForWritingAndReadSettings(Data* out_data); | 132 ScopedLockedFileHandle OpenForWritingAndReadSettings(Data* out_data); |
115 | 133 |
116 // Reads the settings from |handle|. Logs an error and returns false on | 134 // Reads the settings from |handle|. Logs an error and returns false on |
117 // failure. This does not perform recovery. | 135 // failure. This does not perform recovery. |
118 bool ReadSettings(FileHandle handle, Data* out_data); | 136 bool ReadSettings(FileHandle handle, Data* out_data); |
119 | 137 |
120 // Writes the settings to |handle|. Logs an error and returns false on | 138 // Writes the settings to |handle|. Logs an error and returns false on |
121 // failure. This does not perform recovery. | 139 // failure. This does not perform recovery. |
122 bool WriteSettings(FileHandle handle, const Data& data); | 140 bool WriteSettings(FileHandle handle, const Data& data); |
123 | 141 |
124 // Recovers the settings file by re-initializing the data. If |handle| is the | 142 // Recovers the settings file by re-initializing the data. If |handle| is the |
(...skipping 13 matching lines...) Expand all Loading... |
138 base::FilePath file_path_; | 156 base::FilePath file_path_; |
139 | 157 |
140 InitializationStateDcheck initialized_; | 158 InitializationStateDcheck initialized_; |
141 | 159 |
142 DISALLOW_COPY_AND_ASSIGN(Settings); | 160 DISALLOW_COPY_AND_ASSIGN(Settings); |
143 }; | 161 }; |
144 | 162 |
145 } // namespace crashpad | 163 } // namespace crashpad |
146 | 164 |
147 #endif // CRASHPAD_CLIENT_SETTINGS_H_ | 165 #endif // CRASHPAD_CLIENT_SETTINGS_H_ |
OLD | NEW |