OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/supports_user_data.h" | |
9 #include "chrome/browser/chromeos/gdata/gdata.pb.h" | 10 #include "chrome/browser/chromeos/gdata/gdata.pb.h" |
10 #include "chrome/browser/chromeos/gdata/gdata_documents_service.h" | 11 #include "chrome/browser/chromeos/gdata/gdata_documents_service.h" |
11 #include "chrome/browser/chromeos/gdata/gdata_file_system_interface.h" | 12 #include "chrome/browser/chromeos/gdata/gdata_file_system_interface.h" |
12 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" | 13 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" |
13 #include "chrome/browser/chromeos/gdata/gdata_upload_file_info.h" | 14 #include "chrome/browser/chromeos/gdata/gdata_upload_file_info.h" |
14 #include "chrome/browser/chromeos/gdata/gdata_uploader.h" | 15 #include "chrome/browser/chromeos/gdata/gdata_uploader.h" |
15 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 16 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
16 #include "chrome/browser/download/download_completion_blocker.h" | 17 #include "chrome/browser/download/download_completion_blocker.h" |
17 #include "chrome/browser/profiles/profile_manager.h" | 18 #include "chrome/browser/profiles/profile_manager.h" |
18 #include "net/base/net_util.h" | 19 #include "net/base/net_util.h" |
19 | 20 |
20 using content::BrowserThread; | 21 using content::BrowserThread; |
21 using content::DownloadManager; | 22 using content::DownloadManager; |
22 using content::DownloadItem; | 23 using content::DownloadItem; |
23 | 24 |
24 namespace gdata { | 25 namespace gdata { |
25 namespace { | 26 namespace { |
26 | 27 |
27 // Threshold file size after which we stream the file. | 28 // Threshold file size after which we stream the file. |
28 const int64 kStreamingFileSize = 1 << 20; // 1MB | 29 const int64 kStreamingFileSize = 1 << 20; // 1MB |
29 | 30 |
30 // Keys for DownloadItem::ExternalData. | 31 // Keys for DownloadItem::ExternalData. |
Avi (use Gerrit)
2012/08/07 19:06:24
comment out of date
benjhayden
2012/08/08 13:21:19
Done.
| |
31 const char kUploadingKey[] = "Uploading"; | 32 const char kUploadingKey[] = "Uploading"; |
32 const char kGDataPathKey[] = "GDataPath"; | 33 const char kGDataPathKey[] = "GDataPath"; |
33 | 34 |
34 // External Data stored in DownloadItem for ongoing uploads. | 35 // External Data stored in DownloadItem for ongoing uploads. |
35 class UploadingExternalData : public DownloadCompletionBlocker { | 36 class UploadingExternalData : public DownloadCompletionBlocker { |
Avi (use Gerrit)
2012/08/07 19:06:24
rename class? Update comment above?
benjhayden
2012/08/08 13:21:19
Done.
| |
36 public: | 37 public: |
37 explicit UploadingExternalData(GDataUploader* uploader) | 38 explicit UploadingExternalData(GDataUploader* uploader) |
38 : uploader_(uploader), | 39 : uploader_(uploader), |
39 upload_id_(-1) { | 40 upload_id_(-1) { |
40 } | 41 } |
41 virtual ~UploadingExternalData() {} | 42 virtual ~UploadingExternalData() {} |
42 | 43 |
43 GDataUploader* uploader() { return uploader_; } | 44 GDataUploader* uploader() { return uploader_; } |
44 void set_upload_id(int upload_id) { upload_id_ = upload_id; } | 45 void set_upload_id(int upload_id) { upload_id_ = upload_id; } |
45 int upload_id() const { return upload_id_; } | 46 int upload_id() const { return upload_id_; } |
46 void set_virtual_dir_path(const FilePath& path) { virtual_dir_path_ = path; } | 47 void set_virtual_dir_path(const FilePath& path) { virtual_dir_path_ = path; } |
47 const FilePath& virtual_dir_path() const { return virtual_dir_path_; } | 48 const FilePath& virtual_dir_path() const { return virtual_dir_path_; } |
48 void set_entry(scoped_ptr<DocumentEntry> entry) { entry_ = entry.Pass(); } | 49 void set_entry(scoped_ptr<DocumentEntry> entry) { entry_ = entry.Pass(); } |
49 scoped_ptr<DocumentEntry> entry_passed() { return entry_.Pass(); } | 50 scoped_ptr<DocumentEntry> entry_passed() { return entry_.Pass(); } |
50 | 51 |
51 private: | 52 private: |
52 GDataUploader* uploader_; | 53 GDataUploader* uploader_; |
53 int upload_id_; | 54 int upload_id_; |
54 FilePath virtual_dir_path_; | 55 FilePath virtual_dir_path_; |
55 scoped_ptr<DocumentEntry> entry_; | 56 scoped_ptr<DocumentEntry> entry_; |
56 | 57 |
57 DISALLOW_COPY_AND_ASSIGN(UploadingExternalData); | 58 DISALLOW_COPY_AND_ASSIGN(UploadingExternalData); |
58 }; | 59 }; |
59 | 60 |
60 // External Data stored in DownloadItem for gdata path. | 61 // External Data stored in DownloadItem for gdata path. |
61 class GDataExternalData : public DownloadItem::ExternalData { | 62 class GDataExternalData : public base::SupportsUserData::Data { |
62 public: | 63 public: |
63 explicit GDataExternalData(const FilePath& path) : file_path_(path) {} | 64 explicit GDataExternalData(const FilePath& path) : file_path_(path) {} |
64 virtual ~GDataExternalData() {} | 65 virtual ~GDataExternalData() {} |
65 | 66 |
66 const FilePath& file_path() const { return file_path_; } | 67 const FilePath& file_path() const { return file_path_; } |
67 | 68 |
68 private: | 69 private: |
69 FilePath file_path_; | 70 FilePath file_path_; |
70 }; | 71 }; |
71 | 72 |
72 // Extracts UploadingExternalData* from |download|. | 73 // Extracts UploadingExternalData* from |download|. |
73 UploadingExternalData* GetUploadingExternalData(DownloadItem* download) { | 74 UploadingExternalData* GetUploadingExternalData(DownloadItem* download) { |
74 return static_cast<UploadingExternalData*>( | 75 return static_cast<UploadingExternalData*>( |
75 download->GetExternalData(&kUploadingKey)); | 76 download->GetUserData(&kUploadingKey)); |
76 } | 77 } |
77 | 78 |
78 // Extracts GDataExternalData* from |download|. | 79 // Extracts GDataExternalData* from |download|. |
79 GDataExternalData* GetGDataExternalData(DownloadItem* download) { | 80 GDataExternalData* GetGDataExternalData(DownloadItem* download) { |
80 return static_cast<GDataExternalData*>( | 81 return static_cast<GDataExternalData*>( |
81 download->GetExternalData(&kGDataPathKey)); | 82 download->GetUserData(&kGDataPathKey)); |
82 } | 83 } |
83 | 84 |
84 void RunSubstituteGDataDownloadCallback( | 85 void RunSubstituteGDataDownloadCallback( |
85 const GDataDownloadObserver::SubstituteGDataDownloadPathCallback& callback, | 86 const GDataDownloadObserver::SubstituteGDataDownloadPathCallback& callback, |
86 const FilePath* file_path) { | 87 const FilePath* file_path) { |
87 callback.Run(*file_path); | 88 callback.Run(*file_path); |
88 } | 89 } |
89 | 90 |
90 gdata::GDataSystemService* GetSystemService(Profile* profile) { | 91 gdata::GDataSystemService* GetSystemService(Profile* profile) { |
91 gdata::GDataSystemService* system_service = | 92 gdata::GDataSystemService* system_service = |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 } | 227 } |
227 } | 228 } |
228 | 229 |
229 // static | 230 // static |
230 void GDataDownloadObserver::SetDownloadParams(const FilePath& gdata_path, | 231 void GDataDownloadObserver::SetDownloadParams(const FilePath& gdata_path, |
231 DownloadItem* download) { | 232 DownloadItem* download) { |
232 if (!download) | 233 if (!download) |
233 return; | 234 return; |
234 | 235 |
235 if (gdata::util::IsUnderGDataMountPoint(gdata_path)) { | 236 if (gdata::util::IsUnderGDataMountPoint(gdata_path)) { |
236 download->SetExternalData(&kGDataPathKey, | 237 download->SetUserData(&kGDataPathKey, |
237 new GDataExternalData(gdata_path)); | 238 new GDataExternalData(gdata_path)); |
238 download->SetDisplayName(gdata_path.BaseName()); | 239 download->SetDisplayName(gdata_path.BaseName()); |
239 download->SetIsTemporary(true); | 240 download->SetIsTemporary(true); |
240 } else if (IsGDataDownload(download)) { | 241 } else if (IsGDataDownload(download)) { |
241 // This may have been previously set if the default download folder is | 242 // This may have been previously set if the default download folder is |
242 // /drive, and the user has now changed the download target to a local | 243 // /drive, and the user has now changed the download target to a local |
243 // folder. | 244 // folder. |
244 download->SetExternalData(&kGDataPathKey, NULL); | 245 download->SetUserData(&kGDataPathKey, NULL); |
245 download->SetDisplayName(gdata_path); | 246 download->SetDisplayName(gdata_path); |
246 // TODO(achuith): This is not quite right. | 247 // TODO(achuith): This is not quite right. |
247 download->SetIsTemporary(false); | 248 download->SetIsTemporary(false); |
248 } | 249 } |
249 } | 250 } |
250 | 251 |
251 // static | 252 // static |
252 FilePath GDataDownloadObserver::GetGDataPath(DownloadItem* download) { | 253 FilePath GDataDownloadObserver::GetGDataPath(DownloadItem* download) { |
253 GDataExternalData* data = GetGDataExternalData(download); | 254 GDataExternalData* data = GetGDataExternalData(download); |
254 // If data is NULL, we've somehow lost the gdata path selected by the file | 255 // If data is NULL, we've somehow lost the gdata path selected by the file |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
393 DCHECK(!download->IsInProgress()); | 394 DCHECK(!download->IsInProgress()); |
394 | 395 |
395 DownloadMap::iterator it = pending_downloads_.find(download->GetId()); | 396 DownloadMap::iterator it = pending_downloads_.find(download->GetId()); |
396 if (it != pending_downloads_.end()) { | 397 if (it != pending_downloads_.end()) { |
397 DetachFromDownload(download); | 398 DetachFromDownload(download); |
398 pending_downloads_.erase(it); | 399 pending_downloads_.erase(it); |
399 } | 400 } |
400 } | 401 } |
401 | 402 |
402 void GDataDownloadObserver::DetachFromDownload(DownloadItem* download) { | 403 void GDataDownloadObserver::DetachFromDownload(DownloadItem* download) { |
403 download->SetExternalData(&kUploadingKey, NULL); | 404 download->SetUserData(&kUploadingKey, NULL); |
404 download->SetExternalData(&kGDataPathKey, NULL); | 405 download->SetUserData(&kGDataPathKey, NULL); |
405 download->RemoveObserver(this); | 406 download->RemoveObserver(this); |
406 } | 407 } |
407 | 408 |
408 void GDataDownloadObserver::UploadDownloadItem(DownloadItem* download) { | 409 void GDataDownloadObserver::UploadDownloadItem(DownloadItem* download) { |
409 // Update metadata of ongoing upload. | 410 // Update metadata of ongoing upload. |
410 UpdateUpload(download); | 411 UpdateUpload(download); |
411 | 412 |
412 if (!ShouldUpload(download)) | 413 if (!ShouldUpload(download)) |
413 return; | 414 return; |
414 | 415 |
415 // Initialize uploading external data. | 416 // Initialize uploading external data. |
416 download->SetExternalData(&kUploadingKey, | 417 download->SetUserData(&kUploadingKey, |
417 new UploadingExternalData(gdata_uploader_)); | 418 new UploadingExternalData(gdata_uploader_)); |
418 | 419 |
419 // Create UploadFileInfo structure for the download item. | 420 // Create UploadFileInfo structure for the download item. |
420 CreateUploadFileInfo(download); | 421 CreateUploadFileInfo(download); |
421 } | 422 } |
422 | 423 |
423 void GDataDownloadObserver::UpdateUpload(DownloadItem* download) { | 424 void GDataDownloadObserver::UpdateUpload(DownloadItem* download) { |
424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 425 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
425 | 426 |
426 UploadingExternalData* upload_data = GetUploadingExternalData(download); | 427 UploadingExternalData* upload_data = GetUploadingExternalData(download); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
570 // use the final target path when the download item is in COMPLETE state. | 571 // use the final target path when the download item is in COMPLETE state. |
571 file_system_->AddUploadedFile(UPLOAD_NEW_FILE, | 572 file_system_->AddUploadedFile(UPLOAD_NEW_FILE, |
572 upload_data->virtual_dir_path(), | 573 upload_data->virtual_dir_path(), |
573 entry.Pass(), | 574 entry.Pass(), |
574 download->GetTargetFilePath(), | 575 download->GetTargetFilePath(), |
575 GDataCache::FILE_OPERATION_MOVE, | 576 GDataCache::FILE_OPERATION_MOVE, |
576 base::Bind(&base::DoNothing)); | 577 base::Bind(&base::DoNothing)); |
577 } | 578 } |
578 | 579 |
579 } // namespace gdata | 580 } // namespace gdata |
OLD | NEW |