copy_operation.h revision 3551c9c881056c480085172ff9840cab31610854
1// Copyright (c) 2012 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_COPY_OPERATION_H_
6#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_COPY_OPERATION_H_
7
8#include "base/basictypes.h"
9#include "base/memory/ref_counted.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/memory/weak_ptr.h"
12#include "chrome/browser/chromeos/drive/resource_metadata.h"
13#include "chrome/browser/google_apis/drive_api_parser.h"
14#include "chrome/browser/google_apis/gdata_errorcode.h"
15
16namespace base {
17class FilePath;
18class SequencedTaskRunner;
19}  // namespace base
20
21namespace google_apis {
22class ResourceEntry;
23}  // namespace google_apis
24
25namespace drive {
26
27class DriveServiceInterface;
28class JobScheduler;
29class ResourceEntry;
30
31namespace internal {
32class FileCache;
33}  // namespace internal
34
35namespace file_system {
36
37class CreateFileOperation;
38class DownloadOperation;
39class MoveOperation;
40class OperationObserver;
41
42// This class encapsulates the drive Copy function.  It is responsible for
43// sending the request to the drive API, then updating the local state and
44// metadata to reflect the new state.
45class CopyOperation {
46 public:
47  CopyOperation(base::SequencedTaskRunner* blocking_task_runner,
48                OperationObserver* observer,
49                JobScheduler* scheduler,
50                internal::ResourceMetadata* metadata,
51                internal::FileCache* cache,
52                DriveServiceInterface* drive_service,
53                const base::FilePath& temporary_file_directory);
54  ~CopyOperation();
55
56  // Performs the copy operation on the file at drive path |src_file_path|
57  // with a target of |dest_file_path|. Invokes |callback| when finished with
58  // the result of the operation. |callback| must not be null.
59  void Copy(const base::FilePath& src_file_path,
60            const base::FilePath& dest_file_path,
61            const FileOperationCallback& callback);
62
63  // Initiates transfer of |local_src_file_path| to |remote_dest_file_path|.
64  // |local_src_file_path| must be a file from the local file system.
65  // |remote_dest_file_path| is the virtual destination path within Drive file
66  // system.
67  //
68  // |callback| must not be null.
69  void TransferFileFromLocalToRemote(
70      const base::FilePath& local_src_file_path,
71      const base::FilePath& remote_dest_file_path,
72      const FileOperationCallback& callback);
73
74 private:
75  // Creates an empty file on the server at |remote_dest_path| to ensure
76  // the location, stores a file at |local_file_path| in cache and marks it
77  // dirty, so that SyncClient will upload the data later.
78  void ScheduleTransferRegularFile(const base::FilePath& local_src_path,
79                                   const base::FilePath& remote_dest_path,
80                                   const FileOperationCallback& callback);
81
82  // Part of ScheduleTransferRegularFile(). Called after GetFileSize() is
83  // completed.
84  void ScheduleTransferRegularFileAfterGetFileSize(
85      const base::FilePath& local_src_path,
86      const base::FilePath& remote_dest_path,
87      const FileOperationCallback& callback,
88      int64 local_file_size);
89
90  // Part of ScheduleTransferRegularFile(). Called after GetAboutResource()
91  // is completed.
92  void ScheduleTransferRegularFileAfterGetAboutResource(
93      const base::FilePath& local_src_path,
94      const base::FilePath& remote_dest_path,
95      const FileOperationCallback& callback,
96      int64 local_file_size,
97      google_apis::GDataErrorCode status,
98      scoped_ptr<google_apis::AboutResource> about_resource);
99
100  // Part of ScheduleTransferRegularFile(). Called after file creation.
101  void ScheduleTransferRegularFileAfterCreate(
102      const base::FilePath& local_src_path,
103      const base::FilePath& remote_dest_path,
104      const FileOperationCallback& callback,
105      FileError error);
106
107  // Part of ScheduleTransferRegularFile(). Called after updating local state
108  // is completed.
109  void ScheduleTransferRegularFileAfterUpdateLocalState(
110      const FileOperationCallback& callback,
111      std::string* resource_id,
112      FileError error);
113
114  // Copies a hosted document with |resource_id| to |dest_path|
115  void CopyHostedDocumentToDirectory(const base::FilePath& dest_path,
116                                     const std::string& resource_id,
117                                     const FileOperationCallback& callback);
118
119  // Callback for handling document copy attempt.
120  void OnCopyHostedDocumentCompleted(
121      const base::FilePath& dest_path,
122      const FileOperationCallback& callback,
123      google_apis::GDataErrorCode status,
124      scoped_ptr<google_apis::ResourceEntry> resource_entry);
125
126  // Moves a file or directory at |file_path| in the root directory to
127  // |dest_path|. This function does nothing if |dest_path| points to a
128  // resource directly under the root directory.
129  void MoveEntryFromRootDirectory(const base::FilePath& dest_path,
130                                  const FileOperationCallback& callback,
131                                  FileError error,
132                                  const base::FilePath& file_path);
133
134  // Part of Copy(). Called after GetResourceEntryPairByPaths() is complete.
135  void CopyAfterGetResourceEntryPair(const base::FilePath& dest_file_path,
136                                     const FileOperationCallback& callback,
137                                     scoped_ptr<EntryInfoPairResult> result);
138
139  // Callback for handling resource copy on the server.
140  // |callback| must not be null.
141  void OnCopyResourceCompleted(
142      const FileOperationCallback& callback,
143      google_apis::GDataErrorCode status,
144      scoped_ptr<google_apis::ResourceEntry> resource_entry);
145
146  // Invoked upon completion of GetFileByPath initiated by Copy. If
147  // GetFileByPath reports no error, calls TransferRegularFile to transfer
148  // |local_file_path| to |remote_dest_file_path|.
149  void OnGetFileCompleteForCopy(const base::FilePath& remote_dest_file_path,
150                                const FileOperationCallback& callback,
151                                FileError error,
152                                const base::FilePath& local_file_path,
153                                scoped_ptr<ResourceEntry> entry);
154
155  // Part of TransferFileFromLocalToRemote(). Called after |parent_entry| is
156  // retrieved in the blocking pool.
157  void TransferFileFromLocalToRemoteAfterPrepare(
158      const base::FilePath& local_src_file_path,
159      const base::FilePath& remote_dest_file_path,
160      const FileOperationCallback& callback,
161      ResourceEntry* parent_entry,
162      FileError error);
163
164  // Part of TransferFileFromLocalToRemote(). Called after the result of
165  // GetAboutResource() is avaiable.
166  void TransferFileFromLocalToRemoteAfterGetQuota(
167      const base::FilePath& local_src_file_path,
168      const base::FilePath& remote_dest_file_path,
169      const FileOperationCallback& callback,
170      google_apis::GDataErrorCode status,
171      scoped_ptr<google_apis::AboutResource> about_resource);
172
173  // Initiates transfer of |local_file_path| with |resource_id| to
174  // |remote_dest_file_path|. |local_file_path| must be a file from the local
175  // file system, |remote_dest_file_path| is the virtual destination path within
176  // Drive file system. If |resource_id| is a non-empty string, the transfer is
177  // handled by CopyDocumentToDirectory. Otherwise, the transfer is handled by
178  // TransferRegularFile.
179  void TransferFileForResourceId(const base::FilePath& local_file_path,
180                                 const base::FilePath& remote_dest_file_path,
181                                 const FileOperationCallback& callback,
182                                 const std::string& resource_id);
183
184  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
185  OperationObserver* observer_;
186  JobScheduler* scheduler_;
187  internal::ResourceMetadata* metadata_;
188  internal::FileCache* cache_;
189  DriveServiceInterface* drive_service_;
190
191  // Uploading a new file is internally implemented by creating a dirty file.
192  scoped_ptr<CreateFileOperation> create_file_operation_;
193  // Copying from remote to local (and to remote in WAPI) involves downloading.
194  scoped_ptr<DownloadOperation> download_operation_;
195  // Copying a hosted document is internally implemented by using a move.
196  scoped_ptr<MoveOperation> move_operation_;
197
198  // Note: This should remain the last member so it'll be destroyed and
199  // invalidate the weak pointers before any other members are destroyed.
200  base::WeakPtrFactory<CopyOperation> weak_ptr_factory_;
201  DISALLOW_COPY_AND_ASSIGN(CopyOperation);
202};
203
204}  // namespace file_system
205}  // namespace drive
206
207#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_COPY_OPERATION_H_
208