Home | History | Annotate | Download | only in host
      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 #ifndef PPAPI_HOST_PPAPI_HOST_H_
      6 #define PPAPI_HOST_PPAPI_HOST_H_
      7 
      8 #include <map>
      9 
     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"
     21 
     22 namespace ppapi {
     23 
     24 namespace proxy {
     25 class ResourceMessageCallParams;
     26 class ResourceMessageReplyParams;
     27 }
     28 
     29 namespace host {
     30 
     31 class HostFactory;
     32 struct HostMessageContext;
     33 class InstanceMessageFilter;
     34 struct ReplyMessageContext;
     35 class ResourceHost;
     36 
     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 {
     41  public:
     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);
     47   virtual ~PpapiHost();
     48 
     49   const PpapiPermissions& permissions() const { return permissions_; }
     50 
     51   // Sender implementation. Forwards to the sender_.
     52   virtual bool Send(IPC::Message* msg) OVERRIDE;
     53 
     54   // Listener implementation.
     55   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
     56 
     57   // Sends the given reply message to the plugin.
     58   void SendReply(const ReplyMessageContext& context,
     59                  const IPC::Message& msg);
     60 
     61   // Sends the given unsolicited reply message to the plugin.
     62   void SendUnsolicitedReply(PP_Resource resource, const IPC::Message& msg);
     63 
     64   // Create a ResourceHost with the given |nested_msg|.
     65   scoped_ptr<ResourceHost> CreateResourceHost(
     66       const proxy::ResourceMessageCallParams& params,
     67       PP_Instance instance,
     68       const IPC::Message& nested_msg);
     69 
     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);
     74 
     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);
     78 
     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);
     82 
     83   // Returns null if the resource doesn't exist.
     84   host::ResourceHost* GetResourceHost(PP_Resource resource) const;
     85 
     86  private:
     87   friend class InstanceMessageFilter;
     88 
     89   void HandleResourceCall(
     90       const proxy::ResourceMessageCallParams& params,
     91       const IPC::Message& nested_msg,
     92       HostMessageContext* context);
     93 
     94   // Message handlers.
     95   void OnHostMsgResourceCall(const proxy::ResourceMessageCallParams& params,
     96                              const IPC::Message& nested_msg);
     97   void OnHostMsgInProcessResourceCall(
     98       int routing_id,
     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);
    110 
    111   // Non-owning pointer.
    112   IPC::Sender* sender_;
    113 
    114   PpapiPermissions permissions_;
    115 
    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
    119   // an ObserverList.
    120   ScopedVector<HostFactory> host_factory_filters_;
    121 
    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
    125   // ObserverList.
    126   ScopedVector<InstanceMessageFilter> instance_message_filters_;
    127 
    128   typedef std::map<PP_Resource, linked_ptr<ResourceHost> > ResourceMap;
    129   ResourceMap resources_;
    130 
    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_;
    137 
    138   DISALLOW_COPY_AND_ASSIGN(PpapiHost);
    139 };
    140 
    141 }  // namespace host
    142 }  // namespace ppapi
    143 
    144 #endif  // PPAPI_HOST_PPAPIE_HOST_H_
    145