1 // Copyright (c) 2011 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. 4 5 #include "content/browser/worker_host/worker_message_filter.h" 6 7 #include "content/browser/worker_host/message_port_service.h" 8 #include "content/browser/worker_host/worker_service_impl.h" 9 #include "content/common/view_messages.h" 10 #include "content/common/worker_messages.h" 11 #include "content/public/browser/resource_context.h" 12 13 namespace content { 14 15 WorkerMessageFilter::WorkerMessageFilter( 16 int render_process_id, 17 ResourceContext* resource_context, 18 const WorkerStoragePartition& partition, 19 const NextRoutingIDCallback& callback) 20 : render_process_id_(render_process_id), 21 resource_context_(resource_context), 22 partition_(partition), 23 next_routing_id_(callback) { 24 // Note: This constructor is called on both IO or UI thread. 25 DCHECK(resource_context); 26 } 27 28 WorkerMessageFilter::~WorkerMessageFilter() { 29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 30 } 31 32 void WorkerMessageFilter::OnChannelClosing() { 33 BrowserMessageFilter::OnChannelClosing(); 34 35 MessagePortService::GetInstance()->OnWorkerMessageFilterClosing(this); 36 WorkerServiceImpl::GetInstance()->OnWorkerMessageFilterClosing(this); 37 } 38 39 bool WorkerMessageFilter::OnMessageReceived(const IPC::Message& message, 40 bool* message_was_ok) { 41 bool handled = true; 42 IPC_BEGIN_MESSAGE_MAP_EX(WorkerMessageFilter, message, *message_was_ok) 43 // Worker messages. 44 // Only sent from renderer for now, until we have nested workers. 45 IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWorker, OnCreateWorker) 46 // Only sent from renderer for now, until we have nested workers. 47 IPC_MESSAGE_HANDLER(ViewHostMsg_LookupSharedWorker, OnLookupSharedWorker) 48 IPC_MESSAGE_HANDLER(ViewHostMsg_ForwardToWorker, OnForwardToWorker) 49 // Only sent from renderer. 50 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentDetached, OnDocumentDetached) 51 // Message Port related messages. 52 IPC_MESSAGE_HANDLER(WorkerProcessHostMsg_CreateMessagePort, 53 OnCreateMessagePort) 54 IPC_MESSAGE_FORWARD(WorkerProcessHostMsg_DestroyMessagePort, 55 MessagePortService::GetInstance(), 56 MessagePortService::Destroy) 57 IPC_MESSAGE_FORWARD(WorkerProcessHostMsg_Entangle, 58 MessagePortService::GetInstance(), 59 MessagePortService::Entangle) 60 IPC_MESSAGE_FORWARD(WorkerProcessHostMsg_PostMessage, 61 MessagePortService::GetInstance(), 62 MessagePortService::PostMessage) 63 IPC_MESSAGE_FORWARD(WorkerProcessHostMsg_QueueMessages, 64 MessagePortService::GetInstance(), 65 MessagePortService::QueueMessages) 66 IPC_MESSAGE_FORWARD(WorkerProcessHostMsg_SendQueuedMessages, 67 MessagePortService::GetInstance(), 68 MessagePortService::SendQueuedMessages) 69 IPC_MESSAGE_UNHANDLED(handled = false) 70 IPC_END_MESSAGE_MAP_EX() 71 72 return handled; 73 } 74 75 int WorkerMessageFilter::GetNextRoutingID() { 76 return next_routing_id_.Run(); 77 } 78 79 void WorkerMessageFilter::OnCreateWorker( 80 const ViewHostMsg_CreateWorker_Params& params, 81 int* route_id) { 82 *route_id = params.route_id != MSG_ROUTING_NONE ? 83 params.route_id : next_routing_id_.Run(); 84 WorkerServiceImpl::GetInstance()->CreateWorker( 85 params, *route_id, this, resource_context_, partition_); 86 } 87 88 void WorkerMessageFilter::OnLookupSharedWorker( 89 const ViewHostMsg_CreateWorker_Params& params, 90 bool* exists, 91 int* route_id, 92 bool* url_error) { 93 *route_id = next_routing_id_.Run(); 94 95 WorkerServiceImpl::GetInstance()->LookupSharedWorker( 96 params, *route_id, this, resource_context_, partition_, exists, 97 url_error); 98 } 99 100 void WorkerMessageFilter::OnForwardToWorker(const IPC::Message& message) { 101 WorkerServiceImpl::GetInstance()->ForwardToWorker(message, this); 102 } 103 104 void WorkerMessageFilter::OnDocumentDetached(unsigned long long document_id) { 105 WorkerServiceImpl::GetInstance()->DocumentDetached(document_id, this); 106 } 107 108 void WorkerMessageFilter::OnCreateMessagePort(int *route_id, 109 int* message_port_id) { 110 *route_id = next_routing_id_.Run(); 111 MessagePortService::GetInstance()->Create(*route_id, this, message_port_id); 112 } 113 114 } // namespace content 115