summaryrefslogtreecommitdiffstats
path: root/webkit/fileapi/cross_operation_delegate.h
blob: 3be003a0bbd796bfe4d9d50bd108821b5e2e11fb (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
// Copyright (c) 2013 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 WEBKIT_FILEAPI_CROSS_OPERATION_DELEGATE_H_
#define WEBKIT_FILEAPI_CROSS_OPERATION_DELEGATE_H_

#include <stack>

#include "base/memory/ref_counted.h"
#include "webkit/fileapi/recursive_operation_delegate.h"

namespace webkit_blob {
class ShareableFileReference;
}

namespace fileapi {

// A delegate class for recursive copy or move operations.
class CrossOperationDelegate
    : public RecursiveOperationDelegate,
      public base::SupportsWeakPtr<CrossOperationDelegate> {
 public:
  enum OperationType {
    OPERATION_COPY,
    OPERATION_MOVE
  };

  CrossOperationDelegate(LocalFileSystemOperation* original_operation,
                         const FileSystemURL& src_root,
                         const FileSystemURL& dest_root,
                         OperationType operation_type,
                         const StatusCallback& callback);
  virtual ~CrossOperationDelegate();

  // RecursiveOperationDelegate overrides:
  virtual void Run() OVERRIDE;
  virtual void RunRecursively() OVERRIDE;
  virtual void ProcessFile(const FileSystemURL& url,
                           const StatusCallback& callback) OVERRIDE;
  virtual void ProcessDirectory(const FileSystemURL& url,
                                const StatusCallback& callback) OVERRIDE;

  using base::SupportsWeakPtr<CrossOperationDelegate>::AsWeakPtr;

 private:
  void DidTryCopyOrMoveFile(base::PlatformFileError error);
  void DidTryRemoveDestRoot(base::PlatformFileError error);
  void CopyOrMoveFile(
      const FileSystemURL& src,
      const FileSystemURL& dest,
      const StatusCallback& callback);
  void DidCreateSnapshot(
      const FileSystemURL& dest,
      const StatusCallback& callback,
      base::PlatformFileError error,
      const base::PlatformFileInfo& file_info,
      const base::FilePath& platform_path,
      const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref);
  void DidFinishCopy(
      const FileSystemURL& src,
      const StatusCallback& callback,
      base::PlatformFileError error);
  void DidRemoveSourceForMove(
      const StatusCallback& callback,
      base::PlatformFileError error);

  FileSystemURL CreateDestURL(const FileSystemURL& src_url) const;

  // Create nested operations for recursive task.
  // When the creation fails it fires callback_ with the
  // error code and returns NULL.
  //
  // - NewDestOperation is basically a thin wrapper of
  //   RecursiveOperationDelegate::NewOperation().
  //   (Since the original_operation must have been created for the destination
  //   URL. TODO(kinuko): we should have some assertion for this assumption)
  //
  // - NewSourceOperation also redirects the request to
  //   RecursiveOperationDelegate::NewOperation() **iff** same_file_system_
  //   is true.
  //   Otherwise it's for cross-filesystem operation and it needs a
  //   separate FileSystemOperationContext, so it creates a new operation
  //   which inherits context from src_root_operation_.
  //
  LocalFileSystemOperation* NewSourceOperation(const FileSystemURL& url);
  LocalFileSystemOperation* NewDestOperation(const FileSystemURL& url);

  FileSystemURL src_root_;
  FileSystemURL dest_root_;
  bool same_file_system_;
  OperationType operation_type_;
  StatusCallback callback_;

  LocalFileSystemOperation* src_root_operation_;

  scoped_refptr<webkit_blob::ShareableFileReference> current_file_ref_;

  DISALLOW_COPY_AND_ASSIGN(CrossOperationDelegate);
};

}  // namespace fileapi

#endif  // WEBKIT_FILEAPI_CROSS_OPERATION_DELEGATE_H_