summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgab@chromium.org <gab@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-05-09 11:57:31 +0000
committergab@chromium.org <gab@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-05-09 11:57:31 +0000
commit16d3e3de030b8f8fd15056f8eacab657ea5be73d (patch)
tree3d594836da8bd7acdcda713e9853e013748a3bd3
parentd24692452eaf11b69a3c93d3002261e70d4c6ac6 (diff)
downloadchromium_src-16d3e3de030b8f8fd15056f8eacab657ea5be73d.zip
chromium_src-16d3e3de030b8f8fd15056f8eacab657ea5be73d.tar.gz
chromium_src-16d3e3de030b8f8fd15056f8eacab657ea5be73d.tar.bz2
Allow MakeCriticalClosure to enclose closures with non-void return types.
This is required in order to allow ImportantFileWriter::WriteFileAtomically() to report its bool result to the calling thread via PostTaskAndReplyWithResult() in https://codereview.chromium.org/257003007/ BUG=365769 R=stuartmorgan@chromium.org, thakis@chromium.org Review URL: https://codereview.chromium.org/274773003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269238 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/BUILD.gn2
-rw-r--r--base/base.gypi4
-rw-r--r--base/critical_closure.h53
-rw-r--r--base/critical_closure_internal_ios.mm17
-rw-r--r--base/critical_closure_ios.mm49
5 files changed, 68 insertions, 57 deletions
diff --git a/base/BUILD.gn b/base/BUILD.gn
index 3d07813..28967db 100644
--- a/base/BUILD.gn
+++ b/base/BUILD.gn
@@ -111,7 +111,7 @@ component("base") {
"cpu.cc",
"cpu.h",
"critical_closure.h",
- "critical_closure_ios.mm",
+ "critical_closure_internal_ios.mm",
"debug/alias.cc",
"debug/alias.h",
"debug/crash_logging.cc",
diff --git a/base/base.gypi b/base/base.gypi
index 8110287..fbe46b7 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -125,7 +125,7 @@
'cpu.cc',
'cpu.h',
'critical_closure.h',
- 'critical_closure_ios.mm',
+ 'critical_closure_internal_ios.mm',
'debug/alias.cc',
'debug/alias.h',
'debug/crash_logging.cc',
@@ -137,7 +137,7 @@
'debug/dump_without_crashing.cc',
'debug/dump_without_crashing.h',
'debug/gdi_debug_util_win.cc',
- 'debug/gdi_debug_util_win.h',
+ 'debug/gdi_debug_util_win.h',
# This file depends on files from the 'allocator' target,
# but this target does not depend on 'allocator' (see
# allocator.gyp for details).
diff --git a/base/critical_closure.h b/base/critical_closure.h
index ca51ed5..ac07911 100644
--- a/base/critical_closure.h
+++ b/base/critical_closure.h
@@ -7,9 +7,46 @@
#include "base/callback.h"
+#if defined(OS_IOS)
+#include "base/bind.h"
+#include "base/ios/scoped_critical_action.h"
+#endif
+
namespace base {
-// Returns a closure that will continue to run for a period of time when the
+namespace internal {
+
+#if defined(OS_IOS)
+// Returns true if multi-tasking is supported on this iOS device.
+bool IsMultiTaskingSupported();
+
+// This class wraps a closure so it can continue to run for a period of time
+// when the application goes to the background by using
+// |ios::ScopedCriticalAction|.
+template <typename R>
+class CriticalClosure {
+ public:
+ explicit CriticalClosure(const Callback<R(void)>& closure)
+ : closure_(closure) {}
+
+ ~CriticalClosure() {}
+
+ R Run() {
+ return closure_.Run();
+ }
+
+ private:
+ ios::ScopedCriticalAction critical_action_;
+ Callback<R(void)> closure_;
+
+ DISALLOW_COPY_AND_ASSIGN(CriticalClosure);
+};
+#endif // defined(OS_IOS)
+
+} // namespace internal
+
+// Returns a closure (which may return a result, but must not require any extra
+// arguments) that will continue to run for a period of time when the
// application goes to the background if possible on platforms where
// applications don't execute while backgrounded, otherwise the original task is
// returned.
@@ -23,14 +60,20 @@ namespace base {
// background running time, |MakeCriticalClosure| should be applied on them
// before posting.
#if defined(OS_IOS)
-base::Closure MakeCriticalClosure(const base::Closure& closure);
-#else
-inline base::Closure MakeCriticalClosure(const base::Closure& closure) {
+template <typename R>
+Callback<R(void)> MakeCriticalClosure(const Callback<R(void)>& closure) {
+ DCHECK(internal::IsMultiTaskingSupported());
+ return base::Bind(&internal::CriticalClosure<R>::Run,
+ Owned(new internal::CriticalClosure<R>(closure)));
+}
+#else // defined(OS_IOS)
+template <typename R>
+inline Callback<R(void)> MakeCriticalClosure(const Callback<R(void)>& closure) {
// No-op for platforms where the application does not need to acquire
// background time for closures to finish when it goes into the background.
return closure;
}
-#endif // !defined(OS_IOS)
+#endif // defined(OS_IOS)
} // namespace base
diff --git a/base/critical_closure_internal_ios.mm b/base/critical_closure_internal_ios.mm
new file mode 100644
index 0000000..b8fec14
--- /dev/null
+++ b/base/critical_closure_internal_ios.mm
@@ -0,0 +1,17 @@
+// Copyright 2014 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 "base/critical_closure.h"
+
+#import <UIKit/UIKit.h>
+
+namespace base {
+namespace internal {
+
+bool IsMultiTaskingSupported() {
+ return [[UIDevice currentDevice] isMultitaskingSupported];
+}
+
+} // namespace internal
+} // namespace base
diff --git a/base/critical_closure_ios.mm b/base/critical_closure_ios.mm
deleted file mode 100644
index d605cad..0000000
--- a/base/critical_closure_ios.mm
+++ /dev/null
@@ -1,49 +0,0 @@
-// Copyright (c) 2012 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 "base/critical_closure.h"
-
-#import <UIKit/UIKit.h>
-
-#include "base/bind.h"
-#include "base/ios/scoped_critical_action.h"
-#include "base/memory/ref_counted.h"
-
-namespace {
-
-// This class wraps a closure so it can continue to run for a period of time
-// when the application goes to the background by using
-// |base::ios::ScopedCriticalAction|.
-class CriticalClosure : public base::RefCountedThreadSafe<CriticalClosure> {
- public:
- explicit CriticalClosure(base::Closure* closure) : closure_(closure) {
- }
-
- void Run() {
- closure_->Run();
- }
-
- private:
- friend class base::RefCountedThreadSafe<CriticalClosure>;
-
- virtual ~CriticalClosure() {}
-
- base::ios::ScopedCriticalAction criticial_action_;
- scoped_ptr<base::Closure> closure_;
-
- DISALLOW_COPY_AND_ASSIGN(CriticalClosure);
-};
-
-} // namespace
-
-namespace base {
-
-base::Closure MakeCriticalClosure(const base::Closure& closure) {
- DCHECK([[UIDevice currentDevice] isMultitaskingSupported]);
- scoped_refptr<CriticalClosure> critical_closure(
- new CriticalClosure(new base::Closure(closure)));
- return base::Bind(&CriticalClosure::Run, critical_closure.get());
-}
-
-} // namespace base