11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// found in the LICENSE file.
41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/child/service_worker/service_worker_message_filter.h"
61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/message_loop/message_loop_proxy.h"
81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/child/service_worker/service_worker_dispatcher.h"
91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/child/thread_safe_sender.h"
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "content/child/worker_thread_task_runner.h"
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "ipc/ipc_message_macros.h"
121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace content {
141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)ServiceWorkerMessageFilter::ServiceWorkerMessageFilter(ThreadSafeSender* sender)
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    : main_thread_loop_proxy_(base::MessageLoopProxy::current()),
171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      thread_safe_sender_(sender) {}
181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)ServiceWorkerMessageFilter::~ServiceWorkerMessageFilter() {}
201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)base::TaskRunner* ServiceWorkerMessageFilter::OverrideTaskRunnerForMessage(
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    const IPC::Message& msg) {
231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (IPC_MESSAGE_CLASS(msg) != ServiceWorkerMsgStart)
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return NULL;
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  int ipc_thread_id = 0;
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const bool success = PickleIterator(msg).ReadInt(&ipc_thread_id);
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK(success);
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!ipc_thread_id)
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return main_thread_loop_proxy_.get();
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return new WorkerThreadTaskRunner(ipc_thread_id);
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool ServiceWorkerMessageFilter::OnMessageReceived(const IPC::Message& msg) {
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (IPC_MESSAGE_CLASS(msg) != ServiceWorkerMsgStart)
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return false;
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ServiceWorkerDispatcher::ThreadSpecificInstance(thread_safe_sender_.get())
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      ->OnMessageReceived(msg);
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return true;
391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namespace content
42