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_PROXY_DISPATCHER_H_
6#define PPAPI_PROXY_DISPATCHER_H_
7
8#include <set>
9#include <string>
10#include <vector>
11
12#include "base/callback_forward.h"
13#include "base/compiler_specific.h"
14#include "base/memory/ref_counted.h"
15#include "base/tracked_objects.h"
16#include "ipc/ipc_channel_proxy.h"
17#include "ppapi/c/pp_instance.h"
18#include "ppapi/c/pp_module.h"
19#include "ppapi/c/ppp.h"
20#include "ppapi/proxy/proxy_channel.h"
21#include "ppapi/proxy/interface_list.h"
22#include "ppapi/proxy/interface_proxy.h"
23#include "ppapi/proxy/plugin_var_tracker.h"
24#include "ppapi/shared_impl/api_id.h"
25
26namespace ppapi {
27
28namespace proxy {
29
30class VarSerializationRules;
31
32// An interface proxy can represent either end of a cross-process interface
33// call. The "source" side is where the call is invoked, and the "target" side
34// is where the call ends up being executed.
35//
36// Plugin side                          | Browser side
37// -------------------------------------|--------------------------------------
38//                                      |
39//    "Source"                          |    "Target"
40//    InterfaceProxy ----------------------> InterfaceProxy
41//                                      |
42//                                      |
43//    "Target"                          |    "Source"
44//    InterfaceProxy <---------------------- InterfaceProxy
45//                                      |
46class PPAPI_PROXY_EXPORT Dispatcher : public ProxyChannel {
47 public:
48  virtual ~Dispatcher();
49
50  // Returns true if the dispatcher is on the plugin side, or false if it's the
51  // browser side.
52  virtual bool IsPlugin() const = 0;
53
54  void AddFilter(IPC::Listener* listener);
55
56  VarSerializationRules* serialization_rules() const {
57    return serialization_rules_.get();
58  }
59
60  // Returns a non-owning pointer to the interface proxy for the given ID, or
61  // NULL if the ID isn't found. This will create the proxy if it hasn't been
62  // created so far.
63  InterfaceProxy* GetInterfaceProxy(ApiID id);
64
65  // Returns the pointer to the IO thread for processing IPC messages.
66  // TODO(brettw) remove this. It's a hack to support the Flash
67  // ModuleLocalThreadAdapter. When the thread stuff is sorted out, this
68  // implementation detail should be hidden.
69  base::MessageLoopProxy* GetIPCMessageLoop();
70
71  // Adds the given filter to the IO thread. Takes ownership of the pointer.
72  void AddIOThreadMessageFilter(IPC::ChannelProxy::MessageFilter* filter);
73
74  // TODO(brettw): What is this comment referring to?
75  // Called if the remote side is declaring to us which interfaces it supports
76  // so we don't have to query for each one. We'll pre-create proxies for
77  // each of the given interfaces.
78
79  // IPC::Listener implementation.
80  virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
81
82  PP_GetInterface_Func local_get_interface() const {
83    return local_get_interface_;
84  }
85
86  const PpapiPermissions& permissions() const { return permissions_; }
87
88 protected:
89  explicit Dispatcher(PP_GetInterface_Func local_get_interface,
90                      const PpapiPermissions& permissions);
91
92  // Setter for the derived classes to set the appropriate var serialization.
93  // Takes one reference of the given pointer, which must be on the heap.
94  void SetSerializationRules(VarSerializationRules* var_serialization_rules);
95
96  // Called when an invalid message is received from the remote site. The
97  // default implementation does nothing, derived classes can override.
98  virtual void OnInvalidMessageReceived();
99
100 protected:
101  std::vector<IPC::Listener*> filters_;
102
103 private:
104  friend class HostDispatcherTest;
105  friend class PluginDispatcherTest;
106
107  // Lists all lazily-created interface proxies.
108  scoped_ptr<InterfaceProxy> proxies_[API_ID_COUNT];
109
110  bool disallow_trusted_interfaces_;
111
112  PP_GetInterface_Func local_get_interface_;
113
114  scoped_refptr<VarSerializationRules> serialization_rules_;
115
116  PpapiPermissions permissions_;
117
118  DISALLOW_COPY_AND_ASSIGN(Dispatcher);
119};
120
121}  // namespace proxy
122}  // namespace ppapi
123
124#endif  // PPAPI_PROXY_DISPATCHER_H_
125