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/in_memory_history_backend.h" | 5 #include "chrome/browser/history/in_memory_history_backend.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
15 #include "chrome/browser/history/history_notifications.h" | |
16 #include "chrome/browser/history/history_service.h" | 15 #include "chrome/browser/history/history_service.h" |
17 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
18 #include "components/history/core/browser/in_memory_database.h" | 17 #include "components/history/core/browser/in_memory_database.h" |
19 #include "components/history/core/browser/url_database.h" | 18 #include "components/history/core/browser/url_database.h" |
20 #include "content/public/browser/notification_details.h" | 19 #include "content/public/browser/notification_details.h" |
21 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
22 | 21 |
23 namespace history { | 22 namespace history { |
24 | 23 |
25 InMemoryHistoryBackend::InMemoryHistoryBackend() | 24 InMemoryHistoryBackend::InMemoryHistoryBackend() |
(...skipping 26 matching lines...) Expand all Loading... |
52 | 51 |
53 // TODO(evanm): this is currently necessitated by generate_profile, which | 52 // TODO(evanm): this is currently necessitated by generate_profile, which |
54 // runs without a browser process. generate_profile should really create | 53 // runs without a browser process. generate_profile should really create |
55 // a browser process, at which point this check can then be nuked. | 54 // a browser process, at which point this check can then be nuked. |
56 if (!g_browser_process) | 55 if (!g_browser_process) |
57 return; | 56 return; |
58 | 57 |
59 // Register for the notifications we care about. | 58 // Register for the notifications we care about. |
60 // We only want notifications for the associated profile. | 59 // We only want notifications for the associated profile. |
61 content::Source<Profile> source(profile_); | 60 content::Source<Profile> source(profile_); |
62 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, source); | |
63 registrar_.Add( | 61 registrar_.Add( |
64 this, chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_UPDATED, source); | 62 this, chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_UPDATED, source); |
65 registrar_.Add( | 63 registrar_.Add( |
66 this, chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_DELETED, source); | 64 this, chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_DELETED, source); |
67 } | 65 } |
68 | 66 |
69 void InMemoryHistoryBackend::DeleteAllSearchTermsForKeyword( | 67 void InMemoryHistoryBackend::DeleteAllSearchTermsForKeyword( |
70 KeywordID keyword_id) { | 68 KeywordID keyword_id) { |
71 // For simplicity, this will not remove the corresponding URLRows, but | 69 // For simplicity, this will not remove the corresponding URLRows, but |
72 // this is okay, as the main database does not do so either. | 70 // this is okay, as the main database does not do so either. |
73 db_->DeleteAllSearchTermsForKeyword(keyword_id); | 71 db_->DeleteAllSearchTermsForKeyword(keyword_id); |
74 } | 72 } |
75 | 73 |
76 void InMemoryHistoryBackend::OnURLVisited(HistoryService* history_service, | 74 void InMemoryHistoryBackend::OnURLVisited(HistoryService* history_service, |
77 ui::PageTransition transition, | 75 ui::PageTransition transition, |
78 const URLRow& row, | 76 const URLRow& row, |
79 const RedirectList& redirects, | 77 const RedirectList& redirects, |
80 base::Time visit_time) { | 78 base::Time visit_time) { |
81 OnURLVisitedOrModified(row); | 79 OnURLVisitedOrModified(row); |
82 } | 80 } |
83 | 81 |
84 void InMemoryHistoryBackend::OnURLsModified(HistoryService* history_service, | 82 void InMemoryHistoryBackend::OnURLsModified(HistoryService* history_service, |
85 const URLRows& changed_urls) { | 83 const URLRows& changed_urls) { |
86 for (const auto& row : changed_urls) { | 84 for (const auto& row : changed_urls) { |
87 OnURLVisitedOrModified(row); | 85 OnURLVisitedOrModified(row); |
88 } | 86 } |
89 } | 87 } |
90 | 88 |
| 89 void InMemoryHistoryBackend::OnURLsDeleted(HistoryService* history_service, |
| 90 bool all_history, |
| 91 bool expired, |
| 92 const URLRows& deleted_rows, |
| 93 const std::set<GURL>& favicon_urls) { |
| 94 DCHECK(db_); |
| 95 |
| 96 if (all_history) { |
| 97 // When all history is deleted, the individual URLs won't be listed. Just |
| 98 // create a new database to quickly clear everything out. |
| 99 db_.reset(new InMemoryDatabase); |
| 100 if (!db_->InitFromScratch()) |
| 101 db_.reset(); |
| 102 return; |
| 103 } |
| 104 |
| 105 // Delete all matching URLs in our database. |
| 106 for (const auto& row : deleted_rows) { |
| 107 // This will also delete the corresponding keyword search term. |
| 108 // Ignore errors, as we typically only cache a subset of URLRows. |
| 109 db_->DeleteURLRow(row.id()); |
| 110 } |
| 111 } |
| 112 |
91 void InMemoryHistoryBackend::Observe( | 113 void InMemoryHistoryBackend::Observe( |
92 int type, | 114 int type, |
93 const content::NotificationSource& source, | 115 const content::NotificationSource& source, |
94 const content::NotificationDetails& details) { | 116 const content::NotificationDetails& details) { |
95 switch (type) { | 117 switch (type) { |
96 case chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_UPDATED: | 118 case chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_UPDATED: |
97 OnKeywordSearchTermUpdated( | 119 OnKeywordSearchTermUpdated( |
98 *content::Details<KeywordSearchUpdatedDetails>(details).ptr()); | 120 *content::Details<KeywordSearchUpdatedDetails>(details).ptr()); |
99 break; | 121 break; |
100 case chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_DELETED: | 122 case chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_DELETED: |
101 OnKeywordSearchTermDeleted( | 123 OnKeywordSearchTermDeleted( |
102 *content::Details<KeywordSearchDeletedDetails>(details).ptr()); | 124 *content::Details<KeywordSearchDeletedDetails>(details).ptr()); |
103 break; | 125 break; |
104 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: | |
105 OnURLsDeleted(*content::Details<URLsDeletedDetails>(details).ptr()); | |
106 break; | |
107 default: | 126 default: |
108 // For simplicity, the unit tests send us all notifications, even when | 127 // For simplicity, the unit tests send us all notifications, even when |
109 // we haven't registered for them, so don't assert here. | 128 // we haven't registered for them, so don't assert here. |
110 break; | 129 break; |
111 } | 130 } |
112 } | 131 } |
113 | 132 |
114 void InMemoryHistoryBackend::OnURLVisitedOrModified(const URLRow& url_row) { | 133 void InMemoryHistoryBackend::OnURLVisitedOrModified(const URLRow& url_row) { |
115 DCHECK(db_); | 134 DCHECK(db_); |
116 DCHECK(url_row.id()); | 135 DCHECK(url_row.id()); |
117 if (url_row.typed_count() || db_->GetKeywordSearchTermRow(url_row.id(), NULL)) | 136 if (url_row.typed_count() || db_->GetKeywordSearchTermRow(url_row.id(), NULL)) |
118 db_->InsertOrUpdateURLRowByID(url_row); | 137 db_->InsertOrUpdateURLRowByID(url_row); |
119 else | 138 else |
120 db_->DeleteURLRow(url_row.id()); | 139 db_->DeleteURLRow(url_row.id()); |
121 } | 140 } |
122 | 141 |
123 void InMemoryHistoryBackend::OnURLsDeleted(const URLsDeletedDetails& details) { | |
124 DCHECK(db_); | |
125 | |
126 if (details.all_history) { | |
127 // When all history is deleted, the individual URLs won't be listed. Just | |
128 // create a new database to quickly clear everything out. | |
129 db_.reset(new InMemoryDatabase); | |
130 if (!db_->InitFromScratch()) | |
131 db_.reset(); | |
132 return; | |
133 } | |
134 | |
135 // Delete all matching URLs in our database. | |
136 for (URLRows::const_iterator row = details.rows.begin(); | |
137 row != details.rows.end(); ++row) { | |
138 // This will also delete the corresponding keyword search term. | |
139 // Ignore errors, as we typically only cache a subset of URLRows. | |
140 db_->DeleteURLRow(row->id()); | |
141 } | |
142 } | |
143 | |
144 void InMemoryHistoryBackend::OnKeywordSearchTermUpdated( | 142 void InMemoryHistoryBackend::OnKeywordSearchTermUpdated( |
145 const KeywordSearchUpdatedDetails& details) { | 143 const KeywordSearchUpdatedDetails& details) { |
146 DCHECK(details.url_row.id()); | 144 DCHECK(details.url_row.id()); |
147 db_->InsertOrUpdateURLRowByID(details.url_row); | 145 db_->InsertOrUpdateURLRowByID(details.url_row); |
148 db_->SetKeywordSearchTermsForURL( | 146 db_->SetKeywordSearchTermsForURL( |
149 details.url_row.id(), details.keyword_id, details.term); | 147 details.url_row.id(), details.keyword_id, details.term); |
150 } | 148 } |
151 | 149 |
152 void InMemoryHistoryBackend::OnKeywordSearchTermDeleted( | 150 void InMemoryHistoryBackend::OnKeywordSearchTermDeleted( |
153 const KeywordSearchDeletedDetails& details) { | 151 const KeywordSearchDeletedDetails& details) { |
154 // For simplicity, this will not remove the corresponding URLRow, but this is | 152 // For simplicity, this will not remove the corresponding URLRow, but this is |
155 // okay, as the main database does not do so either. | 153 // okay, as the main database does not do so either. |
156 db_->DeleteKeywordSearchTermForURL(details.url_row_id); | 154 db_->DeleteKeywordSearchTermForURL(details.url_row_id); |
157 } | 155 } |
158 | 156 |
159 } // namespace history | 157 } // namespace history |
OLD | NEW |