Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / child_message_filter.cc
blob66b7555b376eb2be2350190697e9f8ce6110ae75
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.
5 #include "content/child/child_message_filter.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/location.h"
10 #include "base/task_runner.h"
11 #include "content/child/child_thread_impl.h"
12 #include "content/child/thread_safe_sender.h"
13 #include "ipc/message_filter.h"
15 namespace content {
17 class ChildMessageFilter::Internal : public IPC::MessageFilter {
18 public:
19 explicit Internal(ChildMessageFilter* filter) : filter_(filter) {}
21 bool OnMessageReceived(const IPC::Message& msg) override {
22 scoped_refptr<base::TaskRunner> runner =
23 filter_->OverrideTaskRunnerForMessage(msg);
24 if (runner.get() && !runner->RunsTasksOnCurrentThread()) {
25 if (!runner->PostTask(
26 FROM_HERE,
27 base::Bind(
28 base::IgnoreResult(&ChildMessageFilter::OnMessageReceived),
29 filter_, msg)))
30 filter_->OnStaleMessageReceived(msg);
31 return true;
34 return filter_->OnMessageReceived(msg);
37 private:
38 ~Internal() override {}
39 scoped_refptr<ChildMessageFilter> filter_;
41 DISALLOW_COPY_AND_ASSIGN(Internal);
44 bool ChildMessageFilter::Send(IPC::Message* message) {
45 return thread_safe_sender_->Send(message);
48 base::TaskRunner* ChildMessageFilter::OverrideTaskRunnerForMessage(
49 const IPC::Message& msg) {
50 return NULL;
53 ChildMessageFilter::ChildMessageFilter()
54 : internal_(NULL),
55 thread_safe_sender_(ChildThreadImpl::current()->thread_safe_sender()) {}
57 ChildMessageFilter::~ChildMessageFilter() {}
59 IPC::MessageFilter* ChildMessageFilter::GetFilter() {
60 if (!internal_)
61 internal_ = new Internal(this);
62 return internal_;
65 } // namespace content