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_OPEN_FILE_OPERATION_H_
6#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_OPEN_FILE_OPERATION_H_
7
8#include <map>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "chrome/browser/chromeos/drive/file_errors.h"
15#include "chrome/browser/chromeos/drive/file_system_interface.h"
16
17namespace base {
18class FilePath;
19class ScopedClosureRunner;
20class SequencedTaskRunner;
21}  // namespace base
22
23namespace drive {
24
25class JobScheduler;
26class ResourceEntry;
27
28namespace internal {
29class ResourceMetadata;
30class FileCache;
31}  // namespace internal
32
33namespace file_system {
34
35class CreateFileOperation;
36class DownloadOperation;
37class OperationDelegate;
38
39class OpenFileOperation {
40 public:
41  OpenFileOperation(base::SequencedTaskRunner* blocking_task_runner,
42                    OperationDelegate* delegate,
43                    JobScheduler* scheduler,
44                    internal::ResourceMetadata* metadata,
45                    internal::FileCache* cache,
46                    const base::FilePath& temporary_file_directory);
47  ~OpenFileOperation();
48
49  // Opens the file at |file_path|.
50  // If the file is not actually downloaded, this method starts
51  // to download it to the cache, and then runs |callback| upon the
52  // completion with the path to the local cache file.
53  // See also the definition of OpenMode for its meaning.
54  // If |mime_type| is non empty and the file is created by this OpenFile()
55  // call, the mime type is used as the file's property.
56  // |callback| must not be null.
57  void OpenFile(const base::FilePath& file_path,
58                OpenMode open_mode,
59                const std::string& mime_type,
60                const OpenFileCallback& callback);
61
62 private:
63  // Part of OpenFile(). Called after file creation is completed.
64  void OpenFileAfterCreateFile(const base::FilePath& file_path,
65                               const OpenFileCallback& callback,
66                               FileError error);
67
68  // Part of OpenFile(). Called after file downloading is completed.
69  void OpenFileAfterFileDownloaded(const OpenFileCallback& callback,
70                                   FileError error,
71                                   const base::FilePath& local_file_path,
72                                   scoped_ptr<ResourceEntry> entry);
73
74  // Part of OpenFile(). Called after opening the cache file.
75  void OpenFileAfterOpenForWrite(
76      const base::FilePath& local_file_path,
77      const std::string& local_id,
78      const OpenFileCallback& callback,
79      scoped_ptr<base::ScopedClosureRunner>* file_closer,
80      FileError error);
81
82  // Closes the file with |local_id|.
83  void CloseFile(const std::string& local_id,
84                 scoped_ptr<base::ScopedClosureRunner> file_closer);
85
86  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
87  OperationDelegate* delegate_;
88  internal::FileCache* cache_;
89
90  scoped_ptr<CreateFileOperation> create_file_operation_;
91  scoped_ptr<DownloadOperation> download_operation_;
92
93  // The map from local id for an opened file to the number how many times
94  // the file is opened.
95  std::map<std::string, int> open_files_;
96
97  // Note: This should remain the last member so it'll be destroyed and
98  // invalidate its weak pointers before any other members are destroyed.
99  base::WeakPtrFactory<OpenFileOperation> weak_ptr_factory_;
100  DISALLOW_COPY_AND_ASSIGN(OpenFileOperation);
101};
102
103}  // namespace file_system
104}  // namespace drive
105
106#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_OPEN_FILE_OPERATION_H_
107