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

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: Fix destruction. 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 DCheckIfValid(const base::WeakPtr<Router>& router,
26 const std::string message) {
yzshen1 2016/03/18 23:47:04 "const &"?
Anand Mistry (off Chromium) 2016/03/19 01:02:13 Done.
27 if (router && !router->encountered_error() && router->is_valid()) {
28 NOTREACHED() << message;
yzshen1 2016/03/18 23:47:04 nit: maybe consider DCHECK(!the_current_condition)
Anand Mistry (off Chromium) 2016/03/19 01:02:13 Done.
29 }
30 }
31
22 class ResponderThunk : public MessageReceiverWithStatus { 32 class ResponderThunk : public MessageReceiverWithStatus {
23 public: 33 public:
24 explicit ResponderThunk(const base::WeakPtr<Router>& router) 34 explicit ResponderThunk(const base::WeakPtr<Router>& router)
25 : router_(router), accept_was_invoked_(false) {} 35 : router_(router), accept_was_invoked_(false),
36 task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
26 ~ResponderThunk() override { 37 ~ResponderThunk() override {
27 if (!accept_was_invoked_) { 38 if (!accept_was_invoked_) {
28 // The Mojo application handled a message that was expecting a response 39 // The Mojo application handled a message that was expecting a response
29 // but did not send a response. 40 // but did not send a response.
30 if (router_) { 41 if (task_runner_->RunsTasksOnCurrentThread()) {
31 // We raise an error to signal the calling application that an error 42 if (router_) {
32 // condition occurred. Without this the calling application would have 43 // We raise an error to signal the calling application that an error
33 // no way of knowing it should stop waiting for a response. 44 // condition occurred. Without this the calling application would have
34 router_->RaiseError(); 45 // no way of knowing it should stop waiting for a response.
46 router_->RaiseError();
yzshen1 2016/03/18 23:47:04 In interface_endpoint_client.cc (which is used for
Anand Mistry (off Chromium) 2016/03/19 01:02:13 I traced the code down to Connector and noticed th
yzshen1 2016/03/19 01:29:54 I think you are right. :) So maybe we should chang
Anand Mistry (off Chromium) 2016/03/19 03:56:53 Done.
47 }
48 } else {
49 task_runner_->PostTask(FROM_HERE,
50 base::Bind(&Router::RaiseError, router_));
35 } 51 }
36 } 52 }
37 } 53 }
38 54
39 // MessageReceiver implementation: 55 // MessageReceiver implementation:
40 bool Accept(Message* message) override { 56 bool Accept(Message* message) override {
57 DCHECK(task_runner_->RunsTasksOnCurrentThread());
41 accept_was_invoked_ = true; 58 accept_was_invoked_ = true;
42 DCHECK(message->has_flag(kMessageIsResponse)); 59 DCHECK(message->has_flag(kMessageIsResponse));
43 60
44 bool result = false; 61 bool result = false;
45 62
46 if (router_) 63 if (router_)
47 result = router_->Accept(message); 64 result = router_->Accept(message);
48 65
49 return result; 66 return result;
50 } 67 }
51 68
52 // MessageReceiverWithStatus implementation: 69 // MessageReceiverWithStatus implementation:
53 bool IsValid() override { 70 bool IsValid() override {
71 DCHECK(task_runner_->RunsTasksOnCurrentThread());
54 return router_ && !router_->encountered_error() && router_->is_valid(); 72 return router_ && !router_->encountered_error() && router_->is_valid();
55 } 73 }
56 74
75 void DCheckValid(const std::string& message) override {
yzshen1 2016/03/18 23:47:04 Please do the same thing to the ResponderThunk in
Anand Mistry (off Chromium) 2016/03/19 01:02:13 Done.
76 if (task_runner_->RunsTasksOnCurrentThread()) {
77 DCheckIfValid(router_, message);
78 } else {
79 task_runner_->PostTask(FROM_HERE,
80 base::Bind(&DCheckIfValid, router_, message));
81 }
82 }
83
57 private: 84 private:
58 base::WeakPtr<Router> router_; 85 base::WeakPtr<Router> router_;
59 bool accept_was_invoked_; 86 bool accept_was_invoked_;
87 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
60 }; 88 };
61 89
62 } // namespace 90 } // namespace
63 91
64 // ---------------------------------------------------------------------------- 92 // ----------------------------------------------------------------------------
65 93
66 Router::SyncResponseInfo::SyncResponseInfo(bool* in_response_received) 94 Router::SyncResponseInfo::SyncResponseInfo(bool* in_response_received)
67 : response_received(in_response_received) {} 95 : response_received(in_response_received) {}
68 96
69 Router::SyncResponseInfo::~SyncResponseInfo() {} 97 Router::SyncResponseInfo::~SyncResponseInfo() {}
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 } 298 }
271 299
272 encountered_error_ = true; 300 encountered_error_ = true;
273 error_handler_.Run(); 301 error_handler_.Run();
274 } 302 }
275 303
276 // ---------------------------------------------------------------------------- 304 // ----------------------------------------------------------------------------
277 305
278 } // namespace internal 306 } // namespace internal
279 } // namespace mojo 307 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698