- Made BookmarkService::GetBookmarks return both urls and title,

  it still return the unique URLs, not matter what the title is.

BUG=http://b/6696843

TEST=AndroidProviderBackendTest, BookmarkModelTest, BookmarkModelSQLHandlerTest

Review URL: https://chromiumcodereview.appspot.com/10825147

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@150136 0039d316-1c4b-4281-b951-d872f2087c98
parent 40175c86
...@@ -400,15 +400,20 @@ bool BookmarkModel::IsBookmarked(const GURL& url) { ...@@ -400,15 +400,20 @@ bool BookmarkModel::IsBookmarked(const GURL& url) {
return IsBookmarkedNoLock(url); return IsBookmarkedNoLock(url);
} }
void BookmarkModel::GetBookmarks(std::vector<GURL>* urls) { void BookmarkModel::GetBookmarks(
std::vector<BookmarkService::URLAndTitle>* bookmarks) {
base::AutoLock url_lock(url_lock_); base::AutoLock url_lock(url_lock_);
const GURL* last_url = NULL; const GURL* last_url = NULL;
for (NodesOrderedByURLSet::iterator i = nodes_ordered_by_url_set_.begin(); for (NodesOrderedByURLSet::iterator i = nodes_ordered_by_url_set_.begin();
i != nodes_ordered_by_url_set_.end(); ++i) { i != nodes_ordered_by_url_set_.end(); ++i) {
const GURL* url = &((*i)->url()); const GURL* url = &((*i)->url());
// Only add unique URLs. // Only add unique URLs.
if (!last_url || *url != *last_url) if (!last_url || *url != *last_url) {
urls->push_back(*url); BookmarkService::URLAndTitle bookmark;
bookmark.url = *url;
bookmark.title = (*i)->GetTitle();
bookmarks->push_back(bookmark);
}
last_url = url; last_url = url;
} }
} }
......
...@@ -292,10 +292,11 @@ class BookmarkModel : public content::NotificationObserver, ...@@ -292,10 +292,11 @@ class BookmarkModel : public content::NotificationObserver,
// See BookmarkService for more details on this. // See BookmarkService for more details on this.
virtual bool IsBookmarked(const GURL& url) OVERRIDE; virtual bool IsBookmarked(const GURL& url) OVERRIDE;
// Returns all the bookmarked urls. // Returns all the bookmarked urls and their titles.
// This method is thread safe. // This method is thread safe.
// See BookmarkService for more details on this. // See BookmarkService for more details on this.
virtual void GetBookmarks(std::vector<GURL>* urls) OVERRIDE; virtual void GetBookmarks(
std::vector<BookmarkService::URLAndTitle>* urls) OVERRIDE;
// Blocks until loaded; this is NOT invoked on the main thread. // Blocks until loaded; this is NOT invoked on the main thread.
// See BookmarkService for more details on this. // See BookmarkService for more details on this.
......
...@@ -614,13 +614,21 @@ TEST_F(BookmarkModelTest, GetMostRecentlyAddedNodeForURL) { ...@@ -614,13 +614,21 @@ TEST_F(BookmarkModelTest, GetMostRecentlyAddedNodeForURL) {
// Makes sure GetBookmarks removes duplicates. // Makes sure GetBookmarks removes duplicates.
TEST_F(BookmarkModelTest, GetBookmarksWithDups) { TEST_F(BookmarkModelTest, GetBookmarksWithDups) {
const GURL url("http://foo.com/0"); const GURL url("http://foo.com/0");
model_.AddURL(model_.bookmark_bar_node(), 0, ASCIIToUTF16("blah"), url); const string16 title(ASCIIToUTF16("blah"));
model_.AddURL(model_.bookmark_bar_node(), 1, ASCIIToUTF16("blah"), url); model_.AddURL(model_.bookmark_bar_node(), 0, title, url);
model_.AddURL(model_.bookmark_bar_node(), 1, title, url);
std::vector<GURL> urls;
model_.GetBookmarks(&urls); std::vector<BookmarkService::URLAndTitle> bookmarks;
EXPECT_EQ(1U, urls.size()); model_.GetBookmarks(&bookmarks);
ASSERT_TRUE(urls[0] == url); ASSERT_EQ(1U, bookmarks.size());
EXPECT_EQ(url, bookmarks[0].url);
EXPECT_EQ(title, bookmarks[0].title);
model_.AddURL(model_.bookmark_bar_node(), 2, ASCIIToUTF16("Title2"), url);
// Only one returned, even titles are different.
bookmarks.clear();
model_.GetBookmarks(&bookmarks);
EXPECT_EQ(1U, bookmarks.size());
} }
TEST_F(BookmarkModelTest, HasBookmarks) { TEST_F(BookmarkModelTest, HasBookmarks) {
......
// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
#include <vector> #include <vector>
class GURL; #include "base/string16.h"
#include "googleurl/src/gurl.h"
// BookmarkService provides a thread safe view of bookmarks. It is used by // BookmarkService provides a thread safe view of bookmarks. It is used by
// HistoryBackend when it needs to determine the set of bookmarked URLs // HistoryBackend when it needs to determine the set of bookmarked URLs
...@@ -16,17 +17,23 @@ class GURL; ...@@ -16,17 +17,23 @@ class GURL;
// BookmarkService is owned by Profile and deleted when the Profile is deleted. // BookmarkService is owned by Profile and deleted when the Profile is deleted.
class BookmarkService { class BookmarkService {
public: public:
struct URLAndTitle {
GURL url;
string16 title;
};
// Returns true if the specified URL is bookmarked. // Returns true if the specified URL is bookmarked.
// //
// If not on the main thread you *must* invoke BlockTillLoaded first. // If not on the main thread you *must* invoke BlockTillLoaded first.
virtual bool IsBookmarked(const GURL& url) = 0; virtual bool IsBookmarked(const GURL& url) = 0;
// Returns, by reference in urls, the set of bookmarked urls. This returns // Returns, by reference in |bookmarks|, the set of bookmarked urls and their
// the unique set of URLs. For example, if two bookmarks reference the same // titles. This returns the unique set of URLs. For example, if two bookmarks
// URL only one entry is added. // reference the same URL only one entry is added not matter the titles are
// same or not.
// //
// If not on the main thread you *must* invoke BlockTillLoaded first. // If not on the main thread you *must* invoke BlockTillLoaded first.
virtual void GetBookmarks(std::vector<GURL>* urls) = 0; virtual void GetBookmarks(std::vector<URLAndTitle>* bookmarks) = 0;
// Blocks until loaded. This is intended for usage on a thread other than // Blocks until loaded. This is intended for usage on a thread other than
// the main thread. // the main thread.
......
...@@ -749,16 +749,16 @@ bool AndroidProviderBackend::UpdateBookmarks() { ...@@ -749,16 +749,16 @@ bool AndroidProviderBackend::UpdateBookmarks() {
} }
bookmark_service_->BlockTillLoaded(); bookmark_service_->BlockTillLoaded();
std::vector<GURL> bookmark_urls; std::vector<BookmarkService::URLAndTitle> bookmarks;
bookmark_service_->GetBookmarks(&bookmark_urls); bookmark_service_->GetBookmarks(&bookmarks);
if (bookmark_urls.empty()) if (bookmarks.empty())
return true; return true;
std::vector<URLID> url_ids; std::vector<URLID> url_ids;
for (std::vector<GURL>::const_iterator i = bookmark_urls.begin(); for (std::vector<BookmarkService::URLAndTitle>::const_iterator i =
i != bookmark_urls.end(); ++i) { bookmarks.begin(); i != bookmarks.end(); ++i) {
URLID url_id = history_db_->GetRowForURL(*i, NULL); URLID url_id = history_db_->GetRowForURL(i->url, NULL);
if (url_id == 0) if (url_id == 0)
// TODO(michaelbai): Add a row to url and android_url table as the // TODO(michaelbai): Add a row to url and android_url table as the
// bookmark could be added manually by user or insertted by sync. // bookmark could be added manually by user or insertted by sync.
......
...@@ -138,10 +138,12 @@ TEST_F(BookmarkModelSQLHandlerTest, UpdateHistoryToBookmark) { ...@@ -138,10 +138,12 @@ TEST_F(BookmarkModelSQLHandlerTest, UpdateHistoryToBookmark) {
ASSERT_TRUE(handler.Update(row, id_rows)); ASSERT_TRUE(handler.Update(row, id_rows));
RunMessageLoopForUI(); RunMessageLoopForUI();
// Get all bookmarks and verify there is only one. // Get all bookmarks and verify there is only one.
std::vector<GURL> urls; std::vector<BookmarkService::URLAndTitle> bookmarks;
bookmark_model_->GetBookmarks(&urls); bookmark_model_->GetBookmarks(&bookmarks);
EXPECT_EQ(1u, urls.size()); ASSERT_EQ(1u, bookmarks.size());
EXPECT_EQ(url_row.url(), urls[0]); EXPECT_EQ(url_row.url(), bookmarks[0].url);
EXPECT_EQ(url_row.title(), bookmarks[0].title);
// Get the bookmark node. // Get the bookmark node.
std::vector<const BookmarkNode*> nodes; std::vector<const BookmarkNode*> nodes;
bookmark_model_->GetNodesByURL(row.url(), &nodes); bookmark_model_->GetNodesByURL(row.url(), &nodes);
...@@ -155,9 +157,9 @@ TEST_F(BookmarkModelSQLHandlerTest, UpdateHistoryToBookmark) { ...@@ -155,9 +157,9 @@ TEST_F(BookmarkModelSQLHandlerTest, UpdateHistoryToBookmark) {
row.set_is_bookmark(false); row.set_is_bookmark(false);
ASSERT_TRUE(handler.Update(row, id_rows)); ASSERT_TRUE(handler.Update(row, id_rows));
RunMessageLoopForUI(); RunMessageLoopForUI();
urls.clear(); bookmarks.clear();
bookmark_model_->GetBookmarks(&urls); bookmark_model_->GetBookmarks(&bookmarks);
EXPECT_TRUE(urls.empty()); EXPECT_TRUE(bookmarks.empty());
// Update with the parent id. // Update with the parent id.
row.set_parent_id(bookmark_model_->other_node()->id()); row.set_parent_id(bookmark_model_->other_node()->id());
...@@ -165,10 +167,11 @@ TEST_F(BookmarkModelSQLHandlerTest, UpdateHistoryToBookmark) { ...@@ -165,10 +167,11 @@ TEST_F(BookmarkModelSQLHandlerTest, UpdateHistoryToBookmark) {
ASSERT_TRUE(handler.Update(row, id_rows)); ASSERT_TRUE(handler.Update(row, id_rows));
RunMessageLoopForUI(); RunMessageLoopForUI();
// Get all bookmarks and verify there is only one. // Get all bookmarks and verify there is only one.
urls.clear(); bookmarks.clear();
bookmark_model_->GetBookmarks(&urls); bookmark_model_->GetBookmarks(&bookmarks);
EXPECT_EQ(1u, urls.size()); ASSERT_EQ(1u, bookmarks.size());
EXPECT_EQ(url_row.url(), urls[0]); EXPECT_EQ(url_row.url(), bookmarks[0].url);
EXPECT_EQ(url_row.title(), bookmarks[0].title);
// Get the bookmark node. // Get the bookmark node.
nodes.clear(); nodes.clear();
bookmark_model_->GetNodesByURL(row.url(), &nodes); bookmark_model_->GetNodesByURL(row.url(), &nodes);
...@@ -186,10 +189,11 @@ TEST_F(BookmarkModelSQLHandlerTest, UpdateHistoryToBookmark) { ...@@ -186,10 +189,11 @@ TEST_F(BookmarkModelSQLHandlerTest, UpdateHistoryToBookmark) {
ASSERT_TRUE(handler.Update(update_title, id_rows)); ASSERT_TRUE(handler.Update(update_title, id_rows));
RunMessageLoopForUI(); RunMessageLoopForUI();
// Get all bookmarks and verify there is only one. // Get all bookmarks and verify there is only one.
urls.clear(); bookmarks.clear();
bookmark_model_->GetBookmarks(&urls); bookmark_model_->GetBookmarks(&bookmarks);
EXPECT_EQ(1u, urls.size()); ASSERT_EQ(1u, bookmarks.size());
EXPECT_EQ(url_row.url(), urls[0]); EXPECT_EQ(url_row.url(), bookmarks[0].url);
EXPECT_EQ(url_row.title(), bookmarks[0].title);
// Get the bookmark node. // Get the bookmark node.
nodes.clear(); nodes.clear();
bookmark_model_->GetNodesByURL(row.url(), &nodes); bookmark_model_->GetNodesByURL(row.url(), &nodes);
......
...@@ -2257,7 +2257,7 @@ void HistoryBackend::DeleteAllHistory() { ...@@ -2257,7 +2257,7 @@ void HistoryBackend::DeleteAllHistory() {
// the original tables directly. // the original tables directly.
// Get the bookmarked URLs. // Get the bookmarked URLs.
std::vector<GURL> starred_urls; std::vector<BookmarkService::URLAndTitle> starred_urls;
BookmarkService* bookmark_service = GetBookmarkService(); BookmarkService* bookmark_service = GetBookmarkService();
if (bookmark_service) if (bookmark_service)
bookmark_service_->GetBookmarks(&starred_urls); bookmark_service_->GetBookmarks(&starred_urls);
...@@ -2265,7 +2265,7 @@ void HistoryBackend::DeleteAllHistory() { ...@@ -2265,7 +2265,7 @@ void HistoryBackend::DeleteAllHistory() {
URLRows kept_urls; URLRows kept_urls;
for (size_t i = 0; i < starred_urls.size(); i++) { for (size_t i = 0; i < starred_urls.size(); i++) {
URLRow row; URLRow row;
if (!db_->GetRowForURL(starred_urls[i], &row)) if (!db_->GetRowForURL(starred_urls[i].url, &row))
continue; continue;
// Clear the last visit time so when we write these rows they are "clean." // Clear the last visit time so when we write these rows they are "clean."
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment