1// 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"
9#include "content/common/content_export.h"
10#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).
16class CONTENT_EXPORT MessagePortMessageFilter : public BrowserMessageFilter {
17 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;
26  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
27  virtual void OnDestruct() const OVERRIDE;
28
29  int GetNextRoutingID();
30
31  // 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
37 protected:
38  // This is protected, so we can define sub classes for testing.
39  virtual ~MessagePortMessageFilter();
40
41 private:
42  friend class BrowserThread;
43  friend class base::DeleteHelper<MessagePortMessageFilter>;
44
45  // 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_
58