1 // Copyright (c) 2012 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 PPAPI_PROXY_RESOURCE_MESSAGE_TEST_SINK_H_
6 #define PPAPI_PROXY_RESOURCE_MESSAGE_TEST_SINK_H_
7 
8 #include "ipc/ipc_listener.h"
9 #include "ipc/ipc_test_sink.h"
10 #include "ppapi/c/pp_stdint.h"
11 
12 namespace ppapi {
13 namespace proxy {
14 
15 class ResourceMessageCallParams;
16 class ResourceMessageReplyParams;
17 class SerializedHandle;
18 
19 // Extends IPC::TestSink to add extra capabilities for searching for and
20 // decoding resource messages.
21 class ResourceMessageTestSink : public IPC::TestSink {
22  public:
23   ResourceMessageTestSink();
24   ~ResourceMessageTestSink() override;
25 
26   // IPC::TestSink.
27   // Overridden to handle sync messages.
28   bool Send(IPC::Message* msg) override;
29 
30   // Sets the reply message that will be returned to the next sync message sent.
31   // This test sink owns any reply messages passed into this method.
32   void SetSyncReplyMessage(IPC::Message* reply_msg);
33 
34   // Searches the queue for the first resource call message with a nested
35   // message matching the given ID. On success, returns true and populates the
36   // given params and nested message.
37   bool GetFirstResourceCallMatching(uint32_t id,
38                                     ResourceMessageCallParams* params,
39                                     IPC::Message* nested_msg) const;
40 
41   // Like GetFirstResourceCallMatching except for replies.
42   bool GetFirstResourceReplyMatching(uint32_t id,
43                                      ResourceMessageReplyParams* params,
44                                      IPC::Message* nested_msg);
45 
46   // Searches the queue for all resource call messages with a nested message
47   // matching the given ID.
48   typedef std::pair<ResourceMessageCallParams, IPC::Message> ResourceCall;
49   typedef std::vector<ResourceCall> ResourceCallVector;
50   ResourceCallVector GetAllResourceCallsMatching(uint32_t id);
51 
52   // Like GetAllResourceCallsMatching except for replies.
53   typedef std::pair<ResourceMessageReplyParams, IPC::Message> ResourceReply;
54   typedef std::vector<ResourceReply> ResourceReplyVector;
55   ResourceReplyVector GetAllResourceRepliesMatching(uint32_t id);
56 
57  private:
58   std::unique_ptr<IPC::Message> sync_reply_msg_;
59 };
60 
61 // This is a message handler which generates reply messages for synchronous
62 // resource calls. This allows unit testing of the plugin side of resources
63 // which send sync messages. If you want to reply to a sync message type named
64 // |PpapiHostMsg_X_Y| with |PpapiPluginMsg_X_YReply| then usage would be as
65 // follows (from within |PluginProxyTest|s):
66 //
67 // PpapiHostMsg_X_YReply my_reply;
68 // ResourceSyncCallHandler handler(&sink(),
69 //                                 PpapiHostMsg_X_Y::ID,
70 //                                 PP_OK,
71 //                                 my_reply);
72 // sink().AddFilter(&handler);
73 // // Do stuff to send a sync message ...
74 // // You can check handler.last_handled_msg() to ensure the correct message was
75 // // handled.
76 // sink().RemoveFilter(&handler);
77 class ResourceSyncCallHandler : public IPC::Listener {
78  public:
79   ResourceSyncCallHandler(ResourceMessageTestSink* test_sink,
80                           uint32_t incoming_type,
81                           int32_t result,
82                           const IPC::Message& reply_msg);
83   ~ResourceSyncCallHandler() override;
84 
85   // IPC::Listener.
86   bool OnMessageReceived(const IPC::Message& message) override;
87 
last_handled_msg()88   IPC::Message last_handled_msg() { return last_handled_msg_; }
89 
90   // Sets a handle to be appended to the ReplyParams.
91   void set_serialized_handle(
92       std::unique_ptr<SerializedHandle> serialized_handle);
93 
94  private:
95   ResourceMessageTestSink* test_sink_;
96   uint32_t incoming_type_;
97   int32_t result_;
98   std::unique_ptr<SerializedHandle> serialized_handle_;
99   IPC::Message reply_msg_;
100   IPC::Message last_handled_msg_;
101 };
102 
103 }  // namespace proxy
104 }  // namespace ppapi
105 
106 #endif  // PPAPI_PROXY_RESOURCE_MESSAGE_TEST_SINK_H_
107