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 "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 FileResource;
     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   // 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 TransferFileFromLocalToRemote(). Called after preparation is done.
     93   // |gdoc_resource_id| and |parent_resource_id| is available only if the file
     94   // is JSON GDoc file.
     95   void TransferFileFromLocalToRemoteAfterPrepare(
     96       const base::FilePath& local_src_path,
     97       const base::FilePath& remote_dest_path,
     98       const FileOperationCallback& callback,
     99       std::string* gdoc_resource_id,
    100       ResourceEntry* parent_entry,
    101       FileError error);
    102 
    103   // Part of TransferFileFromLocalToRemote().
    104   void TransferJsonGdocFileAfterLocalWork(TransferJsonGdocParams* params,
    105                                           FileError error);
    106 
    107   // Copies resource with |resource_id| into the directory |parent_resource_id|
    108   // with renaming it to |new_title|.
    109   void CopyResourceOnServer(const std::string& resource_id,
    110                             const std::string& parent_resource_id,
    111                             const std::string& new_title,
    112                             const base::Time& last_modified,
    113                             const FileOperationCallback& callback);
    114 
    115   // Part of CopyResourceOnServer and TransferFileFromLocalToRemote.
    116   // Called after server side operation is done.
    117   void UpdateAfterServerSideOperation(
    118       const FileOperationCallback& callback,
    119       google_apis::GDataErrorCode status,
    120       scoped_ptr<google_apis::FileResource> entry);
    121 
    122   // Part of CopyResourceOnServer and TransferFileFromLocalToRemote.
    123   // Called after local state update is done.
    124   void UpdateAfterLocalStateUpdate(
    125       const FileOperationCallback& callback,
    126       base::FilePath* file_path,
    127       FileError error);
    128 
    129   // Creates an empty file on the server at |remote_dest_path| to ensure
    130   // the location, stores a file at |local_file_path| in cache and marks it
    131   // dirty, so that SyncClient will upload the data later.
    132   void ScheduleTransferRegularFile(const base::FilePath& local_src_path,
    133                                    const base::FilePath& remote_dest_path,
    134                                    const FileOperationCallback& callback);
    135 
    136   // Part of ScheduleTransferRegularFile(). Called after file creation.
    137   void ScheduleTransferRegularFileAfterCreate(
    138       const base::FilePath& local_src_path,
    139       const base::FilePath& remote_dest_path,
    140       const FileOperationCallback& callback,
    141       FileError error);
    142 
    143   // Part of ScheduleTransferRegularFile(). Called after updating local state
    144   // is completed.
    145   void ScheduleTransferRegularFileAfterUpdateLocalState(
    146       const FileOperationCallback& callback,
    147       const base::FilePath& remote_dest_path,
    148       std::string* local_id,
    149       FileError error);
    150 
    151   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
    152   OperationObserver* observer_;
    153   JobScheduler* scheduler_;
    154   internal::ResourceMetadata* metadata_;
    155   internal::FileCache* cache_;
    156   ResourceIdCanonicalizer id_canonicalizer_;
    157 
    158   // Uploading a new file is internally implemented by creating a dirty file.
    159   scoped_ptr<CreateFileOperation> create_file_operation_;
    160 
    161   // Note: This should remain the last member so it'll be destroyed and
    162   // invalidate the weak pointers before any other members are destroyed.
    163   base::WeakPtrFactory<CopyOperation> weak_ptr_factory_;
    164   DISALLOW_COPY_AND_ASSIGN(CopyOperation);
    165 };
    166 
    167 }  // namespace file_system
    168 }  // namespace drive
    169 
    170 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_COPY_OPERATION_H_
    171