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.
5 #include "ppapi/proxy/resource_message_test_sink.h"
7 #include "ppapi/proxy/ppapi_messages.h"
8 #include "ppapi/proxy/resource_message_params.h"
9 #include "ppapi/proxy/serialized_handle.h"
16 // Backend for GetAllResource[Calls|Replies]Matching.
17 template<class WrapperMessage
, class Params
>
18 std::vector
<std::pair
<Params
, IPC::Message
> >
19 GetAllResourceMessagesMatching(const ResourceMessageTestSink
& sink
,
21 std::vector
<std::pair
<Params
, IPC::Message
> > result
;
22 for (size_t i
= 0; i
< sink
.message_count(); i
++) {
23 const IPC::Message
* msg
= sink
.GetMessageAt(i
);
24 if (msg
->type() == WrapperMessage::ID
) {
27 WrapperMessage::Read(msg
, &cur_params
, &cur_msg
);
28 if (cur_msg
.type() == id
) {
29 result
.push_back(std::make_pair(cur_params
, cur_msg
));
38 ResourceMessageTestSink::ResourceMessageTestSink() {
41 ResourceMessageTestSink::~ResourceMessageTestSink() {
44 bool ResourceMessageTestSink::Send(IPC::Message
* msg
) {
46 scoped_ptr
<IPC::MessageReplyDeserializer
> reply_deserializer
;
48 reply_deserializer
.reset(
49 static_cast<IPC::SyncMessage
*>(msg
)->GetReplyDeserializer());
50 message_id
= IPC::SyncMessage::GetMessageId(*msg
);
52 bool result
= IPC::TestSink::Send(msg
); // Deletes |msg|.
53 if (sync_reply_msg_
.get()) {
54 // |sync_reply_msg_| should always be a reply to the pending sync message.
55 DCHECK(IPC::SyncMessage::IsMessageReplyTo(*sync_reply_msg_
.get(),
57 reply_deserializer
->SerializeOutputParameters(*sync_reply_msg_
.get());
58 sync_reply_msg_
.reset(NULL
);
63 void ResourceMessageTestSink::SetSyncReplyMessage(IPC::Message
* reply_msg
) {
64 DCHECK(!sync_reply_msg_
.get());
65 sync_reply_msg_
.reset(reply_msg
);
68 bool ResourceMessageTestSink::GetFirstResourceCallMatching(
70 ResourceMessageCallParams
* params
,
71 IPC::Message
* nested_msg
) const {
72 ResourceCallVector matching_messages
=
73 GetAllResourceMessagesMatching
<PpapiHostMsg_ResourceCall
,
74 ResourceMessageCallParams
>(*this, id
);
75 if (matching_messages
.empty())
78 *params
= matching_messages
[0].first
;
79 *nested_msg
= matching_messages
[0].second
;
83 bool ResourceMessageTestSink::GetFirstResourceReplyMatching(
85 ResourceMessageReplyParams
* params
,
86 IPC::Message
* nested_msg
) {
87 ResourceReplyVector matching_messages
=
88 GetAllResourceMessagesMatching
<PpapiPluginMsg_ResourceReply
,
89 ResourceMessageReplyParams
>(*this, id
);
90 if (matching_messages
.empty())
93 *params
= matching_messages
[0].first
;
94 *nested_msg
= matching_messages
[0].second
;
98 ResourceMessageTestSink::ResourceCallVector
99 ResourceMessageTestSink::GetAllResourceCallsMatching(uint32 id
) {
100 return GetAllResourceMessagesMatching
<PpapiHostMsg_ResourceCall
,
101 ResourceMessageCallParams
>(*this, id
);
104 ResourceMessageTestSink::ResourceReplyVector
105 ResourceMessageTestSink::GetAllResourceRepliesMatching(uint32 id
) {
106 return GetAllResourceMessagesMatching
<PpapiPluginMsg_ResourceReply
,
107 ResourceMessageReplyParams
>(*this, id
);
110 ResourceSyncCallHandler::ResourceSyncCallHandler(
111 ResourceMessageTestSink
* test_sink
,
112 uint32 incoming_type
,
114 const IPC::Message
& reply_msg
)
115 : test_sink_(test_sink
),
116 incoming_type_(incoming_type
),
118 serialized_handle_(NULL
),
119 reply_msg_(reply_msg
) {
122 ResourceSyncCallHandler::~ResourceSyncCallHandler() {
125 bool ResourceSyncCallHandler::OnMessageReceived(const IPC::Message
& msg
) {
126 if (msg
.type() != PpapiHostMsg_ResourceSyncCall::ID
)
128 PpapiHostMsg_ResourceSyncCall::Schema::SendParam send_params
;
129 bool success
= PpapiHostMsg_ResourceSyncCall::ReadSendParam(
132 ResourceMessageCallParams call_params
= send_params
.a
;
133 IPC::Message call_msg
= send_params
.b
;
134 if (call_msg
.type() != incoming_type_
)
136 IPC::Message
* wrapper_reply_msg
= IPC::SyncMessage::GenerateReply(&msg
);
137 ResourceMessageReplyParams
reply_params(call_params
.pp_resource(),
138 call_params
.sequence());
139 reply_params
.set_result(result_
);
140 if (serialized_handle_
)
141 reply_params
.AppendHandle(*serialized_handle_
);
142 PpapiHostMsg_ResourceSyncCall::WriteReplyParams(
143 wrapper_reply_msg
, reply_params
, reply_msg_
);
144 test_sink_
->SetSyncReplyMessage(wrapper_reply_msg
);
146 // Stash a copy of the message for inspection later.
147 last_handled_msg_
= call_msg
;