summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/download_handler.h
blob: cd4aa10f99fc1227e72a06f792ae437d99b866e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DOWNLOAD_HANDLER_H_
#define CHROME_BROWSER_CHROMEOS_DRIVE_DOWNLOAD_HANDLER_H_

#include "base/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/chromeos/drive/file_errors.h"
#include "chrome/browser/download/all_download_item_notifier.h"
#include "content/public/browser/download_manager_delegate.h"

class Profile;

namespace content {
class DownloadItem;
class DownloadManager;
}

namespace drive {

class FileSystemInterface;
class ResourceEntry;

// Observes downloads to temporary local drive folder. Schedules these
// downloads for upload to drive service.
class DownloadHandler : public AllDownloadItemNotifier::Observer {
 public:
  explicit DownloadHandler(FileSystemInterface* file_system);
  virtual ~DownloadHandler();

  // Utility method to get DownloadHandler with profile.
  static DownloadHandler* GetForProfile(Profile* profile);

  // Become an observer of DownloadManager.
  void Initialize(content::DownloadManager* download_manager,
                  const base::FilePath& drive_tmp_download_path);

  // In addition to the DownloadManager passed to Initialize(), observe another
  // download manager. This should be called only for the DownloadManager of the
  // incognito version of the profile where |file_system_| resides.
  void ObserveIncognitoDownloadManager(
      content::DownloadManager* download_manager);

  // Callback used to return results from SubstituteDriveDownloadPath.
  // TODO(hashimoto): Report error with a FileError. crbug.com/171345
  typedef base::Callback<void(const base::FilePath&)>
      SubstituteDriveDownloadPathCallback;

  void SubstituteDriveDownloadPath(
      const base::FilePath& drive_path,
      content::DownloadItem* download,
      const SubstituteDriveDownloadPathCallback& callback);

  // Sets drive path, for example, '/special/drive/MyFolder/MyFile',
  // to external data in |download|. Also sets display name and
  // makes |download| a temporary.
  void SetDownloadParams(const base::FilePath& drive_path,
                         content::DownloadItem* download);

  // Gets the target drive path from external data in |download|.
  base::FilePath GetTargetPath(const content::DownloadItem* download);

  // Gets the downloaded drive cache file path from external data in |download|.
  base::FilePath GetCacheFilePath(const content::DownloadItem* download);

  // Checks if there is a Drive upload associated with |download|
  bool IsDriveDownload(const content::DownloadItem* download);

  // Checks a file corresponding to the download item exists in Drive.
  void CheckForFileExistence(
      const content::DownloadItem* download,
      const content::CheckForFileExistenceCallback& callback);

 private:
  // AllDownloadItemNotifier::Observer overrides:
  virtual void OnDownloadCreated(content::DownloadManager* manager,
                                 content::DownloadItem* download) override;
  virtual void OnDownloadUpdated(content::DownloadManager* manager,
                                 content::DownloadItem* download) override;

  // Removes the download.
  void RemoveDownload(void* manager_id, int id);

  // Callback for FileSystem::CreateDirectory().
  // Used to implement SubstituteDriveDownloadPath().
  void OnCreateDirectory(const SubstituteDriveDownloadPathCallback& callback,
                         FileError error);

  // Starts the upload of a downloaded/downloading file.
  void UploadDownloadItem(content::DownloadManager* manager,
                          content::DownloadItem* download);

  // Sets |cache_file_path| as user data of the download item specified by |id|.
  void SetCacheFilePath(void* manager_id,
                        int id,
                        const base::FilePath* cache_file_path,
                        FileError error);

  // Gets a download manager, given a |manager_id| casted from the pointer to
  // the manager. This is used to validate the manager that may be deleted while
  // asynchronous task posting. Returns NULL if the manager is already gone.
  content::DownloadManager* GetDownloadManager(void* manager_id);

  FileSystemInterface* file_system_;  // Owned by DriveIntegrationService.
  // Observe the DownloadManager for new downloads.
  scoped_ptr<AllDownloadItemNotifier> notifier_;
  scoped_ptr<AllDownloadItemNotifier> notifier_incognito_;

  // Temporary download location directory.
  base::FilePath drive_tmp_download_path_;

  // Note: This should remain the last member so it'll be destroyed and
  // invalidate its weak pointers before any other members are destroyed.
  base::WeakPtrFactory<DownloadHandler> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(DownloadHandler);
};

}  // namespace drive

#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_DOWNLOAD_HANDLER_H_