Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / file_system / copy_operation.h
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/file_errors.h"
13 #include "chrome/browser/drive/drive_service_interface.h"
14 #include "google_apis/drive/gdata_errorcode.h"
15
16 namespace base {
17 class FilePath;
18 class SequencedTaskRunner;
19 class Time;
20 }  // namespace base
21
22 namespace google_apis {
23 class AboutResource;
24 class ResourceEntry;
25 }  // namespace google_apis
26
27 namespace drive {
28
29 class JobScheduler;
30 class ResourceEntry;
31
32 namespace internal {
33 class FileCache;
34 class ResourceMetadata;
35 }  // namespace internal
36
37 namespace file_system {
38
39 class CreateFileOperation;
40 class 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.
45 class 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                 const ResourceIdCanonicalizer& id_canonicalizer);
53   ~CopyOperation();
54
55   // Performs the copy operation on the file at drive path |src_file_path|
56   // with a target of |dest_file_path|.
57   // If |preserve_last_modified| is set to true, this tries to preserve
58   // last modified time stamp. This is supported only on Drive API v2.
59   // Invokes |callback| when finished with the result of the operation.
60   // |callback| must not be null.
61   void Copy(const base::FilePath& src_file_path,
62             const base::FilePath& dest_file_path,
63             bool preserve_last_modified,
64             const FileOperationCallback& callback);
65
66   // Initiates transfer of |local_src_file_path| to |remote_dest_file_path|.
67   // |local_src_file_path| must be a file from the local file system.
68   // |remote_dest_file_path| is the virtual destination path within Drive file
69   // system.
70   //
71   // |callback| must not be null.
72   void TransferFileFromLocalToRemote(
73       const base::FilePath& local_src_file_path,
74       const base::FilePath& remote_dest_file_path,
75       const FileOperationCallback& callback);
76
77   // Params for Copy().
78   struct CopyParams;
79
80  private:
81   // Part of Copy(). Called after trying to copy locally.
82   void CopyAfterTryToCopyLocally(
83       const CopyParams* params,
84       const std::vector<std::string>* updated_local_ids,
85       const bool* directory_changed,
86       const bool* should_copy_on_server,
87       FileError error);
88
89   // Part of TransferFileFromLocalToRemote(). Called after preparation is done.
90   // |gdoc_resource_id| and |parent_resource_id| is available only if the file
91   // is JSON GDoc file.
92   void TransferFileFromLocalToRemoteAfterPrepare(
93       const base::FilePath& local_src_path,
94       const base::FilePath& remote_dest_path,
95       const FileOperationCallback& callback,
96       std::string* gdoc_resource_id,
97       std::string* parent_resource_id,
98       FileError error);
99
100   // Copies resource with |resource_id| into the directory |parent_resource_id|
101   // with renaming it to |new_title|.
102   void CopyResourceOnServer(const std::string& resource_id,
103                             const std::string& parent_resource_id,
104                             const std::string& new_title,
105                             const base::Time& last_modified,
106                             const FileOperationCallback& callback);
107
108   // Part of CopyResourceOnServer. Called after server side copy is done.
109   void CopyResourceOnServerAfterServerSideCopy(
110       const FileOperationCallback& callback,
111       google_apis::GDataErrorCode status,
112       scoped_ptr<google_apis::ResourceEntry> resource_entry);
113
114   // Part of CopyResourceOnServer. Called after local state update is done.
115   void CopyResourceOnServerAfterUpdateLocalState(
116       const FileOperationCallback& callback,
117       base::FilePath* file_path,
118       FileError error);
119
120   // Creates an empty file on the server at |remote_dest_path| to ensure
121   // the location, stores a file at |local_file_path| in cache and marks it
122   // dirty, so that SyncClient will upload the data later.
123   void ScheduleTransferRegularFile(const base::FilePath& local_src_path,
124                                    const base::FilePath& remote_dest_path,
125                                    const FileOperationCallback& callback);
126
127   // Part of ScheduleTransferRegularFile(). Called after file creation.
128   void ScheduleTransferRegularFileAfterCreate(
129       const base::FilePath& local_src_path,
130       const base::FilePath& remote_dest_path,
131       const FileOperationCallback& callback,
132       FileError error);
133
134   // Part of ScheduleTransferRegularFile(). Called after updating local state
135   // is completed.
136   void ScheduleTransferRegularFileAfterUpdateLocalState(
137       const FileOperationCallback& callback,
138       const base::FilePath& remote_dest_path,
139       std::string* local_id,
140       FileError error);
141
142   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
143   OperationObserver* observer_;
144   JobScheduler* scheduler_;
145   internal::ResourceMetadata* metadata_;
146   internal::FileCache* cache_;
147   ResourceIdCanonicalizer id_canonicalizer_;
148
149   // Uploading a new file is internally implemented by creating a dirty file.
150   scoped_ptr<CreateFileOperation> create_file_operation_;
151
152   // Note: This should remain the last member so it'll be destroyed and
153   // invalidate the weak pointers before any other members are destroyed.
154   base::WeakPtrFactory<CopyOperation> weak_ptr_factory_;
155   DISALLOW_COPY_AND_ASSIGN(CopyOperation);
156 };
157
158 }  // namespace file_system
159 }  // namespace drive
160
161 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_COPY_OPERATION_H_