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