| OLD | NEW |
| 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 DCheckIfInvalid(const base::WeakPtr<InterfaceEndpointClient>& client, |
| 28 const std::string& message) { |
| 29 bool is_valid = client && !client->encountered_error(); |
| 30 DCHECK(!is_valid) << message; |
| 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 endpoint_client_->RaiseError(); |
| 43 // still alive when the task is run. That way it won't break the case | 53 } |
| 44 // where the user abandons the interface endpoint client soon after | 54 } else { |
| 45 // he/she abandons the callback. | 55 task_runner_->PostTask( |
| 46 base::MessageLoop::current()->PostTask( | |
| 47 FROM_HERE, | 56 FROM_HERE, |
| 48 base::Bind(&InterfaceEndpointClient::RaiseError, endpoint_client_)); | 57 base::Bind(&InterfaceEndpointClient::RaiseError, endpoint_client_)); |
| 49 } | 58 } |
| 50 } | 59 } |
| 51 } | 60 } |
| 52 | 61 |
| 53 // MessageReceiver implementation: | 62 // MessageReceiver implementation: |
| 54 bool Accept(Message* message) override { | 63 bool Accept(Message* message) override { |
| 64 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 55 accept_was_invoked_ = true; | 65 accept_was_invoked_ = true; |
| 56 DCHECK(message->has_flag(kMessageIsResponse)); | 66 DCHECK(message->has_flag(kMessageIsResponse)); |
| 57 | 67 |
| 58 bool result = false; | 68 bool result = false; |
| 59 | 69 |
| 60 if (endpoint_client_) | 70 if (endpoint_client_) |
| 61 result = endpoint_client_->Accept(message); | 71 result = endpoint_client_->Accept(message); |
| 62 | 72 |
| 63 return result; | 73 return result; |
| 64 } | 74 } |
| 65 | 75 |
| 66 // MessageReceiverWithStatus implementation: | 76 // MessageReceiverWithStatus implementation: |
| 67 bool IsValid() override { | 77 bool IsValid() override { |
| 78 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 68 return endpoint_client_ && !endpoint_client_->encountered_error(); | 79 return endpoint_client_ && !endpoint_client_->encountered_error(); |
| 69 } | 80 } |
| 70 | 81 |
| 82 void DCheckInvalid(const std::string& message) override { |
| 83 if (task_runner_->RunsTasksOnCurrentThread()) { |
| 84 DCheckIfInvalid(endpoint_client_, message); |
| 85 } else { |
| 86 task_runner_->PostTask( |
| 87 FROM_HERE, base::Bind(&DCheckIfInvalid, endpoint_client_, message)); |
| 88 } |
| 89 } |
| 90 |
| 71 private: | 91 private: |
| 72 base::WeakPtr<InterfaceEndpointClient> endpoint_client_; | 92 base::WeakPtr<InterfaceEndpointClient> endpoint_client_; |
| 73 bool accept_was_invoked_; | 93 bool accept_was_invoked_; |
| 94 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 74 | 95 |
| 75 DISALLOW_COPY_AND_ASSIGN(ResponderThunk); | 96 DISALLOW_COPY_AND_ASSIGN(ResponderThunk); |
| 76 }; | 97 }; |
| 77 | 98 |
| 78 } // namespace | 99 } // namespace |
| 79 | 100 |
| 80 // ---------------------------------------------------------------------------- | 101 // ---------------------------------------------------------------------------- |
| 81 | 102 |
| 82 InterfaceEndpointClient::HandleIncomingMessageThunk::HandleIncomingMessageThunk( | 103 InterfaceEndpointClient::HandleIncomingMessageThunk::HandleIncomingMessageThunk( |
| 83 InterfaceEndpointClient* owner) | 104 InterfaceEndpointClient* owner) |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 } else { | 248 } else { |
| 228 if (!incoming_receiver_) | 249 if (!incoming_receiver_) |
| 229 return false; | 250 return false; |
| 230 | 251 |
| 231 return incoming_receiver_->Accept(message); | 252 return incoming_receiver_->Accept(message); |
| 232 } | 253 } |
| 233 } | 254 } |
| 234 | 255 |
| 235 } // namespace internal | 256 } // namespace internal |
| 236 } // namespace mojo | 257 } // namespace mojo |
| OLD | NEW |