Skip to content

Commit

Permalink
Merge pull request #3778 from brave/bookmark-api
Browse files Browse the repository at this point in the history
Redirect other_node() to bookmark_bar_node() in bookmarks::GetBookmarkNodeByID
  • Loading branch information
darkdh authored Oct 24, 2019
2 parents c7202dc + f8dceb0 commit edd63cf
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
6 changes: 4 additions & 2 deletions browser/extensions/brave_extension_management.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace extensions {

BraveExtensionManagement::BraveExtensionManagement(Profile* profile)
: ExtensionManagement(profile),
extension_registry_observer_(this) {
extension_registry_observer_(this),
profile_(profile) {
extension_registry_observer_.Add(ExtensionRegistry::Get(
static_cast<content::BrowserContext*>(profile)));
providers_.push_back(
Expand All @@ -44,7 +45,8 @@ void BraveExtensionManagement::RegisterBraveExtensions() {
#if BUILDFLAG(ENABLE_TOR)
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (!command_line.HasSwitch(switches::kDisableTorClientUpdaterExtension))
if (!command_line.HasSwitch(switches::kDisableTorClientUpdaterExtension) &&
!profile_->AsTestingProfile())
g_brave_browser_process->tor_client_updater()->Register();
#endif
}
Expand Down
2 changes: 2 additions & 0 deletions browser/extensions/brave_extension_management.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class BraveExtensionManagement : public ExtensionManagement,
ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
extension_registry_observer_;

Profile* profile_;

DISALLOW_COPY_AND_ASSIGN(BraveExtensionManagement);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Copyright (c) 2019 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 "../../../../../../../chrome/browser/extensions/api/bookmarks/bookmarks_api_unittest.cc" // NOLINT

namespace extensions {

TEST_F(BookmarksApiUnittest, Create) {
bookmarks::BookmarkModel* model =
BookmarkModelFactory::GetForBrowserContext(profile());
{
auto create_function = base::MakeRefCounted<BookmarksCreateFunction>();
const std::string other_node_id =
base::NumberToString(model->other_node()->id());
// Specify other_node() as parent
const GURL url("https://brave.com");
api_test_utils::RunFunction(
create_function.get(),
base::StringPrintf(
R"([{"parentId": "%s",
"title": "brave",
"url": "%s"}])", other_node_id.c_str(), url.spec().c_str()),
profile());
auto* node = model->GetMostRecentlyAddedUserNodeForURL(url);
EXPECT_EQ(node->url(), url);
EXPECT_EQ(node->parent(), model->bookmark_bar_node());
}
{
auto create_function = base::MakeRefCounted<BookmarksCreateFunction>();
// default parent
const GURL url("https://brave2.com");
api_test_utils::RunFunction(
create_function.get(),
base::StringPrintf(
R"([{"title": "brave2",
"url": "%s"}])", url.spec().c_str()),
profile());
auto* node = model->GetMostRecentlyAddedUserNodeForURL(url);
EXPECT_EQ(node->url(), url);
EXPECT_EQ(node->parent(), model->bookmark_bar_node());
}
}

TEST_F(BookmarksApiUnittest, Move) {
bookmarks::BookmarkModel* model =
BookmarkModelFactory::GetForBrowserContext(profile());
const std::string other_node_id =
base::NumberToString(model->other_node()->id());
const bookmarks::BookmarkNode* node = model->AddURL(
model->bookmark_bar_node(), 0, base::ASCIIToUTF16("brave"),
GURL("https://brave.com"));
auto move_function = base::MakeRefCounted<BookmarksMoveFunction>();
api_test_utils::RunFunction(
move_function.get(),
base::StringPrintf(
R"(["%s", {"parentId": "%s"}])",
base::NumberToString(node->id()).c_str(), other_node_id.c_str()),
profile());
EXPECT_EQ(node->parent(), model->bookmark_bar_node());
}

} // namespace extensions
26 changes: 26 additions & 0 deletions chromium_src/components/bookmarks/browser/bookmark_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* Copyright (c) 2019 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/bookmarks/browser/bookmark_utils.h"
namespace bookmarks {
// DeleteBookmarkFolders won't get a chance to delete other_node(), even with
// malicious usage deleting bookmark_bar_node() and other_node() are both
// prohibited so no need to redirect other_node() to bookmark_bar_node()
const BookmarkNode* GetBookmarkNodeByID_ChromiumImpl(const BookmarkModel* model,
int64_t id);
} // namespace bookmarks
#define GetBookmarkNodeByID GetBookmarkNodeByID_ChromiumImpl
#include "../../../../../components/bookmarks/browser/bookmark_utils.cc"
#undef GetBookmarkNodeByID
namespace bookmarks {

const BookmarkNode* GetBookmarkNodeByID(const BookmarkModel* model,
int64_t id) {
if (id == model->other_node()->id())
id = model->bookmark_bar_node()->id();
return GetBookmarkNodeByID_ChromiumImpl(model, id);
}

} // namespace bookmarks
9 changes: 9 additions & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ test("brave_unit_tests") {
sources += [
"//brave/chromium_src/extensions/browser/sandboxed_unpacker_unittest.cc",
"//brave/chromium_src/extensions/common/permissions/permissions_data_unittest.cc",
"//chrome/browser/extensions/extension_service_test_base.cc",
"//chrome/browser/extensions/extension_service_test_base.h",
"//chrome/browser/extensions/api/bookmarks/bookmarks_api_unittest.cc",
]
}

Expand Down Expand Up @@ -308,6 +311,12 @@ test("brave_unit_tests") {
"//third_party/cacheinvalidation",
]

if (enable_extensions) {
deps += [
"//extensions/browser:test_support",
]
}

if (is_android) {
deps += [
"//brave:brave_pak_assets",
Expand Down

0 comments on commit edd63cf

Please sign in to comment.