Commit c25437c6 authored by Gabriel Charette's avatar Gabriel Charette Committed by Commit Bot

[MessageLoopCurrent] Better ScopedNestableTaskAllower API

Using MessageLoopCurrent* was an incorrect move of the old code.
In this case the preferred type would be a MessageLoopCurrent (which
is a proxy for a MessageLoop*), but we can't define
ScopedNestableTaskAllower with a MessageLoopCurrent member because
MessageLoopCurrent isn't fully defined by the time the inner-class is
defined.

Instead use a MessageLoop* member like before and move the impl to .cc

Also deprecating the old constructor which required a pointless
indirection:
  base::MessageLoop* loop = base::MessageLoop::current();
  base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop);
I will follow-up with a scripted cleanup to remove such usage.

R=kylechar@chromium.org, thestig@chromium.org

Bug: 825327
Change-Id: I28c68d6450cf7c1743908118ce4dc2b4e09a4e76
Reviewed-on: https://chromium-review.googlesource.com/1014267Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatarkylechar <kylechar@chromium.org>
Commit-Queue: Gabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#551194}
parent 0920b5ee
......@@ -94,6 +94,19 @@ bool MessageLoopCurrent::NestableTasksAllowed() const {
return current_->task_execution_allowed_;
}
MessageLoopCurrent::ScopedNestableTaskAllower::ScopedNestableTaskAllower()
: ScopedNestableTaskAllower(MessageLoopCurrent::Get()) {}
MessageLoopCurrent::ScopedNestableTaskAllower::ScopedNestableTaskAllower(
MessageLoop* loop)
: loop_(loop), old_state_(loop_->NestableTasksAllowed()) {
loop_->SetNestableTasksAllowed(true);
}
MessageLoopCurrent::ScopedNestableTaskAllower::~ScopedNestableTaskAllower() {
loop_->SetNestableTasksAllowed(old_state_);
}
// static
void MessageLoopCurrent::BindToCurrentThreadInternal(MessageLoop* current) {
DCHECK(!GetTLSMessageLoop()->Get())
......
......@@ -152,23 +152,25 @@ class BASE_EXPORT MessageLoopCurrent {
void SetNestableTasksAllowed(bool allowed);
bool NestableTasksAllowed() const;
// Enables nestable tasks on |loop| while in scope.
// Enables nestable tasks on the current MessageLoop while in scope.
// DEPRECATED(https://crbug.com/750779): This should not be used when the
// nested loop is driven by RunLoop (use RunLoop::Type::kNestableTasksAllowed
// instead). It can however still be useful in a few scenarios where re-
// entrancy is caused by a native message loop.
// TODO(gab): Remove usage of this class alongside RunLoop and rename it to
// ScopedApplicationTasksAllowedInNativeNestedLoop(?) for remaining use cases.
class ScopedNestableTaskAllower {
class BASE_EXPORT ScopedNestableTaskAllower {
public:
explicit ScopedNestableTaskAllower(MessageLoopCurrent* loop)
: loop_(loop), old_state_(loop_->NestableTasksAllowed()) {
loop_->SetNestableTasksAllowed(true);
}
~ScopedNestableTaskAllower() { loop_->SetNestableTasksAllowed(old_state_); }
ScopedNestableTaskAllower();
// DEPRECATED(https://crbug.com/750779): Prefer the argument less
// constructor to obtaining and injecting MessageLoopCurrent manually.
explicit ScopedNestableTaskAllower(MessageLoop* loop);
~ScopedNestableTaskAllower();
private:
MessageLoopCurrent* const loop_;
MessageLoop* loop_;
const bool old_state_;
};
......@@ -194,7 +196,7 @@ class BASE_EXPORT MessageLoopCurrent {
static bool IsBoundToCurrentThreadInternal(MessageLoop* message_loop);
protected:
MessageLoopCurrent(MessageLoop* current) : current_(current) {}
explicit MessageLoopCurrent(MessageLoop* current) : current_(current) {}
MessageLoop* const current_;
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment