summaryrefslogtreecommitdiffstats
path: root/chrome/plugin/npobject_proxy.h
blob: 8858a1b2b085412af970f18e85d2f09e54b8bd28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// A proxy for NPObject that sends all calls to the object to an NPObjectStub
// running in a different process.

#ifndef CHROME_PLUGIN_NPOBJECT_PROXY_H_
#define CHROME_PLUGIN_NPOBJECT_PROXY_H_

#include "base/ref_counted.h"
#include "googleurl/src/gurl.h"
#include "ipc/ipc_channel.h"
#include "third_party/npapi/bindings/npruntime.h"

class PluginChannelBase;
struct NPObject;

namespace base {
class WaitableEvent;
}

// When running a plugin in a different process from the renderer, we need to
// proxy calls to NPObjects across process boundaries.  This happens both ways,
// as a plugin can get an NPObject for the window, and a page can get an
// NPObject for the plugin.  In the process that interacts with the NPobject we
// give it an NPObjectProxy instead.  All calls to it are sent across an IPC
// channel (specifically, a PluginChannelBase).  The NPObjectStub on the other
// side translates the IPC messages into calls to the actual NPObject, and
// returns the marshalled result.
class NPObjectProxy : public IPC::Channel::Listener,
                      public IPC::Message::Sender {
 public:
  ~NPObjectProxy();

  // modal_dialog_event_ is must be valid for the lifetime of the NPObjectProxy.
  static NPObject* Create(PluginChannelBase* channel,
                          int route_id,
                          intptr_t npobject_ptr,
                          base::WaitableEvent* modal_dialog_event,
                          const GURL& page_url);

  // IPC::Message::Sender implementation:
  bool Send(IPC::Message* msg);
  int route_id() { return route_id_; }
  PluginChannelBase* channel() { return channel_; }

  // Returns the real NPObject's pointer (obviously only valid in the other
  // process).
  intptr_t npobject_ptr() { return npobject_ptr_; }

  // The next 9 functions are called on NPObjects from the plugin and browser.
  static bool NPHasMethod(NPObject *obj,
                          NPIdentifier name);
  static bool NPInvoke(NPObject *obj,
                       NPIdentifier name,
                       const NPVariant *args,
                       uint32_t arg_count,
                       NPVariant *result);
  static bool NPInvokeDefault(NPObject *npobj,
                              const NPVariant *args,
                              uint32_t arg_count,
                              NPVariant *result);
  static bool NPHasProperty(NPObject *obj,
                            NPIdentifier name);
  static bool NPGetProperty(NPObject *obj,
                            NPIdentifier name,
                            NPVariant *result);
  static bool NPSetProperty(NPObject *obj,
                            NPIdentifier name,
                            const NPVariant *value);
  static bool NPRemoveProperty(NPObject *obj,
                               NPIdentifier name);
  static bool NPNEnumerate(NPObject *obj,
                           NPIdentifier **value,
                           uint32_t *count);
  static bool NPNConstruct(NPObject *npobj,
                           const NPVariant *args,
                           uint32_t arg_count,
                           NPVariant *result);

  // The next two functions are only called on NPObjects from the browser.
  static bool NPNEvaluate(NPP npp,
                          NPObject *obj,
                          NPString *script,
                          NPVariant *result);

  static void NPNSetException(NPObject *obj,
                              const NPUTF8 *message);

  static bool NPInvokePrivate(NPP npp,
                              NPObject *obj,
                              bool is_default,
                              NPIdentifier name,
                              const NPVariant *args,
                              uint32_t arg_count,
                              NPVariant *result);

  static NPObjectProxy* GetProxy(NPObject* object);
  static const NPClass* npclass() { return &npclass_proxy_; }

 private:
  NPObjectProxy(PluginChannelBase* channel,
                int route_id,
                intptr_t npobject_ptr,
                base::WaitableEvent* modal_dialog_event,
                const GURL& page_url);

  // IPC::Channel::Listener implementation:
  void OnMessageReceived(const IPC::Message& msg);
  void OnChannelError();

  static NPObject* NPAllocate(NPP, NPClass*);
  static void NPDeallocate(NPObject* npObj);

  // This function is only caled on NPObjects from the plugin.
  static void NPPInvalidate(NPObject *obj);
  static NPClass npclass_proxy_;

  scoped_refptr<PluginChannelBase> channel_;
  int route_id_;
  intptr_t npobject_ptr_;
  base::WaitableEvent* modal_dialog_event_;

  // The url of the main frame hosting the plugin.
  GURL page_url_;
};

#endif  // CHROME_PLUGIN_NPOBJECT_PROXY_H_