get_file_for_saving_operation.h revision f2477e01787aa58f445919b809d89e252beef54f
1// Copyright 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_GET_FILE_FOR_SAVING_OPERATION_H_
6#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_GET_FILE_FOR_SAVING_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/chromeos/drive/file_system_interface.h"
14
15namespace base {
16class FilePath;
17class SequencedTaskRunner;
18}  // namespace base
19
20namespace drive {
21namespace internal {
22class FileCache;
23class FileWriteWatcher;
24class ResourceMetadata;
25}  // namespace internal
26
27class JobScheduler;
28class ResourceEntry;
29
30namespace file_system {
31
32class CreateFileOperation;
33class DownloadOperation;
34class OperationObserver;
35
36// Implements GetFileForSaving() operation that prepares a local cache for
37// a Drive file whose next modification is monitored and notified to the
38// OperationObserver.
39// TODO(kinaba): crbug.com/269424: we might want to monitor all the changes
40// to the cache directory, not just the one immediately after the save dialog.
41class GetFileForSavingOperation {
42 public:
43  GetFileForSavingOperation(base::SequencedTaskRunner* blocking_task_runner,
44                            OperationObserver* observer,
45                            JobScheduler* scheduler,
46                            internal::ResourceMetadata* metadata,
47                            internal::FileCache* cache,
48                            const base::FilePath& temporary_file_directory);
49  ~GetFileForSavingOperation();
50
51  // Makes sure that |file_path| in the file system is available in the local
52  // cache, and marks it as dirty. The next modification to the cache file is
53  // watched and is automatically notified to the observer. If the entry is not
54  // present in the file system, it is created.
55  void GetFileForSaving(const base::FilePath& file_path,
56                        const GetFileCallback& callback);
57
58  internal::FileWriteWatcher* file_write_watcher_for_testing() {
59    return file_write_watcher_.get();
60  }
61
62 private:
63  void GetFileForSavingAfterCreate(const base::FilePath& file_path,
64                                   const GetFileCallback& callback,
65                                   FileError error);
66  void GetFileForSavingAfterDownload(const GetFileCallback& callback,
67                                     FileError error,
68                                     const base::FilePath& cache_path,
69                                     scoped_ptr<ResourceEntry> entry);
70  void GetFileForSavingAfterMarkDirty(const GetFileCallback& callback,
71                                      const base::FilePath& cache_path,
72                                      scoped_ptr<ResourceEntry> entry,
73                                      FileError error);
74  void GetFileForSavingAfterWatch(const GetFileCallback& callback,
75                                  const base::FilePath& cache_path,
76                                  scoped_ptr<ResourceEntry> entry,
77                                  bool success);
78  // Called when the cache file for |local_id| is written.
79  void OnWriteEvent(const std::string& local_id);
80
81  scoped_ptr<CreateFileOperation> create_file_operation_;
82  scoped_ptr<DownloadOperation> download_operation_;
83  scoped_ptr<internal::FileWriteWatcher> file_write_watcher_;
84  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
85  OperationObserver* observer_;
86  internal::ResourceMetadata* metadata_;
87  internal::FileCache* cache_;
88
89  // Note: This should remain the last member so it'll be destroyed and
90  // invalidate the weak pointers before any other members are destroyed.
91  base::WeakPtrFactory<GetFileForSavingOperation> weak_ptr_factory_;
92  DISALLOW_COPY_AND_ASSIGN(GetFileForSavingOperation);
93};
94
95}  // namespace file_system
96}  // namespace drive
97
98#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_GET_FILE_FOR_SAVING_OPERATION_H_
99