OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/public/cpp/bindings/tests/router_test_util.h" |
| 6 |
| 7 #include <string.h> |
| 8 |
| 9 #include "mojo/public/cpp/bindings/lib/message_builder.h" |
| 10 #include "mojo/public/cpp/bindings/tests/message_queue.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace mojo { |
| 14 namespace test { |
| 15 |
| 16 void AllocRequestMessage(uint32_t name, const char* text, Message* message) { |
| 17 size_t payload_size = strlen(text) + 1; // Plus null terminator. |
| 18 internal::RequestMessageBuilder builder(name, payload_size); |
| 19 memcpy(builder.buffer()->Allocate(payload_size), text, payload_size); |
| 20 |
| 21 builder.message()->MoveTo(message); |
| 22 } |
| 23 |
| 24 void AllocResponseMessage(uint32_t name, |
| 25 const char* text, |
| 26 uint64_t request_id, |
| 27 Message* message) { |
| 28 size_t payload_size = strlen(text) + 1; // Plus null terminator. |
| 29 internal::ResponseMessageBuilder builder(name, payload_size, request_id); |
| 30 memcpy(builder.buffer()->Allocate(payload_size), text, payload_size); |
| 31 |
| 32 builder.message()->MoveTo(message); |
| 33 } |
| 34 |
| 35 MessageAccumulator::MessageAccumulator(MessageQueue* queue) : queue_(queue) {} |
| 36 |
| 37 bool MessageAccumulator::Accept(Message* message) { |
| 38 queue_->Push(message); |
| 39 return true; |
| 40 } |
| 41 |
| 42 ResponseGenerator::ResponseGenerator() {} |
| 43 |
| 44 bool ResponseGenerator::Accept(Message* message) { |
| 45 return false; |
| 46 } |
| 47 |
| 48 bool ResponseGenerator::AcceptWithResponder( |
| 49 Message* message, |
| 50 MessageReceiverWithStatus* responder) { |
| 51 EXPECT_TRUE(message->has_flag(internal::kMessageExpectsResponse)); |
| 52 |
| 53 bool result = SendResponse(message->name(), message->request_id(), |
| 54 reinterpret_cast<const char*>(message->payload()), |
| 55 responder); |
| 56 EXPECT_TRUE(responder->IsValid()); |
| 57 delete responder; |
| 58 return result; |
| 59 } |
| 60 |
| 61 bool ResponseGenerator::SendResponse(uint32_t name, |
| 62 uint64_t request_id, |
| 63 const char* request_string, |
| 64 MessageReceiver* responder) { |
| 65 Message response; |
| 66 std::string response_string(request_string); |
| 67 response_string += " world!"; |
| 68 AllocResponseMessage(name, response_string.c_str(), request_id, &response); |
| 69 |
| 70 return responder->Accept(&response); |
| 71 } |
| 72 |
| 73 LazyResponseGenerator::LazyResponseGenerator() |
| 74 : responder_(nullptr), name_(0), request_id_(0) {} |
| 75 |
| 76 LazyResponseGenerator::~LazyResponseGenerator() { |
| 77 delete responder_; |
| 78 } |
| 79 |
| 80 bool LazyResponseGenerator::AcceptWithResponder( |
| 81 Message* message, |
| 82 MessageReceiverWithStatus* responder) { |
| 83 name_ = message->name(); |
| 84 request_id_ = message->request_id(); |
| 85 request_string_ = |
| 86 std::string(reinterpret_cast<const char*>(message->payload())); |
| 87 responder_ = responder; |
| 88 return true; |
| 89 } |
| 90 |
| 91 void LazyResponseGenerator::Complete(bool send_response) { |
| 92 if (send_response) { |
| 93 SendResponse(name_, request_id_, request_string_.c_str(), responder_); |
| 94 } |
| 95 delete responder_; |
| 96 responder_ = nullptr; |
| 97 } |
| 98 |
| 99 } // namespace test |
| 100 } // namespace mojo |
OLD | NEW |