Skip to content

Commit

Permalink
Uplift of #4324 (squashed) to dev
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-browser-releases authored and mkarolin committed Jan 31, 2020
1 parent 2c8eba9 commit 16c516c
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 277 deletions.
1 change: 0 additions & 1 deletion app/brave_main_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ bool BraveMainDelegate::BasicStartupComplete(int* exit_code) {
features::kWebXr.name,
features::kWebXrGamepadModule.name,
unified_consent::kUnifiedConsent.name,
switches::kSyncUSSBookmarks.name,
};
command_line.AppendFeatures(enabled_features, disabled_features);

Expand Down
2 changes: 0 additions & 2 deletions app/brave_main_delegate_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/omnibox/common/omnibox_features.h"
#include "components/password_manager/core/common/password_manager_features.h"
#include "components/sync/driver/sync_driver_switches.h"
#include "components/unified_consent/feature.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/common/content_features.h"
Expand Down Expand Up @@ -53,7 +52,6 @@ IN_PROC_BROWSER_TEST_F(BraveMainDelegateBrowserTest, DisabledFeatures) {
&features::kWebXrGamepadModule,
&unified_consent::kUnifiedConsent,
&features::kLookalikeUrlNavigationSuggestionsUI,
&switches::kSyncUSSBookmarks,
};

for (const auto* feature : disabled_features)
Expand Down
100 changes: 0 additions & 100 deletions chromium_src/components/sync_bookmarks/bookmark_change_processor.cc

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Copyright 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "components/sync_bookmarks/bookmark_model_observer_impl.h"

#include <utility>

namespace sync_bookmarks {

class BraveBookmarkModelObserverImpl : public BookmarkModelObserverImpl {
public:
// |bookmark_tracker_| must not be null and must outlive this object.
BraveBookmarkModelObserverImpl(
const base::RepeatingClosure& nudge_for_commit_closure,
base::OnceClosure on_bookmark_model_being_deleted_closure,
SyncedBookmarkTracker* bookmark_tracker)
: BookmarkModelObserverImpl(
nudge_for_commit_closure,
std::move(on_bookmark_model_being_deleted_closure),
bookmark_tracker) {}
~BraveBookmarkModelObserverImpl() override = default;

// BookmarkModelObserver:
void BookmarkMetaInfoChanged(bookmarks::BookmarkModel* model,
const bookmarks::BookmarkNode* node) override {}
void BookmarkNodeFaviconChanged(
bookmarks::BookmarkModel* model,
const bookmarks::BookmarkNode* node) override {}

private:
DISALLOW_COPY_AND_ASSIGN(BraveBookmarkModelObserverImpl);
};

} // namespace sync_bookmarks

#define BookmarkModelObserverImpl BraveBookmarkModelObserverImpl
#include "../../../../components/sync_bookmarks/bookmark_model_type_processor.cc"
#undef BookmarkModelObserverImpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/components/brave_sync/syncer_helper.h"

#define BRAVE_APPLY_REMOTE_UPDATE true ? brave_sync::GetIndex(new_parent, node):

#define BRAVE_PROCESS_CREATE_1 \
std::string order; \
std::string object_id; \
for (int i = 0; i < update_entity.specifics.bookmark().meta_info_size(); \
++i) { \
if (update_entity.specifics.bookmark().meta_info(i).key() == "order") { \
order = update_entity.specifics.bookmark().meta_info(i).value(); \
} else if (update_entity.specifics.bookmark().meta_info(i).key() == \
"object_id") { \
object_id = update_entity.specifics.bookmark().meta_info(i).value(); \
} \
}

#define BRAVE_PROCESS_CREATE_2 \
true ? brave_sync::GetIndex(parent_node, order, object_id):
#include "../../../../components/sync_bookmarks/bookmark_remote_updates_handler.cc"
#undef BRAVE_APPLY_REMOTE_UPDATE
#undef BRAVE_PROCESS_CREATE_1
#undef BRAVE_PROCESS_CREATE_2
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "components/sync_bookmarks/bookmark_specifics_conversions.h"

#include "brave/components/brave_sync/syncer_helper.h"
#define CreateSpecificsFromBookmarkNode \
CreateSpecificsFromBookmarkNodeChromiumImpl
#include "../../../../components/sync_bookmarks/bookmark_specifics_conversions.cc"
#undef CreateSpecificsFromBookmarkNode

namespace sync_bookmarks {

sync_pb::EntitySpecifics CreateSpecificsFromBookmarkNode(
const bookmarks::BookmarkNode* node,
bookmarks::BookmarkModel* model,
bool force_favicon_load) {
brave_sync::AddBraveMetaInfo(node);
return CreateSpecificsFromBookmarkNodeChromiumImpl(node, model,
force_favicon_load);
}

} // namespace sync_bookmarks
125 changes: 40 additions & 85 deletions components/brave_sync/syncer_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
#include "base/strings/string_number_conversions.h"
#include "brave/components/brave_sync/bookmark_order_util.h"
#include "brave/components/brave_sync/tools.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_node.h"

namespace brave_sync {
namespace {

void SetOrder(bookmarks::BookmarkModel* model,
const bookmarks::BookmarkNode* node,
// Get mutable node to prevent BookmarkMetaInfoChanged from being triggered
bookmarks::BookmarkNode* AsMutable(const bookmarks::BookmarkNode* node) {
return const_cast<bookmarks::BookmarkNode*>(node);
}

void SetOrder(const bookmarks::BookmarkNode* node,
const std::string& parent_order) {
DCHECK(!parent_order.empty());
int index = node->parent()->GetIndexOf(node);
Expand All @@ -37,117 +41,68 @@ void SetOrder(bookmarks::BookmarkModel* model,

std::string order =
brave_sync::GetOrder(prev_order, next_order, parent_order);
model->SetNodeMetaInfo(node, "order", order);
}

size_t GetIndexByOrder(const std::string& record_order) {
size_t index = 0;
size_t last_dot_index = record_order.rfind(".");
DCHECK(last_dot_index != std::string::npos);
std::string last_digit = record_order.substr(last_dot_index + 1);
bool result = base::StringToSizeT(last_digit, &index);
DCHECK(result);
DCHECK_GE(index, 1u);
--index;
return index;
AsMutable(node)->SetMetaInfo("order", order);
}

} // namespace

size_t GetIndexByCompareOrderStartFrom(const bookmarks::BookmarkNode* parent,
const bookmarks::BookmarkNode* src,
size_t index) {
std::string src_order;
src->GetMetaInfo("order", &src_order);
DCHECK(!src_order.empty());
bool use_order = true; // If false use object_id
std::string src_object_id;
while (index < parent->children().size()) {
const bookmarks::BookmarkNode* node = parent->children()[index].get();
if (src->id() == node->id()) {
// We reached ourselves, no sense to go further, because we know all
// unsorted elements are in the end
return index;
}

if (use_order) {
std::string node_order;
node->GetMetaInfo("order", &node_order);
if (!node_order.empty() &&
brave_sync::CompareOrder(src_order, node_order)) {
return index;
}

if (src_order == node_order) {
use_order = false;
size_t GetIndex(const bookmarks::BookmarkNode* parent,
const std::string& order,
const std::string& object_id) {
DCHECK(!order.empty());
DCHECK(!object_id.empty());
for (size_t i = 0; i < parent->children().size(); ++i) {
const bookmarks::BookmarkNode* child = parent->children()[i].get();
std::string child_order;
child->GetMetaInfo("order", &child_order);
if (!child_order.empty() &&
brave_sync::CompareOrder(order, child_order)) {
return i;
} else if (order == child_order) {
std::string child_object_id;
child->GetMetaInfo("object_id", &child_object_id);
if (object_id <= child_object_id) {
return i;
}
}
}
return parent->children().size();
}

if (!use_order) {
if (src_object_id.empty()) {
src->GetMetaInfo("object_id", &src_object_id);
}

std::string node_object_id;
node->GetMetaInfo("object_id", &node_object_id);
size_t GetIndex(const bookmarks::BookmarkNode* parent,
const bookmarks::BookmarkNode* node) {
std::string order;
node->GetMetaInfo("order", &order);
std::string object_id;
node->GetMetaInfo("object_id", &object_id);

if (src_object_id < node_object_id) {
return index;
}
}
++index;
}
return index;
return GetIndex(parent, order, object_id);
}

void AddBraveMetaInfo(const bookmarks::BookmarkNode* node,
bookmarks::BookmarkModel* model) {
void AddBraveMetaInfo(const bookmarks::BookmarkNode* node) {
std::string parent_order;
node->parent()->GetMetaInfo("order", &parent_order);
SetOrder(model, node, parent_order);
SetOrder(node, parent_order);

std::string object_id;
node->GetMetaInfo("object_id", &object_id);
// newly created node
if (object_id.empty()) {
object_id = tools::GenerateObjectId();
}
model->SetNodeMetaInfo(node, "object_id", object_id);
AsMutable(node)->SetMetaInfo("object_id", object_id);

std::string parent_object_id;
node->parent()->GetMetaInfo("object_id", &parent_object_id);
model->SetNodeMetaInfo(node, "parent_object_id", parent_object_id);
AsMutable(node)->SetMetaInfo("parent_object_id", parent_object_id);

std::string sync_timestamp;
node->GetMetaInfo("sync_timestamp", &sync_timestamp);
if (sync_timestamp.empty()) {
sync_timestamp = std::to_string(base::Time::Now().ToJsTime());
model->SetNodeMetaInfo(node, "sync_timestamp", sync_timestamp);
AsMutable(node)->SetMetaInfo("sync_timestamp", sync_timestamp);
}
DCHECK(!sync_timestamp.empty());
}

size_t GetIndex(const bookmarks::BookmarkNode* parent,
const bookmarks::BookmarkNode* src) {
DCHECK(parent);
DCHECK(src);
size_t index = 0;
std::string src_order;
src->GetMetaInfo("order", &src_order);
DCHECK(!src_order.empty());
index = GetIndexByOrder(src_order);
if (index < parent->children().size()) {
const bookmarks::BookmarkNode* node = parent->children()[index].get();
if (node) {
std::string node_order;
node->GetMetaInfo("order", &node_order);

DCHECK(!node_order.empty());
if (CompareOrder(node_order, src_order))
return index + 1;
}
}
return index;
}

} // namespace brave_sync
Loading

0 comments on commit 16c516c

Please sign in to comment.