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_RESOURCE_HOST_H_
      6 #define PPAPI_HOST_RESOURCE_HOST_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "ppapi/c/pp_resource.h"
     13 #include "ppapi/host/ppapi_host_export.h"
     14 #include "ppapi/host/resource_message_handler.h"
     15 #include "ppapi/shared_impl/host_resource.h"
     16 
     17 namespace IPC {
     18 class Message;
     19 }
     20 
     21 namespace ppapi {
     22 namespace host {
     23 
     24 struct HostMessageContext;
     25 class PpapiHost;
     26 class ResourceMessageFilter;
     27 
     28 // Some (but not all) resources have a corresponding object in the host side
     29 // that is kept alive as long as the resource in the plugin is alive. This is
     30 // the base class for such objects.
     31 class PPAPI_HOST_EXPORT ResourceHost : public ResourceMessageHandler {
     32  public:
     33   ResourceHost(PpapiHost* host, PP_Instance instance, PP_Resource resource);
     34   virtual ~ResourceHost();
     35 
     36   PpapiHost* host() { return host_; }
     37   PP_Instance pp_instance() const { return pp_instance_; }
     38   PP_Resource pp_resource() const { return pp_resource_; }
     39 
     40   // This runs any message filters in |message_filters_|. If the message is not
     41   // handled by these filters then the host's own message handler is run. True
     42   // is always returned (the message will always be handled in some way).
     43   virtual bool HandleMessage(const IPC::Message& msg,
     44                              HostMessageContext* context) OVERRIDE;
     45 
     46   // Sets the PP_Resource ID when the plugin attaches to a pending resource
     47   // host. This will notify subclasses by calling
     48   // DidConnectPendingHostToResource.
     49   //
     50   // The current PP_Resource for all pending hosts should be 0. See
     51   // PpapiHostMsg_AttachToPendingHost.
     52   void SetPPResourceForPendingHost(PP_Resource pp_resource);
     53 
     54   virtual void SendReply(const ReplyMessageContext& context,
     55                          const IPC::Message& msg) OVERRIDE;
     56 
     57   // Simple RTTI. A subclass that is a host for one of these APIs will override
     58   // the appropriate function and return true.
     59   virtual bool IsFileRefHost();
     60   virtual bool IsFileSystemHost();
     61   virtual bool IsGraphics2DHost();
     62 
     63  protected:
     64   // Adds a ResourceMessageFilter to handle resource messages. Incoming
     65   // messages will be passed to the handlers of these filters before being
     66   // handled by the resource host's own message handler. This allows
     67   // ResourceHosts to easily handle messages on other threads.
     68   void AddFilter(scoped_refptr<ResourceMessageFilter> filter);
     69 
     70   // Called when this resource host is pending and the corresponding plugin has
     71   // just connected to it. The host resource subclass can implement this
     72   // function if it wants to do processing (typically sending queued data).
     73   //
     74   // The PP_Resource will be valid for this call but not before.
     75   virtual void DidConnectPendingHostToResource() {}
     76 
     77  private:
     78   // The host that owns this object.
     79   PpapiHost* host_;
     80 
     81   PP_Instance pp_instance_;
     82   PP_Resource pp_resource_;
     83 
     84   // A vector of message filters which the host will forward incoming resource
     85   // messages to.
     86   std::vector<scoped_refptr<ResourceMessageFilter> > message_filters_;
     87 
     88   DISALLOW_COPY_AND_ASSIGN(ResourceHost);
     89 };
     90 
     91 }  // namespace host
     92 }  // namespace ppapi
     93 
     94 #endif  // PPAPI_HOST_RESOURCE_HOST_H_
     95