push_messaging_message_filter.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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.
4
5#include "content/browser/push_messaging_message_filter.h"
6
7#include <string>
8
9#include "base/bind.h"
10#include "content/browser/renderer_host/render_process_host_impl.h"
11#include "content/common/push_messaging_messages.h"
12#include "content/public/browser/browser_context.h"
13#include "content/public/browser/browser_thread.h"
14#include "content/public/browser/push_messaging_service.h"
15
16namespace content {
17
18PushMessagingMessageFilter::PushMessagingMessageFilter(int render_process_id)
19    : BrowserMessageFilter(PushMessagingMsgStart),
20      render_process_id_(render_process_id),
21      service_(NULL),
22      weak_factory_(this) {}
23
24PushMessagingMessageFilter::~PushMessagingMessageFilter() {}
25
26bool PushMessagingMessageFilter::OnMessageReceived(
27    const IPC::Message& message) {
28  bool handled = true;
29  IPC_BEGIN_MESSAGE_MAP(PushMessagingMessageFilter, message)
30    IPC_MESSAGE_HANDLER(PushMessagingHostMsg_Register, OnRegister)
31    IPC_MESSAGE_UNHANDLED(handled = false)
32  IPC_END_MESSAGE_MAP()
33  return handled;
34}
35
36void PushMessagingMessageFilter::OnRegister(int routing_id,
37                                            int callbacks_id,
38                                            const std::string& sender_id) {
39  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
40  // TODO(mvanouwerkerk): Validate arguments?
41  // TODO(mvanouwerkerk): A WebContentsObserver could avoid this PostTask
42  //                      by receiving the IPC on the UI thread.
43  BrowserThread::PostTask(BrowserThread::UI,
44                          FROM_HERE,
45                          base::Bind(&PushMessagingMessageFilter::DoRegister,
46                                     weak_factory_.GetWeakPtr(),
47                                     routing_id,
48                                     callbacks_id,
49                                     sender_id));
50}
51
52void PushMessagingMessageFilter::DoRegister(int routing_id,
53                                            int callbacks_id,
54                                            const std::string& sender_id) {
55  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
56  if (!service()) {
57    DidRegister(routing_id, callbacks_id, GURL(), "", false);
58    return;
59  }
60  // TODO(mvanouwerkerk): Pass in a real app ID based on Service Worker ID.
61  std::string app_id = "https://example.com 0";
62  service_->Register(app_id,
63                     sender_id,
64                     base::Bind(&PushMessagingMessageFilter::DidRegister,
65                                weak_factory_.GetWeakPtr(),
66                                routing_id,
67                                callbacks_id));
68}
69
70void PushMessagingMessageFilter::DidRegister(int routing_id,
71                                             int callbacks_id,
72                                             const GURL& endpoint,
73                                             const std::string& registration_id,
74                                             bool success) {
75  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
76  if (success) {
77    Send(new PushMessagingMsg_RegisterSuccess(routing_id,
78                                              callbacks_id,
79                                              endpoint,
80                                              registration_id));
81  } else {
82    Send(new PushMessagingMsg_RegisterError(routing_id, callbacks_id));
83  }
84}
85
86PushMessagingService* PushMessagingMessageFilter::service() {
87  if (!service_) {
88    RenderProcessHostImpl* host = static_cast<RenderProcessHostImpl*>(
89        RenderProcessHost::FromID(render_process_id_));
90    if (!host)
91      return NULL;
92    service_ = host->GetBrowserContext()->GetPushMessagingService();
93  }
94  return service_;
95}
96
97}  // namespace content
98