blob: 1cfc3f10c6e695d3b7f0384fce7b654827b43d93 [file] [log] [blame]
[email protected]eccf80312012-07-14 15:43:421// 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/proxy/plugin_resource.h"
6
[email protected]ff44fc12012-10-03 00:52:167#include "ppapi/c/pp_errors.h"
[email protected]eccf80312012-07-14 15:43:428#include "ppapi/proxy/ppapi_messages.h"
9#include "ppapi/proxy/resource_message_params.h"
10
11namespace ppapi {
12namespace proxy {
13
[email protected]93df81e2012-08-10 22:22:4614PluginResource::PluginResource(Connection connection, PP_Instance instance)
[email protected]eccf80312012-07-14 15:43:4215 : Resource(OBJECT_IS_PROXY, instance),
[email protected]93df81e2012-08-10 22:22:4616 connection_(connection),
[email protected]eccf80312012-07-14 15:43:4217 next_sequence_number_(0),
[email protected]93df81e2012-08-10 22:22:4618 sent_create_to_browser_(false),
[email protected]eccf80312012-07-14 15:43:4219 sent_create_to_renderer_(false) {
20}
21
22PluginResource::~PluginResource() {
[email protected]93df81e2012-08-10 22:22:4623 if (sent_create_to_browser_) {
24 connection_.browser_sender->Send(
25 new PpapiHostMsg_ResourceDestroyed(pp_resource()));
26 }
27 if (sent_create_to_renderer_) {
28 connection_.renderer_sender->Send(
29 new PpapiHostMsg_ResourceDestroyed(pp_resource()));
30 }
[email protected]eccf80312012-07-14 15:43:4231}
32
[email protected]e1f5c9b2012-10-04 00:07:4433void PluginResource::OnReplyReceived(
34 const proxy::ResourceMessageReplyParams& params,
35 const IPC::Message& msg) {
36 // Grab the callback for the reply sequence number and run it with |msg|.
37 CallbackMap::iterator it = callbacks_.find(params.sequence());
38 if (it == callbacks_.end()) {
39 DCHECK(false) << "Callback does not exist for an expected sequence number.";
40 } else {
41 scoped_refptr<PluginResourceCallbackBase> callback = it->second;
42 callbacks_.erase(it);
43 callback->Run(params, msg);
44 }
45}
46
[email protected]93df81e2012-08-10 22:22:4647void PluginResource::SendCreateToBrowser(const IPC::Message& msg) {
48 DCHECK(!sent_create_to_browser_);
49 sent_create_to_browser_ = true;
50 ResourceMessageCallParams params(pp_resource(),
51 next_sequence_number_++);
52 connection_.browser_sender->Send(
53 new PpapiHostMsg_ResourceCreated(params, pp_instance(), msg));
[email protected]eccf80312012-07-14 15:43:4254}
55
56void PluginResource::SendCreateToRenderer(const IPC::Message& msg) {
57 DCHECK(!sent_create_to_renderer_);
58 sent_create_to_renderer_ = true;
59 ResourceMessageCallParams params(pp_resource(),
60 next_sequence_number_++);
[email protected]93df81e2012-08-10 22:22:4661 connection_.renderer_sender->Send(
62 new PpapiHostMsg_ResourceCreated(params, pp_instance(), msg));
63}
64
65void PluginResource::PostToBrowser(const IPC::Message& msg) {
66 ResourceMessageCallParams params(pp_resource(),
67 next_sequence_number_++);
[email protected]e1f5c9b2012-10-04 00:07:4468 SendResourceCall(connection_.browser_sender, params, msg);
[email protected]eccf80312012-07-14 15:43:4269}
70
71void PluginResource::PostToRenderer(const IPC::Message& msg) {
72 ResourceMessageCallParams params(pp_resource(),
73 next_sequence_number_++);
[email protected]e1f5c9b2012-10-04 00:07:4474 SendResourceCall(connection_.renderer_sender, params, msg);
[email protected]93df81e2012-08-10 22:22:4675}
76
[email protected]e1f5c9b2012-10-04 00:07:4477bool PluginResource::SendResourceCall(
78 IPC::Sender* sender,
79 const ResourceMessageCallParams& call_params,
80 const IPC::Message& nested_msg) {
81 return sender->Send(new PpapiHostMsg_ResourceCall(call_params, nested_msg));
[email protected]eccf80312012-07-14 15:43:4282}
83
[email protected]ff44fc12012-10-03 00:52:1684int32_t PluginResource::CallBrowserSync(const IPC::Message& msg,
85 IPC::Message* reply) {
86 ResourceMessageCallParams params(pp_resource(),
87 next_sequence_number_++);
88 params.set_has_callback();
89 ResourceMessageReplyParams reply_params;
90 bool success =
91 connection_.browser_sender->Send(new PpapiHostMsg_ResourceSyncCall(
92 params, msg, &reply_params, reply));
93 if (success)
94 return reply_params.result();
95 return PP_ERROR_FAILED;
96}
97
98int32_t PluginResource::CallRendererSync(const IPC::Message& msg,
99 IPC::Message* reply) {
100 ResourceMessageCallParams params(pp_resource(),
101 next_sequence_number_++);
102 params.set_has_callback();
103 ResourceMessageReplyParams reply_params;
104 bool success =
105 connection_.renderer_sender->Send(new PpapiHostMsg_ResourceSyncCall(
106 params, msg, &reply_params, reply));
107 if (success)
108 return reply_params.result();
109 return PP_ERROR_FAILED;
110}
111
[email protected]eccf80312012-07-14 15:43:42112} // namespace proxy
113} // namespace ppapi