Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1498)

Unified Diff: chrome/browser/download/download_history.h

Issue 10915180: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/download/download_history.h
diff --git a/chrome/browser/download/download_history.h b/chrome/browser/download/download_history.h
index b1f827b4c0f64bf7eb3bcf224b45800ae46bbc86..4f7854fc87318d63289600d9d49c99e60f7d875d 100644
--- a/chrome/browser/download/download_history.h
+++ b/chrome/browser/download/download_history.h
@@ -6,86 +6,85 @@
#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_HISTORY_H_
#include <map>
+#include <set>
+#include <vector>
-#include "base/basictypes.h"
-#include "base/callback.h"
-#include "chrome/browser/cancelable_request.h"
+#include "base/memory/weak_ptr.h"
+#include "base/memory/scoped_ptr.h"
#include "chrome/browser/history/history.h"
+#include "content/public/browser/download_item.h"
+#include "content/public/browser/download_manager.h"
-class Profile;
+struct DownloadPersistentStoreInfo;
-namespace base {
-class Time;
-}
-
-namespace content {
-class DownloadItem;
-}
-
-// Interacts with the HistoryService on behalf of the download subsystem.
-class DownloadHistory {
+// Observes a single DownloadManager and all its DownloadItems, keeping the
+// DownloadDatabase up to date.
+class DownloadHistory: public content::DownloadManager::Observer,
+ public content::DownloadItem::Observer {
public:
typedef base::Callback<void(bool)> VisitedBeforeDoneCallback;
- explicit DownloadHistory(Profile* profile);
- ~DownloadHistory();
-
- // Retrieves the next_id counter from the sql meta_table.
- // Should be much faster than Load so that we may delay downloads until after
- // this call with minimal performance penalty.
- void GetNextId(const HistoryService::DownloadNextIdCallback& callback);
+ // Neither |manager| nor |history| may be NULL.
+ DownloadHistory(
+ content::DownloadManager* manager,
+ HistoryService* history);
Randy Smith (Not in Mondays) 2012/09/11 18:36:53 Comment that constructor must guarantee that Histo
benjhayden 2012/09/13 15:18:16 Done.
- // Retrieves DownloadCreateInfos saved in the history.
- void Load(const HistoryService::DownloadQueryCallback& callback);
+ virtual ~DownloadHistory();
- // Checks whether |referrer_url| has been visited before today. This takes
- // ownership of |callback|.
+ // Checks whether |referrer_url| has been visited before today.
void CheckVisitedReferrerBefore(int32 download_id,
const GURL& referrer_url,
const VisitedBeforeDoneCallback& callback);
- // Adds a new entry for a download to the history database.
- void AddEntry(content::DownloadItem* download_item,
- const HistoryService::DownloadCreateCallback& callback);
-
- // Updates the history entry for |download_item|.
- void UpdateEntry(content::DownloadItem* download_item);
-
- // Updates the download path for |download_item| to |new_path|.
- void UpdateDownloadPath(content::DownloadItem* download_item,
- const FilePath& new_path);
-
- // Removes |download_item| from the history database.
- void RemoveEntry(content::DownloadItem* download_item);
+ // content::DownloadManager::Observer
+ virtual void OnDownloadCreated(
+ content::DownloadManager* manager,
+ content::DownloadItem* item) OVERRIDE;
+ virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE;
- // Removes download-related history entries in the given time range.
- void RemoveEntriesBetween(const base::Time remove_begin,
- const base::Time remove_end);
-
- // Returns a new unique database handle which will not collide with real ones.
- int64 GetNextFakeDbHandle();
+ // content::DownloadItem::Observer
+ virtual void OnDownloadUpdated(content::DownloadItem* item) OVERRIDE;
+ virtual void OnDownloadOpened(content::DownloadItem* item) OVERRIDE;
+ virtual void OnDownloadRemoved(content::DownloadItem* item) OVERRIDE;
+ virtual void OnDownloadDestroyed(content::DownloadItem* item) OVERRIDE;
private:
- typedef std::map<HistoryService::Handle, VisitedBeforeDoneCallback>
- VisitedBeforeRequestsMap;
-
- void OnGotVisitCountToHost(HistoryService::Handle handle,
- bool found_visits,
- int count,
- base::Time first_visit);
-
- Profile* profile_;
-
- // In case we don't have a valid db_handle, we use |fake_db_handle_| instead.
- // This is useful for incognito mode or when the history database is offline.
- // Downloads are expected to have unique handles, so we decrement the next
- // fake handle value on every use.
- int64 next_fake_db_handle_;
-
- CancelableRequestConsumer history_consumer_;
-
- // The outstanding requests made by CheckVisitedReferrerBefore().
- VisitedBeforeRequestsMap visited_before_requests_;
+ typedef std::set<int64> HandleSet;
+ typedef std::set<int32> IdSet;
+ typedef std::set<content::DownloadItem*> ItemSet;
+
+ void MaybeAddToHistory(content::DownloadItem* item);
+ void ItemAdded(int32 id, int64 db_handle);
+ void RemoveDownloadsBatch();
+ void OnGotVisitCountToHost(
+ const VisitedBeforeDoneCallback& callback,
+ bool found_visits, int count, base::Time first_visit);
+ void QueryCallback(
+ scoped_ptr<std::vector<DownloadPersistentStoreInfo> > infos);
+
+ content::DownloadManager* manager_;
+ ItemSet observing_items_;
+
+ HistoryService* history_;
+
+ // |db_handle| of the item being created in response to QueryCallback(),
+ // matched up with created items in OnDownloadCreated() so that the item is
+ // not re-added to the database. For items not created by QueryCallback(),
+ // this is DownloadDatabase::kUninitializedHandle.
+ int64 loading_db_handle_;
+
+ // |db_handles| of items that are scheduled for removal from history, to
+ // facilitate batching removals together for database efficiency.
+ HandleSet removing_;
+
+ // |GetId()|s of items that were removed while they were being added, so that
+ // they can be removed when their db_handles are received from the database.
+ IdSet removed_while_adding_;
+
+ // Count the number of items in the history for UMA.
+ int64 history_size_;
+
+ base::WeakPtrFactory<DownloadHistory> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(DownloadHistory);
};

Powered by Google App Engine
This is Rietveld 408576698