summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/plugin_resource.cc
blob: 1cfc3f10c6e695d3b7f0384fce7b654827b43d93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/proxy/plugin_resource.h"

#include "ppapi/c/pp_errors.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/proxy/resource_message_params.h"

namespace ppapi {
namespace proxy {

PluginResource::PluginResource(Connection connection, PP_Instance instance)
    : Resource(OBJECT_IS_PROXY, instance),
      connection_(connection),
      next_sequence_number_(0),
      sent_create_to_browser_(false),
      sent_create_to_renderer_(false) {
}

PluginResource::~PluginResource() {
  if (sent_create_to_browser_) {
    connection_.browser_sender->Send(
        new PpapiHostMsg_ResourceDestroyed(pp_resource()));
  }
  if (sent_create_to_renderer_) {
    connection_.renderer_sender->Send(
        new PpapiHostMsg_ResourceDestroyed(pp_resource()));
  }
}

void PluginResource::OnReplyReceived(
    const proxy::ResourceMessageReplyParams& params,
    const IPC::Message& msg) {
  // Grab the callback for the reply sequence number and run it with |msg|.
  CallbackMap::iterator it = callbacks_.find(params.sequence());
  if (it == callbacks_.end()) {
    DCHECK(false) << "Callback does not exist for an expected sequence number.";
  } else {
    scoped_refptr<PluginResourceCallbackBase> callback = it->second;
    callbacks_.erase(it);
    callback->Run(params, msg);
  }
}

void PluginResource::SendCreateToBrowser(const IPC::Message& msg) {
  DCHECK(!sent_create_to_browser_);
  sent_create_to_browser_ = true;
  ResourceMessageCallParams params(pp_resource(),
                                   next_sequence_number_++);
  connection_.browser_sender->Send(
      new PpapiHostMsg_ResourceCreated(params, pp_instance(), msg));
}

void PluginResource::SendCreateToRenderer(const IPC::Message& msg) {
  DCHECK(!sent_create_to_renderer_);
  sent_create_to_renderer_ = true;
  ResourceMessageCallParams params(pp_resource(),
                                   next_sequence_number_++);
  connection_.renderer_sender->Send(
      new PpapiHostMsg_ResourceCreated(params, pp_instance(), msg));
}

void PluginResource::PostToBrowser(const IPC::Message& msg) {
  ResourceMessageCallParams params(pp_resource(),
                                   next_sequence_number_++);
  SendResourceCall(connection_.browser_sender, params, msg);
}

void PluginResource::PostToRenderer(const IPC::Message& msg) {
  ResourceMessageCallParams params(pp_resource(),
                                   next_sequence_number_++);
  SendResourceCall(connection_.renderer_sender, params, msg);
}

bool PluginResource::SendResourceCall(
    IPC::Sender* sender,
    const ResourceMessageCallParams& call_params,
    const IPC::Message& nested_msg) {
  return sender->Send(new PpapiHostMsg_ResourceCall(call_params, nested_msg));
}

int32_t PluginResource::CallBrowserSync(const IPC::Message& msg,
                                        IPC::Message* reply) {
  ResourceMessageCallParams params(pp_resource(),
                                   next_sequence_number_++);
  params.set_has_callback();
  ResourceMessageReplyParams reply_params;
  bool success =
      connection_.browser_sender->Send(new PpapiHostMsg_ResourceSyncCall(
          params, msg, &reply_params, reply));
  if (success)
    return reply_params.result();
  return PP_ERROR_FAILED;
}

int32_t PluginResource::CallRendererSync(const IPC::Message& msg,
                                         IPC::Message* reply) {
  ResourceMessageCallParams params(pp_resource(),
                                   next_sequence_number_++);
  params.set_has_callback();
  ResourceMessageReplyParams reply_params;
  bool success =
      connection_.renderer_sender->Send(new PpapiHostMsg_ResourceSyncCall(
          params, msg, &reply_params, reply));
  if (success)
    return reply_params.result();
  return PP_ERROR_FAILED;
}

}  // namespace proxy
}  // namespace ppapi