1 // Copyright (c) 2012 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 "ppapi/host/resource_message_filter.h"
7 #include "base/message_loop.h"
8 #include "base/message_loop_proxy.h"
9 #include "base/task_runner.h"
10 #include "ipc/ipc_message.h"
11 #include "ppapi/c/pp_errors.h"
12 #include "ppapi/host/ppapi_host.h"
13 #include "ppapi/host/resource_host.h"
18 ResourceMessageFilter::ResourceMessageFilter()
19 : reply_thread_message_loop_proxy_(
20 MessageLoop::current()->message_loop_proxy()),
21 resource_host_(NULL
) {
24 ResourceMessageFilter::ResourceMessageFilter(
25 scoped_refptr
<base::MessageLoopProxy
> reply_thread_message_loop_proxy
)
26 : reply_thread_message_loop_proxy_(reply_thread_message_loop_proxy
),
27 resource_host_(NULL
) {
30 ResourceMessageFilter::~ResourceMessageFilter() {
33 void ResourceMessageFilter::OnFilterAdded(ResourceHost
* resource_host
) {
34 resource_host_
= resource_host
;
37 void ResourceMessageFilter::OnFilterDestroyed() {
38 resource_host_
= NULL
;
41 bool ResourceMessageFilter::HandleMessage(const IPC::Message
& msg
,
42 HostMessageContext
* context
) {
43 scoped_refptr
<base::TaskRunner
> runner
= OverrideTaskRunnerForMessage(msg
);
45 // TODO(raymes): We need to make a copy so the context can be used on other
46 // threads. It would be better to have a thread-safe refcounted context.
47 HostMessageContext context_copy
= *context
;
48 runner
->PostTask(FROM_HERE
, base::Bind(
49 &ResourceMessageFilter::DispatchMessage
, this, msg
, context_copy
));
56 void ResourceMessageFilter::SendReply(const ReplyMessageContext
& context
,
57 const IPC::Message
& msg
) {
58 if (!reply_thread_message_loop_proxy_
->BelongsToCurrentThread()) {
59 reply_thread_message_loop_proxy_
->PostTask(FROM_HERE
,
60 base::Bind(&ResourceMessageFilter::SendReply
, this, context
, msg
));
64 resource_host_
->SendReply(context
, msg
);
67 scoped_refptr
<base::TaskRunner
>
68 ResourceMessageFilter::OverrideTaskRunnerForMessage(const IPC::Message
& msg
) {
72 void ResourceMessageFilter::DispatchMessage(const IPC::Message
& msg
,
73 HostMessageContext context
) {
74 RunMessageHandlerAndReply(msg
, &context
);