Skip to content

Commit

Permalink
fixed threading for reset sync
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyBarabash committed Sep 19, 2018
1 parent d608bd5 commit 115a013
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 35 deletions.
6 changes: 4 additions & 2 deletions common/extensions/api/brave_sync.json
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,13 @@
"parameters": [
{
"type": "binary",
"name": "seed"
"name": "seed",
"optional": true
},
{
"type": "binary",
"name": "device_id"
"name": "device_id",
"optional": true
}
]
},
Expand Down
12 changes: 8 additions & 4 deletions components/brave_sync/api/brave_sync_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,16 @@ ExtensionFunction::ResponseAction BraveSyncSaveInitDataFunction::Run() {
brave_sync::SaveInitData::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());

LOG(ERROR) << "TAGAB BraveSyncSaveInitDataFunction::Run params->seed=" << params->seed.size();
LOG(ERROR) << "TAGAB BraveSyncSaveInitDataFunction::Run params->device_id=" << params->device_id.size();
if (params->seed) {
LOG(ERROR) << "TAGAB BraveSyncSaveInitDataFunction::Run params->seed=" << params->seed->size();
}
if (params->device_id) {
LOG(ERROR) << "TAGAB BraveSyncSaveInitDataFunction::Run params->device_id=" << params->device_id->size();
}
::brave_sync::BraveSyncClient* sync_client = ::brave_sync::BraveSyncClientFactory::GetForBrowserContext(browser_context());
sync_client->GetSyncToBrowserHandler()->OnSaveInitData(
::brave_sync::Uint8ArrayFromUnsignedCharVec(params->seed),
::brave_sync::Uint8ArrayFromUnsignedCharVec(params->device_id)
::brave_sync::Uint8ArrayFromUnsignedCharVec(params->seed ? *params->seed : std::vector<uint8_t>()),
::brave_sync::Uint8ArrayFromUnsignedCharVec(params->device_id ? *params->device_id : std::vector<uint8_t>() )
);

return RespondNow(NoArguments());
Expand Down
76 changes: 57 additions & 19 deletions components/brave_sync/controller_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,32 @@ void ControllerImpl::OnSetupSyncNewToSync(const std::string &device_name) {
void ControllerImpl::OnDeleteDevice(const std::string &device_id) {
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnDeleteDevice";
LOG(ERROR) << "TAGAB device_id="<<device_id;
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

CHECK(sync_client_ != nullptr);
CHECK(sync_initialized_);

task_runner_->PostTask(
FROM_HERE,
base::Bind(&ControllerImpl::OnDeleteDeviceFileWork, base::Unretained(this), device_id)
);
}

void ControllerImpl::OnDeleteDeviceFileWork(const std::string &device_id) {
LOG(ERROR) << "TAGAB ControllerImpl::OnDeleteDeviceFileWork";
std::string json = sync_obj_map_->GetObjectIdByLocalId(jslib_const::DEVICES_NAMES);
SyncDevices syncDevices;
syncDevices.FromJson(json);
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnDeleteDevice json="<<json;
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnDeleteDeviceFileWork json="<<json;

const SyncDevice *device = syncDevices.GetByDeviceId(device_id);
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnDeleteDevice device="<<device;
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnDeleteDeviceFileWork device="<<device;
//DCHECK(device); // once I saw it nullptr
if (device) {
const std::string device_name = device->name_;
const std::string object_id = device->object_id_;
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnDeleteDevice device_name="<<device_name;
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnDeleteDevice object_id="<<object_id;
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnDeleteDeviceFileWork device_name="<<device_name;
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnDeleteDeviceFileWork object_id="<<object_id;

SendDeviceSyncRecord(jslib::SyncRecord::Action::DELETE,
device_name,
Expand All @@ -188,17 +197,35 @@ void ControllerImpl::OnDeleteDevice(const std::string &device_id) {

void ControllerImpl::OnResetSync() {
LOG(ERROR) << "TAGAB ControllerImpl::OnResetSync";
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

CHECK(sync_client_ != nullptr);
CHECK(sync_initialized_);

const std::string device_id = sync_prefs_->GetThisDeviceId();
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnResetSync device_id="<<device_id;
OnDeleteDevice(device_id);

sync_prefs_->Clear();
task_runner_->PostTask(
FROM_HERE,
base::Bind(&ControllerImpl::OnResetSyncFileWork, base::Unretained(this), device_id)
);
}

void ControllerImpl::OnResetSyncFileWork(const std::string &device_id) {
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnResetSyncFileWork";
OnDeleteDeviceFileWork(device_id);
sync_obj_map_->DestroyDB();

content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
base::Bind(&ControllerImpl::OnResetSyncPostFileUiWork, base::Unretained(this))
);
}

void ControllerImpl::OnResetSyncPostFileUiWork() {
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnResetSyncPostFileUiWork";
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
sync_prefs_->Clear();

if (sync_ui_) {
sync_ui_->OnSyncStateChanged();
}
Expand Down Expand Up @@ -292,24 +319,34 @@ void ControllerImpl::OnGetInitData(const std::string &sync_version) {

seen_get_init_data_ = true;

Uint8Array seed2;
if (!temp_storage_.seed_.empty()) {
seed2.assign(temp_storage_.seed_.begin(), temp_storage_.seed_.end());
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnGetInitData temp_storage_.seed_str_=<" << temp_storage_.seed_str_ << ">";
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnGetInitData sync_prefs_->GetSeed()=<" << sync_prefs_->GetSeed() << ">";

Uint8Array seed;
if (!temp_storage_.seed_str_.empty()) {
seed = Uint8ArrayFromString(temp_storage_.seed_str_);
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnGetInitData take seed from temp store";
} else if (!sync_prefs_->GetSeed().empty()) {
seed2 = Uint8ArrayFromString(sync_prefs_->GetSeed());
seed = Uint8ArrayFromString(sync_prefs_->GetSeed());
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnGetInitData take seed from prefs store";
} else {
// We are starting a new chain, so we don't know neither seed nor device id
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnGetInitData starting new chain, use no seeds";
}

Uint8Array device_id2;
Uint8Array device_id;
if (!sync_prefs_->GetThisDeviceId().empty()) {
device_id2 = Uint8ArrayFromString(sync_prefs_->GetThisDeviceId());
device_id = Uint8ArrayFromString(sync_prefs_->GetThisDeviceId());
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnGetInitData use device id from prefs StrFromUint8Array(device_id)="<<StrFromUint8Array(device_id);
} else {
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnGetInitData use empty device id";
}
brave_sync::client_data::Config config2;
config2.api_version = "0";
config2.server_url = "https://sync-staging.brave.com";
config2.debug = true;
sync_client_->SendGotInitData(seed2, device_id2, config2);

brave_sync::client_data::Config config;
config.api_version = "0";
config.server_url = "https://sync-staging.brave.com";
config.debug = true;
sync_client_->SendGotInitData(seed, device_id, config);

LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnGetInitData called sync_client_->SendGotInitData---";
}
Expand Down Expand Up @@ -646,8 +683,9 @@ void ControllerImpl::OnBytesFromSyncWordsPrepared(const Uint8Array &bytes,
LOG(ERROR) << "TAGAB error_message=" << error_message;

if (!bytes.empty()) {
DCHECK(temp_storage_.seed_str_.empty());
//DCHECK(temp_storage_.seed_str_.empty()); can be not epmty if try to do twice with error on first time
temp_storage_.seed_str_ = StrFromUint8Array(bytes);
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnBytesFromSyncWordsPrepared temp_storage_.seed_str_=<" << temp_storage_.seed_str_ << ">";
LOG(ERROR) << "TAGAB brave_sync::ControllerImpl::OnWordsToBytesDone: call InitJsLib";
InitJsLib(true);//Init will cause load of the Script;
} else {
Expand All @@ -658,7 +696,7 @@ void ControllerImpl::OnBytesFromSyncWordsPrepared(const Uint8Array &bytes,
}
}

bool once_done = false;
//bool once_done = false;

// Here we query sync lib for the records after initialization (or again later)
void ControllerImpl::RequestSyncData() {
Expand Down
8 changes: 5 additions & 3 deletions components/brave_sync/controller_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ class ControllerImpl : public Controller,

void ShutdownFileWork();

void OnDeleteDeviceFileWork(const std::string &device_id);
void OnResetSyncFileWork(const std::string &device_id);
void OnResetSyncPostFileUiWork();

// Other private methods
void RequestSyncData();
void FetchSyncRecords(const bool &bookmarks, const bool &history,
Expand Down Expand Up @@ -211,9 +215,7 @@ class ControllerImpl : public Controller,
// This should be used only for passing
// between OnSetupSyncHaveCode or OnSetupSyncNewToSync to OnSaveInitData
std::string device_name_;
// Between OnWordsToBytesDone => InitJsLib|OnGotInitData
std::vector<char> seed_;
// Between OnWordsToBytesDone => OnSaveInitData
// Between OnWordsToBytesDone => InitJsLib|OnSaveInitData
std::string seed_str_;
};
TempStorage temp_storage_;
Expand Down
14 changes: 13 additions & 1 deletion components/brave_sync/extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,19 @@ class InjectedObject {
chrome.braveSync.syncDebug(arg1/*message*/);
break;
case "save-init-data":
chrome.braveSync.saveInitData(arg1/*seed*/, arg2/*deviceId*/);
var deviceId = arg2;
if ( typeof deviceId == "number" ) {
deviceId = new Uint8Array([deviceId]);
}
if (!arg1) {
arg1 = null;
}
console.log('background.js save-init-data arg1=',JSON.stringify(arg1));
console.log('background.js save-init-data deviceId=',JSON.stringify(deviceId));
console.log('background.js save-init-data typeof arg1=',typeof arg1);
console.log('background.js save-init-data typeof deviceId=',typeof deviceId);

chrome.braveSync.saveInitData(arg1/*seed*/, deviceId);
break;
case "sync-ready":
chrome.braveSync.syncReady();
Expand Down
9 changes: 5 additions & 4 deletions components/brave_sync/object_map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,18 @@ void ObjectMap::CloseDBHandle() {
void ObjectMap::DestroyDB() {
LOG(ERROR) << "TAGAB brave_sync::ObjectMap::DestroyDB, DCHECK_CALLED_ON_VALID_SEQUENCE " << GetThreadInfoString();
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(profile_);

LOG(ERROR) << "TAGAB brave_sync::ObjectMap::ResetObjects";
CloseDBHandle();
base::FilePath app_data_path;
bool success = base::PathService::Get(chrome::DIR_USER_DATA, &app_data_path);
CHECK(success);
base::FilePath dbFilePath = app_data_path.Append(DB_FILE_NAME);

base::FilePath dbFilePath = profile_->GetPath().Append(DB_FILE_NAME);
LOG(ERROR) << "TAGAB ResetObjects dbFilePath=" << dbFilePath;

leveldb::Status db_status = leveldb::DestroyDB(dbFilePath.value(), leveldb::Options());
if (!db_status.ok()) {
LOG(ERROR) << "sync level db destroy error " << db_status.ToString();
DCHECK(false);
}
}

Expand Down
2 changes: 0 additions & 2 deletions components/brave_sync/object_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class ObjectMap {
void CreateOpenDatabase();
void TraceAll();

void GetLocalIdByObjectIdImpl(const std::string &object_id, brave_sync::Bookmarks *bookmarks, std::string *local_id);

Profile *profile_;
std::unique_ptr<leveldb::DB> level_db_;

Expand Down

0 comments on commit 115a013

Please sign in to comment.