Skip to content

Commit

Permalink
Move ScopedClosureRunner to callback_helpers, add Reset.
Browse files Browse the repository at this point in the history
BUG=none
TEST=none
TBR=ben@chromium.org

Review URL: https://chromiumcodereview.appspot.com/23514018

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@220473 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
avi@chromium.org committed Aug 30, 2013
1 parent 85267b1 commit 1c232c2
Show file tree
Hide file tree
Showing 30 changed files with 137 additions and 73 deletions.
2 changes: 1 addition & 1 deletion base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,11 @@
'atomicops_unittest.cc',
'barrier_closure_unittest.cc',
'base64_unittest.cc',
'bind_helpers_unittest.cc',
'bind_unittest.cc',
'bind_unittest.nc',
'bits_unittest.cc',
'build_time_unittest.cc',
'callback_helpers_unittest.cc',
'callback_unittest.cc',
'callback_unittest.nc',
'cancelable_callback_unittest.cc',
Expand Down
1 change: 1 addition & 0 deletions base/base.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
'build_time.cc',
'build_time.h',
'callback.h',
'callback_helpers.cc',
'callback_helpers.h',
'callback_internal.cc',
'callback_internal.h',
Expand Down
15 changes: 0 additions & 15 deletions base/bind_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,4 @@ namespace base {
void DoNothing() {
}

ScopedClosureRunner::ScopedClosureRunner(const Closure& closure)
: closure_(closure) {
}

ScopedClosureRunner::~ScopedClosureRunner() {
if (!closure_.is_null())
closure_.Run();
}

Closure ScopedClosureRunner::Release() {
Closure result = closure_;
closure_.Reset();
return result;
}

} // namespace base
19 changes: 0 additions & 19 deletions base/bind_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,6 @@
// pointer when invoked. Only use this when necessary.
// In most cases MessageLoop::DeleteSoon() is a better
// fit.
// ScopedClosureRunner - Scoper object that runs the wrapped closure when it
// goes out of scope. It's conceptually similar to
// scoped_ptr<> but calls Run() instead of deleting
// the pointer.

#ifndef BASE_BIND_HELPERS_H_
#define BASE_BIND_HELPERS_H_
Expand Down Expand Up @@ -543,21 +539,6 @@ void DeletePointer(T* obj) {
delete obj;
}

// ScopedClosureRunner is akin to scoped_ptr for Closures. It ensures that the
// Closure is executed and deleted no matter how the current scope exits.
class BASE_EXPORT ScopedClosureRunner {
public:
explicit ScopedClosureRunner(const Closure& closure);
~ScopedClosureRunner();

Closure Release();

private:
Closure closure_;

DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedClosureRunner);
};

} // namespace base

#endif // BASE_BIND_HELPERS_H_
42 changes: 42 additions & 0 deletions base/callback_helpers.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 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 "base/callback_helpers.h"

#include "base/callback.h"

namespace base {

ScopedClosureRunner::ScopedClosureRunner() {
}

ScopedClosureRunner::ScopedClosureRunner(const Closure& closure)
: closure_(closure) {
}

ScopedClosureRunner::~ScopedClosureRunner() {
if (!closure_.is_null())
closure_.Run();
}

void ScopedClosureRunner::Reset() {
Closure old_closure = Release();
if (!old_closure.is_null())
old_closure.Run();
}

void ScopedClosureRunner::Reset(const Closure& closure) {
Closure old_closure = Release();
closure_ = closure;
if (!old_closure.is_null())
old_closure.Run();
}

Closure ScopedClosureRunner::Release() {
Closure result = closure_;
closure_.Reset();
return result;
}

} // namespace base
20 changes: 20 additions & 0 deletions base/callback_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#ifndef BASE_CALLBACK_HELPERS_H_
#define BASE_CALLBACK_HELPERS_H_

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/compiler_specific.h"

namespace base {

Expand All @@ -25,6 +27,24 @@ base::Callback<Sig> ResetAndReturn(base::Callback<Sig>* cb) {
return ret;
}

// ScopedClosureRunner is akin to scoped_ptr for Closures. It ensures that the
// Closure is executed and deleted no matter how the current scope exits.
class BASE_EXPORT ScopedClosureRunner {
public:
ScopedClosureRunner();
explicit ScopedClosureRunner(const Closure& closure);
~ScopedClosureRunner();

void Reset();
void Reset(const Closure& closure);
Closure Release() WARN_UNUSED_RESULT;

private:
Closure closure_;

DISALLOW_COPY_AND_ASSIGN(ScopedClosureRunner);
};

} // namespace base

#endif // BASE_CALLBACK_HELPERS_H_
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Copyright 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 "base/bind_helpers.h"
#include "base/callback_helpers.h"

#include "base/callback.h"
#include "base/bind.h"
#include "base/callback.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {
Expand Down Expand Up @@ -36,4 +36,26 @@ TEST(BindHelpersTest, TestScopedClosureRunnerRelease) {
EXPECT_EQ(1, run_count);
}

TEST(BindHelpersTest, TestScopedClosureRunnerReset) {
int run_count_1 = 0;
int run_count_2 = 0;
{
base::ScopedClosureRunner runner;
runner.Reset(base::Bind(&Increment, &run_count_1));
runner.Reset(base::Bind(&Increment, &run_count_2));
EXPECT_EQ(1, run_count_1);
EXPECT_EQ(0, run_count_2);
}
EXPECT_EQ(1, run_count_2);

int run_count_3 = 0;
{
base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count_3));
EXPECT_EQ(0, run_count_3);
runner.Reset();
EXPECT_EQ(1, run_count_3);
}
EXPECT_EQ(1, run_count_3);
}

} // namespace
2 changes: 1 addition & 1 deletion base/mac/bind_objc_block_unittest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "base/callback.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {
Expand Down
2 changes: 1 addition & 1 deletion base/prefs/pref_member.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include "base/prefs/pref_member.h"

#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/prefs/pref_service.h"
#include "base/value_conversions.h"
Expand Down
1 change: 1 addition & 0 deletions base/test/unit_test_launcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "base/test/unit_test_launcher.h"

#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/files/scoped_temp_dir.h"
Expand Down
5 changes: 3 additions & 2 deletions chrome/browser/ui/android/ssl_client_certificate_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/compiler_specific.h"
Expand Down Expand Up @@ -115,7 +116,7 @@ void StartClientCertificateRequest(
return;
}

guard.Release();
ignore_result(guard.Release());

// Ownership was transferred to Java.
chrome::SelectCertificateCallback* ALLOW_UNUSED dummy =
Expand Down Expand Up @@ -187,7 +188,7 @@ static void OnSystemRequestCompletion(
return;
}

guard.Release();
ignore_result(guard.Release());

// RecordClientCertificateKey() must be called on the I/O thread,
// before the callback is called with the selected certificate on
Expand Down
2 changes: 1 addition & 1 deletion chrome/common/cancelable_task_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <utility>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback_helpers.h"
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
Expand Down
1 change: 1 addition & 0 deletions chromeos/dbus/blocking_method_caller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "chromeos/dbus/blocking_method_caller.h"

#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/threading/thread_restrictions.h"
#include "dbus/bus.h"
Expand Down
1 change: 1 addition & 0 deletions cloud_print/service/win/cloud_print_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "base/at_exit.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/guid.h"
Expand Down
1 change: 1 addition & 0 deletions cloud_print/service/win/cloud_print_service_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "base/at_exit.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/message_loop/message_loop.h"
Expand Down
11 changes: 6 additions & 5 deletions content/browser/gpu/gpu_process_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

#include "base/base64.h"
#include "base/base_switches.h"
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback_helpers.h"
#include "base/command_line.h"
#include "base/debug/trace_event.h"
#include "base/logging.h"
Expand Down Expand Up @@ -900,7 +901,7 @@ void GpuProcessHost::OnAcceleratedSurfaceBuffersSwapped(
// if the browser is waiting for a new frame. Otherwise the RenderWidgetHelper
// will forward to the RenderWidgetHostView via RenderProcessHostImpl and
// RenderWidgetHostImpl.
scoped_completion_runner.Release();
ignore_result(scoped_completion_runner.Release());

ViewHostMsg_CompositorSurfaceBuffersSwapped_Params view_params;
view_params.surface_id = params.surface_id;
Expand Down Expand Up @@ -936,7 +937,7 @@ void GpuProcessHost::OnAcceleratedSurfaceBuffersSwapped(
TRACE_EVENT1("gpu", "SurfaceIDNotFound_RoutingToUI",
"surface_id", params.surface_id);
// This is a content area swap, send it on to the UI thread.
scoped_completion_runner.Release();
ignore_result(scoped_completion_runner.Release());
RouteOnUIThread(GpuHostMsg_AcceleratedSurfaceBuffersSwapped(params));
return;
}
Expand All @@ -950,7 +951,7 @@ void GpuProcessHost::OnAcceleratedSurfaceBuffersSwapped(
"EarlyOut_NativeWindowNotFound",
"handle",
handle.handle);
scoped_completion_runner.Release();
ignore_result(scoped_completion_runner.Release());
AcceleratedSurfaceBuffersSwappedCompleted(host_id_,
params.route_id,
params.surface_id,
Expand All @@ -961,7 +962,7 @@ void GpuProcessHost::OnAcceleratedSurfaceBuffersSwapped(
return;
}

scoped_completion_runner.Release();
ignore_result(scoped_completion_runner.Release());
presenter->AsyncPresentAndAcknowledge(
params.size,
params.surface_handle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback_forward.h"
#include "base/callback_helpers.h"
#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
Expand Down Expand Up @@ -693,7 +693,7 @@ void RenderVideoFrame(const SkBitmap& input,
}

// The result is now ready.
failure_handler.Release();
ignore_result(failure_handler.Release());
done_cb.Run(true);
}

Expand Down
13 changes: 7 additions & 6 deletions content/browser/renderer_host/render_widget_host_view_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

#include <android/bitmap.h>

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback_helpers.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
Expand Down Expand Up @@ -420,9 +421,9 @@ void RenderWidgetHostViewAndroid::OnTextInputStateChanged(
// If an acknowledgement is required for this event, regardless of how we exit
// from this method, we must acknowledge that we processed the input state
// change.
base::ScopedClosureRunner ack_caller(base::Bind(&SendImeEventAck, host_));
if (!params.require_ack)
ack_caller.Release();
base::ScopedClosureRunner ack_caller;
if (params.require_ack)
ack_caller.Reset(base::Bind(&SendImeEventAck, host_));

if (!IsShowing())
return;
Expand Down Expand Up @@ -1228,7 +1229,7 @@ void RenderWidgetHostViewAndroid::PrepareTextureCopyOutputResult(
if (!texture_mailbox->IsTexture())
return;

scoped_callback_runner.Release();
ignore_result(scoped_callback_runner.Release());

gl_helper->CropScaleReadbackAndCleanMailbox(
texture_mailbox->name(),
Expand Down Expand Up @@ -1264,7 +1265,7 @@ void RenderWidgetHostViewAndroid::PrepareBitmapCopyOutputResult(
DCHECK_EQ(source->width(), dst_size_in_pixel.width());
DCHECK_EQ(source->height(), dst_size_in_pixel.height());

scoped_callback_runner.Release();
ignore_result(scoped_callback_runner.Release());
callback.Run(true, *source);
}

Expand Down
Loading

0 comments on commit 1c232c2

Please sign in to comment.