1// Copyright (c) 2011 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_PLUGIN_MESSAGE_FILTER_H_
6#define PPAPI_PROXY_PLUGIN_MESSAGE_FILTER_H_
7
8#include <set>
9
10#include "base/compiler_specific.h"
11#include "base/memory/ref_counted.h"
12#include "ipc/ipc_sender.h"
13#include "ipc/message_filter.h"
14#include "ppapi/c/pp_instance.h"
15#include "ppapi/proxy/ppapi_proxy_export.h"
16
17namespace ppapi {
18namespace proxy {
19
20class ResourceMessageReplyParams;
21class ResourceReplyThreadRegistrar;
22
23// Listens for messages on the I/O thread of the plugin and handles some of
24// them to avoid needing to block on the plugin.
25//
26// There is one instance of this class for each renderer channel (same as for
27// the PluginDispatchers).
28class PPAPI_PROXY_EXPORT PluginMessageFilter : public IPC::MessageFilter,
29                                               public IPC::Sender {
30 public:
31  // |seen_instance_ids| is a pointer to a set that will be used to uniquify
32  // PP_Instances across all renderer channels. The same pointer should be
33  // passed to each MessageFilter to ensure uniqueness, and the value should
34  // outlive this class. It could be NULL if this filter is for a browser
35  // channel.
36  // |thread_registrar| is used to look up handling threads for resource
37  // reply messages. It shouldn't be NULL.
38  PluginMessageFilter(
39      std::set<PP_Instance>* seen_instance_ids,
40      scoped_refptr<ResourceReplyThreadRegistrar> thread_registrar);
41  virtual ~PluginMessageFilter();
42
43  // MessageFilter implementation.
44  virtual void OnFilterAdded(IPC::Sender* sender) OVERRIDE;
45  virtual void OnFilterRemoved() OVERRIDE;
46  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
47
48  // IPC::Sender implementation.
49  virtual bool Send(IPC::Message* msg) OVERRIDE;
50
51  // Simulates an incoming resource reply that is handled on the calling thread.
52  // For testing only.
53  static void DispatchResourceReplyForTest(
54      const ResourceMessageReplyParams& reply_params,
55      const IPC::Message& nested_msg);
56
57 private:
58  void OnMsgReserveInstanceId(PP_Instance instance, bool* usable);
59  void OnMsgResourceReply(const ResourceMessageReplyParams& reply_params,
60                          const IPC::Message& nested_msg);
61
62  // Dispatches the given resource reply to the appropriate resource in the
63  // plugin process.
64  static void DispatchResourceReply(
65      const ResourceMessageReplyParams& reply_params,
66      const IPC::Message& nested_msg);
67
68  // All instance IDs ever queried by any renderer on this plugin. This is used
69  // to make sure that new instance IDs are unique. This is a non-owning
70  // pointer. It is managed by PluginDispatcher::PluginDelegate.
71  std::set<PP_Instance>* seen_instance_ids_;
72
73  scoped_refptr<ResourceReplyThreadRegistrar> resource_reply_thread_registrar_;
74
75  // The IPC sender to the renderer. May be NULL if we're not currently
76  // attached as a filter.
77  IPC::Sender* sender_;
78};
79
80}  // namespace proxy
81}  // namespace ppapi
82
83#endif  // PPAPI_PROXY_PLUGIN_MESSAGE_FILTER_H_
84