summaryrefslogtreecommitdiffstats
path: root/components/drive/file_system/download_operation.h
blob: ac5ffa6fc12c8f0c121a70fcbfc896e1c6ae1e36 (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
124
125
126
127
128
129
130
131
132
133
// Copyright 2013 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 COMPONENTS_DRIVE_FILE_SYSTEM_DOWNLOAD_OPERATION_H_
#define COMPONENTS_DRIVE_FILE_SYSTEM_DOWNLOAD_OPERATION_H_

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/thread_checker.h"
#include "components/drive/file_errors.h"
#include "components/drive/file_system_interface.h"
#include "components/drive/job_list.h"
#include "google_apis/drive/drive_api_error_codes.h"

namespace base {
class FilePath;
class SequencedTaskRunner;
}  // namespace base

namespace google_apis {
class ResourceEntry;
}  // namespace google_apis

namespace drive {

class JobScheduler;
class ResourceEntry;
struct ClientContext;

namespace internal {
class FileCache;
class ResourceMetadata;
}  // namespace internal

namespace file_system {

class OperationDelegate;

class DownloadOperation {
 public:
  DownloadOperation(base::SequencedTaskRunner* blocking_task_runner,
                    OperationDelegate* delegate,
                    JobScheduler* scheduler,
                    internal::ResourceMetadata* metadata,
                    internal::FileCache* cache,
                    const base::FilePath& temporary_file_directory);
  ~DownloadOperation();

  // Ensures that the file content specified by |local_id| is locally
  // downloaded and returns a closure to cancel the task.
  // For hosted documents, this method may create a JSON file representing the
  // file.
  // For regular files, if the locally cached file is found, returns it.
  // If not found, start to download the file from the server.
  // When a JSON file is created, the cache file is found or downloading is
  // being started, |initialized_callback| is called with |local_file|
  // for JSON file or the cache file, or with |cancel_download_closure| for
  // downloading.
  // During the downloading |get_content_callback| will be called periodically
  // with the downloaded content.
  // Upon completion or an error is found, |completion_callback| will be called.
  // |initialized_callback| and |get_content_callback| can be null if not
  // needed.
  // |completion_callback| must not be null.
  base::Closure EnsureFileDownloadedByLocalId(
      const std::string& local_id,
      const ClientContext& context,
      const GetFileContentInitializedCallback& initialized_callback,
      const google_apis::GetContentCallback& get_content_callback,
      const GetFileCallback& completion_callback);

  // Does the same thing as EnsureFileDownloadedByLocalId for the file
  // specified by |file_path|.
  base::Closure EnsureFileDownloadedByPath(
      const base::FilePath& file_path,
      const ClientContext& context,
      const GetFileContentInitializedCallback& initialized_callback,
      const google_apis::GetContentCallback& get_content_callback,
      const GetFileCallback& completion_callback);

 private:
  // Parameters for EnsureFileDownloaded.
  class DownloadParams;

  // Part of EnsureFileDownloaded(). Called upon the completion of precondition
  // check.
  void EnsureFileDownloadedAfterCheckPreCondition(
      scoped_ptr<DownloadParams> params,
      const ClientContext& context,
      base::FilePath* drive_file_path,
      base::FilePath* cache_file_path,
      base::FilePath* temp_download_file_path,
      FileError error);

  // Part of EnsureFileDownloaded(). Called after the actual downloading.
  void EnsureFileDownloadedAfterDownloadFile(
      const base::FilePath& drive_file_path,
      scoped_ptr<DownloadParams> params,
      google_apis::DriveApiErrorCode gdata_error,
      const base::FilePath& downloaded_file_path);

  // Part of EnsureFileDownloaded(). Called after updating local state is
  // completed.
  void EnsureFileDownloadedAfterUpdateLocalState(
      const base::FilePath& file_path,
      scoped_ptr<DownloadParams> params,
      scoped_ptr<ResourceEntry> entry_after_update,
      base::FilePath* cache_file_path,
      FileError error);

  // Cancels the job with |job_id| in the scheduler.
  void CancelJob(JobID job_id);

  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
  OperationDelegate* delegate_;
  JobScheduler* scheduler_;
  internal::ResourceMetadata* metadata_;
  internal::FileCache* cache_;
  const base::FilePath temporary_file_directory_;

  base::ThreadChecker thread_checker_;

  // 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<DownloadOperation> weak_ptr_factory_;
  DISALLOW_COPY_AND_ASSIGN(DownloadOperation);
};

}  // namespace file_system
}  // namespace drive

#endif  // COMPONENTS_DRIVE_FILE_SYSTEM_DOWNLOAD_OPERATION_H_