blob: 64d07d6c328b3e3e5430dcecb19d6aca0759424b [file] [log] [blame]
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +01001// Copyright 2013 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_BROWSER_MESSAGE_PORT_MESSAGE_FILTER_H_
6#define CONTENT_BROWSER_MESSAGE_PORT_MESSAGE_FILTER_H_
7
8#include "base/callback.h"
Torne (Richard Coles)a1401312014-03-18 10:20:56 +00009#include "content/common/content_export.h"
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010010#include "content/public/browser/browser_message_filter.h"
11
12namespace content {
13
14// Filter for MessagePort related IPC messages (creating and destroying a
15// MessagePort, sending a message via a MessagePort etc).
Torne (Richard Coles)a1401312014-03-18 10:20:56 +000016class CONTENT_EXPORT MessagePortMessageFilter : public BrowserMessageFilter {
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010017 public:
18 typedef base::Callback<int(void)> NextRoutingIDCallback;
19
20 // |next_routing_id| is owned by this object. It can be used up until
21 // OnChannelClosing.
22 explicit MessagePortMessageFilter(const NextRoutingIDCallback& callback);
23
24 // BrowserMessageFilter implementation.
25 virtual void OnChannelClosing() OVERRIDE;
Torne (Richard Coles)cedac222014-06-03 10:58:34 +010026 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010027 virtual void OnDestruct() const OVERRIDE;
28
29 int GetNextRoutingID();
30
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +010031 // Updates message ports registered for |message_port_ids| and returns
32 // new routing IDs for the updated ports via |new_routing_ids|.
33 void UpdateMessagePortsWithNewRoutes(
34 const std::vector<int>& message_port_ids,
35 std::vector<int>* new_routing_ids);
36
Torne (Richard Coles)a1401312014-03-18 10:20:56 +000037 protected:
38 // This is protected, so we can define sub classes for testing.
39 virtual ~MessagePortMessageFilter();
40
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010041 private:
42 friend class BrowserThread;
43 friend class base::DeleteHelper<MessagePortMessageFilter>;
44
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010045 // Message handlers.
46 void OnCreateMessagePort(int* route_id, int* message_port_id);
47
48 // This is guaranteed to be valid until OnChannelClosing is invoked, and it's
49 // not used after.
50 NextRoutingIDCallback next_routing_id_;
51
52 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePortMessageFilter);
53};
54
55} // namespace content
56
57#endif // CONTENT_BROWSER_WORKER_MESSAGE_FILTER_H_