Home | History | Annotate | Download | only in file_system
      1 // Copyright (c) 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.
      4 
      5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_CREATE_DIRECTORY_OPERATION_H_
      6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_CREATE_DIRECTORY_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/google_apis/gdata_errorcode.h"
     14 
     15 namespace base {
     16 class FilePath;
     17 class SequencedTaskRunner;
     18 }  // namespace base
     19 
     20 namespace google_apis {
     21 class ResourceEntry;
     22 }  // namespace google_apis
     23 
     24 namespace drive {
     25 
     26 class JobScheduler;
     27 class ResourceEntry;
     28 
     29 namespace internal {
     30 class ResourceMetadata;
     31 }  // namespace internal
     32 
     33 namespace file_system {
     34 
     35 class OperationObserver;
     36 
     37 // This class encapsulates the drive Create Directory function.  It is
     38 // responsible for sending the request to the drive API, then updating the
     39 // local state and metadata to reflect the new state.
     40 class CreateDirectoryOperation {
     41  public:
     42   CreateDirectoryOperation(base::SequencedTaskRunner* blocking_task_runner,
     43                            OperationObserver* observer,
     44                            JobScheduler* scheduler,
     45                            internal::ResourceMetadata* metadata);
     46   ~CreateDirectoryOperation();
     47 
     48   // Creates a new directory at |directory_path|.
     49   // If |is_exclusive| is true, an error is raised in case a directory exists
     50   // already at the |directory_path|.
     51   // If |is_recursive| is true, the invocation creates parent directories as
     52   // needed just like mkdir -p does.
     53   // Invokes |callback| when finished with the result of the operation.
     54   // |callback| must not be null.
     55   void CreateDirectory(const base::FilePath& directory_path,
     56                        bool is_exclusive,
     57                        bool is_recursive,
     58                        const FileOperationCallback& callback);
     59 
     60  private:
     61   // Returns the file path to the existing deepest directory, which appears
     62   // in the |file_path|, with |entry| storing the directory's resource entry.
     63   // If not found, returns an empty file path.
     64   // This should run on |blocking_task_runner_|.
     65   static base::FilePath GetExistingDeepestDirectory(
     66       internal::ResourceMetadata* metadata,
     67       const base::FilePath& directory_path,
     68       ResourceEntry* entry);
     69 
     70   // Part of CreateDirectory(). Called after GetExistingDeepestDirectory
     71   // is completed.
     72   void CreateDirectoryAfterGetExistingDeepestDirectory(
     73       const base::FilePath& directory_path,
     74       bool is_exclusive,
     75       bool is_recursive,
     76       const FileOperationCallback& callback,
     77       ResourceEntry* existing_deepest_directory_entry,
     78       const base::FilePath& existing_deepest_directory_path);
     79 
     80   // Creates directories specified by |relative_file_path| under the directory
     81   // with |parent_resource_id|.
     82   // For example, if |relative_file_path| is "a/b/c", then "a", "a/b" and
     83   // "a/b/c" directories will be created.
     84   // Runs |callback| upon completion.
     85   void CreateDirectoryRecursively(
     86       const std::string& parent_resource_id,
     87       const base::FilePath& relative_file_path,
     88       const FileOperationCallback& callback);
     89 
     90   // Part of CreateDirectoryRecursively(). Called after AddNewDirectory() on
     91   // the server is completed.
     92   void CreateDirectoryRecursivelyAfterAddNewDirectory(
     93       const base::FilePath& remaining_path,
     94       const FileOperationCallback& callback,
     95       google_apis::GDataErrorCode gdata_error,
     96       scoped_ptr<google_apis::ResourceEntry> resource_entry);
     97 
     98   // Part of CreateDirectoryRecursively(). Called after updating local state
     99   // is completed.
    100   void CreateDirectoryRecursivelyAfterUpdateLocalState(
    101       const std::string& resource_id,
    102       const base::FilePath& remaining_path,
    103       const FileOperationCallback& callback,
    104       base::FilePath* file_path,
    105       FileError error);
    106 
    107   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
    108   OperationObserver* observer_;
    109   JobScheduler* scheduler_;
    110   internal::ResourceMetadata* metadata_;
    111 
    112   // Note: This should remain the last member so it'll be destroyed and
    113   // invalidate the weak pointers before any other members are destroyed.
    114   base::WeakPtrFactory<CreateDirectoryOperation> weak_ptr_factory_;
    115   DISALLOW_COPY_AND_ASSIGN(CreateDirectoryOperation);
    116 };
    117 
    118 }  // namespace file_system
    119 }  // namespace drive
    120 
    121 #endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_CREATE_DIRECTORY_OPERATION_H_
    122