1 // Copyright (c) 2012 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.
5 #ifndef STORAGE_BROWSER_BLOB_SHAREABLE_FILE_REFERENCE_H_
6 #define STORAGE_BROWSER_BLOB_SHAREABLE_FILE_REFERENCE_H_
10 #include "storage/browser/blob/scoped_file.h"
11 #include "storage/browser/storage_browser_export.h"
15 // ShareableFileReference allows consumers to share FileReference for the
16 // same path if it already exists in its internal map.
17 // This class is non-thread-safe and all methods must be called on a single
19 class STORAGE_EXPORT ShareableFileReference
20 : public base::RefCounted
<ShareableFileReference
> {
22 typedef ScopedFile::ScopeOutCallback FinalReleaseCallback
;
24 enum FinalReleasePolicy
{
25 DELETE_ON_FINAL_RELEASE
= ScopedFile::DELETE_ON_SCOPE_OUT
,
26 DONT_DELETE_ON_FINAL_RELEASE
= ScopedFile::DONT_DELETE_ON_SCOPE_OUT
,
29 // Returns a ShareableFileReference for the given path, if no reference
30 // for this path exists returns NULL.
31 static scoped_refptr
<ShareableFileReference
> Get(const base::FilePath
& path
);
33 // Returns a ShareableFileReference for the given path, creating a new
34 // reference if none yet exists. If there's a pre-existing reference for
35 // the path, the policy parameter of this method is ignored.
36 static scoped_refptr
<ShareableFileReference
> GetOrCreate(
37 const base::FilePath
& path
,
38 FinalReleasePolicy policy
,
39 base::TaskRunner
* file_task_runner
);
41 // Returns a ShareableFileReference for the given path of the |scoped_file|,
42 // creating a new reference if none yet exists. The ownership of |scoped_file|
43 // is passed to this reference.
44 // If there's a pre-existing reference for the path, the scope out policy
45 // and scope-out-callbacks of the given |scoped_file| is ignored.
46 // If the given scoped_file has an empty path (e.g. maybe already
47 // released) this returns NULL reference.
49 // TODO(kinuko): Make sure if this behavior is ok, we could alternatively
50 // merge callbacks to the existing one.
51 static scoped_refptr
<ShareableFileReference
> GetOrCreate(
52 ScopedFile scoped_file
);
54 // The full file path.
55 const base::FilePath
& path() const { return scoped_file_
.path(); }
57 // The |callback| is fired when the final reference of this instance
58 // is released. If release policy is DELETE_ON_FINAL_RELEASE the
59 // callback task(s) is/are posted before the deletion is scheduled.
60 void AddFinalReleaseCallback(const FinalReleaseCallback
& callback
);
63 friend class base::RefCounted
<ShareableFileReference
>;
65 ShareableFileReference(ScopedFile scoped_file
);
66 ~ShareableFileReference();
68 ScopedFile scoped_file_
;
70 DISALLOW_COPY_AND_ASSIGN(ShareableFileReference
);
73 } // namespace storage
75 #endif // STORAGE_BROWSER_BLOB_SHAREABLE_FILE_REFERENCE_H_