Home | History | Annotate | Download | only in file_system
      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 <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "chrome/browser/chromeos/drive/file_errors.h"
     16 #include "google_apis/drive/gdata_errorcode.h"
     17 
     18 namespace base {
     19 class FilePath;
     20 class SequencedTaskRunner;
     21 class Time;
     22 }  // namespace base
     23 
     24 namespace google_apis {
     25 class FileResource;
     26 }  // namespace google_apis
     27 
     28 namespace drive {
     29 
     30 class JobScheduler;
     31 class ResourceEntry;
     32 
     33 namespace internal {
     34 class FileCache;
     35 class ResourceMetadata;
     36 }  // namespace internal
     37 
     38 namespace file_system {
     39 
     40 class CreateFileOperation;
     41 class OperationDelegate;
     42 
     43 // This class encapsulates the drive Copy function.  It is responsible for
     44 // sending the request to the drive API, then updating the local state and
     45 // metadata to reflect the new state.
     46 class CopyOperation {
     47  public:
     48   CopyOperation(base::SequencedTaskRunner* blocking_task_runner,
     49                 OperationDelegate* delegate,
     50                 JobScheduler* scheduler,
     51                 internal::ResourceMetadata* metadata,
     52                 internal::FileCache* cache);
     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   // Params for TransferJsonGdocFileAfterLocalWork.
     81   struct TransferJsonGdocParams;
     82 
     83  private:
     84   // Part of Copy(). Called after trying to copy locally.
     85   void CopyAfterTryToCopyLocally(
     86       const CopyParams* params,
     87       const std::vector<std::string>* updated_local_ids,
     88       const bool* directory_changed,
     89       const bool* should_copy_on_server,
     90       FileError error);
     91 
     92   // Part of Copy(). Called after the parent entry gets synced.
     93   void CopyAfterParentSync(const CopyParams& params, FileError error);
     94 
     95   // Part of Copy(). Called after the parent resource ID is resolved.
     96   void CopyAfterGetParentResourceId(const CopyParams& params,
     97                                     const ResourceEntry* parent,
     98                                     FileError error);
     99 
    100   // Part of TransferFileFromLocalToRemote(). Called after preparation is done.
    101   // |gdoc_resource_id| and |parent_resource_id| is available only if the file
    102   // is JSON GDoc file.
    103   void TransferFileFromLocalToRemoteAfterPrepare(
    104       const base::FilePath& local_src_path,
    105       const base::FilePath& remote_dest_path,
    106       const FileOperationCallback& callback,
    107       std::string* gdoc_resource_id,
    108       ResourceEntry* parent_entry,
    109       FileError error);
    110 
    111   // Part of TransferFileFromLocalToRemote().
    112   void TransferJsonGdocFileAfterLocalWork(TransferJsonGdocParams* params,
    113                                           FileError error);
    114 
    115   // Copies resource with |resource_id| into the directory |parent_resource_id|
    116   // with renaming it to |new_title|.
    117   void CopyResourceOnServer(const std::string& resource_id,
    118                             const std::string& parent_resource_id,
    119                             const std::string& new_title,
    120                             const base::Time& last_modified,
    121                             const FileOperationCallback& callback);
    122 
    123   // Part of CopyResourceOnServer and TransferFileFromLocalToRemote.
    124   // Called after server side operation is done.
    125   void UpdateAfterServerSideOperation(
    126       const FileOperationCallback& callback,
    127       google_apis::GDataErrorCode status,
    128       scoped_ptr<google_apis::FileResource> entry);
    129 
    130   // Part of CopyResourceOnServer and TransferFileFromLocalToRemote.
    131   // Called after local state update is done.
    132   void UpdateAfterLocalStateUpdate(const FileOperationCallback& callback,
    133                                    base::FilePath* file_path,
    134                                    const ResourceEntry* entry,
    135                                    FileError error);
    136 
    137   // Creates an empty file on the server at |remote_dest_path| to ensure
    138   // the location, stores a file at |local_file_path| in cache and marks it
    139   // dirty, so that SyncClient will upload the data later.
    140   void ScheduleTransferRegularFile(const base::FilePath& local_src_path,
    141                                    const base::FilePath& remote_dest_path,
    142                                    const FileOperationCallback& callback);
    143 
    144   // Part of ScheduleTransferRegularFile(). Called after file creation.
    145   void ScheduleTransferRegularFileAfterCreate(
    146       const base::FilePath& local_src_path,
    147       const base::FilePath& remote_dest_path,
    148       const FileOperationCallback& callback,
    149       FileError error);
    150 
    151   // Part of ScheduleTransferRegularFile(). Called after updating local state
    152   // is completed.
    153   void ScheduleTransferRegularFileAfterUpdateLocalState(
    154       const FileOperationCallback& callback,
    155       const base::FilePath& remote_dest_path,
    156       const ResourceEntry* entry,
    157       std::string* local_id,
    158       FileError error);
    159 
    160   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
    161   OperationDelegate* delegate_;
    162   JobScheduler* scheduler_;
    163   internal::ResourceMetadata* metadata_;
    164   internal::FileCache* cache_;
    165 
    166   // Uploading a new file is internally implemented by creating a dirty file.
    167   scoped_ptr<CreateFileOperation> create_file_operation_;
    168 
    169   // Note: This should remain the last member so it'll be destroyed and
    170   // invalidate the weak pointers before any other members are destroyed.
    171   base::WeakPtrFactory<CopyOperation> weak_ptr_factory_;
    172   DISALLOW_COPY_AND_ASSIGN(CopyOperation);
    173 };
    174 
    175 }  // namespace file_system
    176 }  // namespace drive
    177 
    178 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_COPY_OPERATION_H_
    179