Don't accidentally add downloads with db_handle = kUninitializedHandle, which can happen if the sqlite history db is corrupt / offline.

BUG=25492
TEST=none
Review URL: http://codereview.chromium.org/492017

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@36736 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/download/download_manager.cc b/chrome/browser/download/download_manager.cc
index 6c798ba2..c0b9e82a 100644
--- a/chrome/browser/download/download_manager.cc
+++ b/chrome/browser/download/download_manager.cc
@@ -16,6 +16,7 @@
 #include "base/task.h"
 #include "base/thread.h"
 #include "base/timer.h"
+#include "build/build_config.h"
 #include "chrome/browser/browser_list.h"
 #include "chrome/browser/browser_process.h"
 #include "chrome/browser/chrome_thread.h"
@@ -29,8 +30,8 @@
 #include "chrome/browser/renderer_host/render_process_host.h"
 #include "chrome/browser/renderer_host/render_view_host.h"
 #include "chrome/browser/renderer_host/resource_dispatcher_host.h"
-#include "chrome/browser/tab_contents/tab_util.h"
 #include "chrome/browser/tab_contents/tab_contents.h"
+#include "chrome/browser/tab_contents/tab_util.h"
 #include "chrome/common/chrome_constants.h"
 #include "chrome/common/chrome_paths.h"
 #include "chrome/common/extensions/extension.h"
@@ -53,10 +54,6 @@
 #include "base/win_util.h"
 #endif
 
-#if defined(OS_LINUX)
-#include <gtk/gtk.h>
-#endif
-
 // Periodically update our observers.
 class DownloadItemUpdateTask : public Task {
  public:
@@ -361,7 +358,8 @@
 DownloadManager::DownloadManager()
     : shutdown_needed_(false),
       profile_(NULL),
-      file_manager_(NULL) {
+      file_manager_(NULL),
+      fake_db_handle_(kUninitializedHandle - 1) {
 }
 
 DownloadManager::~DownloadManager() {
@@ -767,8 +765,7 @@
   // the neck first. YMMV.
   if (profile_->IsOffTheRecord() || download->is_extension_install() ||
       download->is_temporary()) {
-    static int64 fake_db_handle = kUninitializedHandle - 1;
-    OnCreateDownloadEntryComplete(*info, fake_db_handle--);
+    OnCreateDownloadEntryComplete(*info, fake_db_handle_.GetNext());
   } else {
     // Update the history system with the new download.
     // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong.
@@ -1586,6 +1583,15 @@
   DCHECK(it != in_progress_.end());
 
   DownloadItem* download = it->second;
+
+  // It's not immediately obvious, but HistoryBackend::CreateDownload() can
+  // call this function with an invalid |db_handle|. For instance, this can
+  // happen when the history database is offline. We cannot have multiple
+  // DownloadItems with the same invalid db_handle, so we need to assign a
+  // unique |db_handle| here.
+  if (db_handle == kUninitializedHandle)
+    db_handle = fake_db_handle_.GetNext();
+
   DCHECK(download->db_handle() == kUninitializedHandle);
   download->set_db_handle(db_handle);
 
@@ -1655,4 +1661,3 @@
 void DownloadManager::ClearLastDownloadPath() {
   last_download_path_ = FilePath();
 }
-