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

Side by Side Diff: mojo/public/cpp/bindings/lib/interface_endpoint_client.cc

Issue 1819463002: Make mojo::Callback safe to copy across threads. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: stuff 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
« no previous file with comments | « mojo/public/cpp/bindings/callback.h ('k') | mojo/public/cpp/bindings/lib/router.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/interface_endpoint_client.h" 5 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.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/macros.h" 12 #include "base/macros.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 #include "mojo/public/cpp/bindings/associated_group.h" 17 #include "mojo/public/cpp/bindings/associated_group.h"
15 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" 18 #include "mojo/public/cpp/bindings/lib/multiplex_router.h"
16 19
17 namespace mojo { 20 namespace mojo {
18 namespace internal { 21 namespace internal {
19 22
20 // ---------------------------------------------------------------------------- 23 // ----------------------------------------------------------------------------
21 24
22 namespace { 25 namespace {
23 26
27 void DCheckIfValid(const base::WeakPtr<InterfaceEndpointClient>& client,
28 const std::string& message) {
29 bool is_valid = client && !client->encountered_error();
30 DCHECK(!is_valid) << message;
yzshen1 2016/03/19 01:29:54 So this function and ResponderThunk::DCheckValid s
Anand Mistry (off Chromium) 2016/03/19 03:56:53 Done.
31 }
32
24 // When receiving an incoming message which expects a repsonse, 33 // When receiving an incoming message which expects a repsonse,
25 // InterfaceEndpointClient creates a ResponderThunk object and passes it to the 34 // InterfaceEndpointClient creates a ResponderThunk object and passes it to the
26 // incoming message receiver. When the receiver finishes processing the message, 35 // incoming message receiver. When the receiver finishes processing the message,
27 // it can provide a response using this object. 36 // it can provide a response using this object.
28 class ResponderThunk : public MessageReceiverWithStatus { 37 class ResponderThunk : public MessageReceiverWithStatus {
29 public: 38 public:
30 explicit ResponderThunk( 39 explicit ResponderThunk(
31 const base::WeakPtr<InterfaceEndpointClient>& endpoint_client) 40 const base::WeakPtr<InterfaceEndpointClient>& endpoint_client)
32 : endpoint_client_(endpoint_client), accept_was_invoked_(false) {} 41 : endpoint_client_(endpoint_client), accept_was_invoked_(false),
42 task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
33 ~ResponderThunk() override { 43 ~ResponderThunk() override {
34 if (!accept_was_invoked_) { 44 if (!accept_was_invoked_) {
35 // The Mojo application handled a message that was expecting a response 45 // The Mojo application handled a message that was expecting a response
36 // but did not send a response. 46 // but did not send a response.
37 if (endpoint_client_) { 47 if (task_runner_->RunsTasksOnCurrentThread()) {
38 // We raise an error to signal the calling application that an error 48 if (endpoint_client_) {
39 // condition occurred. Without this the calling application would have 49 // We raise an error to signal the calling application that an error
40 // no way of knowing it should stop waiting for a response. 50 // condition occurred. Without this the calling application would have
41 // 51 // no way of knowing it should stop waiting for a response.
42 // We raise the error asynchronously and only if |endpoint_client_| is 52 //
43 // still alive when the task is run. That way it won't break the case 53 // We raise the error asynchronously and only if |endpoint_client_| is
44 // where the user abandons the interface endpoint client soon after 54 // still alive when the task is run. That way it won't break the case
45 // he/she abandons the callback. 55 // where the user abandons the interface endpoint client soon after
46 base::MessageLoop::current()->PostTask( 56 // he/she abandons the callback.
57 task_runner_->PostTask(
58 FROM_HERE,
59 base::Bind(&InterfaceEndpointClient::RaiseError,
60 endpoint_client_));
61 }
62 } else {
63 task_runner_->PostTask(
47 FROM_HERE, 64 FROM_HERE,
48 base::Bind(&InterfaceEndpointClient::RaiseError, endpoint_client_)); 65 base::Bind(&InterfaceEndpointClient::RaiseError, endpoint_client_));
49 } 66 }
50 } 67 }
51 } 68 }
52 69
53 // MessageReceiver implementation: 70 // MessageReceiver implementation:
54 bool Accept(Message* message) override { 71 bool Accept(Message* message) override {
72 DCHECK(task_runner_->RunsTasksOnCurrentThread());
55 accept_was_invoked_ = true; 73 accept_was_invoked_ = true;
56 DCHECK(message->has_flag(kMessageIsResponse)); 74 DCHECK(message->has_flag(kMessageIsResponse));
57 75
58 bool result = false; 76 bool result = false;
59 77
60 if (endpoint_client_) 78 if (endpoint_client_)
61 result = endpoint_client_->Accept(message); 79 result = endpoint_client_->Accept(message);
62 80
63 return result; 81 return result;
64 } 82 }
65 83
66 // MessageReceiverWithStatus implementation: 84 // MessageReceiverWithStatus implementation:
67 bool IsValid() override { 85 bool IsValid() override {
86 DCHECK(task_runner_->RunsTasksOnCurrentThread());
68 return endpoint_client_ && !endpoint_client_->encountered_error(); 87 return endpoint_client_ && !endpoint_client_->encountered_error();
69 } 88 }
70 89
90 void DCheckValid(const std::string& message) override {
91 if (task_runner_->RunsTasksOnCurrentThread()) {
92 DCheckIfValid(endpoint_client_, message);
93 } else {
94 task_runner_->PostTask(
95 FROM_HERE, base::Bind(&DCheckIfValid, endpoint_client_, message));
96 }
97 }
98
71 private: 99 private:
72 base::WeakPtr<InterfaceEndpointClient> endpoint_client_; 100 base::WeakPtr<InterfaceEndpointClient> endpoint_client_;
73 bool accept_was_invoked_; 101 bool accept_was_invoked_;
102 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
74 103
75 DISALLOW_COPY_AND_ASSIGN(ResponderThunk); 104 DISALLOW_COPY_AND_ASSIGN(ResponderThunk);
76 }; 105 };
77 106
78 } // namespace 107 } // namespace
79 108
80 // ---------------------------------------------------------------------------- 109 // ----------------------------------------------------------------------------
81 110
82 InterfaceEndpointClient::HandleIncomingMessageThunk::HandleIncomingMessageThunk( 111 InterfaceEndpointClient::HandleIncomingMessageThunk::HandleIncomingMessageThunk(
83 InterfaceEndpointClient* owner) 112 InterfaceEndpointClient* owner)
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 } else { 256 } else {
228 if (!incoming_receiver_) 257 if (!incoming_receiver_)
229 return false; 258 return false;
230 259
231 return incoming_receiver_->Accept(message); 260 return incoming_receiver_->Accept(message);
232 } 261 }
233 } 262 }
234 263
235 } // namespace internal 264 } // namespace internal
236 } // namespace mojo 265 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/callback.h ('k') | mojo/public/cpp/bindings/lib/router.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698