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" | 15 #include "chrome/browser/history/history_notifications.h" |
| 16 #include "chrome/browser/history/history_service.h" |
16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
17 #include "components/history/core/browser/in_memory_database.h" | 18 #include "components/history/core/browser/in_memory_database.h" |
18 #include "components/history/core/browser/url_database.h" | 19 #include "components/history/core/browser/url_database.h" |
19 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
20 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
21 | 22 |
22 namespace history { | 23 namespace history { |
23 | 24 |
24 InMemoryHistoryBackend::InMemoryHistoryBackend() | 25 InMemoryHistoryBackend::InMemoryHistoryBackend() |
25 : profile_(NULL) { | 26 : profile_(nullptr), history_service_(nullptr) { |
26 } | 27 } |
27 | 28 |
28 InMemoryHistoryBackend::~InMemoryHistoryBackend() {} | 29 InMemoryHistoryBackend::~InMemoryHistoryBackend() { |
| 30 if (history_service_) |
| 31 history_service_->RemoveObserver(this); |
| 32 } |
29 | 33 |
30 bool InMemoryHistoryBackend::Init(const base::FilePath& history_filename) { | 34 bool InMemoryHistoryBackend::Init(const base::FilePath& history_filename) { |
31 db_.reset(new InMemoryDatabase); | 35 db_.reset(new InMemoryDatabase); |
32 return db_->InitFromDisk(history_filename); | 36 return db_->InitFromDisk(history_filename); |
33 } | 37 } |
34 | 38 |
35 void InMemoryHistoryBackend::AttachToHistoryService(Profile* profile) { | 39 void InMemoryHistoryBackend::AttachToHistoryService( |
| 40 Profile* profile, |
| 41 HistoryService* history_service) { |
36 if (!db_) { | 42 if (!db_) { |
37 NOTREACHED(); | 43 NOTREACHED(); |
38 return; | 44 return; |
39 } | 45 } |
40 | 46 |
| 47 DCHECK(history_service); |
| 48 history_service_ = history_service; |
| 49 history_service_->AddObserver(this); |
| 50 |
41 profile_ = profile; | 51 profile_ = profile; |
42 | 52 |
43 // TODO(evanm): this is currently necessitated by generate_profile, which | 53 // TODO(evanm): this is currently necessitated by generate_profile, which |
44 // runs without a browser process. generate_profile should really create | 54 // runs without a browser process. generate_profile should really create |
45 // a browser process, at which point this check can then be nuked. | 55 // a browser process, at which point this check can then be nuked. |
46 if (!g_browser_process) | 56 if (!g_browser_process) |
47 return; | 57 return; |
48 | 58 |
49 // Register for the notifications we care about. | 59 // Register for the notifications we care about. |
50 // We only want notifications for the associated profile. | 60 // We only want notifications for the associated profile. |
51 content::Source<Profile> source(profile_); | 61 content::Source<Profile> source(profile_); |
52 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED, source); | |
53 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, source); | 62 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, source); |
54 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, source); | 63 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, source); |
55 registrar_.Add( | 64 registrar_.Add( |
56 this, chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_UPDATED, source); | 65 this, chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_UPDATED, source); |
57 registrar_.Add( | 66 registrar_.Add( |
58 this, chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_DELETED, source); | 67 this, chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_DELETED, source); |
59 } | 68 } |
60 | 69 |
61 void InMemoryHistoryBackend::DeleteAllSearchTermsForKeyword( | 70 void InMemoryHistoryBackend::DeleteAllSearchTermsForKeyword( |
62 KeywordID keyword_id) { | 71 KeywordID keyword_id) { |
63 // For simplicity, this will not remove the corresponding URLRows, but | 72 // For simplicity, this will not remove the corresponding URLRows, but |
64 // this is okay, as the main database does not do so either. | 73 // this is okay, as the main database does not do so either. |
65 db_->DeleteAllSearchTermsForKeyword(keyword_id); | 74 db_->DeleteAllSearchTermsForKeyword(keyword_id); |
66 } | 75 } |
67 | 76 |
| 77 void InMemoryHistoryBackend::OnURLVisited(HistoryService* history_service, |
| 78 ui::PageTransition transition, |
| 79 const URLRow& row, |
| 80 const RedirectList& redirects, |
| 81 base::Time visit_time) { |
| 82 OnURLVisitedOrModified(row); |
| 83 } |
| 84 |
68 void InMemoryHistoryBackend::Observe( | 85 void InMemoryHistoryBackend::Observe( |
69 int type, | 86 int type, |
70 const content::NotificationSource& source, | 87 const content::NotificationSource& source, |
71 const content::NotificationDetails& details) { | 88 const content::NotificationDetails& details) { |
72 switch (type) { | 89 switch (type) { |
73 case chrome::NOTIFICATION_HISTORY_URL_VISITED: | |
74 OnURLVisitedOrModified(content::Details<URLVisitedDetails>(details)->row); | |
75 break; | |
76 case chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_UPDATED: | 90 case chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_UPDATED: |
77 OnKeywordSearchTermUpdated( | 91 OnKeywordSearchTermUpdated( |
78 *content::Details<KeywordSearchUpdatedDetails>(details).ptr()); | 92 *content::Details<KeywordSearchUpdatedDetails>(details).ptr()); |
79 break; | 93 break; |
80 case chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_DELETED: | 94 case chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_DELETED: |
81 OnKeywordSearchTermDeleted( | 95 OnKeywordSearchTermDeleted( |
82 *content::Details<KeywordSearchDeletedDetails>(details).ptr()); | 96 *content::Details<KeywordSearchDeletedDetails>(details).ptr()); |
83 break; | 97 break; |
84 case chrome::NOTIFICATION_HISTORY_URLS_MODIFIED: { | 98 case chrome::NOTIFICATION_HISTORY_URLS_MODIFIED: { |
85 const URLsModifiedDetails* modified_details = | 99 const URLsModifiedDetails* modified_details = |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 } | 154 } |
141 | 155 |
142 void InMemoryHistoryBackend::OnKeywordSearchTermDeleted( | 156 void InMemoryHistoryBackend::OnKeywordSearchTermDeleted( |
143 const KeywordSearchDeletedDetails& details) { | 157 const KeywordSearchDeletedDetails& details) { |
144 // For simplicity, this will not remove the corresponding URLRow, but this is | 158 // For simplicity, this will not remove the corresponding URLRow, but this is |
145 // okay, as the main database does not do so either. | 159 // okay, as the main database does not do so either. |
146 db_->DeleteKeywordSearchTermForURL(details.url_row_id); | 160 db_->DeleteKeywordSearchTermForURL(details.url_row_id); |
147 } | 161 } |
148 | 162 |
149 } // namespace history | 163 } // namespace history |
OLD | NEW |