summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormichaelbai@chromium.org <michaelbai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-06 00:45:51 +0000
committermichaelbai@chromium.org <michaelbai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-06 00:45:51 +0000
commit2cfd1489de967f2c2d381a3f18406f33022b1202 (patch)
treeac46bfcb419b0cc2febe997ddac03ba87a87163e
parent9c94b095231b2b1d7a7792d7310ec81c799d1a58 (diff)
downloadchromium_src-2cfd1489de967f2c2d381a3f18406f33022b1202.zip
chromium_src-2cfd1489de967f2c2d381a3f18406f33022b1202.tar.gz
chromium_src-2cfd1489de967f2c2d381a3f18406f33022b1202.tar.bz2
Added AndroidProviderBackend::DeleteHistory method
This method only deletes the history, the row's visit count will be reset if the row's URL is bookmarked. BUG= TEST= Review URL: http://codereview.chromium.org/9963097 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@131053 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/history/android/android_provider_backend.cc126
-rw-r--r--chrome/browser/history/android/android_provider_backend.h40
-rw-r--r--chrome/browser/history/android/android_provider_backend_unittest.cc60
-rw-r--r--chrome/browser/history/android/sql_handler.cc3
-rw-r--r--chrome/browser/history/android/sql_handler.h2
5 files changed, 195 insertions, 36 deletions
diff --git a/chrome/browser/history/android/android_provider_backend.cc b/chrome/browser/history/android/android_provider_backend.cc
index 74662a5..264d54a 100644
--- a/chrome/browser/history/android/android_provider_backend.cc
+++ b/chrome/browser/history/android/android_provider_backend.cc
@@ -26,7 +26,7 @@ namespace history {
namespace {
-const char* kVirtualBookmarkTable =
+const char* kVirtualHistoryAndBookmarkTable =
"SELECT android_urls.id AS _id, "
"android_cache_db.bookmark_cache.created_time AS created, "
"urls.title AS title, android_urls.raw_url AS url, "
@@ -199,6 +199,22 @@ bool AndroidProviderBackend::DeleteHistoryAndBookmarks(
return true;
}
+bool AndroidProviderBackend::DeleteHistory(
+ const std::string& selection,
+ const std::vector<string16>& selection_args,
+ int* deleted_count) {
+ HistoryNotifications notifications;
+
+ ScopedTransaction transaction(history_db_, thumbnail_db_);
+
+ if (!DeleteHistory(selection, selection_args, deleted_count,
+ &notifications))
+ return false;
+
+ transaction.Commit();
+ BroadcastNotifications(notifications);
+ return true;
+}
AndroidProviderBackend::HistoryNotification::HistoryNotification(
int type,
@@ -250,8 +266,7 @@ bool AndroidProviderBackend::UpdateHistoryAndBookmarks(
return false;
TableIDRows ids_set;
- if (!GetSelectedURLs(selection, selection_args, kVirtualBookmarkTable,
- &ids_set))
+ if (!GetSelectedURLs(selection, selection_args, &ids_set))
return false;
if (ids_set.empty()) {
@@ -364,40 +379,65 @@ bool AndroidProviderBackend::DeleteHistoryAndBookmarks(
return false;
TableIDRows ids_set;
- if (!GetSelectedURLs(selection, selection_args, kVirtualBookmarkTable,
- &ids_set))
+ if (!GetSelectedURLs(selection, selection_args, &ids_set))
return false;
if (ids_set.empty()) {
- deleted_count = 0;
+ *deleted_count = 0;
return true;
}
- scoped_ptr<URLsDeletedDetails> deleted_details(new URLsDeletedDetails);
- scoped_ptr<FaviconChangeDetails> favicon(new FaviconChangeDetails);
+ if (!DeleteHistoryInternal(ids_set, notifications))
+ return false;
+
+ *deleted_count = ids_set.size();
+
+ return true;
+}
+
+bool AndroidProviderBackend::DeleteHistory(
+ const std::string& selection,
+ const std::vector<string16>& selection_args,
+ int* deleted_count,
+ HistoryNotifications* notifications) {
+ if (!EnsureInitializedAndUpdated())
+ return false;
+
+ TableIDRows ids_set;
+ if (!GetSelectedURLs(selection, selection_args, &ids_set))
+ return false;
+
+ if (ids_set.empty()) {
+ *deleted_count = 0;
+ return true;
+ }
+
+ *deleted_count = ids_set.size();
+
+ // Get the rows bookmarked.
+ TableIDRows bookmark_rows;
for (TableIDRows::const_iterator i = ids_set.begin(); i != ids_set.end();
++i) {
- URLRow url_row;
- if (!history_db_->GetURLRow(i->url_id, &url_row))
- return false;
- deleted_details->rows.push_back(url_row);
- deleted_details->urls.insert(url_row.url());
- if (thumbnail_db_->GetIconMappingsForPageURL(url_row.url(), NULL))
- favicon->urls.insert(url_row.url());
+ if (i->bookmarked)
+ bookmark_rows.push_back(*i);
}
- for (std::vector<SQLHandler*>::iterator i =
- sql_handlers_.begin(); i != sql_handlers_.end(); ++i) {
- if (!(*i)->Delete(ids_set))
+ if (!DeleteHistoryInternal(ids_set, notifications))
+ return false;
+
+ scoped_ptr<URLsModifiedDetails> modified(new URLsModifiedDetails);
+ for (TableIDRows::const_iterator i = bookmark_rows.begin();
+ i != bookmark_rows.end(); ++i) {
+ URLRow row(i->url);
+ // Set visit time as UnixEpoch because the Android's system time start from
+ // the unix epoch.
+ row.set_last_visit(Time::UnixEpoch());
+ if (!history_db_->AddURL(row))
return false;
+ modified->changed_urls.push_back(row);
}
- *deleted_count = ids_set.size();
notifications->push_back(HistoryNotification(
- chrome::NOTIFICATION_HISTORY_URLS_DELETED,
- deleted_details.release()));
- if (favicon.get() && !favicon->urls.empty())
- notifications->push_back(HistoryNotification(
- chrome::NOTIFICATION_FAVICON_CHANGED, favicon.release()));
+ chrome::NOTIFICATION_HISTORY_TYPED_URLS_MODIFIED, modified.release()));
return true;
}
@@ -751,10 +791,9 @@ int AndroidProviderBackend::AppendBookmarkResultColumn(
bool AndroidProviderBackend::GetSelectedURLs(
const std::string& selection,
const std::vector<string16>& selection_args,
- const char* virtual_table,
TableIDRows* rows) {
- std::string sql("SELECT url_id, urls_url FROM (");
- sql.append(virtual_table);
+ std::string sql("SELECT url_id, urls_url, bookmark FROM (");
+ sql.append(kVirtualHistoryAndBookmarkTable);
sql.append(" )");
if (!selection.empty()) {
@@ -773,6 +812,7 @@ bool AndroidProviderBackend::GetSelectedURLs(
TableIDRow row;
row.url_id = statement.ColumnInt64(0);
row.url = GURL(statement.ColumnString(1));
+ row.bookmarked = statement.ColumnBool(2);
rows->push_back(row);
}
return true;
@@ -951,7 +991,7 @@ AndroidStatement* AndroidProviderBackend::QueryHistoryAndBookmarksInternal(
sql.append("SELECT ");
int replaced_index = AppendBookmarkResultColumn(projections, &sql);
sql.append(" FROM (");
- sql.append(kVirtualBookmarkTable);
+ sql.append(kVirtualHistoryAndBookmarkTable);
sql.append(")");
if (!selection.empty()) {
@@ -976,6 +1016,36 @@ AndroidStatement* AndroidProviderBackend::QueryHistoryAndBookmarksInternal(
return new AndroidStatement(result, replaced_index);
}
+bool AndroidProviderBackend::DeleteHistoryInternal(
+ const TableIDRows& urls,
+ HistoryNotifications* notifications) {
+ scoped_ptr<URLsDeletedDetails> deleted_details(new URLsDeletedDetails);
+ scoped_ptr<FaviconChangeDetails> favicon(new FaviconChangeDetails);
+ for (TableIDRows::const_iterator i = urls.begin(); i != urls.end(); ++i) {
+ URLRow url_row;
+ if (!history_db_->GetURLRow(i->url_id, &url_row))
+ return false;
+ deleted_details->rows.push_back(url_row);
+ deleted_details->urls.insert(url_row.url());
+ if (thumbnail_db_->GetIconMappingsForPageURL(url_row.url(), NULL))
+ favicon->urls.insert(url_row.url());
+ }
+
+ for (std::vector<SQLHandler*>::iterator i =
+ sql_handlers_.begin(); i != sql_handlers_.end(); ++i) {
+ if (!(*i)->Delete(urls))
+ return false;
+ }
+
+ notifications->push_back(HistoryNotification(
+ chrome::NOTIFICATION_HISTORY_URLS_DELETED,
+ deleted_details.release()));
+ if (favicon.get() && !favicon->urls.empty())
+ notifications->push_back(HistoryNotification(
+ chrome::NOTIFICATION_FAVICON_CHANGED, favicon.release()));
+ return true;
+}
+
void AndroidProviderBackend::BroadcastNotifications(
const HistoryNotifications& notifications) {
for (HistoryNotifications::const_iterator i = notifications.begin();
diff --git a/chrome/browser/history/android/android_provider_backend.h b/chrome/browser/history/android/android_provider_backend.h
index 357f781..08ce78f 100644
--- a/chrome/browser/history/android/android_provider_backend.h
+++ b/chrome/browser/history/android/android_provider_backend.h
@@ -92,6 +92,14 @@ class AndroidProviderBackend {
const std::vector<string16>& selection_args,
int* deleted_count);
+ // Deletes the matched history, returns true on success, false on error.
+ // The number of deleted row is returned in |deleted_count|.
+ // The url row is kept and the visit count is reset if the matched url
+ // is bookmarked.
+ bool DeleteHistory(const std::string& selection,
+ const std::vector<string16>& selection_args,
+ int* deleted_count);
+
// SearchTerms --------------------------------------------------------------
//
// Returns the result of the given query.
@@ -180,7 +188,8 @@ class AndroidProviderBackend {
// Runs the given update and returns the number of updated rows in
// |update_count| and return true on success, false on error.
//
- // The notifications of change is returned in |notifications|.
+ // The notifications are returned in |notifications| and the ownership of them
+ // is transfered to caller.
//
// |row| is the value to update.
// |selection| is the SQL WHERE clause without 'WHERE'.
@@ -192,7 +201,8 @@ class AndroidProviderBackend {
HistoryNotifications* notifications);
// Inserts the given values and returns the URLID of the inserted row.
- // The notifications of change is returned in |notifications|.
+ // The notifications are returned in |notifications| and the ownership of them
+ // is transfered to caller.
AndroidURLID InsertHistoryAndBookmark(const HistoryAndBookmarkRow& values,
HistoryNotifications* notifications);
@@ -201,14 +211,25 @@ class AndroidProviderBackend {
// |selection| is the SQL WHERE clause without 'WHERE'.
// |selection_args| is the arguments for the WHERE clause.
//
- // The notifications of change is returned in |notifications|.
- //
+ // The notifications are returned in |notifications| and the ownership of them
+ // is transfered to the caller.
// if |selection| is empty all history and bookmarks are deleted.
bool DeleteHistoryAndBookmarks(const std::string& selection,
const std::vector<string16>& selection_args,
int* deleted_count,
HistoryNotifications* notifications);
+ // Deletes the matched history, returns true on success, false on error.
+ // The number of deleted row is returned in |deleted_count|.
+ // The notifications are returned in |notifications| and the ownership of them
+ // is transfered to caller.
+ // The url row is kept and the visit is reset if the matched url is
+ // bookmarked.
+ bool DeleteHistory(const std::string& selection,
+ const std::vector<string16>& selection_args,
+ int* deleted_count,
+ HistoryNotifications* notifications);
+
// Initializes and updates tables if necessary.
bool EnsureInitializedAndUpdated();
@@ -247,11 +268,10 @@ class AndroidProviderBackend {
const std::vector<SearchRow::ColumnID>& projections,
std::string* result_column);
- // Runs the given query on |virtual_table| and returns true if succeeds, the
- // selected URLID and url are returned in |rows|.
+ // Runs the given query on history_bookmark virtual table and returns true if
+ // succeeds, the selected URLID and url are returned in |rows|.
bool GetSelectedURLs(const std::string& selection,
const std::vector<string16>& selection_args,
- const char* virtual_table,
TableIDRows* rows);
// Runs the given query on search_terms table and returns true on success,
@@ -275,6 +295,12 @@ class AndroidProviderBackend {
const std::vector<string16>& selection_args,
const std::string& sort_order);
+ // Delete the given urls' history, returns true on success, or false on error.
+ // The notifications are returned in |notifications| and the ownership of them
+ // is transfered to caller.
+ bool DeleteHistoryInternal(const TableIDRows& urls,
+ HistoryNotifications* notifications);
+
void BroadcastNotifications(const HistoryNotifications& notifications);
// Add the search term from the given |values|. It will add the values.url()
diff --git a/chrome/browser/history/android/android_provider_backend_unittest.cc b/chrome/browser/history/android/android_provider_backend_unittest.cc
index 1aa1a1a..8832ab8 100644
--- a/chrome/browser/history/android/android_provider_backend_unittest.cc
+++ b/chrome/browser/history/android/android_provider_backend_unittest.cc
@@ -1446,4 +1446,64 @@ TEST_F(AndroidProviderBackendTest, InsertSearchTerm) {
EXPECT_FALSE(statement->statement()->Step());
}
+TEST_F(AndroidProviderBackendTest, DeleteHistory) {
+ HistoryAndBookmarkRow row1;
+ row1.set_raw_url("cnn.com");
+ row1.set_url(GURL("http://cnn.com"));
+ row1.set_last_visit_time(Time::Now() - TimeDelta::FromDays(1));
+ row1.set_created(Time::Now() - TimeDelta::FromDays(20));
+ row1.set_visit_count(10);
+ row1.set_is_bookmark(true);
+ row1.set_title(UTF8ToUTF16("cnn"));
+
+ HistoryAndBookmarkRow row2;
+ row2.set_raw_url("http://www.example.com");
+ row2.set_url(GURL("http://www.example.com"));
+ row2.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
+ row2.set_is_bookmark(false);
+ row2.set_title(UTF8ToUTF16("example"));
+ std::vector<unsigned char> data;
+ data.push_back('1');
+ row2.set_favicon(data);
+
+ ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_, bookmark_temp_));
+ ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_, NULL,
+ &history_db_));
+ scoped_ptr<AndroidProviderBackend> backend(
+ new AndroidProviderBackend(android_cache_db_name_, &history_db_,
+ &thumbnail_db_, &bookmark_model_, &delegate_));
+
+ AndroidURLID id1 = backend->InsertHistoryAndBookmark(row1);
+ ASSERT_TRUE(id1);
+ AndroidURLID id2 = backend->InsertHistoryAndBookmark(row2);
+ ASSERT_TRUE(id2);
+ AddBookmark(row1.url());
+
+ // Delete history
+ int deleted_count = 0;
+ ASSERT_TRUE(backend->DeleteHistory(std::string(), std::vector<string16>(),
+ &deleted_count));
+ EXPECT_EQ(2, deleted_count);
+ // The row2 was deleted.
+ EXPECT_FALSE(history_db_.GetRowForURL(row2.url(), NULL));
+ // Still find the row1.
+ URLRow url_row;
+ ASSERT_TRUE(history_db_.GetRowForURL(row1.url(), &url_row));
+ // The visit_count was reset.
+ EXPECT_EQ(0, url_row.visit_count());
+ EXPECT_EQ(Time::UnixEpoch(), url_row.last_visit());
+
+ // Verify notification
+ ASSERT_TRUE(delegate_.deleted_details());
+ ASSERT_EQ(2u, delegate_.deleted_details()->rows.size());
+ ASSERT_EQ(2u, delegate_.deleted_details()->urls.size());
+ ASSERT_TRUE(delegate_.modified_details());
+ ASSERT_EQ(1u, delegate_.modified_details()->changed_urls.size());
+ EXPECT_EQ(row1.url(),
+ delegate_.modified_details()->changed_urls[0].url());
+ EXPECT_EQ(Time::UnixEpoch(),
+ delegate_.modified_details()->changed_urls[0].last_visit());
+ EXPECT_EQ(1u, delegate_.favicon_details()->urls.size());
+}
+
} // namespace history
diff --git a/chrome/browser/history/android/sql_handler.cc b/chrome/browser/history/android/sql_handler.cc
index 4316a8f..ebe14dd 100644
--- a/chrome/browser/history/android/sql_handler.cc
+++ b/chrome/browser/history/android/sql_handler.cc
@@ -7,7 +7,8 @@
namespace history {
TableIDRow::TableIDRow()
- : url_id(0) {
+ : url_id(0),
+ bookmarked(false) {
}
TableIDRow::~TableIDRow() {
diff --git a/chrome/browser/history/android/sql_handler.h b/chrome/browser/history/android/sql_handler.h
index 32425f7..43c57ee 100644
--- a/chrome/browser/history/android/sql_handler.h
+++ b/chrome/browser/history/android/sql_handler.h
@@ -18,6 +18,8 @@ struct TableIDRow {
URLID url_id;
GURL url;
+ // Whether the URL was bookmarked.
+ bool bookmarked;
};
typedef std::vector<TableIDRow> TableIDRows;