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 // DownloadHistory manages persisting DownloadItems to the history service by |
| 6 // observing a single DownloadManager and all its DownloadItems. |
| 7 // DownloadHistory decides whether and when to add items to, remove items from, |
| 8 // and update items in the database. DownloadHistory uses DownloadHistoryData to |
| 9 // store per-DownloadItem data such as its db_handle, whether the item is being |
| 10 // added and waiting for its db_handle, and the last DownloadPersistentStoreInfo |
| 11 // that was passed to the database. When the DownloadManager and its delegate |
| 12 // (ChromeDownloadManagerDelegate) are initialized, DownloadHistory is created |
| 13 // and queries the HistoryService. When the HistoryService calls back from |
| 14 // QueryDownloads() to QueryCallback(), DownloadHistory uses |
| 15 // DownloadManager::CreateDownloadItem() to inform DownloadManager of these |
| 16 // persisted DownloadItems. CreateDownloadItem() internally calls |
| 17 // OnDownloadCreated(), which normally adds items to the database, so |
| 18 // QueryCallback() uses |loading_db_handle_| to disable adding these items to |
| 19 // the database as it matches them up with their db_handles. If a download is |
| 20 // removed via OnDownloadRemoved() while the item is still being added to the |
| 21 // database, DownloadHistory uses |removed_while_adding_| to remember to remove |
| 22 // the item when its ItemAdded() callback is called. All callbacks are bound |
| 23 // with a weak pointer to DownloadHistory to prevent use-after-free bugs. |
| 24 // ChromeDownloadManagerDelegate owns DownloadHistory, and deletes it in |
| 25 // Shutdown(), which is called by DownloadManagerImpl::Shutdown() after all |
| 26 // DownloadItems are destroyed. |
| 27 |
5 #include "chrome/browser/download/download_history.h" | 28 #include "chrome/browser/download/download_history.h" |
6 | 29 |
7 #include "base/logging.h" | 30 #include "base/metrics/histogram.h" |
8 #include "chrome/browser/download/download_crx_util.h" | 31 #include "chrome/browser/download/download_crx_util.h" |
9 #include "chrome/browser/history/history_marshaling.h" | 32 #include "chrome/browser/history/download_database.h" |
10 #include "chrome/browser/history/history_service_factory.h" | 33 #include "chrome/browser/history/download_persistent_store_info.h" |
11 #include "chrome/browser/profiles/profile.h" | 34 #include "chrome/browser/history/history.h" |
| 35 #include "content/public/browser/browser_thread.h" |
12 #include "content/public/browser/download_item.h" | 36 #include "content/public/browser/download_item.h" |
13 #include "content/public/browser/download_persistent_store_info.h" | 37 #include "content/public/browser/download_manager.h" |
14 | 38 |
15 using content::DownloadItem; | 39 namespace { |
16 using content::DownloadPersistentStoreInfo; | 40 |
17 | 41 // Per-DownloadItem data. This information does not belong inside DownloadItem, |
18 DownloadHistory::DownloadHistory(Profile* profile) | 42 // and keeping maps in DownloadHistory from DownloadItem to this information is |
19 : profile_(profile), | 43 // error-prone and complicated. Unfortunately, DownloadHistory::removing_*_ and |
20 next_fake_db_handle_(DownloadItem::kUninitializedHandle - 1) { | 44 // removed_while_adding_ cannot be moved into this class partly because |
21 DCHECK(profile); | 45 // DownloadHistoryData is destroyed when DownloadItems are destroyed, and we |
22 } | 46 // have no control over when DownloadItems are destroyed. |
23 | 47 class DownloadHistoryData : public base::SupportsUserData::Data { |
24 DownloadHistory::~DownloadHistory() {} | 48 public: |
25 | 49 static DownloadHistoryData* Get(content::DownloadItem* item) { |
26 void DownloadHistory::GetNextId( | 50 base::SupportsUserData::Data* data = item->GetUserData(kKey); |
27 const HistoryService::DownloadNextIdCallback& callback) { | 51 return (data == NULL) ? NULL : |
28 HistoryService* hs = HistoryServiceFactory::GetForProfile( | 52 static_cast<DownloadHistoryData*>(data); |
29 profile_, Profile::EXPLICIT_ACCESS); | 53 } |
30 if (!hs) | 54 |
31 return; | 55 DownloadHistoryData(content::DownloadItem* item, int64 handle) |
32 | 56 : is_adding_(false), |
33 hs->GetNextDownloadId(&history_consumer_, callback); | 57 db_handle_(handle), |
34 } | 58 info_(NULL) { |
35 | 59 item->SetUserData(kKey, this); |
36 void DownloadHistory::Load( | 60 } |
37 const HistoryService::DownloadQueryCallback& callback) { | 61 |
38 HistoryService* hs = HistoryServiceFactory::GetForProfile( | 62 virtual ~DownloadHistoryData() { |
39 profile_, Profile::EXPLICIT_ACCESS); | 63 } |
40 if (!hs) | 64 |
41 return; | 65 // Whether this item is currently being added to the database. |
42 | 66 bool is_adding() const { return is_adding_; } |
43 hs->QueryDownloads(&history_consumer_, callback); | 67 void set_is_adding(bool a) { is_adding_ = a; } |
44 | 68 |
45 // This is the initial load, so do a cleanup of corrupt in-progress entries. | 69 // Whether this item is already persisted in the database. |
46 hs->CleanUpInProgressEntries(); | 70 bool is_persisted() const { |
| 71 return db_handle_ != history::DownloadDatabase::kUninitializedHandle; |
| 72 } |
| 73 |
| 74 int64 db_handle() const { return db_handle_; } |
| 75 void set_db_handle(int64 h) { db_handle_ = h; } |
| 76 |
| 77 // This allows DownloadHistory::OnDownloadUpdated() to see what changed in a |
| 78 // DownloadItem if anything, in order to prevent writing to the database |
| 79 // unnecessarily. It is nullified when the item is no longer in progress in |
| 80 // order to save memory. |
| 81 DownloadPersistentStoreInfo* info() { return info_.get(); } |
| 82 void set_info(const DownloadPersistentStoreInfo& i) { |
| 83 info_.reset(new DownloadPersistentStoreInfo(i)); |
| 84 } |
| 85 void clear_info() { |
| 86 info_.reset(); |
| 87 } |
| 88 |
| 89 private: |
| 90 static const char kKey[]; |
| 91 |
| 92 bool is_adding_; |
| 93 int64 db_handle_; |
| 94 scoped_ptr<DownloadPersistentStoreInfo> info_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(DownloadHistoryData); |
| 97 }; |
| 98 |
| 99 const char DownloadHistoryData::kKey[] = |
| 100 "DownloadItem DownloadHistoryData"; |
| 101 |
| 102 DownloadPersistentStoreInfo GetPersistentStoreInfo( |
| 103 content::DownloadItem* item) { |
| 104 DownloadHistoryData* dhd = DownloadHistoryData::Get(item); |
| 105 return DownloadPersistentStoreInfo( |
| 106 item->GetFullPath(), |
| 107 item->GetURL(), |
| 108 item->GetReferrerUrl(), |
| 109 item->GetStartTime(), |
| 110 item->GetEndTime(), |
| 111 item->GetReceivedBytes(), |
| 112 item->GetTotalBytes(), |
| 113 item->GetState(), |
| 114 ((dhd != NULL) ? dhd->db_handle() |
| 115 : history::DownloadDatabase::kUninitializedHandle), |
| 116 item->GetOpened()); |
| 117 } |
| 118 |
| 119 typedef std::vector<DownloadPersistentStoreInfo> InfoVector; |
| 120 |
| 121 } // anonymous namespace |
| 122 |
| 123 DownloadHistory::Observer::Observer() {} |
| 124 DownloadHistory::Observer::~Observer() {} |
| 125 |
| 126 DownloadHistory::DownloadHistory( |
| 127 content::DownloadManager* manager, |
| 128 HistoryService* history) |
| 129 : ALLOW_THIS_IN_INITIALIZER_LIST(notifier_(manager, this)), |
| 130 history_(history), |
| 131 loading_db_handle_(history::DownloadDatabase::kUninitializedHandle), |
| 132 history_size_(0), |
| 133 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 134 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 135 content::DownloadManager::DownloadVector items; |
| 136 notifier_.GetManager()->GetAllDownloads(&items); |
| 137 for (content::DownloadManager::DownloadVector::const_iterator |
| 138 it = items.begin(); it != items.end(); ++it) { |
| 139 OnDownloadCreated(notifier_.GetManager(), *it); |
| 140 } |
| 141 history_->QueryDownloads(&history_consumer_, base::Bind( |
| 142 &DownloadHistory::QueryCallback, weak_ptr_factory_.GetWeakPtr())); |
| 143 } |
| 144 |
| 145 DownloadHistory::~DownloadHistory() { |
| 146 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 147 } |
| 148 |
| 149 void DownloadHistory::QueryCallback(InfoVector* infos) { |
| 150 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 151 // ManagerGoingDown() may have happened before the history loaded. |
| 152 if (!notifier_.GetManager()) |
| 153 return; |
| 154 for (InfoVector::const_iterator it = infos->begin(); |
| 155 it != infos->end(); ++it) { |
| 156 // OnDownloadCreated() is called inside DM::CreateDownloadItem(), so set |
| 157 // loading_db_handle_ to match up the created item with its db_handle. All |
| 158 // methods run on the UI thread and CreateDownloadItem() is synchronous. |
| 159 loading_db_handle_ = it->db_handle; |
| 160 content::DownloadItem* download_item = |
| 161 notifier_.GetManager()->CreateDownloadItem( |
| 162 it->path, |
| 163 it->url, |
| 164 it->referrer_url, |
| 165 it->start_time, |
| 166 it->end_time, |
| 167 it->received_bytes, |
| 168 it->total_bytes, |
| 169 it->state, |
| 170 it->opened); |
| 171 DownloadHistoryData* dhd = DownloadHistoryData::Get(download_item); |
| 172 |
| 173 // If this DCHECK fails, then you probably added an Observer that |
| 174 // synchronously creates a DownloadItem in response to |
| 175 // DownloadManager::OnDownloadCreated(), and your observer runs before |
| 176 // DownloadHistory, and DownloadManager creates items synchronously. Just |
| 177 // bounce your DownloadItem creation off the message loop to flush |
| 178 // DownloadHistory::OnDownloadCreated. |
| 179 DCHECK_EQ(it->db_handle, dhd->db_handle()); |
| 180 ++history_size_; |
| 181 } |
| 182 notifier_.GetManager()->CheckForHistoryFilesRemoval(); |
| 183 } |
| 184 |
| 185 void DownloadHistory::OnDownloadCreated( |
| 186 content::DownloadManager* manager, content::DownloadItem* item) { |
| 187 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 188 |
| 189 // All downloads should pass through OnDownloadCreated exactly once. |
| 190 CHECK(!DownloadHistoryData::Get(item)); |
| 191 DownloadHistoryData* dhd = new DownloadHistoryData(item, loading_db_handle_); |
| 192 loading_db_handle_ = history::DownloadDatabase::kUninitializedHandle; |
| 193 if (item->GetState() == content::DownloadItem::IN_PROGRESS) { |
| 194 dhd->set_info(GetPersistentStoreInfo(item)); |
| 195 } |
| 196 MaybeAddToHistory(item); |
| 197 } |
| 198 |
| 199 void DownloadHistory::MaybeAddToHistory(content::DownloadItem* item) { |
| 200 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 201 int32 download_id = item->GetId(); |
| 202 DownloadHistoryData* dhd = DownloadHistoryData::Get(item); |
| 203 bool removing = (removing_handles_.find(dhd->db_handle()) != |
| 204 removing_handles_.end()); |
| 205 // TODO(benjhayden): Remove IsTemporary(). |
| 206 if (download_crx_util::IsExtensionDownload(*item) || |
| 207 dhd->is_persisted() || |
| 208 item->IsTemporary() || |
| 209 removing || |
| 210 dhd->is_adding()) |
| 211 return; |
| 212 dhd->set_is_adding(true); |
| 213 if (dhd->info() == NULL) { |
| 214 // Keep the info here regardless of whether the item is in progress so |
| 215 // that, when ItemAdded() calls OnDownloadUpdated(), it can choose more |
| 216 // intelligently whether to Update the db and/or discard the info. |
| 217 dhd->set_info(GetPersistentStoreInfo(item)); |
| 218 } |
| 219 history_->CreateDownload(*dhd->info(), &history_consumer_, base::Bind( |
| 220 &DownloadHistory::ItemAdded, weak_ptr_factory_.GetWeakPtr(), |
| 221 download_id)); |
| 222 } |
| 223 |
| 224 void DownloadHistory::ItemAdded(int32 download_id, int64 db_handle) { |
| 225 if (removed_while_adding_.find(download_id) != |
| 226 removed_while_adding_.end()) { |
| 227 removed_while_adding_.erase(download_id); |
| 228 if (removing_handles_.empty()) { |
| 229 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 230 base::Bind(&DownloadHistory::RemoveDownloadsBatch, |
| 231 weak_ptr_factory_.GetWeakPtr())); |
| 232 } |
| 233 removing_handles_.insert(db_handle); |
| 234 return; |
| 235 } |
| 236 |
| 237 if (!notifier_.GetManager()) |
| 238 return; |
| 239 |
| 240 content::DownloadItem* item = notifier_.GetManager()->GetDownload( |
| 241 download_id); |
| 242 if (!item) { |
| 243 // This item will have called OnDownloadDestroyed(). If the item should |
| 244 // have been removed from history, then it would have also called |
| 245 // OnDownloadRemoved(), which would have put |download_id| in |
| 246 // removed_while_adding_, handled above. |
| 247 return; |
| 248 } |
| 249 |
| 250 UMA_HISTOGRAM_CUSTOM_COUNTS("Download.HistorySize2", |
| 251 history_size_, |
| 252 0/*min*/, |
| 253 (1 << 23)/*max*/, |
| 254 (1 << 7)/*num_buckets*/); |
| 255 ++history_size_; |
| 256 |
| 257 DownloadHistoryData* dhd = DownloadHistoryData::Get(item); |
| 258 dhd->set_is_adding(false); |
| 259 DCHECK_NE(db_handle, history::DownloadDatabase::kUninitializedHandle); |
| 260 dhd->set_db_handle(db_handle); |
| 261 |
| 262 // Send to observers the actual DownloadPersistentStoreInfo that was sent to |
| 263 // the db, plus the db_handle, instead of completely regenerating the |
| 264 // DownloadPersistentStoreInfo, in order to accurately reflect the contents of |
| 265 // the database. |
| 266 dhd->info()->db_handle = db_handle; |
| 267 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadStored(item, *dhd->info())); |
| 268 |
| 269 // In case the item changed or became temporary while it was being added. |
| 270 // Don't just update all of the item's observers because we're the only |
| 271 // observer that can also see db_handle, which is the only thing that |
| 272 // ItemAdded changed. |
| 273 OnDownloadUpdated(notifier_.GetManager(), item); |
| 274 } |
| 275 |
| 276 void DownloadHistory::OnDownloadUpdated( |
| 277 content::DownloadManager* manager, content::DownloadItem* item) { |
| 278 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 279 |
| 280 DownloadHistoryData* dhd = DownloadHistoryData::Get(item); |
| 281 if (!dhd->is_persisted()) { |
| 282 MaybeAddToHistory(item); |
| 283 return; |
| 284 } |
| 285 if (item->IsTemporary()) { |
| 286 OnDownloadRemoved(notifier_.GetManager(), item); |
| 287 return; |
| 288 } |
| 289 |
| 290 // TODO(asanka): Persist GetTargetFilePath() as well. |
| 291 DownloadPersistentStoreInfo current_info(GetPersistentStoreInfo(item)); |
| 292 DownloadPersistentStoreInfo* previous_info = dhd->info(); |
| 293 bool do_update = ( |
| 294 (previous_info == NULL) || |
| 295 (previous_info->path != current_info.path) || |
| 296 (previous_info->end_time != current_info.end_time) || |
| 297 (previous_info->received_bytes != current_info.received_bytes) || |
| 298 (previous_info->total_bytes != current_info.total_bytes) || |
| 299 (previous_info->state != current_info.state) || |
| 300 (previous_info->opened != current_info.opened)); |
| 301 UMA_HISTOGRAM_ENUMERATION("Download.HistoryPropagatedUpdate", do_update, 2); |
| 302 if (do_update) { |
| 303 history_->UpdateDownload(current_info); |
| 304 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadStored( |
| 305 item, current_info)); |
| 306 } |
| 307 if (item->GetState() == content::DownloadItem::IN_PROGRESS) { |
| 308 dhd->set_info(current_info); |
| 309 } else { |
| 310 dhd->clear_info(); |
| 311 } |
| 312 } |
| 313 |
| 314 void DownloadHistory::OnDownloadOpened( |
| 315 content::DownloadManager* manager, content::DownloadItem* item) { |
| 316 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 317 DownloadHistoryData* dhd = DownloadHistoryData::Get(item); |
| 318 if (!dhd->is_persisted()) { |
| 319 MaybeAddToHistory(item); |
| 320 return; |
| 321 } |
| 322 if (item->IsTemporary()) { |
| 323 OnDownloadRemoved(manager, item); |
| 324 return; |
| 325 } |
| 326 // Downloads may be opened after they are completed, so don't rely on |
| 327 // DownloadHistoryData::info(). |
| 328 |
| 329 DownloadPersistentStoreInfo current_info(GetPersistentStoreInfo(item)); |
| 330 history_->UpdateDownload(current_info); |
| 331 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadStored(item, current_info)); |
| 332 } |
| 333 |
| 334 void DownloadHistory::OnDownloadRemoved( |
| 335 content::DownloadManager* manager, content::DownloadItem* item) { |
| 336 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 337 |
| 338 DownloadHistoryData* dhd = DownloadHistoryData::Get(item); |
| 339 if (!dhd->is_persisted()) { |
| 340 if (dhd->is_adding()) { |
| 341 removed_while_adding_.insert(item->GetId()); |
| 342 } |
| 343 return; |
| 344 } |
| 345 |
| 346 // For database efficiency, batch removals together if they happen all at |
| 347 // once. |
| 348 if (removing_handles_.empty()) { |
| 349 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 350 base::Bind(&DownloadHistory::RemoveDownloadsBatch, |
| 351 weak_ptr_factory_.GetWeakPtr())); |
| 352 } |
| 353 removing_handles_.insert(dhd->db_handle()); |
| 354 removing_ids_.insert(item->GetId()); |
| 355 dhd->set_db_handle(history::DownloadDatabase::kUninitializedHandle); |
| 356 --history_size_; |
| 357 } |
| 358 |
| 359 void DownloadHistory::RemoveDownloadsBatch() { |
| 360 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 361 HandleSet remove_handles; |
| 362 IdSet remove_ids; |
| 363 removing_handles_.swap(remove_handles); |
| 364 removing_ids_.swap(remove_ids); |
| 365 history_->RemoveDownloads(remove_handles); |
| 366 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadsRemoved(remove_ids)); |
47 } | 367 } |
48 | 368 |
49 void DownloadHistory::CheckVisitedReferrerBefore( | 369 void DownloadHistory::CheckVisitedReferrerBefore( |
50 int32 download_id, | |
51 const GURL& referrer_url, | 370 const GURL& referrer_url, |
52 const VisitedBeforeDoneCallback& callback) { | 371 const VisitedBeforeDoneCallback& callback) { |
53 if (referrer_url.is_valid()) { | 372 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
54 HistoryService* hs = HistoryServiceFactory::GetForProfileIfExists( | 373 if (!referrer_url.is_valid()) { |
55 profile_, Profile::EXPLICIT_ACCESS); | 374 callback.Run(false); |
56 if (hs) { | 375 return; |
57 HistoryService::Handle handle = | 376 } |
58 hs->GetVisibleVisitCountToHost(referrer_url, &history_consumer_, | 377 history_->GetVisibleVisitCountToHost( |
59 base::Bind(&DownloadHistory::OnGotVisitCountToHost, | 378 referrer_url, &history_consumer_, base::Bind( |
60 base::Unretained(this))); | 379 &DownloadHistory::OnGotVisitCountToHost, |
61 visited_before_requests_[handle] = callback; | 380 weak_ptr_factory_.GetWeakPtr(), callback)); |
62 return; | 381 } |
63 } | 382 |
64 } | 383 void DownloadHistory::OnGotVisitCountToHost( |
65 callback.Run(false); | 384 const VisitedBeforeDoneCallback& callback, |
66 } | 385 HistoryService::Handle unused_handle, |
67 | 386 bool found_visits, |
68 void DownloadHistory::AddEntry( | 387 int count, |
69 DownloadItem* download_item, | 388 base::Time first_visit) { |
70 const HistoryService::DownloadCreateCallback& callback) { | 389 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
71 DCHECK(download_item); | |
72 // Do not store the download in the history database for a few special cases: | |
73 // - incognito mode (that is the point of this mode) | |
74 // - extensions (users don't think of extension installation as 'downloading') | |
75 // - temporary download, like in drag-and-drop | |
76 // - history service is not available (e.g. in tests) | |
77 // We have to make sure that these handles don't collide with normal db | |
78 // handles, so we use a negative value. Eventually, they could overlap, but | |
79 // you'd have to do enough downloading that your ISP would likely stab you in | |
80 // the neck first. YMMV. | |
81 HistoryService* hs = HistoryServiceFactory::GetForProfileIfExists( | |
82 profile_, Profile::EXPLICIT_ACCESS); | |
83 if (download_crx_util::IsExtensionDownload(*download_item) || | |
84 download_item->IsTemporary() || !hs) { | |
85 callback.Run(download_item->GetId(), GetNextFakeDbHandle()); | |
86 return; | |
87 } | |
88 | |
89 int32 id = download_item->GetId(); | |
90 DownloadPersistentStoreInfo history_info = | |
91 download_item->GetPersistentStoreInfo(); | |
92 hs->CreateDownload(id, history_info, &history_consumer_, callback); | |
93 } | |
94 | |
95 void DownloadHistory::UpdateEntry(DownloadItem* download_item) { | |
96 // Don't store info in the database if the download was initiated while in | |
97 // incognito mode or if it hasn't been initialized in our database table. | |
98 if (download_item->GetDbHandle() <= DownloadItem::kUninitializedHandle) | |
99 return; | |
100 | |
101 HistoryService* hs = HistoryServiceFactory::GetForProfileIfExists( | |
102 profile_, Profile::EXPLICIT_ACCESS); | |
103 if (!hs) | |
104 return; | |
105 hs->UpdateDownload(download_item->GetPersistentStoreInfo()); | |
106 } | |
107 | |
108 void DownloadHistory::UpdateDownloadPath(DownloadItem* download_item, | |
109 const FilePath& new_path) { | |
110 // No update necessary if the download was initiated while in incognito mode. | |
111 if (download_item->GetDbHandle() <= DownloadItem::kUninitializedHandle) | |
112 return; | |
113 | |
114 HistoryService* hs = HistoryServiceFactory::GetForProfileIfExists( | |
115 profile_, Profile::EXPLICIT_ACCESS); | |
116 if (hs) | |
117 hs->UpdateDownloadPath(new_path, download_item->GetDbHandle()); | |
118 } | |
119 | |
120 void DownloadHistory::RemoveEntry(DownloadItem* download_item) { | |
121 // No update necessary if the download was initiated while in incognito mode. | |
122 if (download_item->GetDbHandle() <= DownloadItem::kUninitializedHandle) | |
123 return; | |
124 | |
125 HistoryService* hs = HistoryServiceFactory::GetForProfileIfExists( | |
126 profile_, Profile::EXPLICIT_ACCESS); | |
127 if (hs) | |
128 hs->RemoveDownload(download_item->GetDbHandle()); | |
129 } | |
130 | |
131 void DownloadHistory::RemoveEntriesBetween(const base::Time remove_begin, | |
132 const base::Time remove_end) { | |
133 HistoryService* hs = HistoryServiceFactory::GetForProfileIfExists( | |
134 profile_, Profile::EXPLICIT_ACCESS); | |
135 if (hs) | |
136 hs->RemoveDownloadsBetween(remove_begin, remove_end); | |
137 } | |
138 | |
139 int64 DownloadHistory::GetNextFakeDbHandle() { | |
140 return next_fake_db_handle_--; | |
141 } | |
142 | |
143 void DownloadHistory::OnGotVisitCountToHost(HistoryService::Handle handle, | |
144 bool found_visits, | |
145 int count, | |
146 base::Time first_visit) { | |
147 VisitedBeforeRequestsMap::iterator request = | |
148 visited_before_requests_.find(handle); | |
149 DCHECK(request != visited_before_requests_.end()); | |
150 VisitedBeforeDoneCallback callback = request->second; | |
151 visited_before_requests_.erase(request); | |
152 callback.Run(found_visits && count && | 390 callback.Run(found_visits && count && |
153 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); | 391 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); |
154 } | 392 } |
| 393 |
| 394 void DownloadHistory::AddObserver(DownloadHistory::Observer* observer) { |
| 395 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 396 observers_.AddObserver(observer); |
| 397 } |
| 398 |
| 399 void DownloadHistory::RemoveObserver(DownloadHistory::Observer* observer) { |
| 400 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 401 observers_.RemoveObserver(observer); |
| 402 } |
OLD | NEW |