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_
10 #include "base/compiler_specific.h"
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/observer_list.h"
15 #include "ipc/ipc_listener.h"
16 #include "ipc/ipc_sender.h"
17 #include "ppapi/c/pp_instance.h"
18 #include "ppapi/c/pp_resource.h"
19 #include "ppapi/host/ppapi_host_export.h"
20 #include "ppapi/shared_impl/ppapi_permissions.h"
25 class ResourceMessageCallParams
;
26 class ResourceMessageReplyParams
;
32 struct HostMessageContext
;
33 class InstanceMessageFilter
;
34 struct ReplyMessageContext
;
37 // The host provides routing and tracking for resource message calls that
38 // come from the plugin to the host (browser or renderer), and the
39 // corresponding replies.
40 class PPAPI_HOST_EXPORT PpapiHost
: public IPC::Sender
, public IPC::Listener
{
42 // The sender is the channel to the plugin for outgoing messages.
43 // Normally the creator will add filters for resource creation messages
44 // (AddHostFactoryFilter) and instance messages (AddInstanceMessageFilter)
45 // after construction.
46 PpapiHost(IPC::Sender
* sender
, const PpapiPermissions
& perms
);
49 const PpapiPermissions
& permissions() const { return permissions_
; }
51 // Sender implementation. Forwards to the sender_.
52 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
54 // Listener implementation.
55 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
57 // Sends the given reply message to the plugin.
58 void SendReply(const ReplyMessageContext
& context
,
59 const IPC::Message
& msg
);
61 // Sends the given unsolicited reply message to the plugin.
62 void SendUnsolicitedReply(PP_Resource resource
, const IPC::Message
& msg
);
64 // Create a ResourceHost with the given |nested_msg|.
65 scoped_ptr
<ResourceHost
> CreateResourceHost(
66 const proxy::ResourceMessageCallParams
& params
,
68 const IPC::Message
& nested_msg
);
70 // Adds the given host resource as a pending one (with no corresponding
71 // PluginResource object and no PP_Resource ID yet). The pending resource ID
72 // is returned. See PpapiHostMsg_AttachToPendingHost.
73 int AddPendingResourceHost(scoped_ptr
<ResourceHost
> resource_host
);
75 // Adds the given host factory filter to the host. The PpapiHost will take
76 // ownership of the pointer.
77 void AddHostFactoryFilter(scoped_ptr
<HostFactory
> filter
);
79 // Adds the given message filter to the host. The PpapiHost will take
80 // ownership of the pointer.
81 void AddInstanceMessageFilter(scoped_ptr
<InstanceMessageFilter
> filter
);
83 // Returns null if the resource doesn't exist.
84 host::ResourceHost
* GetResourceHost(PP_Resource resource
) const;
87 friend class InstanceMessageFilter
;
89 void HandleResourceCall(
90 const proxy::ResourceMessageCallParams
& params
,
91 const IPC::Message
& nested_msg
,
92 HostMessageContext
* context
);
95 void OnHostMsgResourceCall(const proxy::ResourceMessageCallParams
& params
,
96 const IPC::Message
& nested_msg
);
97 void OnHostMsgInProcessResourceCall(
99 const proxy::ResourceMessageCallParams
& params
,
100 const IPC::Message
& nested_msg
);
101 void OnHostMsgResourceSyncCall(
102 const proxy::ResourceMessageCallParams
& params
,
103 const IPC::Message
& nested_msg
,
104 IPC::Message
* reply_msg
);
105 void OnHostMsgResourceCreated(const proxy::ResourceMessageCallParams
& param
,
106 PP_Instance instance
,
107 const IPC::Message
& nested_msg
);
108 void OnHostMsgAttachToPendingHost(PP_Resource resource
, int pending_host_id
);
109 void OnHostMsgResourceDestroyed(PP_Resource resource
);
111 // Non-owning pointer.
112 IPC::Sender
* sender_
;
114 PpapiPermissions permissions_
;
116 // Filters for resource creation messages. Note that since we don't support
117 // deleting these dynamically we don't need to worry about modifications
118 // during iteration. If we add that capability, this should be replaced with
120 ScopedVector
<HostFactory
> host_factory_filters_
;
122 // Filters for instance messages. Note that since we don't support deleting
123 // these dynamically we don't need to worry about modifications during
124 // iteration. If we add that capability, this should be replaced with an
126 ScopedVector
<InstanceMessageFilter
> instance_message_filters_
;
128 typedef std::map
<PP_Resource
, linked_ptr
<ResourceHost
> > ResourceMap
;
129 ResourceMap resources_
;
131 // Resources that have been created in the host and have not yet had the
132 // corresponding PluginResource associated with them.
133 // See PpapiHostMsg_AttachToPendingHost.
134 typedef std::map
<int, linked_ptr
<ResourceHost
> > PendingHostResourceMap
;
135 PendingHostResourceMap pending_resource_hosts_
;
136 int next_pending_resource_host_id_
;
138 DISALLOW_COPY_AND_ASSIGN(PpapiHost
);
144 #endif // PPAPI_HOST_PPAPIE_HOST_H_