1// Copyright 2014 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_RENDERER_MEDIA_AEC_DUMP_MESSAGE_FILTER_H_
6#define CONTENT_RENDERER_MEDIA_AEC_DUMP_MESSAGE_FILTER_H_
7
8#include "base/gtest_prod_util.h"
9#include "base/memory/scoped_ptr.h"
10#include "content/common/content_export.h"
11#include "content/renderer/render_thread_impl.h"
12#include "ipc/ipc_platform_file.h"
13#include "ipc/message_filter.h"
14
15namespace base {
16class MessageLoopProxy;
17}
18
19namespace content {
20
21// MessageFilter that handles AEC dump messages and forwards them to an
22// observer.
23class CONTENT_EXPORT AecDumpMessageFilter : public IPC::MessageFilter {
24 public:
25  class AecDumpDelegate {
26   public:
27    virtual void OnAecDumpFile(
28        const IPC::PlatformFileForTransit& file_handle) = 0;
29    virtual void OnDisableAecDump() = 0;
30    virtual void OnIpcClosing() = 0;
31  };
32
33  AecDumpMessageFilter(
34      const scoped_refptr<base::MessageLoopProxy>& io_message_loop,
35      const scoped_refptr<base::MessageLoopProxy>& main_message_loop);
36
37  // Getter for the one AecDumpMessageFilter object.
38  static scoped_refptr<AecDumpMessageFilter> Get();
39
40  // Adds a delegate that receives the enable and disable notifications.
41  void AddDelegate(AecDumpMessageFilter::AecDumpDelegate* delegate);
42
43  // Removes a delegate.
44  void RemoveDelegate(AecDumpMessageFilter::AecDumpDelegate* delegate);
45
46  // IO message loop associated with this message filter.
47  scoped_refptr<base::MessageLoopProxy> io_message_loop() const {
48    return io_message_loop_;
49  }
50
51 protected:
52  virtual ~AecDumpMessageFilter();
53
54 private:
55  // Sends an IPC message using |sender_|.
56  void Send(IPC::Message* message);
57
58  // Registers a consumer of AEC dump in the browser process. This consumer will
59  // get a file handle when the AEC dump is enabled and a notification when it
60  // is disabled.
61  void RegisterAecDumpConsumer(int id);
62
63  // Unregisters a consumer of AEC dump in the browser process.
64  void UnregisterAecDumpConsumer(int id);
65
66  // IPC::MessageFilter override. Called on |io_message_loop|.
67  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
68  virtual void OnFilterAdded(IPC::Sender* sender) OVERRIDE;
69  virtual void OnFilterRemoved() OVERRIDE;
70  virtual void OnChannelClosing() OVERRIDE;
71
72  // Accessed on |io_message_loop|.
73  void OnEnableAecDump(int id, IPC::PlatformFileForTransit file_handle);
74  void OnDisableAecDump();
75
76  // Accessed on |main_message_loop_|.
77  void DoEnableAecDump(int id, IPC::PlatformFileForTransit file_handle);
78  void DoDisableAecDump();
79  void DoChannelClosingOnDelegates();
80  int GetIdForDelegate(AecDumpMessageFilter::AecDumpDelegate* delegate);
81
82  // Accessed on |io_message_loop_|.
83  IPC::Sender* sender_;
84
85  // The delgates for this filter. Must only be accessed on
86  // |main_message_loop_|.
87  typedef std::map<int, AecDumpMessageFilter::AecDumpDelegate*> DelegateMap;
88  DelegateMap delegates_;
89
90  // Counter for generating unique IDs to delegates. Accessed on
91  // |main_message_loop_|.
92  int delegate_id_counter_;
93
94  // Message loop on which IPC calls are driven.
95  const scoped_refptr<base::MessageLoopProxy> io_message_loop_;
96
97  // Main message loop.
98  const scoped_refptr<base::MessageLoopProxy> main_message_loop_;
99
100  // The singleton instance for this filter.
101  static AecDumpMessageFilter* g_filter;
102
103  DISALLOW_COPY_AND_ASSIGN(AecDumpMessageFilter);
104};
105
106}  // namespace content
107
108#endif  // CONTENT_RENDERER_MEDIA_AEC_DUMP_MESSAGE_FILTER_H_
109