-
Notifications
You must be signed in to change notification settings - Fork 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3778 from brave/bookmark-api
Redirect other_node() to bookmark_bar_node() in bookmarks::GetBookmarkNodeByID
- Loading branch information
Showing
5 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
chromium_src/chrome/browser/extensions/api/bookmarks/bookmarks_api_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
chromium_src/components/bookmarks/browser/bookmark_utils.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters