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.
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_CREATE_DIRECTORY_OPERATION_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_CREATE_DIRECTORY_OPERATION_H_
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"
17 class SequencedTaskRunner;
20 namespace google_apis {
22 } // namespace google_apis
30 class ResourceMetadata;
31 } // namespace internal
33 namespace file_system {
35 class OperationObserver;
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 {
42 CreateDirectoryOperation(base::SequencedTaskRunner* blocking_task_runner,
43 OperationObserver* observer,
44 JobScheduler* scheduler,
45 internal::ResourceMetadata* metadata);
46 ~CreateDirectoryOperation();
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,
58 const FileOperationCallback& callback);
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);
70 // Part of CreateDirectory(). Called after GetExistingDeepestDirectory
72 void CreateDirectoryAfterGetExistingDeepestDirectory(
73 const base::FilePath& directory_path,
76 const FileOperationCallback& callback,
77 ResourceEntry* existing_deepest_directory_entry,
78 const base::FilePath& existing_deepest_directory_path);
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);
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);
98 // Part of CreateDirectoryRecursively(). Called after updating local state
100 void CreateDirectoryRecursivelyAfterUpdateLocalState(
101 const std::string& resource_id,
102 const base::FilePath& remaining_path,
103 const FileOperationCallback& callback,
104 base::FilePath* file_path,
107 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
108 OperationObserver* observer_;
109 JobScheduler* scheduler_;
110 internal::ResourceMetadata* metadata_;
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);
118 } // namespace file_system
121 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_CREATE_DIRECTORY_OPERATION_H_