Home | History | Annotate | Download | only in drive
      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   // In addition to the DownloadManager passed to Initialize(), observe another
     41   // download manager. This should be called only for the DownloadManager of the
     42   // incognito version of the profile where |file_system_| resides.
     43   void ObserveIncognitoDownloadManager(
     44       content::DownloadManager* download_manager);
     45 
     46   // Callback used to return results from SubstituteDriveDownloadPath.
     47   // TODO(hashimoto): Report error with a FileError. crbug.com/171345
     48   typedef base::Callback<void(const base::FilePath&)>
     49       SubstituteDriveDownloadPathCallback;
     50 
     51   void SubstituteDriveDownloadPath(
     52       const base::FilePath& drive_path,
     53       content::DownloadItem* download,
     54       const SubstituteDriveDownloadPathCallback& callback);
     55 
     56   // Sets drive path, for example, '/special/drive/MyFolder/MyFile',
     57   // to external data in |download|. Also sets display name and
     58   // makes |download| a temporary.
     59   void SetDownloadParams(const base::FilePath& drive_path,
     60                          content::DownloadItem* download);
     61 
     62   // Gets the target drive path from external data in |download|.
     63   base::FilePath GetTargetPath(const content::DownloadItem* download);
     64 
     65   // Gets the downloaded drive cache file path from external data in |download|.
     66   base::FilePath GetCacheFilePath(const content::DownloadItem* download);
     67 
     68   // Checks if there is a Drive upload associated with |download|
     69   bool IsDriveDownload(const content::DownloadItem* download);
     70 
     71   // Checks a file corresponding to the download item exists in Drive.
     72   void CheckForFileExistence(
     73       const content::DownloadItem* download,
     74       const content::CheckForFileExistenceCallback& callback);
     75 
     76  private:
     77   // AllDownloadItemNotifier::Observer overrides:
     78   virtual void OnDownloadCreated(content::DownloadManager* manager,
     79                                  content::DownloadItem* download) OVERRIDE;
     80   virtual void OnDownloadUpdated(content::DownloadManager* manager,
     81                                  content::DownloadItem* download) OVERRIDE;
     82 
     83   // Removes the download.
     84   void RemoveDownload(void* manager_id, int id);
     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::DownloadManager* manager,
     93                           content::DownloadItem* download);
     94 
     95   // Sets |cache_file_path| as user data of the download item specified by |id|.
     96   void SetCacheFilePath(void* manager_id,
     97                         int id,
     98                         const base::FilePath* cache_file_path,
     99                         FileError error);
    100 
    101   // Gets a download manager, given a |manager_id| casted from the pointer to
    102   // the manager. This is used to validate the manager that may be deleted while
    103   // asynchronous task posting. Returns NULL if the manager is already gone.
    104   content::DownloadManager* GetDownloadManager(void* manager_id);
    105 
    106   FileSystemInterface* file_system_;  // Owned by DriveIntegrationService.
    107   // Observe the DownloadManager for new downloads.
    108   scoped_ptr<AllDownloadItemNotifier> notifier_;
    109   scoped_ptr<AllDownloadItemNotifier> notifier_incognito_;
    110 
    111   // Temporary download location directory.
    112   base::FilePath drive_tmp_download_path_;
    113 
    114   // Note: This should remain the last member so it'll be destroyed and
    115   // invalidate its weak pointers before any other members are destroyed.
    116   base::WeakPtrFactory<DownloadHandler> weak_ptr_factory_;
    117 
    118   DISALLOW_COPY_AND_ASSIGN(DownloadHandler);
    119 };
    120 
    121 }  // namespace drive
    122 
    123 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_DOWNLOAD_HANDLER_H_
    124