| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (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 |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #ifndef CRASHPAD_CLIENT_SETTINGS_H_ |
| 16 #define CRASHPAD_CLIENT_SETTINGS_H_ |
| 17 |
| 18 #include <time.h> |
| 19 |
| 20 #include <string> |
| 21 |
| 22 #include "base/basictypes.h" |
| 23 #include "base/files/file_path.h" |
| 24 #include "base/scoped_generic.h" |
| 25 #include "util/file/file_io.h" |
| 26 #include "util/misc/initialization_state.h" |
| 27 #include "util/misc/uuid.h" |
| 28 |
| 29 namespace crashpad { |
| 30 |
| 31 namespace internal { |
| 32 |
| 33 struct ScopedLockedFileHandleTraits { |
| 34 static FileHandle InvalidValue() { return kInvalidFileHandle; } |
| 35 static void Free(FileHandle handle); |
| 36 }; |
| 37 |
| 38 } // namespace internal |
| 39 |
| 40 //! \brief An interface for accessing and modifying the settings of a |
| 41 //! CrashReportDatabase. |
| 42 //! |
| 43 //! This class must not be instantiated directly, but rather an instance of it |
| 44 //! should be retrieved via CrashReportDatabase::GetSettings(). |
| 45 class Settings { |
| 46 public: |
| 47 explicit Settings(const base::FilePath& file_path); |
| 48 ~Settings(); |
| 49 |
| 50 bool Initialize(); |
| 51 |
| 52 //! \brief Retrieves the immutable identifier for this client, which is used |
| 53 //! on a server to locate all crash reports from a specific Crashpad |
| 54 //! database. |
| 55 //! |
| 56 //! This is automatically initialized when the database is created. |
| 57 //! |
| 58 //! \param[out] client_id The unique client identifier. |
| 59 //! |
| 60 //! \return On success, returns `true`, otherwise returns `false` with an |
| 61 //! error logged. |
| 62 bool GetClientID(UUID* client_id); |
| 63 |
| 64 //! \brief Retrieves the user’s preference for submitting crash reports to a |
| 65 //! collection server. |
| 66 //! |
| 67 //! The default value is `false`. |
| 68 //! |
| 69 //! \param[out] enabled Whether crash reports should be uploaded. |
| 70 //! |
| 71 //! \return On success, returns `true`, otherwise returns `false` with an |
| 72 //! error logged. |
| 73 bool GetUploadsEnabled(bool* enabled); |
| 74 |
| 75 //! \brief Sets the user’s preference for submitting crash reports to a |
| 76 //! collection server. |
| 77 //! |
| 78 //! \param[in] enabled Whether crash reports should be uploaded. |
| 79 //! |
| 80 //! \return On success, returns `true`, otherwise returns `false` with an |
| 81 //! error logged. |
| 82 bool SetUploadsEnabled(bool enabled); |
| 83 |
| 84 //! \brief Retrieves the last time at which a report was attempted to be |
| 85 //! uploaded. |
| 86 //! |
| 87 //! The default value is `0` if it has never been set before. |
| 88 //! |
| 89 //! \param[out] time The last time at which a report was uploaded. |
| 90 //! |
| 91 //! \return On success, returns `true`, otherwise returns `false` with an |
| 92 //! error logged. |
| 93 bool GetLastUploadAttemptTime(time_t* time); |
| 94 |
| 95 //! \brief Sets the last time at which a report was attempted to be uploaded. |
| 96 //! |
| 97 //! This is only meant to be used internally by the CrashReportDatabase. |
| 98 //! |
| 99 //! \param[in] time The last time at which a report was uploaded. |
| 100 //! |
| 101 //! \return On success, returns `true`, otherwise returns `false` with an |
| 102 //! error logged. |
| 103 bool SetLastUploadAttemptTime(time_t time); |
| 104 |
| 105 private: |
| 106 struct Data; |
| 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, internal::ScopedLockedFileHandleTraits>; |
| 112 static ScopedLockedFileHandle MakeScopedLockedFileHandle(FileHandle file, |
| 113 FileLocking locking); |
| 114 |
| 115 // Opens the settings file for reading. On error, logs a message and returns |
| 116 // the invalid handle. |
| 117 ScopedLockedFileHandle OpenForReading(); |
| 118 |
| 119 // Opens the settings file for reading and writing. On error, logs a message |
| 120 // and returns the invalid handle. |mode| determines how the file will be |
| 121 // opened. |mode| must not be FileWriteMode::kTruncateOrCreate. |
| 122 // |
| 123 // If |log_open_error| is false, nothing will be logged for an error |
| 124 // encountered when attempting to open the file, but this method will still |
| 125 // return false. This is intended to be used to suppress error messages when |
| 126 // attempting to create a new settings file when multiple attempts are made. |
| 127 ScopedLockedFileHandle OpenForReadingAndWriting(FileWriteMode mode, |
| 128 bool log_open_error); |
| 129 |
| 130 // Opens the settings file and reads the data. If that fails, an error will |
| 131 // be logged and the settings will be recovered and re-initialized. If that |
| 132 // also fails, returns false with additional log data from recovery. |
| 133 bool OpenAndReadSettings(Data* out_data); |
| 134 |
| 135 // Opens the settings file for writing and reads the data. If reading fails, |
| 136 // recovery is attempted. Returns the opened file handle on success, or the |
| 137 // invalid file handle on failure, with an error logged. |
| 138 ScopedLockedFileHandle OpenForWritingAndReadSettings(Data* out_data); |
| 139 |
| 140 // Reads the settings from |handle|. Logs an error and returns false on |
| 141 // failure. This does not perform recovery. |
| 142 // |
| 143 // |handle| must be the result of OpenForReading() or |
| 144 // OpenForReadingAndWriting(). |
| 145 // |
| 146 // If |log_read_error| is false, nothing will be logged for a read error, but |
| 147 // this method will still return false. This is intended to be used to |
| 148 // suppress error messages when attempting to read a newly created settings |
| 149 // file. |
| 150 bool ReadSettings(FileHandle handle, Data* out_data, bool log_read_error); |
| 151 |
| 152 // Writes the settings to |handle|. Logs an error and returns false on |
| 153 // failure. This does not perform recovery. |
| 154 // |
| 155 // |handle| must be the result of OpenForReadingAndWriting(). |
| 156 bool WriteSettings(FileHandle handle, const Data& data); |
| 157 |
| 158 // Recovers the settings file by re-initializing the data. If |handle| is the |
| 159 // invalid handle, this will open the file; if it is not, then it must be the |
| 160 // result of OpenForReadingAndWriting(). If the invalid handle is passed, the |
| 161 // caller must not be holding the handle. The new settings data are stored in |
| 162 // |out_data|. Returns true on success and false on failure, with an error |
| 163 // logged. |
| 164 bool RecoverSettings(FileHandle handle, Data* out_data); |
| 165 |
| 166 // Initializes a settings file and writes the data to |handle|. Returns true |
| 167 // on success and false on failure, with an error logged. |
| 168 // |
| 169 // |handle| must be the result of OpenForReadingAndWriting(). |
| 170 bool InitializeSettings(FileHandle handle); |
| 171 |
| 172 const base::FilePath& file_path() const { return file_path_; } |
| 173 |
| 174 base::FilePath file_path_; |
| 175 |
| 176 InitializationState initialized_; |
| 177 |
| 178 DISALLOW_COPY_AND_ASSIGN(Settings); |
| 179 }; |
| 180 |
| 181 } // namespace crashpad |
| 182 |
| 183 #endif // CRASHPAD_CLIENT_SETTINGS_H_ |
| OLD | NEW |