• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_TESTS_ROUTER_TEST_UTIL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_TESTS_ROUTER_TEST_UTIL_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 
12 #include "base/callback.h"
13 #include "mojo/public/cpp/bindings/message.h"
14 
15 namespace mojo {
16 namespace test {
17 
18 class MessageQueue;
19 
20 void AllocRequestMessage(uint32_t name, const char* text, Message* message);
21 void AllocResponseMessage(uint32_t name,
22                           const char* text,
23                           uint64_t request_id,
24                           Message* message);
25 
26 class MessageAccumulator : public MessageReceiver {
27  public:
28   MessageAccumulator(MessageQueue* queue,
29                      const base::Closure& closure = base::Closure());
30   ~MessageAccumulator() override;
31 
32   bool Accept(Message* message) override;
33 
34  private:
35   MessageQueue* queue_;
36   base::Closure closure_;
37 };
38 
39 class ResponseGenerator : public MessageReceiverWithResponderStatus {
40  public:
41   ResponseGenerator();
42 
43   bool Accept(Message* message) override;
44 
45   bool AcceptWithResponder(Message* message,
46                            MessageReceiverWithStatus* responder) override;
47 
48   bool SendResponse(uint32_t name,
49                     uint64_t request_id,
50                     const char* request_string,
51                     MessageReceiver* responder);
52 };
53 
54 class LazyResponseGenerator : public ResponseGenerator {
55  public:
56   explicit LazyResponseGenerator(
57       const base::Closure& closure = base::Closure());
58 
59   ~LazyResponseGenerator() override;
60 
61   bool AcceptWithResponder(Message* message,
62                            MessageReceiverWithStatus* responder) override;
63 
has_responder()64   bool has_responder() const { return !!responder_; }
65 
responder_is_valid()66   bool responder_is_valid() const { return responder_->IsValid(); }
67 
set_closure(const base::Closure & closure)68   void set_closure(const base::Closure& closure) { closure_ = closure; }
69 
70   // Sends the response and delete the responder.
CompleteWithResponse()71   void CompleteWithResponse() { Complete(true); }
72 
73   // Deletes the responder without sending a response.
CompleteWithoutResponse()74   void CompleteWithoutResponse() { Complete(false); }
75 
76  private:
77   // Completes the request handling by deleting responder_. Optionally
78   // also sends a response.
79   void Complete(bool send_response);
80 
81   MessageReceiverWithStatus* responder_;
82   uint32_t name_;
83   uint64_t request_id_;
84   std::string request_string_;
85   base::Closure closure_;
86 };
87 
88 }  // namespace test
89 }  // namespace mojo
90 
91 #endif  // MOJO_PUBLIC_CPP_BINDINGS_TESTS_ROUTER_TEST_UTIL_H_
92