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 #ifndef PPAPI_HOST_PPAPI_HOST_H_
6 #define PPAPI_HOST_PPAPI_HOST_H_
11 #include "base/compiler_specific.h"
12 #include "base/memory/linked_ptr.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/observer_list.h"
16 #include "ipc/ipc_listener.h"
17 #include "ipc/ipc_sender.h"
18 #include "ppapi/c/pp_instance.h"
19 #include "ppapi/c/pp_resource.h"
20 #include "ppapi/host/ppapi_host_export.h"
21 #include "ppapi/shared_impl/ppapi_permissions.h"
26 class ResourceMessageCallParams
;
27 class ResourceMessageReplyParams
;
28 class SerializedHandle
;
34 struct HostMessageContext
;
35 class InstanceMessageFilter
;
36 struct ReplyMessageContext
;
39 // The host provides routing and tracking for resource message calls that
40 // come from the plugin to the host (browser or renderer), and the
41 // corresponding replies.
42 class PPAPI_HOST_EXPORT PpapiHost
: public IPC::Sender
, public IPC::Listener
{
44 // The sender is the channel to the plugin for outgoing messages.
45 // Normally the creator will add filters for resource creation messages
46 // (AddHostFactoryFilter) and instance messages (AddInstanceMessageFilter)
47 // after construction.
48 PpapiHost(IPC::Sender
* sender
, const PpapiPermissions
& perms
);
51 const PpapiPermissions
& permissions() const { return permissions_
; }
53 // Sender implementation. Forwards to the sender_.
54 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
56 // Listener implementation.
57 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
59 // Sends the given reply message to the plugin.
60 void SendReply(const ReplyMessageContext
& context
,
61 const IPC::Message
& msg
);
63 // Sends the given unsolicited reply message to the plugin.
64 void SendUnsolicitedReply(PP_Resource resource
, const IPC::Message
& msg
);
66 // Similar to |SendUnsolicitedReply()|, but also sends handles.
67 void SendUnsolicitedReplyWithHandles(
69 const IPC::Message
& msg
,
70 const std::vector
<proxy::SerializedHandle
>& handles
);
72 // Create a ResourceHost with the given |nested_msg|.
73 scoped_ptr
<ResourceHost
> CreateResourceHost(
74 const proxy::ResourceMessageCallParams
& params
,
76 const IPC::Message
& nested_msg
);
78 // Adds the given host resource as a pending one (with no corresponding
79 // PluginResource object and no PP_Resource ID yet). The pending resource ID
80 // is returned. See PpapiHostMsg_AttachToPendingHost.
81 int AddPendingResourceHost(scoped_ptr
<ResourceHost
> resource_host
);
83 // Adds the given host factory filter to the host. The PpapiHost will take
84 // ownership of the pointer.
85 void AddHostFactoryFilter(scoped_ptr
<HostFactory
> filter
);
87 // Adds the given message filter to the host. The PpapiHost will take
88 // ownership of the pointer.
89 void AddInstanceMessageFilter(scoped_ptr
<InstanceMessageFilter
> filter
);
91 // Returns null if the resource doesn't exist.
92 host::ResourceHost
* GetResourceHost(PP_Resource resource
) const;
95 friend class InstanceMessageFilter
;
97 void HandleResourceCall(
98 const proxy::ResourceMessageCallParams
& params
,
99 const IPC::Message
& nested_msg
,
100 HostMessageContext
* context
);
103 void OnHostMsgResourceCall(const proxy::ResourceMessageCallParams
& params
,
104 const IPC::Message
& nested_msg
);
105 void OnHostMsgInProcessResourceCall(
107 const proxy::ResourceMessageCallParams
& params
,
108 const IPC::Message
& nested_msg
);
109 void OnHostMsgResourceSyncCall(
110 const proxy::ResourceMessageCallParams
& params
,
111 const IPC::Message
& nested_msg
,
112 IPC::Message
* reply_msg
);
113 void OnHostMsgResourceCreated(const proxy::ResourceMessageCallParams
& param
,
114 PP_Instance instance
,
115 const IPC::Message
& nested_msg
);
116 void OnHostMsgAttachToPendingHost(PP_Resource resource
, int pending_host_id
);
117 void OnHostMsgResourceDestroyed(PP_Resource resource
);
119 // Non-owning pointer.
120 IPC::Sender
* sender_
;
122 PpapiPermissions permissions_
;
124 // Filters for resource creation messages. Note that since we don't support
125 // deleting these dynamically we don't need to worry about modifications
126 // during iteration. If we add that capability, this should be replaced with
128 ScopedVector
<HostFactory
> host_factory_filters_
;
130 // Filters for instance messages. Note that since we don't support deleting
131 // these dynamically we don't need to worry about modifications during
132 // iteration. If we add that capability, this should be replaced with an
134 ScopedVector
<InstanceMessageFilter
> instance_message_filters_
;
136 typedef std::map
<PP_Resource
, linked_ptr
<ResourceHost
> > ResourceMap
;
137 ResourceMap resources_
;
139 // Resources that have been created in the host and have not yet had the
140 // corresponding PluginResource associated with them.
141 // See PpapiHostMsg_AttachToPendingHost.
142 typedef std::map
<int, linked_ptr
<ResourceHost
> > PendingHostResourceMap
;
143 PendingHostResourceMap pending_resource_hosts_
;
144 int next_pending_resource_host_id_
;
146 DISALLOW_COPY_AND_ASSIGN(PpapiHost
);
152 #endif // PPAPI_HOST_PPAPIE_HOST_H_