This source file includes following definitions.
- AddBookmarkToMobileFolder
- AddBookmark
- RemoveBookmark
- UpdateBookmarkTitle
- GetBookmarkModel
- url_database_
- Update
- Delete
- Insert
#include "chrome/browser/history/android/bookmark_model_sql_handler.h"
#include "base/logging.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/bookmarks/bookmark_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/history/url_database.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "content/public/browser/browser_thread.h"
using base::Time;
using content::BrowserThread;
namespace history {
namespace {
const HistoryAndBookmarkRow::ColumnID kInterestingColumns[] = {
HistoryAndBookmarkRow::BOOKMARK, HistoryAndBookmarkRow::TITLE };
}
BookmarkModelSQLHandler::Task::Task() {
}
void BookmarkModelSQLHandler::Task::AddBookmarkToMobileFolder(
const GURL& url,
const base::string16& title) {
BookmarkModel* bookmark_model = GetBookmarkModel();
if (!bookmark_model)
return;
const BookmarkNode* mobile_node = bookmark_model->mobile_node();
if (mobile_node)
bookmark_model->AddURL(mobile_node, 0, title, url);
}
void BookmarkModelSQLHandler::Task::AddBookmark(const GURL& url,
const base::string16& title,
int64 parent_id) {
BookmarkModel* bookmark_model = GetBookmarkModel();
if (!bookmark_model)
return;
const BookmarkNode* parent = bookmark_model->GetNodeByID(parent_id);
if (parent)
bookmark_model->AddURL(parent, 0, title, url);
}
void BookmarkModelSQLHandler::Task::RemoveBookmark(const GURL& url) {
BookmarkModel* bookmark_model = GetBookmarkModel();
if (!bookmark_model)
return;
std::vector<const BookmarkNode*> nodes;
bookmark_model->GetNodesByURL(url, &nodes);
for (std::vector<const BookmarkNode*>::iterator i = nodes.begin();
i != nodes.end(); ++i) {
const BookmarkNode* parent_node = (*i)->parent();
bookmark_model->Remove(parent_node, parent_node->GetIndexOf(*i));
}
}
void BookmarkModelSQLHandler::Task::UpdateBookmarkTitle(
const GURL& url,
const base::string16& title) {
BookmarkModel* bookmark_model = GetBookmarkModel();
if (!bookmark_model)
return;
std::vector<const BookmarkNode*> nodes;
bookmark_model->GetNodesByURL(url, &nodes);
for (std::vector<const BookmarkNode*>::iterator i = nodes.begin();
i != nodes.end(); ++i) {
bookmark_model->SetTitle(*i, title);
}
}
BookmarkModelSQLHandler::Task::~Task() {
}
BookmarkModel* BookmarkModelSQLHandler::Task::GetBookmarkModel() {
Profile* profile = ProfileManager::GetLastUsedProfile();
if (!profile)
return NULL;
return BookmarkModelFactory::GetForProfile(profile);
}
BookmarkModelSQLHandler::BookmarkModelSQLHandler(
URLDatabase* url_database)
: SQLHandler(kInterestingColumns, arraysize(kInterestingColumns)),
url_database_(url_database) {
}
BookmarkModelSQLHandler::~BookmarkModelSQLHandler() {
}
bool BookmarkModelSQLHandler::Update(const HistoryAndBookmarkRow& row,
const TableIDRows& ids_set) {
for (TableIDRows::const_iterator i = ids_set.begin();
i != ids_set.end(); ++i) {
if (row.is_value_set_explicitly(HistoryAndBookmarkRow::BOOKMARK)) {
if (row.is_bookmark()) {
URLRow url_row;
if (!url_database_->GetURLRow(i->url_id, &url_row))
return false;
if (row.is_value_set_explicitly(HistoryAndBookmarkRow::PARENT_ID)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
&BookmarkModelSQLHandler::Task::AddBookmark,
scoped_refptr<BookmarkModelSQLHandler::Task>(
new BookmarkModelSQLHandler::Task()),
i->url, url_row.title(), row.parent_id()));
} else {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
&BookmarkModelSQLHandler::Task::AddBookmarkToMobileFolder,
scoped_refptr<BookmarkModelSQLHandler::Task>(
new BookmarkModelSQLHandler::Task()),
i->url, url_row.title()));
}
} else {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
&BookmarkModelSQLHandler::Task::RemoveBookmark,
scoped_refptr<BookmarkModelSQLHandler::Task>(
new BookmarkModelSQLHandler::Task()),
i->url));
}
} else if (row.is_value_set_explicitly(HistoryAndBookmarkRow::TITLE)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
&BookmarkModelSQLHandler::Task::UpdateBookmarkTitle,
scoped_refptr<BookmarkModelSQLHandler::Task>(
new BookmarkModelSQLHandler::Task()),
i->url, row.title()));
}
}
return true;
}
bool BookmarkModelSQLHandler::Delete(const TableIDRows& ids_set) {
for (TableIDRows::const_iterator i = ids_set.begin();
i != ids_set.end(); ++i) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
&BookmarkModelSQLHandler::Task::RemoveBookmark,
scoped_refptr<BookmarkModelSQLHandler::Task>(
new BookmarkModelSQLHandler::Task()),
i->url));
}
return true;
}
bool BookmarkModelSQLHandler::Insert(HistoryAndBookmarkRow* row) {
DCHECK(row->is_value_set_explicitly(HistoryAndBookmarkRow::URL));
if (!row->is_value_set_explicitly(HistoryAndBookmarkRow::BOOKMARK) ||
!row->is_bookmark())
return true;
if (row->is_value_set_explicitly(HistoryAndBookmarkRow::PARENT_ID)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
&BookmarkModelSQLHandler::Task::AddBookmark,
scoped_refptr<BookmarkModelSQLHandler::Task>(
new BookmarkModelSQLHandler::Task()),
row->url(), row->title(), row->parent_id()));
} else {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
&BookmarkModelSQLHandler::Task::AddBookmarkToMobileFolder,
scoped_refptr<BookmarkModelSQLHandler::Task>(
new BookmarkModelSQLHandler::Task()),
row->url(), row->title()));
}
return true;
}
}