summaryrefslogtreecommitdiffstats
path: root/chrome/browser/download/base_file.h
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-27 16:27:15 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-27 16:27:15 +0000
commitf15edeb24bd34ef09d6c62b734eca5765f5c2854 (patch)
tree24d4ef0375b0aab1486fb52d0ae9fa41766b262d /chrome/browser/download/base_file.h
parenta0eca45b109d89137269cb9b5004e8a51309a6b2 (diff)
downloadchromium_src-f15edeb24bd34ef09d6c62b734eca5765f5c2854.zip
chromium_src-f15edeb24bd34ef09d6c62b734eca5765f5c2854.tar.gz
chromium_src-f15edeb24bd34ef09d6c62b734eca5765f5c2854.tar.bz2
Download code cleanup: share most of the code between DownloadFile and SaveFile.
I'm not really happy about BaseFile, but removing the remaining differences will require changes to DownloadFileManager and SaveFileManager. I prefer to do that in small steps. TEST=unit_tests, browser_tests, ui_tests BUG=48913 Review URL: http://codereview.chromium.org/3164039 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@57688 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/download/base_file.h')
-rw-r--r--chrome/browser/download/base_file.h82
1 files changed, 82 insertions, 0 deletions
diff --git a/chrome/browser/download/base_file.h b/chrome/browser/download/base_file.h
new file mode 100644
index 0000000..fa3873f
--- /dev/null
+++ b/chrome/browser/download/base_file.h
@@ -0,0 +1,82 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_
+#define CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_
+#pragma once
+
+#include "base/file_path.h"
+#include "base/linked_ptr.h"
+#include "chrome/browser/power_save_blocker.h"
+#include "googleurl/src/gurl.h"
+
+namespace net {
+class FileStream;
+}
+
+// File being downloaded and saved to disk. This is a base class
+// for DownloadFile and SaveFile, which keep more state information.
+class BaseFile {
+ public:
+ BaseFile(const FilePath& full_path,
+ const GURL& source_url,
+ const GURL& referrer_url,
+ const linked_ptr<net::FileStream>& file_stream);
+ ~BaseFile();
+
+ bool Initialize();
+
+ // Write a new chunk of data to the file. Returns true on success (all bytes
+ // written to the file).
+ bool AppendDataToFile(const char* data, size_t data_len);
+
+ // Rename the download file. Returns true on success.
+ // |path_renamed_| is set to true only if |is_final_rename| is true.
+ // Marked virtual for testing.
+ virtual bool Rename(const FilePath& full_path, bool is_final_rename);
+
+ // Abort the download and automatically close the file.
+ void Cancel();
+
+ // Indicate that the download has finished. No new data will be received.
+ void Finish();
+
+ // Informs the OS that this file came from the internet.
+ void AnnotateWithSourceInformation();
+
+ FilePath full_path() const { return full_path_; }
+ bool path_renamed() const { return path_renamed_; }
+ bool in_progress() const { return file_stream_ != NULL; }
+ int64 bytes_so_far() const { return bytes_so_far_; }
+
+ protected:
+ bool Open();
+ void Close();
+
+ // Full path to the file including the file name.
+ FilePath full_path_;
+
+ // Whether the download is still using its initial temporary path.
+ bool path_renamed_;
+
+ private:
+ // Source URL for the file being downloaded.
+ GURL source_url_;
+
+ // The URL where the download was initiated.
+ GURL referrer_url_;
+
+ // OS file stream for writing
+ linked_ptr<net::FileStream> file_stream_;
+
+ // Amount of data received up so far, in bytes.
+ int64 bytes_so_far_;
+
+ // RAII handle to keep the system from sleeping while we're downloading.
+ PowerSaveBlocker power_save_blocker_;
+
+ DISALLOW_COPY_AND_ASSIGN(BaseFile);
+};
+
+#endif // CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_