1 // Copyright 2013 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 // A proxy for NPObject that sends all calls to the object to an NPObjectStub 6 // running in a different process. 7 8 #ifndef CONTENT_CHILD_NPAPI_NPOBJECT_PROXY_H_ 9 #define CONTENT_CHILD_NPAPI_NPOBJECT_PROXY_H_ 10 11 #include "base/memory/ref_counted.h" 12 #include "content/child/npapi/npobject_base.h" 13 #include "ipc/ipc_listener.h" 14 #include "ipc/ipc_sender.h" 15 #include "third_party/npapi/bindings/npruntime.h" 16 #include "ui/gfx/native_widget_types.h" 17 #include "url/gurl.h" 18 19 struct NPObject; 20 21 namespace content { 22 class NPChannelBase; 23 24 // When running a plugin in a different process from the renderer, we need to 25 // proxy calls to NPObjects across process boundaries. This happens both ways, 26 // as a plugin can get an NPObject for the window, and a page can get an 27 // NPObject for the plugin. In the process that interacts with the NPobject we 28 // give it an NPObjectProxy instead. All calls to it are sent across an IPC 29 // channel (specifically, a NPChannelBase). The NPObjectStub on the other 30 // side translates the IPC messages into calls to the actual NPObject, and 31 // returns the marshalled result. 32 class NPObjectProxy : public IPC::Listener, 33 public IPC::Sender, 34 public NPObjectBase { 35 public: 36 virtual ~NPObjectProxy(); 37 38 static NPObject* Create(NPChannelBase* channel, 39 int route_id, 40 int render_view_id, 41 const GURL& page_url, 42 NPP owner); 43 44 // IPC::Sender implementation: 45 virtual bool Send(IPC::Message* msg) OVERRIDE; route_id()46 int route_id() { return route_id_; } channel()47 NPChannelBase* channel() { return channel_.get(); } 48 49 // The next 9 functions are called on NPObjects from the plugin and browser. 50 static bool NPHasMethod(NPObject *obj, 51 NPIdentifier name); 52 static bool NPInvoke(NPObject *obj, 53 NPIdentifier name, 54 const NPVariant *args, 55 uint32_t arg_count, 56 NPVariant *result); 57 static bool NPInvokeDefault(NPObject *npobj, 58 const NPVariant *args, 59 uint32_t arg_count, 60 NPVariant *result); 61 static bool NPHasProperty(NPObject *obj, 62 NPIdentifier name); 63 static bool NPGetProperty(NPObject *obj, 64 NPIdentifier name, 65 NPVariant *result); 66 static bool NPSetProperty(NPObject *obj, 67 NPIdentifier name, 68 const NPVariant *value); 69 static bool NPRemoveProperty(NPObject *obj, 70 NPIdentifier name); 71 static bool NPNEnumerate(NPObject *obj, 72 NPIdentifier **value, 73 uint32_t *count); 74 static bool NPNConstruct(NPObject *npobj, 75 const NPVariant *args, 76 uint32_t arg_count, 77 NPVariant *result); 78 79 // The next two functions are only called on NPObjects from the browser. 80 static bool NPNEvaluate(NPP npp, 81 NPObject *obj, 82 NPString *script, 83 NPVariant *result); 84 85 static bool NPInvokePrivate(NPP npp, 86 NPObject *obj, 87 bool is_default, 88 NPIdentifier name, 89 const NPVariant *args, 90 uint32_t arg_count, 91 NPVariant *result); 92 93 static NPObjectProxy* GetProxy(NPObject* object); npclass()94 static const NPClass* npclass() { return &npclass_proxy_; } 95 96 // NPObjectBase implementation. 97 virtual NPObject* GetUnderlyingNPObject() OVERRIDE; 98 99 virtual IPC::Listener* GetChannelListener() OVERRIDE; 100 101 private: 102 NPObjectProxy(NPChannelBase* channel, 103 int route_id, 104 int render_view_id, 105 const GURL& page_url); 106 107 // IPC::Listener implementation: 108 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; 109 virtual void OnChannelError() OVERRIDE; 110 111 static NPObject* NPAllocate(NPP, NPClass*); 112 static void NPDeallocate(NPObject* npObj); 113 114 // This function is only caled on NPObjects from the plugin. 115 static void NPPInvalidate(NPObject *obj); 116 static NPClass npclass_proxy_; 117 118 scoped_refptr<NPChannelBase> channel_; 119 int route_id_; 120 int render_view_id_; 121 122 // The url of the main frame hosting the plugin. 123 GURL page_url_; 124 }; 125 126 } // namespace content 127 128 #endif // CONTENT_CHILD_NPAPI_NPOBJECT_PROXY_H_ 129