Commit b15df0e2 authored by Michael Chang's avatar Michael Chang Committed by Commit Bot

Implement Registration API with PersistedData

Bug: 1042224
Change-Id: I165b2ab1d625ceac23c31efd92b5e2db82d722a0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2079262
Commit-Queue: Michael Chang <donchan@microsoft.com>
Reviewed-by: default avatarSorin Jianu <sorin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#746119}
parent ef326b6a
......@@ -4,11 +4,15 @@
#include "chrome/updater/persisted_data.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "base/version.h"
#include "build/build_config.h"
#include "chrome/updater/registration_data.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
......@@ -17,8 +21,11 @@ namespace {
// Uses the same pref as the update_client code.
constexpr char kPersistedDataPreference[] = "updateclientdata";
constexpr char kPV[] = "pv";
constexpr char kFP[] = "fp";
constexpr char kPV[] = "pv"; // Key for storing product version.
constexpr char kFP[] = "fp"; // Key for storing fingerprint.
constexpr char kECP[] = "ecp"; // Key for storing existence checker path.
constexpr char kBC[] = "bc"; // Key for storing brand code.
constexpr char kTG[] = "tg"; // Key for storing tag.
} // namespace
......@@ -57,6 +64,45 @@ void PersistedData::SetFingerprint(const std::string& id,
SetString(id, kFP, fingerprint);
}
base::FilePath PersistedData::GetExistenceCheckerPath(
const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return base::FilePath().AppendASCII(GetString(id, kECP));
}
void PersistedData::SetExistenceCheckerPath(const std::string& id,
const base::FilePath& ecp) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
SetString(id, kECP, ecp.AsUTF8Unsafe());
}
std::string PersistedData::GetBrandCode(const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return GetString(id, kBC);
}
void PersistedData::SetBrandCode(const std::string& id, const std::string& bc) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
SetString(id, kBC, bc);
}
std::string PersistedData::GetTag(const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return GetString(id, kTG);
}
void PersistedData::SetTag(const std::string& id, const std::string& tag) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
SetString(id, kTG, tag);
}
void PersistedData::RegisterApp(const RegistrationRequest& rq) {
SetProductVersion(rq.app_id, rq.version);
SetExistenceCheckerPath(rq.app_id, rq.existence_checker_path);
SetBrandCode(rq.app_id, rq.brand_code);
SetTag(rq.app_id, rq.tag);
}
std::vector<std::string> PersistedData::GetAppIds() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
......
......@@ -14,11 +14,14 @@
class PrefService;
namespace base {
class FilePath;
class Version;
}
} // namespace base
namespace updater {
struct RegistrationRequest;
// PersistedData uses the PrefService to persist updater data that outlives
// the updater processes.
//
......@@ -44,6 +47,23 @@ class PersistedData : public base::RefCounted<PersistedData> {
std::string GetFingerprint(const std::string& id) const;
void SetFingerprint(const std::string& id, const std::string& fp);
// These functions access the existence checker path for the specified id.
base::FilePath GetExistenceCheckerPath(const std::string& id) const;
void SetExistenceCheckerPath(const std::string& id,
const base::FilePath& ecp);
// These functions access the brand code for the specified id.
std::string GetBrandCode(const std::string& id) const;
void SetBrandCode(const std::string& id, const std::string& bc);
// These functions access the tag for the specified id.
std::string GetTag(const std::string& id) const;
void SetTag(const std::string& id, const std::string& tag);
// This function sets everything in the registration request object into the
// persistent data store.
void RegisterApp(const RegistrationRequest& rq);
// Returns the app ids of the applications registered in prefs, if the
// application has a valid version.
std::vector<std::string> GetAppIds() const;
......
......@@ -9,6 +9,7 @@
#include "base/stl_util.h"
#include "base/version.h"
#include "chrome/updater/persisted_data.h"
#include "chrome/updater/registration_data.h"
#include "components/prefs/testing_pref_service.h"
#include "components/update_client/update_client.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -43,6 +44,29 @@ TEST(PersistedDataTest, Simple) {
EXPECT_FALSE(base::Contains(app_ids, "appid2-nopv")); // No valid pv.
}
TEST(PersistedDataTest, RegistrationRequest) {
auto pref = std::make_unique<TestingPrefServiceSimple>();
update_client::RegisterPrefs(pref->registry());
auto metadata = base::MakeRefCounted<PersistedData>(pref.get());
RegistrationRequest data;
data.app_id = "someappid";
data.brand_code = "somebrand";
data.tag = "arandom-tag=likethis";
data.version = base::Version("1.0");
data.existence_checker_path =
base::FilePath(FILE_PATH_LITERAL("some/file/path"));
metadata->RegisterApp(data);
EXPECT_TRUE(metadata->GetProductVersion("someappid").IsValid());
EXPECT_STREQ("1.0",
metadata->GetProductVersion("someappid").GetString().c_str());
EXPECT_EQ(FILE_PATH_LITERAL("some/file/path"),
metadata->GetExistenceCheckerPath("someappid").value());
EXPECT_STREQ("arandom-tag=likethis", metadata->GetTag("someappid").c_str());
EXPECT_STREQ("somebrand", metadata->GetBrandCode("someappid").c_str());
}
TEST(PersistedDataTest, SharedPref) {
auto pref = std::make_unique<TestingPrefServiceSimple>();
update_client::RegisterPrefs(pref->registry());
......
......@@ -45,6 +45,7 @@ struct RegistrationRequest {
struct RegistrationResponse {
RegistrationResponse() = default;
explicit RegistrationResponse(int status_code) : status_code(status_code) {}
~RegistrationResponse() = default;
// Status code of the registration. 0 = success. All others = failure.
......
......@@ -40,10 +40,13 @@ void UpdateServiceInProcess::RegisterApp(
base::OnceCallback<void(const RegistrationResponse&)> callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// For now just do the callback.
const RegistrationResponse response;
main_task_runner_->PostTask(FROM_HERE,
base::BindOnce(std::move(callback), response));
persisted_data_->RegisterApp(request);
// Result of registration. Currently there's no error handling in
// PersistedData, so we assume success every time, which is why we respond
// with 0.
main_task_runner_->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), RegistrationResponse(0)));
}
void UpdateServiceInProcess::UpdateAll(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment