browser_message_filter.h revision 58e6fbe4ee35d65e14b626c557d37565bf8ad179
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/process/process.h"
9#include "content/common/content_export.h"
10#include "content/public/browser/browser_thread.h"
11#include "ipc/ipc_channel_proxy.h"
12
13#if defined(OS_WIN)
14#include "base/synchronization/lock.h"
15#endif
16
17namespace base {
18class TaskRunner;
19}
20
21namespace content {
22
23// Base class for message filters in the browser process.  You can receive and
24// send messages on any thread.
25class CONTENT_EXPORT BrowserMessageFilter
26    : public IPC::ChannelProxy::MessageFilter,
27      public IPC::Sender {
28 public:
29  BrowserMessageFilter();
30
31  // IPC::ChannelProxy::MessageFilter methods.  If you override them, make sure
32  // to call them as well.  These are always called on the IO thread.
33  virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE;
34  virtual void OnChannelClosing() OVERRIDE;
35  virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
36  // DON'T OVERRIDE THIS!  Override the other version below.
37  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
38
39  // IPC::Sender implementation.  Can be called on any thread.  Can't send sync
40  // messages (since we don't want to block the browser on any other process).
41  virtual bool Send(IPC::Message* message) OVERRIDE;
42
43  // If you want the given message to be dispatched to your OnMessageReceived on
44  // a different thread, there are two options, either
45  // OverrideThreadForMessage or OverrideTaskRunnerForMessage.
46  // If neither is overriden, the message will be dispatched on the IO thread.
47
48  // If you want the message to be dispatched on a particular well-known
49  // browser thread, change |thread| to the id of the target thread
50  virtual void OverrideThreadForMessage(
51      const IPC::Message& message,
52      BrowserThread::ID* thread);
53
54  // If you want the message to be dispatched via the SequencedWorkerPool,
55  // return a non-null task runner which will target tasks accordingly.
56  // Note: To target the UI thread, please use OverrideThreadForMessage
57  // since that has extra checks to avoid deadlocks.
58  virtual base::TaskRunner* OverrideTaskRunnerForMessage(
59      const IPC::Message& message);
60
61  // Override this to receive messages.
62  // Your function will normally be called on the IO thread.  However, if your
63  // OverrideXForMessage modifies the thread used to dispatch the message,
64  // your function will be called on the requested thread.
65  virtual bool OnMessageReceived(const IPC::Message& message,
66                                 bool* message_was_ok) = 0;
67
68  // Can be called on any thread, after OnChannelConnected is called.
69  base::ProcessHandle PeerHandle();
70
71  // Can be called on any thread, after OnChannelConnected is called.
72  base::ProcessId peer_pid() const { return peer_pid_; }
73
74  // Checks that the given message can be dispatched on the UI thread, depending
75  // on the platform.  If not, returns false and an error ot the sender.
76  static bool CheckCanDispatchOnUI(const IPC::Message& message,
77                                   IPC::Sender* sender);
78
79  // Call this if a message couldn't be deserialized.  This kills the renderer.
80  // Can be called on any thread.
81  virtual void BadMessageReceived();
82
83 protected:
84  virtual ~BrowserMessageFilter();
85
86 private:
87  // Dispatches a message to the derived class.
88  bool DispatchMessage(const IPC::Message& message);
89
90  IPC::Channel* channel_;
91  base::ProcessId peer_pid_;
92
93#if defined(OS_WIN)
94  base::Lock peer_handle_lock_;
95  base::ProcessHandle peer_handle_;
96#endif
97};
98
99}  // namespace content
100
101#endif  // CONTENT_PUBLIC_BROWSER_BROWSER_MESSAGE_FILTER_H_
102