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 GetFirstResource[Call|Reply]Matching.
17 template<class WrapperMessage
, class Params
>
18 bool GetFirstResourceMessageMatching(const ResourceMessageTestSink
& sink
,
21 IPC::Message
* nested_msg
) {
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
) {
30 *nested_msg
= cur_msg
;
40 ResourceMessageTestSink::ResourceMessageTestSink() {
43 ResourceMessageTestSink::~ResourceMessageTestSink() {
46 bool ResourceMessageTestSink::Send(IPC::Message
* msg
) {
48 scoped_ptr
<IPC::MessageReplyDeserializer
> reply_deserializer
;
50 reply_deserializer
.reset(
51 static_cast<IPC::SyncMessage
*>(msg
)->GetReplyDeserializer());
52 message_id
= IPC::SyncMessage::GetMessageId(*msg
);
54 bool result
= IPC::TestSink::Send(msg
); // Deletes |msg|.
55 if (sync_reply_msg_
.get()) {
56 // |sync_reply_msg_| should always be a reply to the pending sync message.
57 DCHECK(IPC::SyncMessage::IsMessageReplyTo(*sync_reply_msg_
.get(),
59 reply_deserializer
->SerializeOutputParameters(*sync_reply_msg_
.get());
60 sync_reply_msg_
.reset(NULL
);
65 void ResourceMessageTestSink::SetSyncReplyMessage(IPC::Message
* reply_msg
) {
66 DCHECK(!sync_reply_msg_
.get());
67 sync_reply_msg_
.reset(reply_msg
);
70 bool ResourceMessageTestSink::GetFirstResourceCallMatching(
72 ResourceMessageCallParams
* params
,
73 IPC::Message
* nested_msg
) const {
74 return GetFirstResourceMessageMatching
<PpapiHostMsg_ResourceCall
,
75 ResourceMessageCallParams
>(
76 *this, id
, params
, nested_msg
);
79 bool ResourceMessageTestSink::GetFirstResourceReplyMatching(
81 ResourceMessageReplyParams
* params
,
82 IPC::Message
* nested_msg
) {
83 return GetFirstResourceMessageMatching
<PpapiPluginMsg_ResourceReply
,
84 ResourceMessageReplyParams
>(
85 *this, id
, params
, nested_msg
);
88 ResourceSyncCallHandler::ResourceSyncCallHandler(
89 ResourceMessageTestSink
* test_sink
,
92 const IPC::Message
& reply_msg
)
93 : test_sink_(test_sink
),
94 incoming_type_(incoming_type
),
96 serialized_handle_(NULL
),
97 reply_msg_(reply_msg
) {
100 ResourceSyncCallHandler::~ResourceSyncCallHandler() {
103 bool ResourceSyncCallHandler::OnMessageReceived(const IPC::Message
& msg
) {
104 if (msg
.type() != PpapiHostMsg_ResourceSyncCall::ID
)
106 PpapiHostMsg_ResourceSyncCall::Schema::SendParam send_params
;
107 bool success
= PpapiHostMsg_ResourceSyncCall::ReadSendParam(
110 ResourceMessageCallParams call_params
= send_params
.a
;
111 IPC::Message call_msg
= send_params
.b
;
112 if (call_msg
.type() != incoming_type_
)
114 IPC::Message
* wrapper_reply_msg
= IPC::SyncMessage::GenerateReply(&msg
);
115 ResourceMessageReplyParams
reply_params(call_params
.pp_resource(),
116 call_params
.sequence());
117 reply_params
.set_result(result_
);
118 if (serialized_handle_
)
119 reply_params
.AppendHandle(*serialized_handle_
);
120 PpapiHostMsg_ResourceSyncCall::WriteReplyParams(
121 wrapper_reply_msg
, reply_params
, reply_msg_
);
122 test_sink_
->SetSyncReplyMessage(wrapper_reply_msg
);
124 // Stash a copy of the message for inspection later.
125 last_handled_msg_
= call_msg
;