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
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@bde4bf8dcd56e41cc064d613219dc3382c178856",
"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",
}
@@ -7,6 +7,7 @@

#include <memory>
#include <utility>
#include <string>
#include <vector>

#include "brave/common/extensions/api/brave_sync.h"
@@ -88,7 +89,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,
@@ -203,13 +203,13 @@ void SyncUIDOMHandler::SyncSavedSiteSettings(const base::ListValue* args) {

void SyncUIDOMHandler::DeleteDevice(const base::ListValue* args) {
DCHECK(sync_service_);
int i_device_id = -1;
if (!args->GetInteger(0, &i_device_id) || i_device_id == -1) {
DCHECK(false) << "[Brave Sync] could not get device id";
std::string device_id_v2;
if (!args->GetString(0, &device_id_v2)) {
DCHECK(false) << "[Brave Sync] could not get device id v2";
return;
}

sync_service_->OnDeleteDevice(std::to_string(i_device_id));
sync_service_->OnDeleteDevice(device_id_v2);
}

void SyncUIDOMHandler::ResetSync(const base::ListValue* args) {
@@ -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
}
]
},
@@ -4,6 +4,16 @@ import("//tools/grit/grit_rule.gni")
import("//tools/grit/repack.gni")
import("buildflags/buildflags.gni")

declare_args() {
brave_sync_endpoint = "https://sync.brave.com"
}

config("brave_sync_config") {
defines = [
"BRAVE_SYNC_ENDPOINT=\"$brave_sync_endpoint\""
]
}

if (enable_brave_sync) {
source_set("js_sync_lib_impl") {
sources = [
@@ -18,6 +28,7 @@ if (enable_brave_sync) {
"client/client_ext_impl_data.h",
]

configs += [ ":brave_sync_config" ]
deps = [
":core",
":crypto",
@@ -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;
}
@@ -280,17 +283,19 @@ void BraveProfileSyncServiceImpl::OnSetupSyncNewToSync(
brave_sync_prefs_->SetSyncEnabled(true);
}

void BraveProfileSyncServiceImpl::OnDeleteDevice(const std::string& device_id) {
void BraveProfileSyncServiceImpl::OnDeleteDevice(
const std::string& device_id_v2) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
auto sync_devices = brave_sync_prefs_->GetSyncDevices();

const SyncDevice* device = sync_devices->GetByDeviceId(device_id);
const SyncDevice* device = sync_devices->GetByDeviceIdV2(device_id_v2);
if (device) {
const std::string device_name = device->name_;
const std::string device_id = device->device_id_;
const std::string object_id = device->object_id_;
SendDeviceSyncRecord(SyncRecord::Action::A_DELETE, device_name, device_id,
object_id);
if (device_id == brave_sync_prefs_->GetThisDeviceId()) {
device_id_v2, object_id);
if (device_id_v2 == brave_sync_prefs_->GetThisDeviceIdV2()) {
// Mark state we have sent DELETE for own device and we are going to
// call ResetSyncInternal() at OnRecordsSent after ensuring we had made
// a proper attemp to send the record
@@ -311,8 +316,8 @@ void BraveProfileSyncServiceImpl::OnResetSync() {
} else {
// We have to send delete record and wait for library deleted response then
// we can reset it by ResetSyncInternal()
const std::string device_id = brave_sync_prefs_->GetThisDeviceId();
OnDeleteDevice(device_id);
const std::string device_id_v2 = brave_sync_prefs_->GetThisDeviceIdV2();
OnDeleteDevice(device_id_v2);
}
}

@@ -407,6 +412,14 @@ void BraveProfileSyncServiceImpl::OnGetInitData(
VLOG(1) << "[Brave Sync] Init empty device id";
}

std::string device_id_v2;
if (!brave_sync_prefs_->GetThisDeviceIdV2().empty()) {
device_id_v2 = brave_sync_prefs_->GetThisDeviceIdV2();
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 +428,20 @@ void BraveProfileSyncServiceImpl::OnGetInitData(

client_data::Config config;
config.api_version = brave_sync_prefs_->GetApiVersion();
config.server_url = "https://sync.brave.com";
config.server_url = BRAVE_SYNC_ENDPOINT;
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 +451,10 @@ 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_->GetThisDeviceIdV2().empty())
send_device_id_v2_update_ = true;
brave_sync_prefs_->SetThisDeviceIdV2(device_id_v2);

brave_sync_initializing_ = false;
}
@@ -860,48 +877,85 @@ void BraveProfileSyncServiceImpl::SendCreateDevice() {

std::string device_name = brave_sync_prefs_->GetThisDeviceName();
std::string object_id = tools::GenerateObjectId();
brave_sync_prefs_->SetThisDeviceObjectId(object_id);
std::string device_id = brave_sync_prefs_->GetThisDeviceId();
CHECK(!device_id.empty());
std::string device_id_v2 = brave_sync_prefs_->GetThisDeviceIdV2();
DCHECK(!device_id_v2.empty());

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

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

std::string device_name = brave_sync_prefs_->GetThisDeviceName();
std::string object_id = brave_sync_prefs_->GetThisDeviceObjectId();
std::string device_id = brave_sync_prefs_->GetThisDeviceId();
std::string device_id_v2 = brave_sync_prefs_->GetThisDeviceIdV2();
if (object_id.empty()) {
auto sync_devices = brave_sync_prefs_->GetSyncDevices();
std::vector<const SyncDevice*> devices =
sync_devices->GetByDeviceId(device_id);
for (auto* device : devices) {
if (device) {
object_id = device->object_id_;
}
SendDeviceSyncRecord(SyncRecord::Action::A_DELETE, device_name, device_id,
device_id_v2, object_id);
}
DCHECK(!object_id.empty());
} else {
DCHECK(!device_id_v2.empty());

SendDeviceSyncRecord(SyncRecord::Action::A_DELETE, 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);
RecordsListPtr records = CreateDeviceRecord(
device_name, object_id, static_cast<SyncRecord::Action>(action),
device_id, device_id_v2);
SendSyncRecords(SyncRecordType_PREFERENCES, std::move(records));
}

void BraveProfileSyncServiceImpl::OnResolvedPreferences(
const RecordsList& records) {
const std::string this_device_id = brave_sync_prefs_->GetThisDeviceId();
const std::string this_device_object_id =
brave_sync_prefs_->GetThisDeviceObjectId();
const std::string this_device_id_v2 =
brave_sync_prefs_->GetThisDeviceIdV2();
bool this_device_deleted = false;

auto sync_devices = brave_sync_prefs_->GetSyncDevices();
for (const auto& record : records) {
DCHECK(record->has_device() || record->has_sitesetting());
if (record->has_device()) {
bool actually_merged = false;
sync_devices->Merge(
SyncDevice(record->GetDevice().name, record->objectId,
record->deviceId, record->syncTimestamp.ToJsTime()),
record->action, &actually_merged);
auto& device = record->GetDevice();
sync_devices->Merge(SyncDevice(record->GetDevice().name, record->objectId,
record->deviceId, device.deviceIdV2,
record->syncTimestamp.ToJsTime()),
record->action, &actually_merged);
// We check object id here specifically because device which doesn't have
// device id v2 also doesn't have this object id stored. So we use this
// trait for migration.
this_device_deleted =
this_device_deleted ||
(record->deviceId == this_device_id &&
(record->objectId == this_device_object_id &&
device.deviceIdV2 == this_device_id_v2 &&
record->action == SyncRecord::Action::A_DELETE && actually_merged);
}
} // for each device

brave_sync_prefs_->SetSyncDevices(*sync_devices);

if (this_device_deleted) {
ResetSyncInternal();
}
@@ -950,6 +1004,15 @@ void BraveProfileSyncServiceImpl::OnPollSyncCycle(GetRecordsCallback cb,
SendCreateDevice();
this_device_created_time_ = base::Time::Now();
}
if (send_device_id_v2_update_) {
// Because device id might get duplicated and we didn't save object id for
// this device so there is no way to send update to propagate device id v2,
// We have to delete previous device records by device id and create a new
// one.
SendDeleteDevice();
SendCreateDevice();
send_device_id_v2_update_ = false;
}

FetchDevices();

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