OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_BROWSER_IO_MESSAGE_FILTER_H_ | 5 #ifndef CHROME_BROWSER_BROWSER_MESSAGE_FILTER_H_ |
6 #define CHROME_BROWSER_BROWSER_IO_MESSAGE_FILTER_H_ | 6 #define CHROME_BROWSER_BROWSER_MESSAGE_FILTER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/process.h" | 9 #include "base/process.h" |
| 10 #include "chrome/browser/browser_thread.h" |
10 #include "ipc/ipc_channel_proxy.h" | 11 #include "ipc/ipc_channel_proxy.h" |
11 | 12 |
12 // Base class for message filters in the browser process that reside on the IO | 13 // Base class for message filters in the browser process. You can receive and |
13 // thread. | 14 // send messages on any thread. |
14 class BrowserIOMessageFilter : public IPC::ChannelProxy::MessageFilter, | 15 class BrowserMessageFilter : public IPC::ChannelProxy::MessageFilter, |
15 public IPC::Message::Sender { | 16 public IPC::Message::Sender { |
16 public: | 17 public: |
17 BrowserIOMessageFilter(); | 18 BrowserMessageFilter(); |
18 virtual ~BrowserIOMessageFilter(); | 19 virtual ~BrowserMessageFilter(); |
19 | 20 |
20 // IPC::ChannelProxy::MessageFilter methods. If you override them, make sure | 21 // IPC::ChannelProxy::MessageFilter methods. If you override them, make sure |
21 // to call them as well. | 22 // to call them as well. These are always called on the IO thread. |
22 virtual void OnFilterAdded(IPC::Channel* channel); | 23 virtual void OnFilterAdded(IPC::Channel* channel); |
23 virtual void OnChannelClosing(); | 24 virtual void OnChannelClosing(); |
24 virtual void OnChannelConnected(int32 peer_pid); | 25 virtual void OnChannelConnected(int32 peer_pid); |
| 26 // DON'T OVERRIDE THIS! Override the other version below. |
| 27 virtual bool OnMessageReceived(const IPC::Message& message); |
25 | 28 |
26 // IPC::Message::Sender implementation: | 29 // IPC::Message::Sender implementation. Can be called on any thread. Can't |
27 virtual bool Send(IPC::Message* msg); | 30 // send sync messages (since we don't want to block the browser on any other |
| 31 // process). |
| 32 virtual bool Send(IPC::Message* message); |
| 33 |
| 34 // If you want the given message to be dispatched to your OnMessageReceived on |
| 35 // a different thread, change |thread| to the id of the target thread. |
| 36 // If you don't handle this message, or want to keep it on the IO thread, do |
| 37 // nothing. |
| 38 virtual void OverrideThreadForMessage(const IPC::Message& message, |
| 39 BrowserThread::ID* thread); |
| 40 |
| 41 // Override this to receive messages. |
| 42 // Your function will normally be called on the IO thread. However, if your |
| 43 // OverrideThreadForMessage modifies the thread used to dispatch the message, |
| 44 // your function will be called on the requested thread. |
| 45 virtual bool OnMessageReceived(const IPC::Message& message, |
| 46 bool* message_was_ok) = 0; |
28 | 47 |
29 protected: | 48 protected: |
| 49 // Can be called on any thread, after OnChannelConnected is called. |
30 base::ProcessHandle peer_handle() { return peer_handle_; } | 50 base::ProcessHandle peer_handle() { return peer_handle_; } |
31 | 51 |
32 // Call this if a message couldn't be deserialized. This kills the renderer. | 52 // Call this if a message couldn't be deserialized. This kills the renderer. |
| 53 // Can be called on any thread. |
33 void BadMessageReceived(uint32 msg_type); | 54 void BadMessageReceived(uint32 msg_type); |
34 | 55 |
35 private: | 56 private: |
| 57 // Dispatches a message to the derived class. |
| 58 bool DispatchMessage(const IPC::Message& message); |
| 59 |
36 IPC::Channel* channel_; | 60 IPC::Channel* channel_; |
37 base::ProcessHandle peer_handle_; | 61 base::ProcessHandle peer_handle_; |
38 }; | 62 }; |
39 | 63 |
40 #endif // CHROME_BROWSER_BROWSER_IO_MESSAGE_FILTER_H_ | 64 #endif // CHROME_BROWSER_BROWSER_MESSAGE_FILTER_H_ |
OLD | NEW |