Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_manager / snapshot_manager.h
1 // Copyright 2014 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_FILE_MANAGER_SNAPSHOT_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_MANAGER_SNAPSHOT_MANAGER_H_
7
8 #include <deque>
9
10 #include "base/callback_forward.h"
11 #include "base/files/file.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14
15 class Profile;
16
17 namespace base {
18 class FilePath;
19 }  // namespace base
20
21 namespace fileapi {
22 class FileSystemURL;
23 }  // namespace fileapi
24
25 namespace webkit_blob {
26 class ShareableFileReference;
27 }  // namespace webkit_blob
28
29 namespace file_manager {
30
31 // Utility class for creating a snapshot of a file system file on local disk.
32 // The class wraps the underlying implementation of fileapi's CreateSnapshotFile
33 // and prolongs the lifetime of snapshot files so that the client code that just
34 // accepts file paths works without problems.
35 class SnapshotManager {
36  public:
37   // The callback type for CreateManagedSnapshot.
38   typedef base::Callback<void(const base::FilePath&)> LocalPathCallback;
39
40   explicit SnapshotManager(Profile* profile);
41   ~SnapshotManager();
42
43   // Creates a snapshot file copy of a file system file |absolute_file_path| and
44   // returns back to |callback|. Returns empty path for failure.
45   void CreateManagedSnapshot(const base::FilePath& absolute_file_path,
46                              const LocalPathCallback& callback);
47
48   // Struct for keeping the snapshot file reference with its file size used for
49   // computing the necessity of clean up.
50   struct FileReferenceWithSizeInfo {
51     FileReferenceWithSizeInfo(
52         scoped_refptr<webkit_blob::ShareableFileReference> ref,
53         int64 size);
54     ~FileReferenceWithSizeInfo();
55     scoped_refptr<webkit_blob::ShareableFileReference> file_ref;
56     int64 file_size;
57   };
58
59  private:
60   // Part of CreateManagedSnapshot.
61   void CreateManagedSnapshotAfterSpaceComputed(
62       const fileapi::FileSystemURL& filesystem_url,
63       const LocalPathCallback& callback,
64       int64 needed_space);
65
66   // Part of CreateManagedSnapshot.
67   void OnCreateSnapshotFile(
68       const LocalPathCallback& callback,
69       base::File::Error result,
70       const base::File::Info& file_info,
71       const base::FilePath& platform_path,
72       const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref);
73
74   Profile* profile_;
75   std::deque<FileReferenceWithSizeInfo> file_refs_;
76
77   // Note: This should remain the last member so it'll be destroyed and
78   // invalidate the weak pointers before any other members are destroyed.
79   base::WeakPtrFactory<SnapshotManager> weak_ptr_factory_;
80   DISALLOW_COPY_AND_ASSIGN(SnapshotManager);
81 };
82
83 }  // namespace file_manager
84
85 #endif  // CHROME_BROWSER_CHROMEOS_FILE_MANAGER_SNAPSHOT_MANAGER_H_