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 "google_apis/drive/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 |local_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 EnsureFileDownloadedByLocalId(
65      const std::string& local_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 EnsureFileDownloadedByLocalId 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  // Parameters for EnsureFileDownloaded.
82  class DownloadParams;
83
84  // Part of EnsureFileDownloaded(). Called upon the completion of precondition
85  // check.
86  void EnsureFileDownloadedAfterCheckPreCondition(
87      scoped_ptr<DownloadParams> params,
88      const ClientContext& context,
89      base::FilePath* drive_file_path,
90      base::FilePath* cache_file_path,
91      FileError error);
92
93  // Part of EnsureFileDownloaded(). Called when it is ready to start
94  // downloading the file.
95  void EnsureFileDownloadedAfterPrepareForDownloadFile(
96      scoped_ptr<DownloadParams> params,
97      const ClientContext& context,
98      const base::FilePath& drive_file_path,
99      base::FilePath* temp_download_file_path,
100      FileError error);
101
102  // Part of EnsureFileDownloaded(). Called after the actual downloading.
103  void EnsureFileDownloadedAfterDownloadFile(
104      const base::FilePath& drive_file_path,
105      scoped_ptr<DownloadParams> params,
106      google_apis::GDataErrorCode gdata_error,
107      const base::FilePath& downloaded_file_path);
108
109  // Part of EnsureFileDownloaded(). Called after updating local state is
110  // completed.
111  void EnsureFileDownloadedAfterUpdateLocalState(
112      const base::FilePath& file_path,
113      scoped_ptr<DownloadParams> params,
114      base::FilePath* cache_file_path,
115      FileError error);
116
117  // Cancels the job with |job_id| in the scheduler.
118  void CancelJob(JobID job_id);
119
120  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
121  OperationObserver* observer_;
122  JobScheduler* scheduler_;
123  internal::ResourceMetadata* metadata_;
124  internal::FileCache* cache_;
125  const base::FilePath temporary_file_directory_;
126
127  // Note: This should remain the last member so it'll be destroyed and
128  // invalidate its weak pointers before any other members are destroyed.
129  base::WeakPtrFactory<DownloadOperation> weak_ptr_factory_;
130  DISALLOW_COPY_AND_ASSIGN(DownloadOperation);
131};
132
133}  // namespace file_system
134}  // namespace drive
135
136#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_DOWNLOAD_OPERATION_H_
137