summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/file_system/move_operation.h
blob: 7e5d91d00be8ff229c0a5e210280d90e453d1ae7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_MOVE_OPERATION_H_
#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_MOVE_OPERATION_H_

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/chromeos/drive/drive_resource_metadata.h"
#include "chrome/browser/google_apis/gdata_errorcode.h"

class FilePath;
class GURL;

namespace drive {

class DriveCache;
class DriveEntryProto;
class DriveResourceMetadata;
class DriveServiceInterface;

namespace file_system {

class OperationObserver;

// This class encapsulates the drive Move function.  It is resposible for
// sending the request to the drive API, then updating the local state and
// metadata to reflect the new state.
class MoveOperation {
 public:
  MoveOperation(DriveServiceInterface* drive_service,
                DriveResourceMetadata* metadata,
                OperationObserver* observer);
  virtual ~MoveOperation();

  // Performs the move operation on the file at drive path |src_file_path|
  // with a target of |dest_file_path|.  Invokes |callback| when finished with
  // the result of the operation.
  void Move(const FilePath& src_file_path,
            const FilePath& dest_file_path,
            const FileOperationCallback& callback);
 private:
  // Part of Move(). Called after GetEntryInfoPairByPaths() is
  // complete. |callback| must not be null.
  void MoveAfterGetEntryInfoPair(
    const FilePath& dest_file_path,
    const FileOperationCallback& callback,
    scoped_ptr<EntryInfoPairResult> result);

  // A pass-through callback used for bridging from
  // FileMoveCallback to FileOperationCallback.
  void OnFilePathUpdated(const FileOperationCallback& cllback,
                         DriveFileError error,
                         const FilePath& file_path);

  // Renames a file or directory at |file_path| to |new_name| in the same
  // directory. |callback| will receive the new file path if the operation is
  // successful. If the new name already exists in the same directory, the file
  // name is uniquified by adding a parenthesized serial number like
  // "foo (2).txt"
  //
  // Can be called from UI thread. |callback| is run on the calling thread.
  // |callback| must not be null.
  void Rename(const FilePath& file_path,
              const FilePath::StringType& new_name,
              const FileMoveCallback& callback);

  // Part of Rename(). Called after GetEntryInfoByPath() is complete.
  // |callback| must not be null.
  void RenameAfterGetEntryInfo(const FilePath& file_path,
                               const FilePath::StringType& new_name,
                               const FileMoveCallback& callback,
                               DriveFileError error,
                               scoped_ptr<DriveEntryProto> entry_proto);

  // Callback for handling resource rename attempt. Renames a file or
  // directory at |file_path| on the client side.
  // |callback| must not be null.
  void RenameEntryLocally(const FilePath& file_path,
                          const FilePath::StringType& new_name,
                          const FileMoveCallback& callback,
                          google_apis::GDataErrorCode status,
                          const GURL& document_url);

  // Removes a file or directory at |file_path| from the current directory if
  // it's not in the root directory. This essentially moves an entry to the
  // root directory on the server side.
  //
  // Can be called from UI thread. |callback| is run on the calling thread.
  // |callback| must not be null.
  void RemoveEntryFromNonRootDirectory(const FileMoveCallback& callback,
                                       DriveFileError error,
                                       const FilePath& file_path);

  // Part of RemoveEntryFromNonRootDirectory(). Called after
  // GetEntryInfoPairByPaths() is complete. |callback| must not be null.
  void RemoveEntryFromNonRootDirectoryAfterEntryInfoPair(
    const FileMoveCallback& callback,
    scoped_ptr<EntryInfoPairResult> result);

  // Moves a file or directory at |file_path| in the root directory to
  // another directory at |dir_path|. This function does nothing if
  // |dir_path| points to the root directory.
  //
  // Can be called from UI thread. |callback| is run on the calling thread.
  // |callback| must not be null.
  void MoveEntryFromRootDirectory(const FilePath& directory_path,
                                  const FileOperationCallback& callback,
                                  DriveFileError error,
                                  const FilePath& file_path);

  // Part of MoveEntryFromRootDirectory(). Called after
  // GetEntryInfoPairByPaths() is complete. |callback| must not be null.
  void MoveEntryFromRootDirectoryAfterGetEntryInfoPair(
    const FileOperationCallback& callback,
    scoped_ptr<EntryInfoPairResult> result);

  // Moves entry specified by |file_path| to the directory specified by
  // |dir_path| and calls |callback| asynchronously.
  // |callback| must not be null.
  void MoveEntryToDirectory(const FilePath& file_path,
                            const FilePath& directory_path,
                            const FileMoveCallback& callback,
                            google_apis::GDataErrorCode status,
                            const GURL& document_url);

  // Callback when an entry is moved to another directory on the client side.
  // Notifies the directory change and runs |callback|.
  // |callback| must not be null.
  void NotifyAndRunFileOperationCallback(
      const FileOperationCallback& callback,
      DriveFileError error,
      const FilePath& moved_file_path);

  // Callback when an entry is moved to another directory on the client side.
  // Notifies the directory change and runs |callback|.
  // |callback| must not be null.
  void NotifyAndRunFileMoveCallback(
      const FileMoveCallback& callback,
      DriveFileError error,
      const FilePath& moved_file_path);

  DriveServiceInterface* drive_service_;
  DriveResourceMetadata* metadata_;
  OperationObserver* observer_;

  // WeakPtrFactory bound to the UI thread.
  // Note: This should remain the last member so it'll be destroyed and
  // invalidate the weak pointers before any other members are destroyed.
  base::WeakPtrFactory<MoveOperation> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(MoveOperation);
};

}  // namespace file_system
}  // namespace drive

#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_MOVE_OPERATION_H_