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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DOWNLOAD_HANDLER_H_ 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DOWNLOAD_HANDLER_H_ 7 8 #include "base/callback_forward.h" 9 #include "base/memory/weak_ptr.h" 10 #include "chrome/browser/chromeos/drive/file_errors.h" 11 #include "chrome/browser/download/all_download_item_notifier.h" 12 #include "content/public/browser/download_manager_delegate.h" 13 14 class Profile; 15 16 namespace content { 17 class DownloadItem; 18 class DownloadManager; 19 } 20 21 namespace drive { 22 23 class FileSystemInterface; 24 class FileWriteHelper; 25 class ResourceEntry; 26 27 // Observes downloads to temporary local drive folder. Schedules these 28 // downloads for upload to drive service. 29 class DownloadHandler : public AllDownloadItemNotifier::Observer { 30 public: 31 DownloadHandler(FileWriteHelper* file_write_helper, 32 FileSystemInterface* file_system); 33 virtual ~DownloadHandler(); 34 35 // Utility method to get DownloadHandler with profile. 36 static DownloadHandler* GetForProfile(Profile* profile); 37 38 // Become an observer of DownloadManager. 39 void Initialize(content::DownloadManager* download_manager, 40 const base::FilePath& drive_tmp_download_path); 41 42 // Callback used to return results from SubstituteDriveDownloadPath. 43 // TODO(hashimoto): Report error with a FileError. crbug.com/171345 44 typedef base::Callback<void(const base::FilePath&)> 45 SubstituteDriveDownloadPathCallback; 46 47 void SubstituteDriveDownloadPath( 48 const base::FilePath& drive_path, 49 content::DownloadItem* download, 50 const SubstituteDriveDownloadPathCallback& callback); 51 52 // Sets drive path, for example, '/special/drive/MyFolder/MyFile', 53 // to external data in |download|. Also sets display name and 54 // makes |download| a temporary. 55 void SetDownloadParams(const base::FilePath& drive_path, 56 content::DownloadItem* download); 57 58 // Gets the target drive path from external data in |download|. 59 base::FilePath GetTargetPath(const content::DownloadItem* download); 60 61 // Checks if there is a Drive upload associated with |download| 62 bool IsDriveDownload(const content::DownloadItem* download); 63 64 // Checks a file corresponding to the download item exists in Drive. 65 void CheckForFileExistence( 66 const content::DownloadItem* download, 67 const content::CheckForFileExistenceCallback& callback); 68 69 private: 70 // AllDownloadItemNotifier::Observer overrides: 71 virtual void OnDownloadCreated(content::DownloadManager* manager, 72 content::DownloadItem* download) OVERRIDE; 73 virtual void OnDownloadUpdated(content::DownloadManager* manager, 74 content::DownloadItem* download) OVERRIDE; 75 76 // Removes the download. 77 void RemoveDownload(int id); 78 79 // Callback for FileSystem::GetResourceEntryByPath(). 80 // Used to implement SubstituteDriveDownloadPath(). 81 void OnEntryFound(const base::FilePath& drive_dir_path, 82 const SubstituteDriveDownloadPathCallback& callback, 83 FileError error, 84 scoped_ptr<ResourceEntry> entry); 85 86 // Callback for FileSystem::CreateDirectory(). 87 // Used to implement SubstituteDriveDownloadPath(). 88 void OnCreateDirectory(const SubstituteDriveDownloadPathCallback& callback, 89 FileError error); 90 91 // Starts the upload of a downloaded/downloading file. 92 void UploadDownloadItem(content::DownloadItem* download); 93 94 FileWriteHelper* file_write_helper_; 95 FileSystemInterface* file_system_; // Owned by DriveIntegrationService. 96 // Observe the DownloadManager for new downloads. 97 scoped_ptr<AllDownloadItemNotifier> notifier_; 98 99 // Temporary download location directory. 100 base::FilePath drive_tmp_download_path_; 101 102 // Note: This should remain the last member so it'll be destroyed and 103 // invalidate its weak pointers before any other members are destroyed. 104 base::WeakPtrFactory<DownloadHandler> weak_ptr_factory_; 105 106 DISALLOW_COPY_AND_ASSIGN(DownloadHandler); 107 }; 108 109 } // namespace drive 110 111 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DOWNLOAD_HANDLER_H_ 112