| OLD | NEW |
| 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 <stdlib.h> | 5 #include <stdlib.h> |
| 6 #include <string.h> | 6 #include <string.h> |
| 7 | 7 |
| 8 #include "mojo/public/cpp/bindings/lib/message_builder.h" | 8 #include "mojo/public/cpp/bindings/lib/message_builder.h" |
| 9 #include "mojo/public/cpp/bindings/lib/message_queue.h" | 9 #include "mojo/public/cpp/bindings/lib/message_queue.h" |
| 10 #include "mojo/public/cpp/bindings/lib/router.h" | 10 #include "mojo/public/cpp/bindings/lib/router.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 bool Accept(Message* message) override { | 41 bool Accept(Message* message) override { |
| 42 queue_->Push(message); | 42 queue_->Push(message); |
| 43 return true; | 43 return true; |
| 44 } | 44 } |
| 45 | 45 |
| 46 private: | 46 private: |
| 47 internal::MessageQueue* queue_; | 47 internal::MessageQueue* queue_; |
| 48 }; | 48 }; |
| 49 | 49 |
| 50 class ResponseGenerator : public MessageReceiverWithResponder { | 50 class ResponseGenerator : public MessageReceiverWithResponderStatus { |
| 51 public: | 51 public: |
| 52 ResponseGenerator() {} | 52 ResponseGenerator() {} |
| 53 | 53 |
| 54 bool Accept(Message* message) override { return false; } | 54 bool Accept(Message* message) override { return false; } |
| 55 | 55 |
| 56 bool AcceptWithResponder(Message* message, | 56 bool AcceptWithResponder(Message* message, |
| 57 MessageReceiver* responder) override { | 57 MessageReceiverWithStatus* responder) override { |
| 58 EXPECT_TRUE(message->has_flag(internal::kMessageExpectsResponse)); | 58 EXPECT_TRUE(message->has_flag(internal::kMessageExpectsResponse)); |
| 59 | 59 |
| 60 bool result = SendResponse( | 60 bool result = SendResponse( |
| 61 message->name(), message->request_id(), | 61 message->name(), message->request_id(), |
| 62 reinterpret_cast<const char*>(message->payload()), responder); | 62 reinterpret_cast<const char*>(message->payload()), responder); |
| 63 EXPECT_TRUE(responder->IsValid()); |
| 63 delete responder; | 64 delete responder; |
| 64 return result; | 65 return result; |
| 65 } | 66 } |
| 66 | 67 |
| 67 bool SendResponse(uint32_t name, | 68 bool SendResponse(uint32_t name, |
| 68 uint64_t request_id, | 69 uint64_t request_id, |
| 69 const char* request_string, | 70 const char* request_string, |
| 70 MessageReceiver* responder) { | 71 MessageReceiver* responder) { |
| 71 Message response; | 72 Message response; |
| 72 std::string response_string(request_string); | 73 std::string response_string(request_string); |
| 73 response_string += " world!"; | 74 response_string += " world!"; |
| 74 AllocResponseMessage(name, response_string.c_str(), request_id, &response); | 75 AllocResponseMessage(name, response_string.c_str(), request_id, &response); |
| 75 | 76 |
| 76 return responder->Accept(&response); | 77 return responder->Accept(&response); |
| 77 } | 78 } |
| 78 }; | 79 }; |
| 79 | 80 |
| 80 class LazyResponseGenerator : public ResponseGenerator { | 81 class LazyResponseGenerator : public ResponseGenerator { |
| 81 public: | 82 public: |
| 82 LazyResponseGenerator() : responder_(nullptr), name_(0), request_id_(0) {} | 83 LazyResponseGenerator() : responder_(nullptr), name_(0), request_id_(0) {} |
| 83 | 84 |
| 84 ~LazyResponseGenerator() override { delete responder_; } | 85 ~LazyResponseGenerator() override { delete responder_; } |
| 85 | 86 |
| 86 bool AcceptWithResponder(Message* message, | 87 bool AcceptWithResponder(Message* message, |
| 87 MessageReceiver* responder) override { | 88 MessageReceiverWithStatus* responder) override { |
| 88 name_ = message->name(); | 89 name_ = message->name(); |
| 89 request_id_ = message->request_id(); | 90 request_id_ = message->request_id(); |
| 90 request_string_ = | 91 request_string_ = |
| 91 std::string(reinterpret_cast<const char*>(message->payload())); | 92 std::string(reinterpret_cast<const char*>(message->payload())); |
| 92 responder_ = responder; | 93 responder_ = responder; |
| 93 return true; | 94 return true; |
| 94 } | 95 } |
| 95 | 96 |
| 96 bool has_responder() const { return !!responder_; } | 97 bool has_responder() const { return !!responder_; } |
| 97 | 98 |
| 99 bool responder_is_valid() const { return responder_->IsValid(); } |
| 100 |
| 98 // Send the response and delete the responder. | 101 // Send the response and delete the responder. |
| 99 void CompleteWithResponse() { Complete(true); } | 102 void CompleteWithResponse() { Complete(true); } |
| 100 | 103 |
| 101 // Delete the responder without sending a response. | 104 // Delete the responder without sending a response. |
| 102 void CompleteWithoutResponse() { Complete(false); } | 105 void CompleteWithoutResponse() { Complete(false); } |
| 103 | 106 |
| 104 private: | 107 private: |
| 105 // Completes the request handling by deleting responder_. Optionally | 108 // Completes the request handling by deleting responder_. Optionally |
| 106 // also sends a response. | 109 // also sends a response. |
| 107 void Complete(bool send_response) { | 110 void Complete(bool send_response) { |
| 108 if (send_response) { | 111 if (send_response) { |
| 109 SendResponse(name_, request_id_, request_string_.c_str(), responder_); | 112 SendResponse(name_, request_id_, request_string_.c_str(), responder_); |
| 110 } | 113 } |
| 111 delete responder_; | 114 delete responder_; |
| 112 responder_ = nullptr; | 115 responder_ = nullptr; |
| 113 } | 116 } |
| 114 | 117 |
| 115 MessageReceiver* responder_; | 118 MessageReceiverWithStatus* responder_; |
| 116 uint32_t name_; | 119 uint32_t name_; |
| 117 uint64_t request_id_; | 120 uint64_t request_id_; |
| 118 std::string request_string_; | 121 std::string request_string_; |
| 119 }; | 122 }; |
| 120 | 123 |
| 121 class RouterTest : public testing::Test { | 124 class RouterTest : public testing::Test { |
| 122 public: | 125 public: |
| 123 RouterTest() {} | 126 RouterTest() {} |
| 124 | 127 |
| 125 void SetUp() override { | 128 void SetUp() override { |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 AllocRequestMessage(1, "hello", &request); | 257 AllocRequestMessage(1, "hello", &request); |
| 255 | 258 |
| 256 internal::MessageQueue message_queue; | 259 internal::MessageQueue message_queue; |
| 257 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); | 260 router0.AcceptWithResponder(&request, new MessageAccumulator(&message_queue)); |
| 258 PumpMessages(); | 261 PumpMessages(); |
| 259 | 262 |
| 260 // The request has been received but the response has not been sent yet. | 263 // The request has been received but the response has not been sent yet. |
| 261 EXPECT_TRUE(message_queue.IsEmpty()); | 264 EXPECT_TRUE(message_queue.IsEmpty()); |
| 262 | 265 |
| 263 // Send the response. | 266 // Send the response. |
| 267 EXPECT_TRUE(generator.responder_is_valid()); |
| 264 generator.CompleteWithResponse(); | 268 generator.CompleteWithResponse(); |
| 265 PumpMessages(); | 269 PumpMessages(); |
| 266 | 270 |
| 267 // Check the response. | 271 // Check the response. |
| 268 EXPECT_FALSE(message_queue.IsEmpty()); | 272 EXPECT_FALSE(message_queue.IsEmpty()); |
| 269 Message response; | 273 Message response; |
| 270 message_queue.Pop(&response); | 274 message_queue.Pop(&response); |
| 271 EXPECT_EQ(std::string("hello world!"), | 275 EXPECT_EQ(std::string("hello world!"), |
| 272 std::string(reinterpret_cast<const char*>(response.payload()))); | 276 std::string(reinterpret_cast<const char*>(response.payload()))); |
| 273 | 277 |
| 274 // Send a second message on the pipe. | 278 // Send a second message on the pipe. |
| 275 Message request2; | 279 Message request2; |
| 276 AllocRequestMessage(1, "hello again", &request2); | 280 AllocRequestMessage(1, "hello again", &request2); |
| 277 | 281 |
| 278 router0.AcceptWithResponder(&request2, | 282 router0.AcceptWithResponder(&request2, |
| 279 new MessageAccumulator(&message_queue)); | 283 new MessageAccumulator(&message_queue)); |
| 280 PumpMessages(); | 284 PumpMessages(); |
| 281 | 285 |
| 282 // The request has been received but the response has not been sent yet. | 286 // The request has been received but the response has not been sent yet. |
| 283 EXPECT_TRUE(message_queue.IsEmpty()); | 287 EXPECT_TRUE(message_queue.IsEmpty()); |
| 284 | 288 |
| 285 // Send the second response. | 289 // Send the second response. |
| 290 EXPECT_TRUE(generator.responder_is_valid()); |
| 286 generator.CompleteWithResponse(); | 291 generator.CompleteWithResponse(); |
| 287 PumpMessages(); | 292 PumpMessages(); |
| 288 | 293 |
| 289 // Check the second response. | 294 // Check the second response. |
| 290 EXPECT_FALSE(message_queue.IsEmpty()); | 295 EXPECT_FALSE(message_queue.IsEmpty()); |
| 291 message_queue.Pop(&response); | 296 message_queue.Pop(&response); |
| 292 EXPECT_EQ(std::string("hello again world!"), | 297 EXPECT_EQ(std::string("hello again world!"), |
| 293 std::string(reinterpret_cast<const char*>(response.payload()))); | 298 std::string(reinterpret_cast<const char*>(response.payload()))); |
| 294 } | 299 } |
| 295 | 300 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 | 356 |
| 352 internal::MessageQueue message_queue; | 357 internal::MessageQueue message_queue; |
| 353 router0.AcceptWithResponder(&request, | 358 router0.AcceptWithResponder(&request, |
| 354 new MessageAccumulator(&message_queue)); | 359 new MessageAccumulator(&message_queue)); |
| 355 | 360 |
| 356 PumpMessages(); | 361 PumpMessages(); |
| 357 | 362 |
| 358 EXPECT_TRUE(generator.has_responder()); | 363 EXPECT_TRUE(generator.has_responder()); |
| 359 } | 364 } |
| 360 | 365 |
| 366 EXPECT_FALSE(generator.responder_is_valid()); |
| 361 generator.CompleteWithResponse(); // This should end up doing nothing. | 367 generator.CompleteWithResponse(); // This should end up doing nothing. |
| 362 } | 368 } |
| 363 | 369 |
| 364 } // namespace | 370 } // namespace |
| 365 } // namespace test | 371 } // namespace test |
| 366 } // namespace mojo | 372 } // namespace mojo |
| OLD | NEW |