Fix the EULA sentinel file path determination.
[chromium-blink-merge.git] / ppapi / proxy / resource_message_test_sink.cc
bloba17e1d5cf0f3d598d7b0d3f94c561769ff449fd5
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"
11 namespace ppapi {
12 namespace proxy {
14 namespace {
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,
20 uint32 id) {
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) {
25 Params cur_params;
26 IPC::Message cur_msg;
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));
33 return result;
36 } // namespace
38 ResourceMessageTestSink::ResourceMessageTestSink() {
41 ResourceMessageTestSink::~ResourceMessageTestSink() {
44 bool ResourceMessageTestSink::Send(IPC::Message* msg) {
45 int message_id = 0;
46 scoped_ptr<IPC::MessageReplyDeserializer> reply_deserializer;
47 if (msg->is_sync()) {
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(),
56 message_id));
57 reply_deserializer->SerializeOutputParameters(*sync_reply_msg_.get());
58 sync_reply_msg_.reset(NULL);
60 return result;
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(
69 uint32 id,
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())
76 return false;
78 *params = matching_messages[0].first;
79 *nested_msg = matching_messages[0].second;
80 return true;
83 bool ResourceMessageTestSink::GetFirstResourceReplyMatching(
84 uint32 id,
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())
91 return false;
93 *params = matching_messages[0].first;
94 *nested_msg = matching_messages[0].second;
95 return true;
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,
113 int32_t result,
114 const IPC::Message& reply_msg)
115 : test_sink_(test_sink),
116 incoming_type_(incoming_type),
117 result_(result),
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)
127 return false;
128 PpapiHostMsg_ResourceSyncCall::Schema::SendParam send_params;
129 bool success = PpapiHostMsg_ResourceSyncCall::ReadSendParam(
130 &msg, &send_params);
131 DCHECK(success);
132 ResourceMessageCallParams call_params = send_params.a;
133 IPC::Message call_msg = send_params.b;
134 if (call_msg.type() != incoming_type_)
135 return false;
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;
148 return true;
151 } // namespace proxy
152 } // namespace ppapi