summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorachuith@chromium.org <achuith@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-10 21:38:07 +0000
committerachuith@chromium.org <achuith@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-10 21:38:07 +0000
commit6a7060644cc90ecf5de6db801af9fcbba578ff57 (patch)
treeeb295e57cc0ff932441a4e0cada98baa604a126f
parent50f2afd87c0daceba354687acf28afa71716297f (diff)
downloadchromium_src-6a7060644cc90ecf5de6db801af9fcbba578ff57.zip
chromium_src-6a7060644cc90ecf5de6db801af9fcbba578ff57.tar.gz
chromium_src-6a7060644cc90ecf5de6db801af9fcbba578ff57.tar.bz2
Consolidate downloads-related methods in GDataDownloadObserver.
Move Set/GetGDataPath from gdata::util to GDataDownloadObserver. Move UploadFileInfo::Init(DownloadItem*) to GDataDownloadObserver. BUG=chromium-os:23516 TEST=compiles. Review URL: https://chromiumcodereview.appspot.com/9657028 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@126051 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/chromeos/gdata/gdata_download_observer.cc63
-rw-r--r--chrome/browser/chromeos/gdata/gdata_download_observer.h15
-rw-r--r--chrome/browser/chromeos/gdata/gdata_upload_file_info.cc22
-rw-r--r--chrome/browser/chromeos/gdata/gdata_upload_file_info.h3
-rw-r--r--chrome/browser/chromeos/gdata/gdata_util.cc32
-rw-r--r--chrome/browser/chromeos/gdata/gdata_util.h8
-rw-r--r--chrome/browser/download/download_file_picker_chromeos.cc3
7 files changed, 75 insertions, 71 deletions
diff --git a/chrome/browser/chromeos/gdata/gdata_download_observer.cc b/chrome/browser/chromeos/gdata/gdata_download_observer.cc
index 28c98ec..5ca8836 100644
--- a/chrome/browser/chromeos/gdata/gdata_download_observer.cc
+++ b/chrome/browser/chromeos/gdata/gdata_download_observer.cc
@@ -7,20 +7,21 @@
#include "chrome/browser/chromeos/gdata/gdata_uploader.h"
#include "chrome/browser/chromeos/gdata/gdata_upload_file_info.h"
#include "chrome/browser/chromeos/gdata/gdata_util.h"
+#include "net/base/net_util.h"
using content::BrowserThread;
using content::DownloadManager;
using content::DownloadItem;
namespace gdata {
-
namespace {
// Threshold file size after which we stream the file.
const int64 kStreamingFileSize = 1 << 20; // 1MB
-// Key for DownloadItem::ExternalData.
+// Keys for DownloadItem::ExternalData.
const char kUploadingKey[] = "Uploading";
+const char kGDataPathKey[] = "GDataPath";
// External Data stored in DownloadItem for ongoing uploads.
class UploadingExternalData : public DownloadItem::ExternalData {
@@ -34,6 +35,18 @@ class UploadingExternalData : public DownloadItem::ExternalData {
GURL file_url_;
};
+// External Data stored in DownloadItem for gdata path.
+class GDataExternalData : public DownloadItem::ExternalData {
+ public:
+ explicit GDataExternalData(const FilePath& path) : file_path_(path) {}
+ virtual ~GDataExternalData() {}
+
+ const FilePath& file_path() const { return file_path_; }
+
+ private:
+ FilePath file_path_;
+};
+
// Extracts UploadingExternalData* from |download|.
UploadingExternalData* GetUploadingExternalData(DownloadItem* download) {
return static_cast<UploadingExternalData*>(download
@@ -65,6 +78,22 @@ void GDataDownloadObserver::Initialize(GDataUploader* gdata_uploader,
download_manager_->AddObserver(this);
}
+void GDataDownloadObserver::SetGDataPath(DownloadItem* download,
+ const FilePath& path) {
+ if (download)
+ download->SetExternalData(&kGDataPathKey,
+ new GDataExternalData(path));
+}
+
+FilePath GDataDownloadObserver::GetGDataPath(DownloadItem* download) {
+ GDataExternalData* data = static_cast<GDataExternalData*>(
+ download->GetExternalData(&kGDataPathKey));
+ // If data is NULL, we've somehow lost the gdata path selected
+ // by the file picker.
+ DCHECK(data);
+ return data ? util::ExtractGDataPath(data->file_path()) : FilePath();
+}
+
void GDataDownloadObserver::ManagerGoingDown(
DownloadManager* download_manager) {
download_manager->RemoveObserver(this);
@@ -141,8 +170,7 @@ void GDataDownloadObserver::UploadDownloadItem(DownloadItem* download) {
if (!ShouldUpload(download))
return;
- UploadFileInfo* upload_file_info = new UploadFileInfo();
- upload_file_info->Init(download);
+ UploadFileInfo* upload_file_info = CreateUploadFileInfo(download);
// Set the UploadingKey in |download|.
download->SetExternalData(&kUploadingKey,
new UploadingExternalData(upload_file_info->file_url));
@@ -168,10 +196,35 @@ bool GDataDownloadObserver::ShouldUpload(DownloadItem* download) {
// Upload if the item is in pending_downloads_,
// is complete or large enough to stream, and,
// is not already being uploaded.
- return pending_downloads_.find(download) != pending_downloads_.end() &&
+ return pending_downloads_.count(download) != 0 &&
(download->IsComplete() ||
download->GetReceivedBytes() > kStreamingFileSize) &&
GetUploadingExternalData(download) == NULL;
}
+UploadFileInfo* GDataDownloadObserver::CreateUploadFileInfo(
+ DownloadItem* download) {
+ UploadFileInfo* upload_file_info = new UploadFileInfo();
+
+ // GetFullPath will be a temporary location if we're streaming.
+ upload_file_info->file_path = download->GetFullPath();
+ upload_file_info->file_url = net::FilePathToFileURL(
+ upload_file_info->file_path);
+ upload_file_info->file_size = download->GetReceivedBytes();
+
+ // Extract the final path from DownloadItem.
+ upload_file_info->gdata_path = GetGDataPath(download);
+
+ // Use the file name as the title.
+ upload_file_info->title = upload_file_info->gdata_path.BaseName().value();
+ upload_file_info->content_type = download->GetMimeType();
+ // GData api handles -1 as unknown file length.
+ upload_file_info->content_length = download->IsInProgress() ?
+ -1 : download->GetReceivedBytes();
+
+ upload_file_info->download_complete = !download->IsInProgress();
+
+ return upload_file_info;
+}
+
} // namespace gdata
diff --git a/chrome/browser/chromeos/gdata/gdata_download_observer.h b/chrome/browser/chromeos/gdata/gdata_download_observer.h
index c6f61167..077ed44 100644
--- a/chrome/browser/chromeos/gdata/gdata_download_observer.h
+++ b/chrome/browser/chromeos/gdata/gdata_download_observer.h
@@ -14,6 +14,7 @@
namespace gdata {
class GDataUploader;
+struct UploadFileInfo;
// Observes downloads to temporary local gdata folder. Schedules these
// downloads for upload to gdata service.
@@ -27,7 +28,18 @@ class GDataDownloadObserver : public content::DownloadManager::Observer,
void Initialize(GDataUploader* gdata_uploader,
content::DownloadManager* download_manager);
+ // Sets gdata path, for example, '/special/gdata/MyFolder/MyFile',
+ // to external data in |download|.
+ static void SetGDataPath(content::DownloadItem* download,
+ const FilePath& gdata_path);
+
private:
+ // Gets the gdata_path from external data in |download|.
+ // GetGDataPath may return an empty path in case SetGDataPath was not
+ // previously called or there was some other internal error
+ // (there is a DCHECK for this).
+ static FilePath GetGDataPath(content::DownloadItem* download);
+
// DownloadManager overrides.
virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE;
virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE;
@@ -50,6 +62,9 @@ class GDataDownloadObserver : public content::DownloadManager::Observer,
// Checks if this DownloadItem should be uploaded.
bool ShouldUpload(content::DownloadItem* download);
+ // Creates UploadFileInfo and initializes it using DownloadItem*.
+ UploadFileInfo* CreateUploadFileInfo(content::DownloadItem* download);
+
// Private data.
// Use GDataUploader to trigger file uploads.
GDataUploader* gdata_uploader_;
diff --git a/chrome/browser/chromeos/gdata/gdata_upload_file_info.cc b/chrome/browser/chromeos/gdata/gdata_upload_file_info.cc
index 009e526..7534b99 100644
--- a/chrome/browser/chromeos/gdata/gdata_upload_file_info.cc
+++ b/chrome/browser/chromeos/gdata/gdata_upload_file_info.cc
@@ -5,9 +5,6 @@
#include "chrome/browser/chromeos/gdata/gdata_upload_file_info.h"
#include "base/string_number_conversions.h"
-#include "chrome/browser/chromeos/gdata/gdata_util.h"
-#include "content/public/browser/download_item.h"
-#include "net/base/net_util.h"
namespace gdata {
@@ -21,25 +18,6 @@ UploadFileInfo::UploadFileInfo()
download_complete(false) {
}
-void UploadFileInfo::Init(content::DownloadItem* download) {
- // GetFullPath will be a temporary location if we're streaming.
- file_path = download->GetFullPath();
- file_url = net::FilePathToFileURL(file_path);
- file_size = download->GetReceivedBytes();
-
- // Extract the final path from DownloadItem.
- gdata_path = util::GetGDataPath(download);
-
- // Use the file name as the title.
- title = gdata_path.BaseName().value();
- content_type = download->GetMimeType();
- // GData api handles -1 as unknown file length.
- content_length = download->IsInProgress() ?
- -1 : download->GetReceivedBytes();
-
- download_complete = !download->IsInProgress();
-}
-
UploadFileInfo::~UploadFileInfo() {
}
diff --git a/chrome/browser/chromeos/gdata/gdata_upload_file_info.h b/chrome/browser/chromeos/gdata/gdata_upload_file_info.h
index b35b95e..6c36a5e 100644
--- a/chrome/browser/chromeos/gdata/gdata_upload_file_info.h
+++ b/chrome/browser/chromeos/gdata/gdata_upload_file_info.h
@@ -29,9 +29,6 @@ struct UploadFileInfo {
UploadFileInfo();
~UploadFileInfo();
- // Initialize from a DownloadItem.
- void Init(content::DownloadItem* download);
-
// Useful for printf debugging.
std::string DebugString() const;
diff --git a/chrome/browser/chromeos/gdata/gdata_util.cc b/chrome/browser/chromeos/gdata/gdata_util.cc
index aab340e..39cf251 100644
--- a/chrome/browser/chromeos/gdata/gdata_util.cc
+++ b/chrome/browser/chromeos/gdata/gdata_util.cc
@@ -26,25 +26,10 @@ const char kGDataMountPointPath[] = "/special/gdata";
const FilePath::CharType kGDataDownloadPath[] = ".gdata";
-// Key for DownloadItem::ExternalData.
-const char kGDataPathKey[] = "GDataPath";
-
const FilePath::CharType* kGDataMountPointPathComponents[] = {
"/", "special", "gdata"
};
-// External Data stored in DownloadItem for gdata path.
-class GDataExternalData : public DownloadItem::ExternalData {
- public:
- explicit GDataExternalData(const FilePath& path) : file_path_(path) {}
- virtual ~GDataExternalData() {}
-
- const FilePath& file_path() const { return file_path_; }
-
- private:
- FilePath file_path_;
-};
-
} // namespace
const FilePath& GetGDataMountPointPath() {
@@ -85,22 +70,5 @@ FilePath GetGDataTempDownloadFolderPath() {
kGDataDownloadPath);
}
-// Store |path| in DownloadItem external data with key GDataPath.
-void SetGDataPath(DownloadItem* download, const FilePath& path) {
- if (download)
- download->SetExternalData(&kGDataPathKey,
- new GDataExternalData(path));
-}
-
-// Return path stored in DownloadItem external data with key GDataPath.
-FilePath GetGDataPath(DownloadItem* download) {
- GDataExternalData* data = static_cast<GDataExternalData*>(
- download->GetExternalData(&kGDataPathKey));
- // If data is NULL, we've somehow lost the gdata path selected
- // by the file picker.
- DCHECK(data);
- return data ? ExtractGDataPath(data->file_path()) : FilePath();
-}
-
} // namespace util
} // namespace gdata
diff --git a/chrome/browser/chromeos/gdata/gdata_util.h b/chrome/browser/chromeos/gdata/gdata_util.h
index 3a68c5f..410d6f4 100644
--- a/chrome/browser/chromeos/gdata/gdata_util.h
+++ b/chrome/browser/chromeos/gdata/gdata_util.h
@@ -36,14 +36,6 @@ FilePath ExtractGDataPath(const FilePath& path);
// located at ~/Downloads/.gdata
FilePath GetGDataTempDownloadFolderPath();
-// TODO(achuith): Move this to GDataDownloadObserver.
-// Sets/Gets GData path, for example, '/special/gdata/MyFolder/MyFile',
-// from external data in |download|. GetGDataPath may return an empty
-// path in case SetGDataPath was not previously called or there was some
-// other internal error (there is a DCHECK for this).
-void SetGDataPath(content::DownloadItem* download, const FilePath& path);
-FilePath GetGDataPath(content::DownloadItem* download);
-
} // namespace util
} // namespace gdata
diff --git a/chrome/browser/download/download_file_picker_chromeos.cc b/chrome/browser/download/download_file_picker_chromeos.cc
index f0188f1..77fc37a 100644
--- a/chrome/browser/download/download_file_picker_chromeos.cc
+++ b/chrome/browser/download/download_file_picker_chromeos.cc
@@ -10,6 +10,7 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_item.h"
#include "content/public/browser/download_manager.h"
+#include "chrome/browser/chromeos/gdata/gdata_download_observer.h"
#include "chrome/browser/chromeos/gdata/gdata_util.h"
using content::BrowserThread;
@@ -65,7 +66,7 @@ void DownloadFilePickerChromeOS::FileSelected(const FilePath& path,
content::DownloadItem* download =
download_manager_->GetActiveDownloadItem(download_id_);
if (download) {
- gdata::util::SetGDataPath(download, path);
+ gdata::GDataDownloadObserver::SetGDataPath(download, path);
download->SetDisplayName(path.BaseName());
// Swap the gdata path with a local path. Local path must be created