download_operation.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
1// Copyright 2013 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_FILE_SYSTEM_DOWNLOAD_OPERATION_H_
6#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_DOWNLOAD_OPERATION_H_
7
8#include "base/basictypes.h"
9#include "base/memory/scoped_ptr.h"
10#include "chrome/browser/chromeos/drive/file_errors.h"
11#include "chrome/browser/chromeos/drive/file_system_interface.h"
12#include "chrome/browser/chromeos/drive/job_list.h"
13#include "chrome/browser/google_apis/gdata_errorcode.h"
14
15namespace base {
16class FilePath;
17class SequencedTaskRunner;
18}  // namespace base
19
20namespace google_apis {
21class ResourceEntry;
22}  // namespace google_apis
23
24namespace drive {
25
26class JobScheduler;
27class ResourceEntry;
28
29namespace internal {
30class FileCache;
31class ResourceMetadata;
32}  // namespace internal
33
34namespace file_system {
35
36class OperationObserver;
37
38class DownloadOperation {
39 public:
40  DownloadOperation(base::SequencedTaskRunner* blocking_task_runner,
41                    OperationObserver* observer,
42                    JobScheduler* scheduler,
43                    internal::ResourceMetadata* metadata,
44                    internal::FileCache* cache,
45                    const base::FilePath& temporary_file_directory);
46  ~DownloadOperation();
47
48  // Ensures that the file content specified by |resource_id| is locally
49  // downloaded.
50  // For hosted documents, this method may create a JSON file representing the
51  // file.
52  // For regular files, if the locally cached file is found, returns it.
53  // If not found, start to download the file from the server.
54  // When a JSON file is created, the cache file is found or downloading is
55  // being started, |initialized_callback| is called with |local_file|
56  // for JSON file or the cache file, or with |cancel_download_closure| for
57  // downloading.
58  // During the downloading |get_content_callback| will be called periodically
59  // with the downloaded content.
60  // Upon completion or an error is found, |completion_callback| will be called.
61  // |initialized_callback| and |get_content_callback| can be null if not
62  // needed.
63  // |completion_callback| must not be null.
64  void EnsureFileDownloadedByResourceId(
65      const std::string& resource_id,
66      const ClientContext& context,
67      const GetFileContentInitializedCallback& initialized_callback,
68      const google_apis::GetContentCallback& get_content_callback,
69      const GetFileCallback& completion_callback);
70
71  // Does the same thing as EnsureFileDownloadedByResourceId for the file
72  // specified by |file_path|.
73  void EnsureFileDownloadedByPath(
74      const base::FilePath& file_path,
75      const ClientContext& context,
76      const GetFileContentInitializedCallback& initialized_callback,
77      const google_apis::GetContentCallback& get_content_callback,
78      const GetFileCallback& completion_callback);
79
80 private:
81  // Thin wrapper of Callbacks for EnsureFileDownloaded.
82  class DownloadCallback;
83
84  // Part of EnsureFileDownloaded(). Called upon the completion of precondition
85  // check.
86  void EnsureFileDownloadedAfterCheckPreCondition(
87      const DownloadCallback& callback,
88      const ClientContext& context,
89      scoped_ptr<ResourceEntry> entry,
90      base::FilePath* drive_file_path,
91      base::FilePath* cache_file_path,
92      FileError error);
93
94  // Part of EnsureFileDownloaded(). Called when it is ready to start
95  // downloading the file.
96  void EnsureFileDownloadedAfterPrepareForDownloadFile(
97      const DownloadCallback& callback,
98      const ClientContext& context,
99      scoped_ptr<ResourceEntry> entry,
100      const base::FilePath& drive_file_path,
101      base::FilePath* temp_download_file_path,
102      FileError error);
103
104  // Part of EnsureFileDownloaded(). Called after the actual downloading.
105  void EnsureFileDownloadedAfterDownloadFile(
106      const base::FilePath& drive_file_path,
107      scoped_ptr<ResourceEntry> entry,
108      const DownloadCallback& callback,
109      google_apis::GDataErrorCode gdata_error,
110      const base::FilePath& downloaded_file_path);
111
112  // Part of EnsureFileDownloaded(). Called after updating local state is
113  // completed.
114  void EnsureFileDownloadedAfterUpdateLocalState(
115      const base::FilePath& file_path,
116      const DownloadCallback& callback,
117      scoped_ptr<ResourceEntry> entry,
118      base::FilePath* cache_file_path,
119      FileError error);
120
121  // Cancels the job with |job_id| in the scheduler.
122  void CancelJob(JobID job_id);
123
124  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
125  OperationObserver* observer_;
126  JobScheduler* scheduler_;
127  internal::ResourceMetadata* metadata_;
128  internal::FileCache* cache_;
129  const base::FilePath temporary_file_directory_;
130
131  // Note: This should remain the last member so it'll be destroyed and
132  // invalidate its weak pointers before any other members are destroyed.
133  base::WeakPtrFactory<DownloadOperation> weak_ptr_factory_;
134  DISALLOW_COPY_AND_ASSIGN(DownloadOperation);
135};
136
137}  // namespace file_system
138}  // namespace drive
139
140#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_DOWNLOAD_OPERATION_H_
141