Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(319)

Unified Diff: webkit/blob/scoped_file.cc

Issue 14261015: Add ScopedFile class which supports scope-out deletion and/or callbacks (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: webkit/blob/scoped_file.cc
diff --git a/webkit/blob/scoped_file.cc b/webkit/blob/scoped_file.cc
new file mode 100644
index 0000000000000000000000000000000000000000..af7dcc9f819fdc8b1b265ff6da42f169d5a4030e
--- /dev/null
+++ b/webkit/blob/scoped_file.cc
@@ -0,0 +1,72 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "webkit/blob/scoped_file.h"
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/files/file_util_proxy.h"
+#include "base/location.h"
+#include "base/message_loop/message_loop_proxy.h"
+#include "base/task_runner.h"
+
+namespace webkit_blob {
+
+ScopedFile::ScopedFile() {
+}
+
+ScopedFile::ScopedFile(
+ const base::FilePath& path, ScopeOutPolicy policy,
+ base::TaskRunner* file_task_runner)
+ : path_(path),
+ scope_out_policy_(policy),
+ file_task_runner_(file_task_runner) {
+}
+
+ScopedFile::ScopedFile(RValue other) {
+ Swap(*other.object);
+}
+
+ScopedFile::~ScopedFile() {
+ ScopeOut();
+}
+
+void ScopedFile::AddScopeOutCallback(
+ const ScopeOutCallback& callback,
+ base::TaskRunner* callback_runner) {
+ if (!callback_runner)
+ callback_runner = base::MessageLoopProxy::current();
+ scope_out_callbacks_.push_back(std::make_pair(callback, callback_runner));
+}
+
+base::FilePath ScopedFile::Release() {
+ base::FilePath path = path_;
+ path_.clear();
+ return path;
+}
+
+void ScopedFile::Swap(ScopedFile& other) {
+ path_ = other.Release();
+ scope_out_policy_ = other.scope_out_policy_;
+ scope_out_callbacks_.swap(other.scope_out_callbacks_);
+ file_task_runner_ = other.file_task_runner_;
+}
+
+void ScopedFile::ScopeOut() {
+ if (path_.empty())
+ return;
+
+ for (ScopeOutCallbackList::iterator iter = scope_out_callbacks_.begin();
+ iter != scope_out_callbacks_.end(); ++iter) {
+ iter->second->PostTask(FROM_HERE, base::Bind(iter->first, path_));
michaeln 2013/04/22 21:07:03 There's a subtle behavior change here (not sure it
kinuko 2013/04/23 06:31:29 Right... hope this has no regression (otherwise we
+ }
+
+ if (scope_out_policy_ == DELETE_ON_SCOPE_OUT) {
+ base::FileUtilProxy::Delete(
+ file_task_runner_, path_, false /* recursive */,
+ base::FileUtilProxy::StatusCallback());
+ }
+}
+
+} // namespace webkit_blob

Powered by Google App Engine
This is Rietveld 408576698