blob: 7055eea05d0520c725ed3ec8c10258183f839ad8 [file] [log] [blame]
Ben Murdoch7dbb3d52013-07-17 14:55:54 +01001// 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 SequencedTaskRunner;
20} // namespace base
21
22namespace drive {
23
24class JobScheduler;
25class ResourceEntry;
26
27namespace internal {
28class ResourceMetadata;
29class FileCache;
30} // namespace internal
31
32namespace file_system {
33
34class CreateFileOperation;
35class DownloadOperation;
36class OperationObserver;
37
38class OpenFileOperation {
39 public:
40 OpenFileOperation(base::SequencedTaskRunner* blocking_task_runner,
41 OperationObserver* observer,
42 JobScheduler* scheduler,
43 internal::ResourceMetadata* metadata,
44 internal::FileCache* cache,
Ben Murdocha3f7b4e2013-07-24 10:36:34 +010045 const base::FilePath& temporary_file_directory);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010046 ~OpenFileOperation();
47
48 // Opens the file at |file_path|.
49 // If the file is not actually downloaded, this method starts
50 // to download it to the cache, and then runs |callback| upon the
51 // completation with the path to the local cache file.
52 // See also the definition of OpenMode for its meaning.
53 // |callback| must not be null.
54 void OpenFile(const base::FilePath& file_path,
55 OpenMode open_mode,
56 const OpenFileCallback& callback);
57
58 private:
59 // Part of OpenFile(). Called after file creation is completed.
60 void OpenFileAfterCreateFile(const base::FilePath& file_path,
61 const OpenFileCallback& callback,
62 FileError error);
63
64 // Part of OpenFile(). Called after file downloading is completed.
Ben Murdocha3f7b4e2013-07-24 10:36:34 +010065 void OpenFileAfterFileDownloaded(const OpenFileCallback& callback,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010066 FileError error,
67 const base::FilePath& local_file_path,
68 scoped_ptr<ResourceEntry> entry);
69
Ben Murdochbb1529c2013-08-08 10:24:53 +010070 // Part of OpenFile(). Called after marking the cache file dirty.
71 void OpenFileAfterMarkDirty(const base::FilePath& local_file_path,
72 const std::string& resource_id,
73 const OpenFileCallback& callback,
74 FileError error);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010075
Ben Murdocha3f7b4e2013-07-24 10:36:34 +010076 // Closes the file with |resource_id|.
77 void CloseFile(const std::string& resource_id);
78
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010079 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
Ben Murdocha3f7b4e2013-07-24 10:36:34 +010080 OperationObserver* observer_;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010081 internal::FileCache* cache_;
82
83 scoped_ptr<CreateFileOperation> create_file_operation_;
84 scoped_ptr<DownloadOperation> download_operation_;
85
Ben Murdocha3f7b4e2013-07-24 10:36:34 +010086 // The map from resource id for an opened file to the number how many times
87 // the file is opened.
88 std::map<std::string, int> open_files_;
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010089
90 // Note: This should remain the last member so it'll be destroyed and
91 // invalidate its weak pointers before any other members are destroyed.
92 base::WeakPtrFactory<OpenFileOperation> weak_ptr_factory_;
93 DISALLOW_COPY_AND_ASSIGN(OpenFileOperation);
94};
95
96} // namespace file_system
97} // namespace drive
98
99#endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_OPEN_FILE_OPERATION_H_