-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add KeyDataProvider interface to provide project keys.
This refactor will be used to decouple the requirement for a user to login to record and use StructuredMetrics in Chromium by using the device key. Bug: b/290096302 Change-Id: I160281df9f9d4c37da56507c652daf63adcac6a1 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4662392 Reviewed-by: Hirthanan Subenderan <hirthanan@google.com> Commit-Queue: Jong Ahn <jongahn@chromium.org> Cr-Commit-Position: refs/heads/main@{#1184868}
- Loading branch information
Jong Ahn
authored and
Chromium LUCI CQ
committed
Aug 17, 2023
1 parent
3c6f47b
commit 08828db
Showing
18 changed files
with
537 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
chrome/browser/metrics/structured/key_data_provider_ash.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/metrics/structured/key_data_provider_ash.h" | ||
|
||
namespace metrics::structured { | ||
namespace { | ||
// The delay period for the PersistentProto. | ||
constexpr int kSaveDelayMs = 1000; | ||
|
||
// The path used to store per-profile keys. Relative to the user's | ||
// cryptohome. This file is created by chromium. | ||
constexpr char kProfileKeyPath[] = "structured_metrics/keys"; | ||
|
||
// The path used to store per-device keys. This file is created by tmpfiles.d | ||
// on start and has its permissions and ownership set such that it is writable | ||
// by chronos. | ||
constexpr char kDeviceKeyPath[] = "/var/lib/metrics/structured/chromium/keys"; | ||
|
||
} // namespace | ||
|
||
KeyDataProviderAsh::KeyDataProviderAsh() | ||
: KeyDataProviderAsh(base::FilePath(kDeviceKeyPath), kSaveDelayMs) {} | ||
|
||
KeyDataProviderAsh::KeyDataProviderAsh(const base::FilePath& device_key_path, | ||
int write_delay_ms) | ||
: device_key_path_(device_key_path), write_delay_ms_(write_delay_ms) {} | ||
|
||
KeyDataProviderAsh::~KeyDataProviderAsh() = default; | ||
|
||
void KeyDataProviderAsh::InitializeDeviceKey(base::OnceClosure callback) { | ||
if (HasDeviceKey()) { | ||
return; | ||
} | ||
|
||
device_key_ = std::make_unique<KeyData>(device_key_path_, | ||
base::Milliseconds(write_delay_ms_), | ||
std::move(callback)); | ||
} | ||
|
||
void KeyDataProviderAsh::InitializeProfileKey( | ||
const base::FilePath& profile_path, | ||
base::OnceClosure callback) { | ||
// Only the primary user's keys should be loaded. If there is already is a | ||
// profile key, no-op. | ||
if (HasProfileKey()) { | ||
return; | ||
} | ||
|
||
profile_key_ = std::make_unique<KeyData>(profile_path.Append(kProfileKeyPath), | ||
base::Milliseconds(write_delay_ms_), | ||
std::move(callback)); | ||
} | ||
|
||
KeyData* KeyDataProviderAsh::GetDeviceKeyData() { | ||
DCHECK(HasDeviceKey()); | ||
return device_key_.get(); | ||
} | ||
|
||
KeyData* KeyDataProviderAsh::GetProfileKeyData() { | ||
DCHECK(HasProfileKey()); | ||
return profile_key_.get(); | ||
} | ||
|
||
void KeyDataProviderAsh::Purge() { | ||
if (HasDeviceKey()) { | ||
GetDeviceKeyData()->Purge(); | ||
} | ||
|
||
if (HasProfileKey()) { | ||
GetProfileKeyData()->Purge(); | ||
} | ||
} | ||
|
||
bool KeyDataProviderAsh::HasProfileKey() { | ||
return profile_key_ != nullptr; | ||
} | ||
|
||
bool KeyDataProviderAsh::HasDeviceKey() { | ||
return profile_key_ != nullptr; | ||
} | ||
|
||
} // namespace metrics::structured |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CHROME_BROWSER_METRICS_STRUCTURED_KEY_DATA_PROVIDER_ASH_H_ | ||
#define CHROME_BROWSER_METRICS_STRUCTURED_KEY_DATA_PROVIDER_ASH_H_ | ||
|
||
#include "components/metrics/structured/key_data_provider.h" | ||
|
||
namespace metrics::structured { | ||
|
||
// Implementation for Ash Chrome to provide keys to StructuredMetricsRecorder. | ||
// | ||
// Device keys are stored in a static path while profile keys are stored in the | ||
// user's cryptohome partition. | ||
// | ||
// InitializeProfileKey should only be called once for the primary user. All | ||
// subsequent calls will no-op. | ||
class KeyDataProviderAsh : public KeyDataProvider { | ||
public: | ||
KeyDataProviderAsh(const base::FilePath& device_key_path, int write_delay_ms); | ||
KeyDataProviderAsh(); | ||
~KeyDataProviderAsh() override; | ||
|
||
// KeyDataProvider: | ||
void InitializeDeviceKey(base::OnceClosure callback) override; | ||
void InitializeProfileKey(const base::FilePath& profile_path, | ||
base::OnceClosure callback) override; | ||
KeyData* GetDeviceKeyData() override; | ||
KeyData* GetProfileKeyData() override; | ||
void Purge() override; | ||
bool HasProfileKey() override; | ||
bool HasDeviceKey() override; | ||
|
||
private: | ||
const base::FilePath device_key_path_; | ||
int write_delay_ms_; | ||
|
||
std::unique_ptr<KeyData> device_key_; | ||
std::unique_ptr<KeyData> profile_key_; | ||
}; | ||
} // namespace metrics::structured | ||
|
||
#endif // CHROME_BROWSER_METRICS_STRUCTURED_KEY_DATA_PROVIDER_ASH_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef COMPONENTS_METRICS_STRUCTURED_KEY_DATA_PROVIDER_H_ | ||
#define COMPONENTS_METRICS_STRUCTURED_KEY_DATA_PROVIDER_H_ | ||
|
||
#include "components/metrics/structured/key_data.h" | ||
#include "third_party/abseil-cpp/absl/types/optional.h" | ||
|
||
namespace metrics::structured { | ||
|
||
// Interface to provide key data to be used for hashing projects. | ||
// | ||
// There are two types of keys: device keys and profile keys. Device keys will | ||
// be ready only InitializeDeviceKey has been called while profile keys should | ||
// be ready once InitializeProfileKey has been called. | ||
class KeyDataProvider { | ||
public: | ||
KeyDataProvider() = default; | ||
|
||
KeyDataProvider(const KeyDataProvider& key_data_provider) = delete; | ||
KeyDataProvider& operator=(const KeyDataProvider& key_data_provider) = delete; | ||
|
||
virtual ~KeyDataProvider() = default; | ||
|
||
// Initializes the device key data. | ||
virtual void InitializeDeviceKey(base::OnceClosure callback) = 0; | ||
|
||
// Called whenever a profile key should be initialized. | ||
virtual void InitializeProfileKey(const base::FilePath& profile_path, | ||
base::OnceClosure callback) = 0; | ||
|
||
// Returns the device key data. | ||
// | ||
// Returns nullptr if InitializeDeviceKey() has not been called or is in | ||
// progress. | ||
virtual KeyData* GetDeviceKeyData() = 0; | ||
|
||
// Returns the profile key data, if available. A call to HasProfileKey() | ||
// should guarantee that this value will not be nullptr. | ||
// | ||
// Returns nullptr otherwise. | ||
virtual KeyData* GetProfileKeyData() = 0; | ||
|
||
// Deletes all key data associated with the provider. | ||
virtual void Purge() = 0; | ||
|
||
virtual bool HasProfileKey() = 0; | ||
virtual bool HasDeviceKey() = 0; | ||
}; | ||
|
||
} // namespace metrics::structured | ||
|
||
#endif // COMPONENTS_METRICS_STRUCTURED_KEY_DATA_PROVIDER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.