Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / push_messaging / push_dispatcher.cc
blob8cc33fc0dd20e53164d0fa60fbb94cd763af0b36
1 // Copyright 2014 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 "content/child/push_messaging/push_dispatcher.h"
7 #include "content/child/push_messaging/push_provider.h"
8 #include "content/common/push_messaging_messages.h"
10 namespace content {
12 PushDispatcher::PushDispatcher(ThreadSafeSender* thread_safe_sender)
13 : WorkerThreadMessageFilter(thread_safe_sender), next_request_id_(0) {
16 PushDispatcher::~PushDispatcher() {
19 int PushDispatcher::GenerateRequestId(int thread_id) {
20 base::AutoLock lock(request_id_map_lock_);
21 request_id_map_[next_request_id_] = thread_id;
22 return next_request_id_++;
25 bool PushDispatcher::ShouldHandleMessage(const IPC::Message& msg) const {
26 // Note that not all Push API IPC messages flow through this class. A subset
27 // of the API functionality requires a direct association with a document and
28 // a frame, and for those cases the IPC messages are handled by a
29 // RenderFrameObserver.
30 return msg.type() == PushMessagingMsg_RegisterFromWorkerSuccess::ID ||
31 msg.type() == PushMessagingMsg_RegisterFromWorkerError::ID ||
32 msg.type() == PushMessagingMsg_GetRegistrationSuccess::ID ||
33 msg.type() == PushMessagingMsg_GetRegistrationError::ID ||
34 msg.type() == PushMessagingMsg_GetPermissionStatusSuccess::ID ||
35 msg.type() == PushMessagingMsg_GetPermissionStatusError::ID ||
36 msg.type() == PushMessagingMsg_UnregisterSuccess::ID ||
37 msg.type() == PushMessagingMsg_UnregisterError::ID;
40 void PushDispatcher::OnFilteredMessageReceived(const IPC::Message& msg) {
41 bool handled = PushProvider::ThreadSpecificInstance(
42 thread_safe_sender(), this)->OnMessageReceived(msg);
43 DCHECK(handled);
46 bool PushDispatcher::GetWorkerThreadIdForMessage(const IPC::Message& msg,
47 int* ipc_thread_id) {
48 int request_id = -1;
50 const bool success = PickleIterator(msg).ReadInt(&request_id);
51 DCHECK(success);
53 base::AutoLock lock(request_id_map_lock_);
54 auto it = request_id_map_.find(request_id);
55 if (it != request_id_map_.end()) {
56 *ipc_thread_id = it->second;
57 request_id_map_.erase(it);
58 return true;
60 return false;
63 } // namespace content