OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 IPC_IPC_CHANNEL_PROXY_H__ | 5 #ifndef IPC_IPC_CHANNEL_PROXY_H__ |
6 #define IPC_IPC_CHANNEL_PROXY_H__ | 6 #define IPC_IPC_CHANNEL_PROXY_H__ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 class ChannelProxy : public Message::Sender { | 48 class ChannelProxy : public Message::Sender { |
49 public: | 49 public: |
50 | 50 |
51 struct MessageFilterTraits; | 51 struct MessageFilterTraits; |
52 | 52 |
53 // A class that receives messages on the thread where the IPC channel is | 53 // A class that receives messages on the thread where the IPC channel is |
54 // running. It can choose to prevent the default action for an IPC message. | 54 // running. It can choose to prevent the default action for an IPC message. |
55 class MessageFilter | 55 class MessageFilter |
56 : public base::RefCountedThreadSafe<MessageFilter, MessageFilterTraits> { | 56 : public base::RefCountedThreadSafe<MessageFilter, MessageFilterTraits> { |
57 public: | 57 public: |
58 virtual ~MessageFilter() {} | 58 virtual ~MessageFilter(); |
59 | 59 |
60 // Called on the background thread to provide the filter with access to the | 60 // Called on the background thread to provide the filter with access to the |
61 // channel. Called when the IPC channel is initialized or when AddFilter | 61 // channel. Called when the IPC channel is initialized or when AddFilter |
62 // is called if the channel is already initialized. | 62 // is called if the channel is already initialized. |
63 virtual void OnFilterAdded(Channel* channel) {} | 63 virtual void OnFilterAdded(Channel* channel); |
64 | 64 |
65 // Called on the background thread when the filter has been removed from | 65 // Called on the background thread when the filter has been removed from |
66 // the ChannelProxy and when the Channel is closing. After a filter is | 66 // the ChannelProxy and when the Channel is closing. After a filter is |
67 // removed, it will not be called again. | 67 // removed, it will not be called again. |
68 virtual void OnFilterRemoved() {} | 68 virtual void OnFilterRemoved(); |
69 | 69 |
70 // Called to inform the filter that the IPC channel is connected and we | 70 // Called to inform the filter that the IPC channel is connected and we |
71 // have received the internal Hello message from the peer. | 71 // have received the internal Hello message from the peer. |
72 virtual void OnChannelConnected(int32 peer_pid) {} | 72 virtual void OnChannelConnected(int32 peer_pid); |
73 | 73 |
74 // Called when there is an error on the channel, typically that the channel | 74 // Called when there is an error on the channel, typically that the channel |
75 // has been closed. | 75 // has been closed. |
76 virtual void OnChannelError() {} | 76 virtual void OnChannelError(); |
77 | 77 |
78 // Called to inform the filter that the IPC channel will be destroyed. | 78 // Called to inform the filter that the IPC channel will be destroyed. |
79 // OnFilterRemoved is called immediately after this. | 79 // OnFilterRemoved is called immediately after this. |
80 virtual void OnChannelClosing() {} | 80 virtual void OnChannelClosing(); |
81 | 81 |
82 // Return true to indicate that the message was handled, or false to let | 82 // Return true to indicate that the message was handled, or false to let |
83 // the message be handled in the default way. | 83 // the message be handled in the default way. |
84 virtual bool OnMessageReceived(const Message& message) { | 84 virtual bool OnMessageReceived(const Message& message); |
85 return false; | |
86 } | |
87 | 85 |
88 // Called when the message filter is about to be deleted. This gives | 86 // Called when the message filter is about to be deleted. This gives |
89 // derived classes the option of controlling which thread they're deleted | 87 // derived classes the option of controlling which thread they're deleted |
90 // on etc. | 88 // on etc. |
91 virtual void OnDestruct() { | 89 virtual void OnDestruct(); |
92 delete this; | |
93 } | |
94 }; | 90 }; |
95 | 91 |
96 struct MessageFilterTraits { | 92 struct MessageFilterTraits { |
97 static void Destruct(MessageFilter* filter) { | 93 static void Destruct(MessageFilter* filter) { |
98 filter->OnDestruct(); | 94 filter->OnDestruct(); |
99 } | 95 } |
100 }; | 96 }; |
101 | 97 |
102 // Initializes a channel proxy. The channel_id and mode parameters are | 98 // Initializes a channel proxy. The channel_id and mode parameters are |
103 // passed directly to the underlying IPC::Channel. The listener is called on | 99 // passed directly to the underlying IPC::Channel. The listener is called on |
104 // the thread that creates the ChannelProxy. The filter's OnMessageReceived | 100 // the thread that creates the ChannelProxy. The filter's OnMessageReceived |
105 // method is called on the thread where the IPC::Channel is running. The | 101 // method is called on the thread where the IPC::Channel is running. The |
106 // filter may be null if the consumer is not interested in handling messages | 102 // filter may be null if the consumer is not interested in handling messages |
107 // on the background thread. Any message not handled by the filter will be | 103 // on the background thread. Any message not handled by the filter will be |
108 // dispatched to the listener. The given message loop indicates where the | 104 // dispatched to the listener. The given message loop indicates where the |
109 // IPC::Channel should be created. | 105 // IPC::Channel should be created. |
110 ChannelProxy(const std::string& channel_id, Channel::Mode mode, | 106 ChannelProxy(const std::string& channel_id, Channel::Mode mode, |
111 Channel::Listener* listener, MessageFilter* filter, | 107 Channel::Listener* listener, MessageFilter* filter, |
112 MessageLoop* ipc_thread_loop); | 108 MessageLoop* ipc_thread_loop); |
113 | 109 |
114 virtual ~ChannelProxy() { | 110 virtual ~ChannelProxy(); |
115 Close(); | |
116 } | |
117 | 111 |
118 // Close the IPC::Channel. This operation completes asynchronously, once the | 112 // Close the IPC::Channel. This operation completes asynchronously, once the |
119 // background thread processes the command to close the channel. It is ok to | 113 // background thread processes the command to close the channel. It is ok to |
120 // call this method multiple times. Redundant calls are ignored. | 114 // call this method multiple times. Redundant calls are ignored. |
121 // | 115 // |
122 // WARNING: The MessageFilter object held by the ChannelProxy is also | 116 // WARNING: The MessageFilter object held by the ChannelProxy is also |
123 // released asynchronously, and it may in fact have its final reference | 117 // released asynchronously, and it may in fact have its final reference |
124 // released on the background thread. The caller should be careful to deal | 118 // released on the background thread. The caller should be careful to deal |
125 // with / allow for this possibility. | 119 // with / allow for this possibility. |
126 void Close(); | 120 void Close(); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 | 224 |
231 // By maintaining this indirection (ref-counted) to our internal state, we | 225 // By maintaining this indirection (ref-counted) to our internal state, we |
232 // can safely be destroyed while the background thread continues to do stuff | 226 // can safely be destroyed while the background thread continues to do stuff |
233 // that involves this data. | 227 // that involves this data. |
234 scoped_refptr<Context> context_; | 228 scoped_refptr<Context> context_; |
235 }; | 229 }; |
236 | 230 |
237 } // namespace IPC | 231 } // namespace IPC |
238 | 232 |
239 #endif // IPC_IPC_CHANNEL_PROXY_H__ | 233 #endif // IPC_IPC_CHANNEL_PROXY_H__ |
OLD | NEW |