| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef WEBKIT_BLOB_SHAREABLE_FILE_REFERENCE_H_ | 5 #ifndef WEBKIT_BLOB_SHAREABLE_FILE_REFERENCE_H_ |
| 6 #define WEBKIT_BLOB_SHAREABLE_FILE_REFERENCE_H_ | 6 #define WEBKIT_BLOB_SHAREABLE_FILE_REFERENCE_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "webkit/blob/scoped_file.h" |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "webkit/storage/webkit_storage_export.h" | |
| 14 | |
| 15 namespace base { | |
| 16 class TaskRunner; | |
| 17 } | |
| 18 | 11 |
| 19 namespace webkit_blob { | 12 namespace webkit_blob { |
| 20 | 13 |
| 21 // A refcounted wrapper around a FilePath that can optionally schedule | 14 // ShareableFileReference allows consumers to share FileReference for the |
| 22 // the file to be deleted upon final release and/or to notify a consumer | 15 // same path if it already exists in its internal map. |
| 23 // when final release occurs. This class is single-threaded and should | 16 // This class is non-thread-safe and all methods must be called on a single |
| 24 // only be invoked on the IO thread in chrome. | 17 // thread. |
| 25 class WEBKIT_STORAGE_EXPORT ShareableFileReference | 18 class WEBKIT_STORAGE_EXPORT ShareableFileReference |
| 26 : public base::RefCounted<ShareableFileReference> { | 19 : public base::RefCounted<ShareableFileReference> { |
| 27 public: | 20 public: |
| 28 typedef base::Callback<void(const base::FilePath&)> FinalReleaseCallback; | 21 typedef ScopedFile::ScopeOutCallback FinalReleaseCallback; |
| 29 | 22 |
| 30 enum FinalReleasePolicy { | 23 enum FinalReleasePolicy { |
| 31 DELETE_ON_FINAL_RELEASE, | 24 DELETE_ON_FINAL_RELEASE = ScopedFile::DELETE_ON_SCOPE_OUT, |
| 32 DONT_DELETE_ON_FINAL_RELEASE, | 25 DONT_DELETE_ON_FINAL_RELEASE = ScopedFile::DONT_DELETE_ON_SCOPE_OUT, |
| 33 }; | 26 }; |
| 34 | 27 |
| 35 // Returns a ShareableFileReference for the given path, if no reference | 28 // Returns a ShareableFileReference for the given path, if no reference |
| 36 // for this path exists returns NULL. | 29 // for this path exists returns NULL. |
| 37 static scoped_refptr<ShareableFileReference> Get(const base::FilePath& path); | 30 static scoped_refptr<ShareableFileReference> Get(const base::FilePath& path); |
| 38 | 31 |
| 39 // Returns a ShareableFileReference for the given path, creating a new | 32 // Returns a ShareableFileReference for the given path, creating a new |
| 40 // reference if none yet exists. If there's a pre-existing reference for | 33 // reference if none yet exists. If there's a pre-existing reference for |
| 41 // the path, the deletable parameter of this method is ignored. | 34 // the path, the policy parameter of this method is ignored. |
| 42 static scoped_refptr<ShareableFileReference> GetOrCreate( | 35 static scoped_refptr<ShareableFileReference> GetOrCreate( |
| 43 const base::FilePath& path, | 36 const base::FilePath& path, |
| 44 FinalReleasePolicy policy, | 37 FinalReleasePolicy policy, |
| 45 base::TaskRunner* file_task_runner); | 38 base::TaskRunner* file_task_runner); |
| 46 | 39 |
| 40 // Returns a ShareableFileReference for the given path of the |scoped_file|, |
| 41 // creating a new reference if none yet exists. The ownership of |scoped_file| |
| 42 // is passed to this reference. |
| 43 // If there's a pre-existing reference for the path, the scope out policy |
| 44 // and scope-out-callbacks of the given |scoped_file| is ignored. |
| 45 // If the given scoped_file has an empty path (e.g. maybe already |
| 46 // released) this returns NULL reference. |
| 47 // |
| 48 // TODO(kinuko): Make sure if this behavior is ok, we could alternatively |
| 49 // merge callbacks to the existing one. |
| 50 static scoped_refptr<ShareableFileReference> GetOrCreate( |
| 51 ScopedFile scoped_file); |
| 52 |
| 47 // The full file path. | 53 // The full file path. |
| 48 const base::FilePath& path() const { return path_; } | 54 const base::FilePath& path() const { return scoped_file_.path(); } |
| 49 | 55 |
| 50 // Whether it's to be deleted on final release. | 56 // The |callback| is fired when the final reference of this instance |
| 51 FinalReleasePolicy final_release_policy() const { | 57 // is released. If release policy is DELETE_ON_FINAL_RELEASE the |
| 52 return final_release_policy_; | 58 // callback task(s) is/are posted before the deletion is scheduled. |
| 53 } | |
| 54 | |
| 55 void AddFinalReleaseCallback(const FinalReleaseCallback& callback); | 59 void AddFinalReleaseCallback(const FinalReleaseCallback& callback); |
| 56 | 60 |
| 57 private: | 61 private: |
| 58 friend class base::RefCounted<ShareableFileReference>; | 62 friend class base::RefCounted<ShareableFileReference>; |
| 59 | 63 |
| 60 ShareableFileReference( | 64 ShareableFileReference(ScopedFile scoped_file); |
| 61 const base::FilePath& path, | |
| 62 FinalReleasePolicy policy, | |
| 63 base::TaskRunner* file_task_runner); | |
| 64 ~ShareableFileReference(); | 65 ~ShareableFileReference(); |
| 65 | 66 |
| 66 const base::FilePath path_; | 67 ScopedFile scoped_file_; |
| 67 const FinalReleasePolicy final_release_policy_; | |
| 68 const scoped_refptr<base::TaskRunner> file_task_runner_; | |
| 69 std::vector<FinalReleaseCallback> final_release_callbacks_; | |
| 70 | 68 |
| 71 DISALLOW_COPY_AND_ASSIGN(ShareableFileReference); | 69 DISALLOW_COPY_AND_ASSIGN(ShareableFileReference); |
| 72 }; | 70 }; |
| 73 | 71 |
| 74 } // namespace webkit_blob | 72 } // namespace webkit_blob |
| 75 | 73 |
| 76 #endif // WEBKIT_BLOB_SHAREABLE_FILE_REFERENCE_H_ | 74 #endif // WEBKIT_BLOB_SHAREABLE_FILE_REFERENCE_H_ |
| OLD | NEW |