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

Unified Diff: mojo/public/cpp/bindings/lib/router.cc

Issue 1819463002: Make mojo::Callback safe to copy across threads. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments Created 4 years, 9 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
« no previous file with comments | « mojo/public/cpp/bindings/lib/interface_endpoint_client.cc ('k') | mojo/public/cpp/bindings/message.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/cpp/bindings/lib/router.cc
diff --git a/mojo/public/cpp/bindings/lib/router.cc b/mojo/public/cpp/bindings/lib/router.cc
index d929e804c21c41358c7261c826b9cb831f14a175..045fc6203fec20204ea20ec915ee5738fc5d1954 100644
--- a/mojo/public/cpp/bindings/lib/router.cc
+++ b/mojo/public/cpp/bindings/lib/router.cc
@@ -8,9 +8,12 @@
#include <utility>
#include "base/bind.h"
+#include "base/location.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
+#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
+#include "base/thread_task_runner_handle.h"
namespace mojo {
namespace internal {
@@ -19,25 +22,38 @@ namespace internal {
namespace {
+void DCheckIfInvalid(const base::WeakPtr<Router>& router,
+ const std::string& message) {
+ bool is_valid = router && !router->encountered_error() && router->is_valid();
+ DCHECK(!is_valid) << message;
+}
+
class ResponderThunk : public MessageReceiverWithStatus {
public:
explicit ResponderThunk(const base::WeakPtr<Router>& router)
- : router_(router), accept_was_invoked_(false) {}
+ : router_(router), accept_was_invoked_(false),
+ task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
~ResponderThunk() override {
if (!accept_was_invoked_) {
// The Mojo application handled a message that was expecting a response
// but did not send a response.
- if (router_) {
- // We raise an error to signal the calling application that an error
- // condition occurred. Without this the calling application would have
- // no way of knowing it should stop waiting for a response.
- router_->RaiseError();
+ if (task_runner_->RunsTasksOnCurrentThread()) {
+ if (router_) {
+ // We raise an error to signal the calling application that an error
+ // condition occurred. Without this the calling application would have
+ // no way of knowing it should stop waiting for a response.
+ router_->RaiseError();
+ }
+ } else {
+ task_runner_->PostTask(FROM_HERE,
+ base::Bind(&Router::RaiseError, router_));
}
}
}
// MessageReceiver implementation:
bool Accept(Message* message) override {
+ DCHECK(task_runner_->RunsTasksOnCurrentThread());
accept_was_invoked_ = true;
DCHECK(message->has_flag(kMessageIsResponse));
@@ -51,12 +67,23 @@ class ResponderThunk : public MessageReceiverWithStatus {
// MessageReceiverWithStatus implementation:
bool IsValid() override {
+ DCHECK(task_runner_->RunsTasksOnCurrentThread());
return router_ && !router_->encountered_error() && router_->is_valid();
}
+ void DCheckInvalid(const std::string& message) override {
+ if (task_runner_->RunsTasksOnCurrentThread()) {
+ DCheckIfInvalid(router_, message);
+ } else {
+ task_runner_->PostTask(FROM_HERE,
+ base::Bind(&DCheckIfInvalid, router_, message));
+ }
+ }
+
private:
base::WeakPtr<Router> router_;
bool accept_was_invoked_;
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
};
} // namespace
« no previous file with comments | « mojo/public/cpp/bindings/lib/interface_endpoint_client.cc ('k') | mojo/public/cpp/bindings/message.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698