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