Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / drive / file_system / download_operation.h
blobe45902bf54737f066f7486f30acf56e2f079057a
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.
5 #ifndef COMPONENTS_DRIVE_FILE_SYSTEM_DOWNLOAD_OPERATION_H_
6 #define COMPONENTS_DRIVE_FILE_SYSTEM_DOWNLOAD_OPERATION_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/threading/thread_checker.h"
11 #include "components/drive/file_errors.h"
12 #include "components/drive/file_system_interface.h"
13 #include "components/drive/job_list.h"
14 #include "google_apis/drive/drive_api_error_codes.h"
16 namespace base {
17 class FilePath;
18 class SequencedTaskRunner;
19 } // namespace base
21 namespace google_apis {
22 class ResourceEntry;
23 } // namespace google_apis
25 namespace drive {
27 class JobScheduler;
28 class ResourceEntry;
29 struct ClientContext;
31 namespace internal {
32 class FileCache;
33 class ResourceMetadata;
34 } // namespace internal
36 namespace file_system {
38 class OperationDelegate;
40 class DownloadOperation {
41 public:
42 DownloadOperation(base::SequencedTaskRunner* blocking_task_runner,
43 OperationDelegate* delegate,
44 JobScheduler* scheduler,
45 internal::ResourceMetadata* metadata,
46 internal::FileCache* cache,
47 const base::FilePath& temporary_file_directory);
48 ~DownloadOperation();
50 // Ensures that the file content specified by |local_id| is locally
51 // downloaded and returns a closure to cancel the task.
52 // For hosted documents, this method may create a JSON file representing the
53 // file.
54 // For regular files, if the locally cached file is found, returns it.
55 // If not found, start to download the file from the server.
56 // When a JSON file is created, the cache file is found or downloading is
57 // being started, |initialized_callback| is called with |local_file|
58 // for JSON file or the cache file, or with |cancel_download_closure| for
59 // downloading.
60 // During the downloading |get_content_callback| will be called periodically
61 // with the downloaded content.
62 // Upon completion or an error is found, |completion_callback| will be called.
63 // |initialized_callback| and |get_content_callback| can be null if not
64 // needed.
65 // |completion_callback| must not be null.
66 base::Closure EnsureFileDownloadedByLocalId(
67 const std::string& local_id,
68 const ClientContext& context,
69 const GetFileContentInitializedCallback& initialized_callback,
70 const google_apis::GetContentCallback& get_content_callback,
71 const GetFileCallback& completion_callback);
73 // Does the same thing as EnsureFileDownloadedByLocalId for the file
74 // specified by |file_path|.
75 base::Closure EnsureFileDownloadedByPath(
76 const base::FilePath& file_path,
77 const ClientContext& context,
78 const GetFileContentInitializedCallback& initialized_callback,
79 const google_apis::GetContentCallback& get_content_callback,
80 const GetFileCallback& completion_callback);
82 private:
83 // Parameters for EnsureFileDownloaded.
84 class DownloadParams;
86 // Part of EnsureFileDownloaded(). Called upon the completion of precondition
87 // check.
88 void EnsureFileDownloadedAfterCheckPreCondition(
89 scoped_ptr<DownloadParams> params,
90 const ClientContext& context,
91 base::FilePath* drive_file_path,
92 base::FilePath* cache_file_path,
93 base::FilePath* temp_download_file_path,
94 FileError error);
96 // Part of EnsureFileDownloaded(). Called after the actual downloading.
97 void EnsureFileDownloadedAfterDownloadFile(
98 const base::FilePath& drive_file_path,
99 scoped_ptr<DownloadParams> params,
100 google_apis::DriveApiErrorCode gdata_error,
101 const base::FilePath& downloaded_file_path);
103 // Part of EnsureFileDownloaded(). Called after updating local state is
104 // completed.
105 void EnsureFileDownloadedAfterUpdateLocalState(
106 const base::FilePath& file_path,
107 scoped_ptr<DownloadParams> params,
108 scoped_ptr<ResourceEntry> entry_after_update,
109 base::FilePath* cache_file_path,
110 FileError error);
112 // Cancels the job with |job_id| in the scheduler.
113 void CancelJob(JobID job_id);
115 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
116 OperationDelegate* delegate_;
117 JobScheduler* scheduler_;
118 internal::ResourceMetadata* metadata_;
119 internal::FileCache* cache_;
120 const base::FilePath temporary_file_directory_;
122 base::ThreadChecker thread_checker_;
124 // Note: This should remain the last member so it'll be destroyed and
125 // invalidate its weak pointers before any other members are destroyed.
126 base::WeakPtrFactory<DownloadOperation> weak_ptr_factory_;
127 DISALLOW_COPY_AND_ASSIGN(DownloadOperation);
130 } // namespace file_system
131 } // namespace drive
133 #endif // COMPONENTS_DRIVE_FILE_SYSTEM_DOWNLOAD_OPERATION_H_