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#include "ppapi/host/resource_message_handler.h"
6
7#include "base/logging.h"
8#include "ipc/ipc_message.h"
9#include "ppapi/c/pp_errors.h"
10#include "ppapi/host/host_message_context.h"
11
12namespace ppapi {
13namespace host {
14
15ResourceMessageHandler::ResourceMessageHandler() {
16}
17
18ResourceMessageHandler::~ResourceMessageHandler() {
19}
20
21void ResourceMessageHandler::RunMessageHandlerAndReply(
22    const IPC::Message& msg,
23    HostMessageContext* context) {
24  ReplyMessageContext reply_context = context->MakeReplyMessageContext();
25  // CAUTION: Handling the message may cause the destruction of this object.
26  // The message handler should ensure that if there is a chance that the
27  // object will be destroyed, PP_OK_COMPLETIONPENDING is returned as the
28  // result of the message handler. Otherwise the code below will attempt to
29  // send a reply message on a destroyed object.
30  reply_context.params.set_result(OnResourceMessageReceived(msg, context));
31
32  // Sanity check the resource handler. Note if the result was
33  // "completion pending" the resource host may have already sent the reply.
34  if (reply_context.params.result() == PP_OK_COMPLETIONPENDING) {
35    // Message handler should have only returned a pending result if a
36    // response will be sent to the plugin.
37    DCHECK(context->params.has_callback());
38
39    // Message handler should not have written a message to be returned if
40    // completion is pending.
41    DCHECK(context->reply_msg.type() == 0);
42  } else if (!context->params.has_callback()) {
43    // When no response is required, the message handler should not have
44    // written a message to be returned.
45    DCHECK(context->reply_msg.type() == 0);
46
47    // If there is no callback and the result of running the message handler
48    // was not PP_OK the client won't find out.
49    DLOG_IF(WARNING, reply_context.params.result() != PP_OK)
50        << "'Post' message handler failed to complete successfully.";
51  }
52
53  if (context->params.has_callback() &&
54      reply_context.params.result() != PP_OK_COMPLETIONPENDING)
55    SendReply(reply_context, context->reply_msg);
56}
57
58int32_t ResourceMessageHandler::OnResourceMessageReceived(
59    const IPC::Message& msg,
60    HostMessageContext* context) {
61  return PP_ERROR_NOTSUPPORTED;
62}
63
64}  // namespace host
65}  // namespace ppapi
66