1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifndef STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_OPERATION_RUNNER_H_
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_OPERATION_RUNNER_H_
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <map>
94e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include <set>
104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include <vector>
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/basictypes.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/id_map.h"
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/memory/weak_ptr.h"
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/blob/blob_data_handle.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_operation.h"
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/fileapi/file_system_url.h"
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/storage_browser_export.h"
207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace net {
227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class URLRequestContext;
237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)namespace storage {
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class FileSystemURL;
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class FileSystemContext;
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// A central interface for running FileSystem API operations.
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// All operation methods take callback and returns OperationID, which is
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// an integer value which can be used for cancelling an operation.
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// All operation methods return kErrorOperationID if running (posting) an
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// operation fails, in addition to dispatching the callback with an error
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// code (therefore in most cases the caller does not need to check the
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// returned operation ID).
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass STORAGE_EXPORT FileSystemOperationRunner
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : public base::SupportsWeakPtr<FileSystemOperationRunner> {
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  typedef FileSystemOperation::GetMetadataCallback GetMetadataCallback;
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  typedef FileSystemOperation::ReadDirectoryCallback ReadDirectoryCallback;
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  typedef FileSystemOperation::SnapshotFileCallback SnapshotFileCallback;
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  typedef FileSystemOperation::StatusCallback StatusCallback;
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  typedef FileSystemOperation::WriteCallback WriteCallback;
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  typedef FileSystemOperation::OpenFileCallback OpenFileCallback;
4658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  typedef FileSystemOperation::CopyProgressCallback CopyProgressCallback;
4758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  typedef FileSystemOperation::CopyFileProgressCallback
4858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      CopyFileProgressCallback;
4968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  typedef FileSystemOperation::CopyOrMoveOption CopyOrMoveOption;
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  typedef int OperationID;
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual ~FileSystemOperationRunner();
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Cancels all inflight operations.
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void Shutdown();
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Creates a file at |url|. If |exclusive| is true, an error is raised
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // in case a file is already present at the URL.
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID CreateFile(const FileSystemURL& url,
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                         bool exclusive,
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                         const StatusCallback& callback);
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID CreateDirectory(const FileSystemURL& url,
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                              bool exclusive,
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                              bool recursive,
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                              const StatusCallback& callback);
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Copies a file or directory from |src_url| to |dest_url|. If
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // |src_url| is a directory, the contents of |src_url| are copied to
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // |dest_url| recursively. A new file or directory is created at
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // |dest_url| as needed.
7368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // For |option| and |progress_callback|, see file_system_operation.h for
7468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // details.
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID Copy(const FileSystemURL& src_url,
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                   const FileSystemURL& dest_url,
7768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                   CopyOrMoveOption option,
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                   const CopyProgressCallback& progress_callback,
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                   const StatusCallback& callback);
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Moves a file or directory from |src_url| to |dest_url|. A new file
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // or directory is created at |dest_url| as needed.
8368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // For |option|, see file_system_operation.h for details.
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID Move(const FileSystemURL& src_url,
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                   const FileSystemURL& dest_url,
8668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                   CopyOrMoveOption option,
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                   const StatusCallback& callback);
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Checks if a directory is present at |url|.
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID DirectoryExists(const FileSystemURL& url,
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                              const StatusCallback& callback);
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Checks if a file is present at |url|.
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID FileExists(const FileSystemURL& url,
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                         const StatusCallback& callback);
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Gets the metadata of a file or directory at |url|.
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID GetMetadata(const FileSystemURL& url,
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                          const GetMetadataCallback& callback);
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Reads contents of a directory at |url|.
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID ReadDirectory(const FileSystemURL& url,
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                            const ReadDirectoryCallback& callback);
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Removes a file or directory at |url|. If |recursive| is true, remove
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // all files and directories under the directory at |url| recursively.
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID Remove(const FileSystemURL& url, bool recursive,
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                     const StatusCallback& callback);
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Writes contents of |blob_url| to |url| at |offset|.
11158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // |url_request_context| is used to read contents in |blob|.
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID Write(const net::URLRequestContext* url_request_context,
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                    const FileSystemURL& url,
11403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                    scoped_ptr<storage::BlobDataHandle> blob,
115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                    int64 offset,
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                    const WriteCallback& callback);
117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Truncates a file at |url| to |length|. If |length| is larger than
119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // the original file size, the file will be extended, and the extended
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // part is filled with null bytes.
121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID Truncate(const FileSystemURL& url, int64 length,
122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                       const StatusCallback& callback);
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Tries to cancel the operation |id| [we support cancelling write or
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // truncate only]. Reports failure for the current operation, then reports
126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // success for the cancel operation itself via the |callback|.
127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void Cancel(OperationID id, const StatusCallback& callback);
128868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Modifies timestamps of a file or directory at |url| with
130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // |last_access_time| and |last_modified_time|. The function DOES NOT
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // create a file unlike 'touch' command on Linux.
132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This function is used only by Pepper as of writing.
134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID TouchFile(const FileSystemURL& url,
135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                        const base::Time& last_access_time,
136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                        const base::Time& last_modified_time,
137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                        const StatusCallback& callback);
138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Opens a file at |url| with |file_flags|, where flags are OR'ed
140868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // values of base::PlatformFileFlags.
141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // |peer_handle| is the process handle of a pepper plugin process, which
143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // is necessary for underlying IPC calls with Pepper plugins.
144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This function is used only by Pepper as of writing.
146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID OpenFile(const FileSystemURL& url,
147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                       int file_flags,
148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                       const OpenFileCallback& callback);
149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Creates a local snapshot file for a given |url| and returns the
151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // metadata and platform url of the snapshot file via |callback|.
152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // In local filesystem cases the implementation may simply return
153868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // the metadata of the file itself (as well as GetMetadata does),
154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // while in remote filesystem case the backend may want to download the file
155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // into a temporary snapshot file and return the metadata of the
156868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // temporary file.  Or if the implementaiton already has the local cache
157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // data for |url| it can simply return the url to the cache.
158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID CreateSnapshotFile(const FileSystemURL& url,
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                 const SnapshotFileCallback& callback);
160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Copies in a single file from a different filesystem.
162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This returns:
16446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if |src_file_path|
165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //   or the parent directory of |dest_url| does not exist.
16646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
167868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //   is not a file.
16846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_FAILED if |dest_url| does not exist and
169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //   its parent path is a file.
170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
171868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID CopyInForeignFile(const base::FilePath& src_local_disk_path,
172868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                const FileSystemURL& dest_url,
173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                const StatusCallback& callback);
174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
175868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Removes a single file.
176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
177868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This returns:
17846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if |url| does not exist.
17946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_NOT_A_FILE if |url| is not a file.
180868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
181868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID RemoveFile(const FileSystemURL& url,
182868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                         const StatusCallback& callback);
183868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
184868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Removes a single empty directory.
185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
186868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This returns:
18746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if |url| does not exist.
18846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_NOT_A_DIRECTORY if |url| is not a directory.
18946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_NOT_EMPTY if |url| is not empty.
190868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
191868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID RemoveDirectory(const FileSystemURL& url,
192868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                              const StatusCallback& callback);
193868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
194868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Copies a file from |src_url| to |dest_url|.
195868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This must be called for files that belong to the same filesystem
196868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // (i.e. type() and origin() of the |src_url| and |dest_url| must match).
19768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // For |option| and |progress_callback|, see file_system_operation.h for
19868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // details.
199868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
200868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This returns:
20146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if |src_url|
202868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //   or the parent directory of |dest_url| does not exist.
20346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file.
20446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
205868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //   is not a file.
20646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_FAILED if |dest_url| does not exist and
207868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //   its parent path is a file.
208868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
209868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID CopyFileLocal(const FileSystemURL& src_url,
210868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                            const FileSystemURL& dest_url,
21168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                            CopyOrMoveOption option,
21258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                            const CopyFileProgressCallback& progress_callback,
213868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                            const StatusCallback& callback);
214868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
215868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Moves a local file from |src_url| to |dest_url|.
216868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This must be called for files that belong to the same filesystem
217868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // (i.e. type() and origin() of the |src_url| and |dest_url| must match).
21868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // For |option|, see file_system_operation.h for details.
219868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
220868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This returns:
22146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_NOT_FOUND if |src_url|
222868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //   or the parent directory of |dest_url| does not exist.
22346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file.
22446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
225868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //   is not a file.
22646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // - File::FILE_ERROR_FAILED if |dest_url| does not exist and
227868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //   its parent path is a file.
228868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
229868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationID MoveFileLocal(const FileSystemURL& src_url,
230868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                            const FileSystemURL& dest_url,
23168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                            CopyOrMoveOption option,
232868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                            const StatusCallback& callback);
233868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // This is called only by pepper plugin as of writing to synchronously get
235868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // the underlying platform path to upload a file in the sandboxed filesystem
236868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // (e.g. TEMPORARY or PERSISTENT).
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::File::Error SyncGetPlatformPath(const FileSystemURL& url,
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                        base::FilePath* platform_path);
239868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
240868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
24158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  class BeginOperationScoper;
24258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
24358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  struct OperationHandle {
24458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    OperationID id;
24558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    base::WeakPtr<BeginOperationScoper> scope;
24658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
24758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    OperationHandle();
24858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ~OperationHandle();
24958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  };
25058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
251868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  friend class FileSystemContext;
252868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  explicit FileSystemOperationRunner(FileSystemContext* file_system_context);
253868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
25458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void DidFinish(const OperationHandle& handle,
255868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 const StatusCallback& callback,
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                 base::File::Error rv);
25758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void DidGetMetadata(const OperationHandle& handle,
258868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                      const GetMetadataCallback& callback,
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                      base::File::Error rv,
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                      const base::File::Info& file_info);
26158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void DidReadDirectory(const OperationHandle& handle,
262868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                        const ReadDirectoryCallback& callback,
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                        base::File::Error rv,
264868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                        const std::vector<DirectoryEntry>& entries,
265868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                        bool has_more);
26658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void DidWrite(const OperationHandle& handle,
267868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                const WriteCallback& callback,
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                base::File::Error rv,
269868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                int64 bytes,
270868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                bool complete);
271868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void DidOpenFile(
27258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const OperationHandle& handle,
273868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const OpenFileCallback& callback,
274cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      base::File file,
2750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      const base::Closure& on_close_callback);
276868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void DidCreateSnapshot(
27758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const OperationHandle& handle,
278868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const SnapshotFileCallback& callback,
2795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::File::Error rv,
2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const base::File::Info& file_info,
281868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const base::FilePath& platform_path,
28203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      const scoped_refptr<storage::ShareableFileReference>& file_ref);
283868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
284d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  void OnCopyProgress(
285d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      const OperationHandle& handle,
286d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      const CopyProgressCallback& callback,
287d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      FileSystemOperation::CopyProgressType type,
28868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      const FileSystemURL& source_url,
28968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      const FileSystemURL& dest_url,
290d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      int64 size);
291d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
292868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void PrepareForWrite(OperationID id, const FileSystemURL& url);
293868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void PrepareForRead(OperationID id, const FileSystemURL& url);
294868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
29558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // These must be called at the beginning and end of any async operations.
29658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  OperationHandle BeginOperation(FileSystemOperation* operation,
29758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                 base::WeakPtr<BeginOperationScoper> scope);
298868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void FinishOperation(OperationID id);
299868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
300868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Not owned; file_system_context owns this.
301868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  FileSystemContext* file_system_context_;
302868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
303868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // IDMap<FileSystemOperation, IDMapOwnPointer> operations_;
304868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  IDMap<FileSystemOperation, IDMapOwnPointer> operations_;
305868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
306868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // We keep track of the file to be modified by each operation so that
307868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // we can notify observers when we're done.
308868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  typedef std::map<OperationID, FileSystemURLSet> OperationToURLSet;
309868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  OperationToURLSet write_target_urls_;
310868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
31158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Operations that are finished but not yet fire their callbacks.
31258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::set<OperationID> finished_operations_;
31358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
31458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Callbacks for stray cancels whose target operation is already finished.
31558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::map<OperationID, StatusCallback> stray_cancel_callbacks_;
31658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
317868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(FileSystemOperationRunner);
318868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
319868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
32003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}  // namespace storage
321868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif  // STORAGE_BROWSER_FILEAPI_FILE_SYSTEM_OPERATION_RUNNER_H_
323