Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / ppapi / host / resource_message_filter.cc
blobd08f5259368cc9d665c3c1cd0354b50338ef7685
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/bind.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"
17 namespace ppapi {
18 namespace host {
20 namespace internal {
22 // static
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
27 // leak in that case.
28 filter->deletion_task_runner_->DeleteSoon(FROM_HERE, filter);
29 } else {
30 delete 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);
63 if (runner.get()) {
64 if (runner->RunsTasksOnCurrentThread()) {
65 DispatchMessage(msg, *context);
66 } else {
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
69 // context.
70 HostMessageContext context_copy = *context;
71 runner->PostTask(FROM_HERE, base::Bind(
72 &ResourceMessageFilter::DispatchMessage, this, msg, context_copy));
74 return true;
77 return false;
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(
84 FROM_HERE,
85 base::Bind(&ResourceMessageFilter::SendReply, this, context, msg));
86 return;
88 if (resource_host_)
89 resource_host_->SendReply(context, msg);
92 scoped_refptr<base::TaskRunner>
93 ResourceMessageFilter::OverrideTaskRunnerForMessage(const IPC::Message& msg) {
94 return NULL;
97 void ResourceMessageFilter::DispatchMessage(const IPC::Message& msg,
98 HostMessageContext context) {
99 RunMessageHandlerAndReply(msg, &context);
102 } // namespace host
103 } // namespace ppapi