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"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "ipc/ipc_message.h"
13 #include "ppapi/c/pp_errors.h"
14 #include "ppapi/host/ppapi_host.h"
15 #include "ppapi/host/resource_host.h"
23 void ResourceMessageFilterDeleteTraits::Destruct(
24 const ResourceMessageFilter
* filter
) {
25 if (!filter
->deletion_task_runner_
->BelongsToCurrentThread()) {
26 // During shutdown the object may not be deleted, but it should be okay to
28 filter
->deletion_task_runner_
->DeleteSoon(FROM_HERE
, filter
);
34 } // namespace internal
36 ResourceMessageFilter::ResourceMessageFilter()
37 : deletion_task_runner_(base::ThreadTaskRunnerHandle::Get()),
38 reply_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()),
39 resource_host_(NULL
) {
42 ResourceMessageFilter::ResourceMessageFilter(
43 scoped_refptr
<base::SingleThreadTaskRunner
> reply_thread_task_runner
)
44 : deletion_task_runner_(base::ThreadTaskRunnerHandle::Get()),
45 reply_thread_task_runner_(reply_thread_task_runner
),
46 resource_host_(NULL
) {
49 ResourceMessageFilter::~ResourceMessageFilter() {
52 void ResourceMessageFilter::OnFilterAdded(ResourceHost
* resource_host
) {
53 resource_host_
= resource_host
;
56 void ResourceMessageFilter::OnFilterDestroyed() {
57 resource_host_
= NULL
;
60 bool ResourceMessageFilter::HandleMessage(const IPC::Message
& msg
,
61 HostMessageContext
* context
) {
62 scoped_refptr
<base::TaskRunner
> runner
= OverrideTaskRunnerForMessage(msg
);
64 if (runner
->RunsTasksOnCurrentThread()) {
65 DispatchMessage(msg
, *context
);
67 // TODO(raymes): We need to make a copy so the context can be used on
68 // other threads. It would be better to have a thread-safe refcounted
70 HostMessageContext context_copy
= *context
;
71 runner
->PostTask(FROM_HERE
, base::Bind(
72 &ResourceMessageFilter::DispatchMessage
, this, msg
, context_copy
));
80 void ResourceMessageFilter::SendReply(const ReplyMessageContext
& context
,
81 const IPC::Message
& msg
) {
82 if (!reply_thread_task_runner_
->BelongsToCurrentThread()) {
83 reply_thread_task_runner_
->PostTask(
85 base::Bind(&ResourceMessageFilter::SendReply
, this, context
, msg
));
89 resource_host_
->SendReply(context
, msg
);
92 scoped_refptr
<base::TaskRunner
>
93 ResourceMessageFilter::OverrideTaskRunnerForMessage(const IPC::Message
& msg
) {
97 void ResourceMessageFilter::DispatchMessage(const IPC::Message
& msg
,
98 HostMessageContext context
) {
99 RunMessageHandlerAndReply(msg
, &context
);