browser_message_filter.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 CONTENT_PUBLIC_BROWSER_BROWSER_MESSAGE_FILTER_H_
6#define CONTENT_PUBLIC_BROWSER_BROWSER_MESSAGE_FILTER_H_
7
8#include "base/memory/ref_counted.h"
9#include "base/process/process.h"
10#include "content/common/content_export.h"
11#include "content/public/browser/browser_thread.h"
12#include "ipc/ipc_channel_proxy.h"
13
14#if defined(OS_WIN)
15#include "base/synchronization/lock.h"
16#endif
17
18namespace base {
19class TaskRunner;
20}
21
22namespace content {
23struct BrowserMessageFilterTraits;
24
25// Base class for message filters in the browser process.  You can receive and
26// send messages on any thread.
27class CONTENT_EXPORT BrowserMessageFilter
28    : public base::RefCountedThreadSafe<
29          BrowserMessageFilter, BrowserMessageFilterTraits>,
30      public IPC::Sender {
31 public:
32  explicit BrowserMessageFilter(uint32 message_class_to_filter);
33  BrowserMessageFilter(const uint32* message_classes_to_filter,
34                       size_t num_message_classes_to_filter);
35
36  // These match the corresponding IPC::ChannelProxy::MessageFilter methods and
37  // are always called on the IO thread.
38  virtual void OnFilterAdded(IPC::Channel* channel) {}
39  virtual void OnFilterRemoved() {}
40  virtual void OnChannelClosing() {}
41  virtual void OnChannelConnected(int32 peer_pid) {}
42
43  // Called when the message filter is about to be deleted.  This gives
44  // derived classes the option of controlling which thread they're deleted
45  // on etc.
46  virtual void OnDestruct() const;
47
48  // IPC::Sender implementation.  Can be called on any thread.  Can't send sync
49  // messages (since we don't want to block the browser on any other process).
50  virtual bool Send(IPC::Message* message) OVERRIDE;
51
52  // If you want the given message to be dispatched to your OnMessageReceived on
53  // a different thread, there are two options, either
54  // OverrideThreadForMessage or OverrideTaskRunnerForMessage.
55  // If neither is overriden, the message will be dispatched on the IO thread.
56
57  // If you want the message to be dispatched on a particular well-known
58  // browser thread, change |thread| to the id of the target thread
59  virtual void OverrideThreadForMessage(
60      const IPC::Message& message,
61      BrowserThread::ID* thread) {}
62
63  // If you want the message to be dispatched via the SequencedWorkerPool,
64  // return a non-null task runner which will target tasks accordingly.
65  // Note: To target the UI thread, please use OverrideThreadForMessage
66  // since that has extra checks to avoid deadlocks.
67  virtual base::TaskRunner* OverrideTaskRunnerForMessage(
68      const IPC::Message& message);
69
70  // Override this to receive messages.
71  // Your function will normally be called on the IO thread.  However, if your
72  // OverrideXForMessage modifies the thread used to dispatch the message,
73  // your function will be called on the requested thread.
74  virtual bool OnMessageReceived(const IPC::Message& message,
75                                 bool* message_was_ok) = 0;
76
77  // Can be called on any thread, after OnChannelConnected is called.
78  base::ProcessHandle PeerHandle();
79
80  // Can be called on any thread, after OnChannelConnected is called.
81  base::ProcessId peer_pid() const { return peer_pid_; }
82
83  void set_peer_pid_for_testing(base::ProcessId peer_pid) {
84    peer_pid_ = peer_pid;
85  }
86
87  // Checks that the given message can be dispatched on the UI thread, depending
88  // on the platform.  If not, returns false and an error ot the sender.
89  static bool CheckCanDispatchOnUI(const IPC::Message& message,
90                                   IPC::Sender* sender);
91
92  // Call this if a message couldn't be deserialized.  This kills the renderer.
93  // Can be called on any thread.
94  virtual void BadMessageReceived();
95
96  const std::vector<uint32>& message_classes_to_filter() const {
97    return message_classes_to_filter_;
98  }
99
100 protected:
101  virtual ~BrowserMessageFilter();
102
103 private:
104  friend class base::RefCountedThreadSafe<BrowserMessageFilter,
105                                          BrowserMessageFilterTraits>;
106
107  class Internal;
108  friend class BrowserChildProcessHostImpl;
109  friend class BrowserPpapiHost;
110  friend class RenderProcessHostImpl;
111
112  // This is private because the only classes that need access to it are made
113  // friends above. This is only guaranteed to be valid on creation, after that
114  // this class could outlive the filter.
115  IPC::ChannelProxy::MessageFilter* GetFilter();
116
117  // This implements IPC::ChannelProxy::MessageFilter so that we can hide that
118  // from child classes. Internal keeps a reference to this class, which is why
119  // there's a weak pointer back. This class could outlive Internal based on
120  // what the child class does in its OnDestruct method.
121  Internal* internal_;
122
123  IPC::Channel* channel_;
124  base::ProcessId peer_pid_;
125
126  std::vector<uint32> message_classes_to_filter_;
127
128#if defined(OS_WIN)
129  base::Lock peer_handle_lock_;
130  base::ProcessHandle peer_handle_;
131#endif
132};
133
134struct BrowserMessageFilterTraits {
135  static void Destruct(const BrowserMessageFilter* filter) {
136    filter->OnDestruct();
137  }
138};
139
140}  // namespace content
141
142#endif  // CONTENT_PUBLIC_BROWSER_BROWSER_MESSAGE_FILTER_H_
143