Pass FilePath to Settings in Initialize()

Pulled out of jperaza's https://crrev.com/c/689745.

Future updates to the CrashReportDatabase would like to be decide on the
Settings location later than the constructor, but still keep the Settings
object embedded inline. To allow this, pass the location FilePath in
Initialize() rather than to the constructor.

Bug: crashpad:206
Change-Id: I8792188314541f6fd0bd04b168d22f8e445bc187
Reviewed-on: https://chromium-review.googlesource.com/916533
Commit-Queue: Scott Graham <scottmg@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
This commit is contained in:
Scott Graham 2018-02-13 14:25:14 -08:00 committed by Commit Bot
parent f878f15517
commit e5bbdaff87
5 changed files with 33 additions and 31 deletions

View File

@ -243,7 +243,7 @@ class CrashReportDatabaseMac : public CrashReportDatabase {
CrashReportDatabaseMac::CrashReportDatabaseMac(const base::FilePath& path) CrashReportDatabaseMac::CrashReportDatabaseMac(const base::FilePath& path)
: CrashReportDatabase(), : CrashReportDatabase(),
base_dir_(path), base_dir_(path),
settings_(base_dir_.Append(kSettings)), settings_(),
xattr_new_names_(false), xattr_new_names_(false),
initialized_() { initialized_() {
} }
@ -268,7 +268,7 @@ bool CrashReportDatabaseMac::Initialize(bool may_create) {
return false; return false;
} }
if (!settings_.Initialize()) if (!settings_.Initialize(base_dir_.Append(kSettings)))
return false; return false;
// Do an xattr operation as the last step, to ensure the filesystem has // Do an xattr operation as the last step, to ensure the filesystem has

View File

@ -610,11 +610,7 @@ class CrashReportDatabaseWin : public CrashReportDatabase {
}; };
CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path)
: CrashReportDatabase(), : CrashReportDatabase(), base_dir_(path), settings_(), initialized_() {}
base_dir_(path),
settings_(base_dir_.Append(kSettings)),
initialized_() {
}
CrashReportDatabaseWin::~CrashReportDatabaseWin() { CrashReportDatabaseWin::~CrashReportDatabaseWin() {
} }
@ -634,7 +630,7 @@ bool CrashReportDatabaseWin::Initialize(bool may_create) {
if (!CreateDirectoryIfNecessary(base_dir_.Append(kReportsDirectory))) if (!CreateDirectoryIfNecessary(base_dir_.Append(kReportsDirectory)))
return false; return false;
if (!settings_.Initialize()) if (!settings_.Initialize(base_dir_.Append(kSettings)))
return false; return false;
INITIALIZATION_STATE_SET_VALID(initialized_); INITIALIZATION_STATE_SET_VALID(initialized_);

View File

@ -59,16 +59,14 @@ struct Settings::Data {
UUID client_id; UUID client_id;
}; };
Settings::Settings(const base::FilePath& file_path) Settings::Settings() = default;
: file_path_(file_path),
initialized_() {
}
Settings::~Settings() { Settings::~Settings() = default;
}
bool Settings::Initialize() { bool Settings::Initialize(const base::FilePath& file_path) {
DCHECK(initialized_.is_uninitialized());
initialized_.set_invalid(); initialized_.set_invalid();
file_path_ = file_path;
Data settings; Data settings;
if (!OpenForWritingAndReadSettings(&settings).is_valid()) if (!OpenForWritingAndReadSettings(&settings).is_valid())

View File

@ -44,10 +44,18 @@ struct ScopedLockedFileHandleTraits {
//! should be retrieved via CrashReportDatabase::GetSettings(). //! should be retrieved via CrashReportDatabase::GetSettings().
class Settings { class Settings {
public: public:
explicit Settings(const base::FilePath& file_path); Settings();
~Settings(); ~Settings();
bool Initialize(); //! \brief Initializes the settings data store.
//!
//! This method must be called only once, and must be successfully called
//! before any other method in this class may be called.
//!
//! \param[in] path The location to store the settings data.
//! \return `true` if the data store was initialized successfully, otherwise
//! `false` with an error logged.
bool Initialize(const base::FilePath& path);
//! \brief Retrieves the immutable identifier for this client, which is used //! \brief Retrieves the immutable identifier for this client, which is used
//! on a server to locate all crash reports from a specific Crashpad //! on a server to locate all crash reports from a specific Crashpad

View File

@ -26,7 +26,7 @@ namespace {
class SettingsTest : public testing::Test { class SettingsTest : public testing::Test {
public: public:
SettingsTest() : settings_(settings_path()) {} SettingsTest() = default;
base::FilePath settings_path() { base::FilePath settings_path() {
return temp_dir_.path().Append(FILE_PATH_LITERAL("settings")); return temp_dir_.path().Append(FILE_PATH_LITERAL("settings"));
@ -49,7 +49,7 @@ class SettingsTest : public testing::Test {
protected: protected:
// testing::Test: // testing::Test:
void SetUp() override { void SetUp() override {
ASSERT_TRUE(settings()->Initialize()); ASSERT_TRUE(settings()->Initialize(settings_path()));
} }
private: private:
@ -64,8 +64,8 @@ TEST_F(SettingsTest, ClientID) {
EXPECT_TRUE(settings()->GetClientID(&client_id)); EXPECT_TRUE(settings()->GetClientID(&client_id));
EXPECT_NE(client_id, UUID()); EXPECT_NE(client_id, UUID());
Settings local_settings(settings_path()); Settings local_settings;
EXPECT_TRUE(local_settings.Initialize()); EXPECT_TRUE(local_settings.Initialize(settings_path()));
UUID actual; UUID actual;
EXPECT_TRUE(local_settings.GetClientID(&actual)); EXPECT_TRUE(local_settings.GetClientID(&actual));
EXPECT_EQ(actual, client_id); EXPECT_EQ(actual, client_id);
@ -81,8 +81,8 @@ TEST_F(SettingsTest, UploadsEnabled) {
EXPECT_TRUE(settings()->GetUploadsEnabled(&enabled)); EXPECT_TRUE(settings()->GetUploadsEnabled(&enabled));
EXPECT_TRUE(enabled); EXPECT_TRUE(enabled);
Settings local_settings(settings_path()); Settings local_settings;
EXPECT_TRUE(local_settings.Initialize()); EXPECT_TRUE(local_settings.Initialize(settings_path()));
enabled = false; enabled = false;
EXPECT_TRUE(local_settings.GetUploadsEnabled(&enabled)); EXPECT_TRUE(local_settings.GetUploadsEnabled(&enabled));
EXPECT_TRUE(enabled); EXPECT_TRUE(enabled);
@ -107,8 +107,8 @@ TEST_F(SettingsTest, LastUploadAttemptTime) {
EXPECT_TRUE(settings()->GetLastUploadAttemptTime(&actual)); EXPECT_TRUE(settings()->GetLastUploadAttemptTime(&actual));
EXPECT_EQ(actual, expected); EXPECT_EQ(actual, expected);
Settings local_settings(settings_path()); Settings local_settings;
EXPECT_TRUE(local_settings.Initialize()); EXPECT_TRUE(local_settings.Initialize(settings_path()));
actual = -1; actual = -1;
EXPECT_TRUE(local_settings.GetLastUploadAttemptTime(&actual)); EXPECT_TRUE(local_settings.GetLastUploadAttemptTime(&actual));
EXPECT_EQ(actual, expected); EXPECT_EQ(actual, expected);
@ -120,8 +120,8 @@ TEST_F(SettingsTest, LastUploadAttemptTime) {
TEST_F(SettingsTest, BadFileOnInitialize) { TEST_F(SettingsTest, BadFileOnInitialize) {
InitializeBadFile(); InitializeBadFile();
Settings settings(settings_path()); Settings settings;
EXPECT_TRUE(settings.Initialize()); EXPECT_TRUE(settings.Initialize(settings_path()));
} }
TEST_F(SettingsTest, BadFileOnGet) { TEST_F(SettingsTest, BadFileOnGet) {
@ -131,8 +131,8 @@ TEST_F(SettingsTest, BadFileOnGet) {
EXPECT_TRUE(settings()->GetClientID(&client_id)); EXPECT_TRUE(settings()->GetClientID(&client_id));
EXPECT_NE(client_id, UUID()); EXPECT_NE(client_id, UUID());
Settings local_settings(settings_path()); Settings local_settings;
EXPECT_TRUE(local_settings.Initialize()); EXPECT_TRUE(local_settings.Initialize(settings_path()));
UUID actual; UUID actual;
EXPECT_TRUE(local_settings.GetClientID(&actual)); EXPECT_TRUE(local_settings.GetClientID(&actual));
EXPECT_EQ(actual, client_id); EXPECT_EQ(actual, client_id);
@ -161,8 +161,8 @@ TEST_F(SettingsTest, UnlinkFile) {
<< ErrnoMessage("unlink"); << ErrnoMessage("unlink");
#endif #endif
Settings local_settings(settings_path()); Settings local_settings;
EXPECT_TRUE(local_settings.Initialize()); EXPECT_TRUE(local_settings.Initialize(settings_path()));
UUID new_client_id; UUID new_client_id;
EXPECT_TRUE(local_settings.GetClientID(&new_client_id)); EXPECT_TRUE(local_settings.GetClientID(&new_client_id));
EXPECT_NE(new_client_id, client_id); EXPECT_NE(new_client_id, client_id);