Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply device id v2 #4159

Merged
merged 10 commits into from Dec 30, 2019
Next

deviceIdV2 and extra SAVE_INIT_DATA for migration

  • Loading branch information
darkdh authored and bridiver committed Nov 13, 2019
commit 6805ec2c47fbda01a68df574fac842010cc7da97
2 DEPS
@@ -15,7 +15,7 @@ deps = {
"vendor/bip39wally-core-native": "https://github.com/brave-intl/bip39wally-core-native.git@13bb40a215248cfbdd87d0a6b425c8397402e9e6",
"vendor/bat-native-anonize": "https://github.com/brave-intl/bat-native-anonize.git@e3742ba3e8942eea9e4755d91532491871bd3116",
"vendor/bat-native-tweetnacl": "https://github.com/brave-intl/bat-native-tweetnacl.git@800f9d40b7409239ff192e0be634764e747c7a75",
"components/brave_sync/extension/brave-sync": "https://github.com/brave/sync.git@14682d439b4bbcbf1bad3c6c2442287f1b4b7854",
"components/brave_sync/extension/brave-sync": "https://github.com/brave/sync.git@77af1794c25d79bbb64b70bfcea0a0effd611798",
"vendor/bat-native-usermodel": "https://github.com/brave-intl/bat-native-usermodel.git@45e32155af9897dbe1d5534dd36697ec4728bb75",
"vendor/challenge_bypass_ristretto_ffi": "https://github.com/brave-intl/challenge-bypass-ristretto-ffi.git@c396fb4eb9e9bf63b89ae5a0ec0b5f201d43c7c5",
}
@@ -88,7 +88,8 @@ ExtensionFunction::ResponseAction BraveSyncSaveInitDataFunction::Run() {
DCHECK(sync_service);
sync_service->GetBraveSyncClient()->sync_message_handler()->OnSaveInitData(
params->seed ? *params->seed : std::vector<uint8_t>(),
params->device_id ? *params->device_id : std::vector<uint8_t>());
params->device_id ? *params->device_id : std::vector<uint8_t>(),
params->device_id_v2 ? *params->device_id_v2 : std::string());

return RespondNow(NoArguments());
}
@@ -25,13 +25,15 @@ BraveSyncEventRouter::~BraveSyncEventRouter() {}
void BraveSyncEventRouter::GotInitData(
const brave_sync::Uint8Array& seed,
const brave_sync::Uint8Array& device_id,
const extensions::api::brave_sync::Config& config) {
const extensions::api::brave_sync::Config& config,
const std::string& device_id_v2) {
const std::vector<int> arg_seed(seed.begin(), seed.end());
const std::vector<int> arg_device_id(device_id.begin(), device_id.end());

std::unique_ptr<base::ListValue> args(
extensions::api::brave_sync::OnGotInitData::Create(arg_seed,
arg_device_id, config)
arg_device_id, config,
device_id_v2)
.release());
std::unique_ptr<Event> event(
new Event(extensions::events::FOR_TEST,
@@ -37,7 +37,8 @@ class BraveSyncEventRouter {

void GotInitData(const brave_sync::Uint8Array& seed,
const brave_sync::Uint8Array& device_id,
const extensions::api::brave_sync::Config& config);
const extensions::api::brave_sync::Config& config,
const std::string& device_id_v2);

void FetchSyncRecords(
const std::vector<std::string>& category_names,
@@ -94,7 +94,8 @@
"type": "object",
"description": "Represents sync device",
"properties": {
"name" : {"type": "string"}
"name" : {"type": "string"},
"deviceIdV2" : {"type": "string"}
}
},
{
@@ -200,6 +201,11 @@
{
"$ref": "Config",
"name": "config"
},
{
"type": "string",
"name": "device_id_v2",
"optional": true
}
]
},
@@ -356,6 +362,11 @@
"type": "binary",
"name": "device_id",
"optional": true
},
{
"type": "string",
"name": "device_id_v2",
"optional": true
}
]
},
@@ -81,7 +81,8 @@ std::string GetDeviceName() {
RecordsListPtr CreateDeviceRecord(const std::string& device_name,
const std::string& object_id,
const SyncRecord::Action& action,
const std::string& device_id) {
const std::string& device_id,
const std::string& device_id_v2) {
RecordsListPtr records = std::make_unique<RecordsList>();

SyncRecordPtr record = std::make_unique<SyncRecord>();
@@ -93,6 +94,7 @@ RecordsListPtr CreateDeviceRecord(const std::string& device_name,

std::unique_ptr<Device> device = std::make_unique<Device>();
device->name = device_name;
device->deviceIdV2 = device_id_v2;
record->SetDevice(std::move(device));

records->emplace_back(std::move(record));
@@ -157,6 +159,7 @@ SyncRecordPtr PrepareResolvedDevice(SyncDevice* device,
std::unique_ptr<jslib::Device> device_record =
std::make_unique<jslib::Device>();
device_record->name = device->name_;
device_record->deviceIdV2 = device->device_id_v2_;
record->SetDevice(std::move(device_record));
return record;
}
@@ -287,9 +290,10 @@ void BraveProfileSyncServiceImpl::OnDeleteDevice(const std::string& device_id) {
const SyncDevice* device = sync_devices->GetByDeviceId(device_id);
if (device) {
const std::string device_name = device->name_;
const std::string device_id_v2 = device->device_id_v2_;
const std::string object_id = device->object_id_;
SendDeviceSyncRecord(SyncRecord::Action::A_DELETE, device_name, device_id,
object_id);
device_id_v2, object_id);
if (device_id == brave_sync_prefs_->GetThisDeviceId()) {
// Mark state we have sent DELETE for own device and we are going to
// call ResetSyncInternal() at OnRecordsSent after ensuring we had made
@@ -407,6 +411,14 @@ void BraveProfileSyncServiceImpl::OnGetInitData(
VLOG(1) << "[Brave Sync] Init empty device id";
}

std::string device_id_v2;
if (!brave_sync_prefs_->GetDeviceIdV2().empty()) {
device_id_v2 = brave_sync_prefs_->GetDeviceIdV2();
VLOG(1) << "[Brave Sync] Init device id_v2 from prefs: " << device_id_v2;
} else {
VLOG(1) << "[Brave Sync] Init empty device id_v2";
}

DCHECK(!sync_version.empty());
// TODO(bridiver) - this seems broken because using the version we get back
// from the server (currently v1.4.2) causes things to break. What is the
@@ -415,20 +427,19 @@ void BraveProfileSyncServiceImpl::OnGetInitData(

client_data::Config config;
config.api_version = brave_sync_prefs_->GetApiVersion();
config.server_url = "https://sync.brave.com";
config.server_url = "https://sync-staging.brave.com";

This comment has been minimized.

Copy link
@cezaraugusto

cezaraugusto Dec 16, 2019

Member

should this point to staging?

This comment has been minimized.

Copy link
@darkdh

darkdh Dec 16, 2019

Author Member

I will change it along with brave/sync bump once the PR in that repo is merged

This comment has been minimized.

Copy link
@bridiver

bridiver Dec 23, 2019

Collaborator

we should convert this to a gn config

config.debug = true;
brave_sync_client_->SendGotInitData(seed, device_id, config);
brave_sync_client_->SendGotInitData(seed, device_id, config, device_id_v2);
}

void BraveProfileSyncServiceImpl::OnSaveInitData(const Uint8Array& seed,
const Uint8Array& device_id) {
void BraveProfileSyncServiceImpl::OnSaveInitData(
const Uint8Array& seed, const Uint8Array& device_id,
const std::string& device_id_v2) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(!brave_sync_ready_);
// If we are here and brave_sync_initializing_ is false, we have came
// not from OnSetupSyncNewToSync or OnSetupSyncHaveCode.
// One case is we put wrong code words and then restarted before cleared
// kSyncEnabled pref. This should not happen.
DCHECK(brave_sync_initializing_);
// OnSaveInitData will not only be triggered by OnSetupSyncNewToSync or
// OnSetupSyncHaveCode, we use it to migrate device which doesn't have
// deviceIdV2

std::string seed_str = StrFromUint8Array(seed);
std::string device_id_str = StrFromUint8Array(device_id);
@@ -438,6 +449,9 @@ void BraveProfileSyncServiceImpl::OnSaveInitData(const Uint8Array& seed,

brave_sync_prefs_->SetSeed(seed_str);
brave_sync_prefs_->SetThisDeviceId(device_id_str);
if (!brave_sync_initializing_ && brave_sync_prefs_->GetDeviceIdV2().empty())
send_device_id_v2_update_ = true;
brave_sync_prefs_->SetDeviceIdV2(device_id_v2);

brave_sync_initializing_ = false;
}
@@ -861,21 +875,41 @@ void BraveProfileSyncServiceImpl::SendCreateDevice() {
std::string device_name = brave_sync_prefs_->GetThisDeviceName();
std::string object_id = tools::GenerateObjectId();
std::string device_id = brave_sync_prefs_->GetThisDeviceId();
std::string device_id_v2 = brave_sync_prefs_->GetDeviceIdV2();
CHECK(!device_id.empty());

SendDeviceSyncRecord(SyncRecord::Action::A_CREATE, device_name, device_id,
object_id);
device_id_v2, object_id);
}

void BraveProfileSyncServiceImpl::SendUpdateDevice() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

std::string device_id = brave_sync_prefs_->GetThisDeviceId();
std::string device_name = brave_sync_prefs_->GetThisDeviceName();
std::string device_id_v2 = brave_sync_prefs_->GetDeviceIdV2();
auto sync_devices = brave_sync_prefs_->GetSyncDevices();
// TODO(darkdh): need to get device by object id
const SyncDevice* device = sync_devices->GetByDeviceId(device_id);
if (device) {
std::string object_id = device->object_id_;

SendDeviceSyncRecord(SyncRecord::Action::A_UPDATE, device_name, device_id,
device_id_v2, object_id);
}
}

void BraveProfileSyncServiceImpl::SendDeviceSyncRecord(
const int action,
const std::string& device_name,
const std::string& device_id,
const std::string& device_id_v2,
const std::string& object_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
RecordsListPtr records =
CreateDeviceRecord(device_name, object_id,
static_cast<SyncRecord::Action>(action), device_id);
static_cast<SyncRecord::Action>(action), device_id,
device_id_v2);
SendSyncRecords(SyncRecordType_PREFERENCES, std::move(records));
}

@@ -889,9 +923,11 @@ void BraveProfileSyncServiceImpl::OnResolvedPreferences(
DCHECK(record->has_device() || record->has_sitesetting());
if (record->has_device()) {
bool actually_merged = false;
auto& device = record->GetDevice();
sync_devices->Merge(
SyncDevice(record->GetDevice().name, record->objectId,
record->deviceId, record->syncTimestamp.ToJsTime()),
record->deviceId, device.deviceIdV2,
record->syncTimestamp.ToJsTime()),
record->action, &actually_merged);
this_device_deleted =
this_device_deleted ||
@@ -950,6 +986,10 @@ void BraveProfileSyncServiceImpl::OnPollSyncCycle(GetRecordsCallback cb,
SendCreateDevice();
this_device_created_time_ = base::Time::Now();
}
if (send_device_id_v2_update_) {
SendUpdateDevice();
send_device_id_v2_update_ = false;
}

FetchDevices();

@@ -88,7 +88,8 @@ class BraveProfileSyncServiceImpl
void OnSyncSetupError(const std::string& error) override;
void OnGetInitData(const std::string& sync_version) override;
void OnSaveInitData(const brave_sync::Uint8Array& seed,
const brave_sync::Uint8Array& device_id) override;
const brave_sync::Uint8Array& device_id,
const std::string& device_id_v2) override;
void OnSyncReady() override;
void OnGetExistingObjects(const std::string& category_name,
std::unique_ptr<brave_sync::RecordsList> records,
@@ -172,9 +173,11 @@ class BraveProfileSyncServiceImpl
int max_records);
void FetchDevices();
void SendCreateDevice();
void SendUpdateDevice();
void SendDeviceSyncRecord(const int action,
const std::string& device_name,
const std::string& device_id,
const std::string& device_id_v2,
const std::string& object_id);
void OnResolvedPreferences(const brave_sync::RecordsList& records);
void OnBraveSyncPrefsChanged(const std::string& pref);
@@ -227,6 +230,8 @@ class BraveProfileSyncServiceImpl
// while being initializing
bool brave_sync_initializing_ = false;

bool send_device_id_v2_update_ = false;

Uint8Array seed_;

brave_sync::GetRecordsCallback get_record_cb_;
@@ -21,6 +21,7 @@ namespace brave_sync {
namespace prefs {

const char kSyncDeviceId[] = "brave_sync.device_id";
const char kSyncDeviceIdV2[] = "brave_sync.device_id_v2";
const char kSyncSeed[] = "brave_sync.seed";
const char kSyncPrevSeed[] = "brave_sync.previous_seed";
const char kSyncDeviceName[] = "brave_sync.device_name";
@@ -46,6 +47,7 @@ Prefs::Prefs(PrefService* pref_service) : pref_service_(pref_service) {}

void Prefs::RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterStringPref(prefs::kSyncDeviceId, std::string());
registry->RegisterStringPref(prefs::kSyncDeviceIdV2, std::string());
registry->RegisterStringPref(prefs::kSyncSeed, std::string());
registry->RegisterStringPref(prefs::kSyncPrevSeed, std::string());
registry->RegisterStringPref(prefs::kSyncDeviceName, std::string());
@@ -88,6 +90,15 @@ void Prefs::SetThisDeviceId(const std::string& device_id) {
pref_service_->SetString(kSyncDeviceId, device_id);
}

std::string Prefs::GetDeviceIdV2() const {
return pref_service_->GetString(kSyncDeviceIdV2);
}

void Prefs::SetDeviceIdV2(const std::string& device_id_v2) {
DCHECK(!device_id_v2.empty());
pref_service_->SetString(kSyncDeviceIdV2, device_id_v2);
}

std::string Prefs::GetThisDeviceName() const {
return pref_service_->GetString(kSyncDeviceName);
}
@@ -141,6 +152,7 @@ std::unique_ptr<brave_sync::Settings> Prefs::GetBraveSyncSettings() const {

settings->this_device_name_ = GetThisDeviceName();
settings->this_device_id_ = GetThisDeviceId();
settings->this_device_id_v2_ = GetDeviceIdV2();
settings->sync_this_device_ = GetSyncEnabled();
settings->sync_bookmarks_ = GetSyncBookmarksEnabled();
settings->sync_settings_ = GetSyncSiteSettingsEnabled();
@@ -34,6 +34,7 @@ namespace prefs {

// String of device id. Supposed to be an integer
extern const char kSyncDeviceId[];
extern const char kSyncDeviceIdV2[];
// String of 32 comma separated bytes
// like "145,58,125,111,85,164,236,38,204,67,40,31,182,114,14,152,242,..."
extern const char kSyncSeed[];
@@ -82,6 +83,8 @@ class Prefs {
void SetSeed(const std::string& seed);
std::string GetThisDeviceId() const;
void SetThisDeviceId(const std::string& device_id);
std::string GetDeviceIdV2() const;
void SetDeviceIdV2(const std::string& device_id_v2);
std::string GetThisDeviceName() const;
void SetThisDeviceName(const std::string& device_name);
std::string GetBookmarksBaseOrder();
@@ -35,7 +35,8 @@ class SyncMessageHandler {
virtual void OnGetInitData(const std::string &sync_version) = 0;
// SAVE_INIT_DATA
virtual void OnSaveInitData(const Uint8Array& seed,
const Uint8Array& device_id) = 0;
const Uint8Array& device_id,
const std::string& device_id_v2) = 0;
// SYNC_READY
virtual void OnSyncReady() = 0;
// GET_EXISTING_OBJECTS
@@ -68,7 +69,8 @@ class BraveSyncClient {

virtual void SendGotInitData(const Uint8Array& seed,
const Uint8Array& device_id,
const client_data::Config& config) = 0;
const client_data::Config& config,
const std::string& device_id_v2) = 0;
virtual void SendFetchSyncRecords(
const std::vector<std::string> &category_names,
const base::Time &startAt,
@@ -64,11 +64,13 @@ SyncMessageHandler* BraveSyncClientImpl::sync_message_handler() {

void BraveSyncClientImpl::SendGotInitData(const Uint8Array& seed,
const Uint8Array& device_id,
const client_data::Config& config) {
const client_data::Config& config,
const std::string& device_id_v2) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
extensions::api::brave_sync::Config config_extension;
ConvertConfig(config, &config_extension);
brave_sync_event_router_->GotInitData(seed, device_id, config_extension);
brave_sync_event_router_->GotInitData(seed, device_id, config_extension,
device_id_v2);
}

void BraveSyncClientImpl::SendFetchSyncRecords(
@@ -47,7 +47,8 @@ class BraveSyncClientImpl : public BraveSyncClient,
// Browser to BraveSync messages
void SendGotInitData(const Uint8Array& seed,
const Uint8Array& device_id,
const client_data::Config& config) override;
const client_data::Config& config,
const std::string& device_id_v2) override;
void SendFetchSyncRecords(
const std::vector<std::string> &category_names, const base::Time &startAt,
const int max_records) override;
@@ -41,6 +41,7 @@ std::unique_ptr<brave_sync::jslib::Device> FromExtDevice(
const extensions::api::brave_sync::Device &ext_device) {
auto device = std::make_unique<brave_sync::jslib::Device>();
device->name = ext_device.name;
device->deviceIdV2 = ext_device.device_id_v2;
return device;
}

@@ -204,6 +205,7 @@ std::unique_ptr<extensions::api::brave_sync::Device> FromLibDevice(
const jslib::Device &lib_device) {
auto ext_device = std::make_unique<extensions::api::brave_sync::Device>();
ext_device->name = lib_device.name;
ext_device->device_id_v2 = lib_device.deviceIdV2;
return ext_device;
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.