OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/history/android/android_provider_backend.h" | 5 #include "chrome/browser/history/android/android_provider_backend.h" |
6 | 6 |
7 #include "base/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
8 #include "chrome/browser/bookmarks/bookmark_service.h" | 8 #include "chrome/browser/bookmarks/bookmark_service.h" |
9 #include "chrome/common/chrome_notification_types.h" | 9 #include "chrome/common/chrome_notification_types.h" |
10 #include "chrome/browser/history/android/android_time.h" | 10 #include "chrome/browser/history/android/android_time.h" |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 BroadcastNotifications(notifications); | 169 BroadcastNotifications(notifications); |
170 return true; | 170 return true; |
171 } | 171 } |
172 | 172 |
173 AndroidURLID AndroidProviderBackend::InsertHistoryAndBookmark( | 173 AndroidURLID AndroidProviderBackend::InsertHistoryAndBookmark( |
174 const HistoryAndBookmarkRow& values) { | 174 const HistoryAndBookmarkRow& values) { |
175 HistoryNotifications notifications; | 175 HistoryNotifications notifications; |
176 | 176 |
177 ScopedTransaction transaction(history_db_, thumbnail_db_); | 177 ScopedTransaction transaction(history_db_, thumbnail_db_); |
178 | 178 |
179 AndroidURLID id = InsertHistoryAndBookmark(values, ¬ifications); | 179 AndroidURLID id = InsertHistoryAndBookmark(values, ¬ifications, true); |
180 if (id) { | 180 if (id) { |
181 transaction.Commit(); | 181 transaction.Commit(); |
182 BroadcastNotifications(notifications); | 182 BroadcastNotifications(notifications); |
183 return id; | 183 return id; |
184 } | 184 } |
185 return 0; | 185 return 0; |
186 } | 186 } |
187 | 187 |
188 bool AndroidProviderBackend::DeleteHistoryAndBookmarks( | 188 bool AndroidProviderBackend::DeleteHistoryAndBookmarks( |
189 const std::string& selection, | 189 const std::string& selection, |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 | 350 |
351 if (!favicon->urls.empty()) | 351 if (!favicon->urls.empty()) |
352 notifications->push_back(HistoryNotification( | 352 notifications->push_back(HistoryNotification( |
353 chrome::NOTIFICATION_FAVICON_CHANGED, favicon.release())); | 353 chrome::NOTIFICATION_FAVICON_CHANGED, favicon.release())); |
354 | 354 |
355 return true; | 355 return true; |
356 } | 356 } |
357 | 357 |
358 AndroidURLID AndroidProviderBackend::InsertHistoryAndBookmark( | 358 AndroidURLID AndroidProviderBackend::InsertHistoryAndBookmark( |
359 const HistoryAndBookmarkRow& values, | 359 const HistoryAndBookmarkRow& values, |
360 HistoryNotifications* notifications) { | 360 HistoryNotifications* notifications, |
| 361 bool ensure_initialized_and_updated) { |
361 if (!IsHistoryAndBookmarkRowValid(values)) | 362 if (!IsHistoryAndBookmarkRowValid(values)) |
362 return false; | 363 return false; |
363 | 364 |
364 if (!EnsureInitializedAndUpdated()) | 365 if (ensure_initialized_and_updated && !EnsureInitializedAndUpdated()) |
365 return 0; | 366 return 0; |
366 | 367 |
367 DCHECK(values.is_value_set_explicitly(HistoryAndBookmarkRow::URL)); | 368 DCHECK(values.is_value_set_explicitly(HistoryAndBookmarkRow::URL)); |
368 // Make a copy of values as we need change it during insert. | 369 // Make a copy of values as we need change it during insert. |
369 HistoryAndBookmarkRow row = values; | 370 HistoryAndBookmarkRow row = values; |
370 for (std::vector<SQLHandler*>::iterator i = | 371 for (std::vector<SQLHandler*>::iterator i = |
371 sql_handlers_.begin(); i != sql_handlers_.end(); ++i) { | 372 sql_handlers_.begin(); i != sql_handlers_.end(); ++i) { |
372 if (!(*i)->Insert(&row)) | 373 if (!(*i)->Insert(&row)) |
373 return 0; | 374 return 0; |
374 } | 375 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 for (TableIDRows::const_iterator i = ids_set.begin(); i != ids_set.end(); | 450 for (TableIDRows::const_iterator i = ids_set.begin(); i != ids_set.end(); |
450 ++i) { | 451 ++i) { |
451 if (i->bookmarked) { | 452 if (i->bookmarked) { |
452 AndroidURLRow android_url_row; | 453 AndroidURLRow android_url_row; |
453 if (!history_db_->GetAndroidURLRow(i->url_id, &android_url_row)) | 454 if (!history_db_->GetAndroidURLRow(i->url_id, &android_url_row)) |
454 return false; | 455 return false; |
455 HistoryAndBookmarkRow row; | 456 HistoryAndBookmarkRow row; |
456 row.set_raw_url(android_url_row.raw_url); | 457 row.set_raw_url(android_url_row.raw_url); |
457 row.set_url(i->url); | 458 row.set_url(i->url); |
458 // Set the visit time to the UnixEpoch since that's when the Android | 459 // Set the visit time to the UnixEpoch since that's when the Android |
459 // system time starts. | 460 // system time starts. The Android have a CTS testcase for this. |
460 row.set_last_visit_time(Time::UnixEpoch()); | 461 row.set_last_visit_time(Time::UnixEpoch()); |
461 row.set_visit_count(0); | 462 row.set_visit_count(0); |
462 // We don't want to change the bookmark model, so set_is_bookmark() is | 463 // We don't want to change the bookmark model, so set_is_bookmark() is |
463 // not called. | 464 // not called. |
464 bookmarks.push_back(row); | 465 bookmarks.push_back(row); |
465 } | 466 } |
466 } | 467 } |
467 | 468 |
468 // Don't delete the bookmark from bookmark model when deleting the history. | 469 // Don't delete the bookmark from bookmark model when deleting the history. |
469 if (!DeleteHistoryInternal(ids_set, false, notifications)) | 470 if (!DeleteHistoryInternal(ids_set, false, notifications)) |
470 return false; | 471 return false; |
471 | 472 |
472 for (std::vector<HistoryAndBookmarkRow>::const_iterator i = bookmarks.begin(); | 473 for (std::vector<HistoryAndBookmarkRow>::const_iterator i = bookmarks.begin(); |
473 i != bookmarks.end(); ++i) { | 474 i != bookmarks.end(); ++i) { |
474 if (!InsertHistoryAndBookmark(*i, notifications)) | 475 // Don't update the tables, otherwise, the bookmarks will be added to |
| 476 // database during UpdateBookmark(), then the insertion will fail. |
| 477 // We can't rely on UpdateBookmark() to insert the bookmarks into history |
| 478 // database as the raw_url will be lost. |
| 479 if (!InsertHistoryAndBookmark(*i, notifications, false)) |
475 return false; | 480 return false; |
476 } | 481 } |
477 | |
478 return true; | 482 return true; |
479 } | 483 } |
480 | 484 |
481 AndroidStatement* AndroidProviderBackend::QuerySearchTerms( | 485 AndroidStatement* AndroidProviderBackend::QuerySearchTerms( |
482 const std::vector<SearchRow::ColumnID>& projections, | 486 const std::vector<SearchRow::ColumnID>& projections, |
483 const std::string& selection, | 487 const std::string& selection, |
484 const std::vector<string16>& selection_args, | 488 const std::vector<string16>& selection_args, |
485 const std::string& sort_order) { | 489 const std::string& sort_order) { |
486 if (projections.empty()) | 490 if (projections.empty()) |
487 return NULL; | 491 return NULL; |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
752 std::vector<BookmarkService::URLAndTitle> bookmarks; | 756 std::vector<BookmarkService::URLAndTitle> bookmarks; |
753 bookmark_service_->GetBookmarks(&bookmarks); | 757 bookmark_service_->GetBookmarks(&bookmarks); |
754 | 758 |
755 if (bookmarks.empty()) | 759 if (bookmarks.empty()) |
756 return true; | 760 return true; |
757 | 761 |
758 std::vector<URLID> url_ids; | 762 std::vector<URLID> url_ids; |
759 for (std::vector<BookmarkService::URLAndTitle>::const_iterator i = | 763 for (std::vector<BookmarkService::URLAndTitle>::const_iterator i = |
760 bookmarks.begin(); i != bookmarks.end(); ++i) { | 764 bookmarks.begin(); i != bookmarks.end(); ++i) { |
761 URLID url_id = history_db_->GetRowForURL(i->url, NULL); | 765 URLID url_id = history_db_->GetRowForURL(i->url, NULL); |
762 if (url_id == 0) | 766 if (url_id == 0) { |
763 // TODO(michaelbai): Add a row to url and android_url table as the | 767 URLRow url_row(i->url); |
764 // bookmark could be added manually by user or insertted by sync. | 768 url_row.set_title(i->title); |
765 continue; | 769 // Set the visit time to the UnixEpoch since that's when the Android |
766 | 770 // system time starts. The Android have a CTS testcase for this. |
| 771 url_row.set_last_visit(Time::UnixEpoch()); |
| 772 url_row.set_hidden(true); |
| 773 url_id = history_db_->AddURL(url_row); |
| 774 if (url_id == 0) { |
| 775 LOG(ERROR) << "Can not add url for the new bookmark"; |
| 776 return false; |
| 777 } |
| 778 if (!history_db_->AddAndroidURLRow(i->url.spec(), url_id)) |
| 779 return false; |
| 780 if (!history_db_->AddBookmarkCacheRow(Time::UnixEpoch(), |
| 781 Time::UnixEpoch(), url_id)) |
| 782 return false; |
| 783 } |
767 url_ids.push_back(url_id); | 784 url_ids.push_back(url_id); |
768 } | 785 } |
769 | 786 |
770 return history_db_->MarkURLsAsBookmarked(url_ids); | 787 return history_db_->MarkURLsAsBookmarked(url_ids); |
771 } | 788 } |
772 | 789 |
773 bool AndroidProviderBackend::UpdateFavicon() { | 790 bool AndroidProviderBackend::UpdateFavicon() { |
774 ThumbnailDatabase::IconMappingEnumerator enumerator; | 791 ThumbnailDatabase::IconMappingEnumerator enumerator; |
775 if (!thumbnail_db_->InitIconMappingEnumerator(FAVICON, &enumerator)) | 792 if (!thumbnail_db_->InitIconMappingEnumerator(FAVICON, &enumerator)) |
776 return false; | 793 return false; |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1147 return false; | 1164 return false; |
1148 | 1165 |
1149 if (!history_db_->SetKeywordSearchTermsForURL(bookmark_row.url_id(), | 1166 if (!history_db_->SetKeywordSearchTermsForURL(bookmark_row.url_id(), |
1150 values.template_url_id(), values.search_term())) | 1167 values.template_url_id(), values.search_term())) |
1151 return false; | 1168 return false; |
1152 } | 1169 } |
1153 return true; | 1170 return true; |
1154 } | 1171 } |
1155 | 1172 |
1156 } // namespace history | 1173 } // namespace history |
OLD | NEW |