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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/public/cpp/bindings/lib/router.h" 5 #include "mojo/public/cpp/bindings/lib/router.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/location.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/single_thread_task_runner.h"
13 #include "base/stl_util.h" 15 #include "base/stl_util.h"
16 #include "base/thread_task_runner_handle.h"
14 17
15 namespace mojo { 18 namespace mojo {
16 namespace internal { 19 namespace internal {
17 20
18 // ---------------------------------------------------------------------------- 21 // ----------------------------------------------------------------------------
19 22
20 namespace { 23 namespace {
21 24
25 void DCheckIfInvalid(const base::WeakPtr<Router>& router,
26 const std::string& message) {
27 bool is_valid = router && !router->encountered_error() && router->is_valid();
28 DCHECK(!is_valid) << message;
29 }
30
22 class ResponderThunk : public MessageReceiverWithStatus { 31 class ResponderThunk : public MessageReceiverWithStatus {
23 public: 32 public:
24 explicit ResponderThunk(const base::WeakPtr<Router>& router) 33 explicit ResponderThunk(const base::WeakPtr<Router>& router)
25 : router_(router), accept_was_invoked_(false) {} 34 : router_(router), accept_was_invoked_(false),
35 task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
26 ~ResponderThunk() override { 36 ~ResponderThunk() override {
27 if (!accept_was_invoked_) { 37 if (!accept_was_invoked_) {
28 // The Mojo application handled a message that was expecting a response 38 // The Mojo application handled a message that was expecting a response
29 // but did not send a response. 39 // but did not send a response.
30 if (router_) { 40 if (task_runner_->RunsTasksOnCurrentThread()) {
31 // We raise an error to signal the calling application that an error 41 if (router_) {
32 // condition occurred. Without this the calling application would have 42 // We raise an error to signal the calling application that an error
33 // no way of knowing it should stop waiting for a response. 43 // condition occurred. Without this the calling application would have
34 router_->RaiseError(); 44 // no way of knowing it should stop waiting for a response.
45 router_->RaiseError();
46 }
47 } else {
48 task_runner_->PostTask(FROM_HERE,
49 base::Bind(&Router::RaiseError, router_));
35 } 50 }
36 } 51 }
37 } 52 }
38 53
39 // MessageReceiver implementation: 54 // MessageReceiver implementation:
40 bool Accept(Message* message) override { 55 bool Accept(Message* message) override {
56 DCHECK(task_runner_->RunsTasksOnCurrentThread());
41 accept_was_invoked_ = true; 57 accept_was_invoked_ = true;
42 DCHECK(message->has_flag(kMessageIsResponse)); 58 DCHECK(message->has_flag(kMessageIsResponse));
43 59
44 bool result = false; 60 bool result = false;
45 61
46 if (router_) 62 if (router_)
47 result = router_->Accept(message); 63 result = router_->Accept(message);
48 64
49 return result; 65 return result;
50 } 66 }
51 67
52 // MessageReceiverWithStatus implementation: 68 // MessageReceiverWithStatus implementation:
53 bool IsValid() override { 69 bool IsValid() override {
70 DCHECK(task_runner_->RunsTasksOnCurrentThread());
54 return router_ && !router_->encountered_error() && router_->is_valid(); 71 return router_ && !router_->encountered_error() && router_->is_valid();
55 } 72 }
56 73
74 void DCheckInvalid(const std::string& message) override {
75 if (task_runner_->RunsTasksOnCurrentThread()) {
76 DCheckIfInvalid(router_, message);
77 } else {
78 task_runner_->PostTask(FROM_HERE,
79 base::Bind(&DCheckIfInvalid, router_, message));
80 }
81 }
82
57 private: 83 private:
58 base::WeakPtr<Router> router_; 84 base::WeakPtr<Router> router_;
59 bool accept_was_invoked_; 85 bool accept_was_invoked_;
86 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
60 }; 87 };
61 88
62 } // namespace 89 } // namespace
63 90
64 // ---------------------------------------------------------------------------- 91 // ----------------------------------------------------------------------------
65 92
66 Router::SyncResponseInfo::SyncResponseInfo(bool* in_response_received) 93 Router::SyncResponseInfo::SyncResponseInfo(bool* in_response_received)
67 : response_received(in_response_received) {} 94 : response_received(in_response_received) {}
68 95
69 Router::SyncResponseInfo::~SyncResponseInfo() {} 96 Router::SyncResponseInfo::~SyncResponseInfo() {}
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 } 297 }
271 298
272 encountered_error_ = true; 299 encountered_error_ = true;
273 error_handler_.Run(); 300 error_handler_.Run();
274 } 301 }
275 302
276 // ---------------------------------------------------------------------------- 303 // ----------------------------------------------------------------------------
277 304
278 } // namespace internal 305 } // namespace internal
279 } // namespace mojo 306 } // namespace mojo
OLDNEW
« 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