Don't write to download metadata cache while in incognito

In incognito mode, we shouldn't write to the metadata cache.
BrowserContext::GetPath() will return the path of the normal
profile in incognito mode.

BUG=778425

Change-Id: If1cd5d819d0b754010f60f776226c34d92e383f4
Reviewed-on: https://chromium-review.googlesource.com/1033540
Reviewed-by: Xing Liu <xingliu@chromium.org>
Commit-Queue: Min Qin <qinmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#554554}
diff --git a/components/download/downloader/in_progress/in_progress_cache_impl.cc b/components/download/downloader/in_progress/in_progress_cache_impl.cc
index 6aa9ec0..3a2c1bd 100644
--- a/components/download/downloader/in_progress/in_progress_cache_impl.cc
+++ b/components/download/downloader/in_progress/in_progress_cache_impl.cc
@@ -60,6 +60,9 @@
 
 // Helper functions for file read/write operations.
 std::vector<char> ReadEntriesFromFile(base::FilePath file_path) {
+  if (file_path.empty())
+    return std::vector<char>();
+
   // Check validity of file.
   base::File entries_file(file_path,
                           base::File::FLAG_OPEN | base::File::FLAG_READ);
@@ -113,7 +116,8 @@
 }
 
 void WriteEntriesToFile(const std::string& entries, base::FilePath file_path) {
-  DCHECK(!file_path.empty());
+  if (file_path.empty())
+    return;
 
   if (!base::ImportantFileWriter::WriteFileAtomically(file_path, entries)) {
     LOG(ERROR) << "Could not write download entries to file: "
diff --git a/components/download/downloader/in_progress/in_progress_cache_impl.h b/components/download/downloader/in_progress/in_progress_cache_impl.h
index 6dd5fce..e2e8a02 100644
--- a/components/download/downloader/in_progress/in_progress_cache_impl.h
+++ b/components/download/downloader/in_progress/in_progress_cache_impl.h
@@ -24,7 +24,7 @@
 // right away.
 class InProgressCacheImpl : public InProgressCache {
  public:
-  explicit InProgressCacheImpl(
+  InProgressCacheImpl(
       const base::FilePath& cache_file_path,
       const scoped_refptr<base::SequencedTaskRunner>& task_runner);
   ~InProgressCacheImpl() override;
diff --git a/components/download/internal/common/in_progress_download_manager.cc b/components/download/internal/common/in_progress_download_manager.cc
index c5b3dea..bee85c3 100644
--- a/components/download/internal/common/in_progress_download_manager.cc
+++ b/components/download/internal/common/in_progress_download_manager.cc
@@ -239,11 +239,9 @@
 void InProgressDownloadManager::Initialize(
     const base::FilePath& metadata_cache_dir,
     const base::RepeatingClosure& callback) {
-  DCHECK(!metadata_cache_dir.empty());
-  base::FilePath metadata_cache_file =
-      metadata_cache_dir.Append(kDownloadMetadataStoreFilename);
   download_metadata_cache_ = std::make_unique<InProgressCacheImpl>(
-      metadata_cache_file,
+      metadata_cache_dir.empty() ? base::FilePath() :
+          metadata_cache_dir.Append(kDownloadMetadataStoreFilename),
       base::CreateSequencedTaskRunnerWithTraits(
           {base::MayBlock(), base::TaskPriority::BACKGROUND,
            base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}));
diff --git a/content/browser/download/download_manager_impl.cc b/content/browser/download/download_manager_impl.cc
index 1ed3db2..591f60dc 100644
--- a/content/browser/download/download_manager_impl.cc
+++ b/content/browser/download/download_manager_impl.cc
@@ -291,7 +291,7 @@
   in_progress_manager_ = std::make_unique<download::InProgressDownloadManager>(
       this, base::BindRepeating(&IsOriginSecure));
   in_progress_manager_->Initialize(
-      browser_context_->GetPath(),
+      IsOffTheRecord() ? base::FilePath() : browser_context_->GetPath(),
       base::BindRepeating(
           &DownloadManagerImpl::PostInitialization, weak_factory_.GetWeakPtr(),
           DOWNLOAD_INITIALIZATION_DEPENDENCY_IN_PROGRESS_CACHE));