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.
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DOWNLOAD_HANDLER_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DOWNLOAD_HANDLER_H_
8 #include "base/callback_forward.h"
9 #include "base/memory/weak_ptr.h"
10 #include "chrome/browser/download/all_download_item_notifier.h"
11 #include "components/drive/file_errors.h"
12 #include "content/public/browser/download_manager_delegate.h"
18 class DownloadManager
;
23 class FileSystemInterface
;
26 // Observes downloads to temporary local drive folder. Schedules these
27 // downloads for upload to drive service.
28 class DownloadHandler
: public AllDownloadItemNotifier::Observer
{
30 explicit DownloadHandler(FileSystemInterface
* file_system
);
31 ~DownloadHandler() override
;
33 // Utility method to get DownloadHandler with profile.
34 static DownloadHandler
* GetForProfile(Profile
* profile
);
36 // Become an observer of DownloadManager.
37 void Initialize(content::DownloadManager
* download_manager
,
38 const base::FilePath
& drive_tmp_download_path
);
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
);
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
;
51 void SubstituteDriveDownloadPath(
52 const base::FilePath
& drive_path
,
53 content::DownloadItem
* download
,
54 const SubstituteDriveDownloadPathCallback
& callback
);
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
);
62 // Gets the target drive path from external data in |download|.
63 base::FilePath
GetTargetPath(const content::DownloadItem
* download
);
65 // Gets the downloaded drive cache file path from external data in |download|.
66 base::FilePath
GetCacheFilePath(const content::DownloadItem
* download
);
68 // Checks if there is a Drive upload associated with |download|
69 bool IsDriveDownload(const content::DownloadItem
* download
);
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
);
77 // AllDownloadItemNotifier::Observer overrides:
78 void OnDownloadCreated(content::DownloadManager
* manager
,
79 content::DownloadItem
* download
) override
;
80 void OnDownloadUpdated(content::DownloadManager
* manager
,
81 content::DownloadItem
* download
) override
;
83 // Removes the download.
84 void RemoveDownload(void* manager_id
, int id
);
86 // Callback for FileSystem::CreateDirectory().
87 // Used to implement SubstituteDriveDownloadPath().
88 void OnCreateDirectory(const SubstituteDriveDownloadPathCallback
& callback
,
91 // Starts the upload of a downloaded/downloading file.
92 void UploadDownloadItem(content::DownloadManager
* manager
,
93 content::DownloadItem
* download
);
95 // Sets |cache_file_path| as user data of the download item specified by |id|.
96 void SetCacheFilePath(void* manager_id
,
98 const base::FilePath
* cache_file_path
,
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
);
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_
;
111 // Temporary download location directory.
112 base::FilePath drive_tmp_download_path_
;
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_
;
118 DISALLOW_COPY_AND_ASSIGN(DownloadHandler
);
123 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DOWNLOAD_HANDLER_H_