1c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch// found in the LICENSE file.
4c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
5c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "content/browser/push_messaging_message_filter.h"
6c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
7c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include <string>
8c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
9f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "base/bind.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/strings/string_number_conversions.h"
11f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "content/browser/renderer_host/render_process_host_impl.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/browser/service_worker/service_worker_context_wrapper.h"
13c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "content/common/push_messaging_messages.h"
14f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "content/public/browser/browser_context.h"
15c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "content/public/browser/browser_thread.h"
16f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "content/public/browser/push_messaging_service.h"
17c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
18c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochnamespace content {
19c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
20116680a4aac90f2aa7413d9095a592090648e557Ben MurdochPushMessagingMessageFilter::PushMessagingMessageFilter(
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    int render_process_id,
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    ServiceWorkerContextWrapper* service_worker_context)
23f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    : BrowserMessageFilter(PushMessagingMsgStart),
24f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      render_process_id_(render_process_id),
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      service_worker_context_(service_worker_context),
26f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      service_(NULL),
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      weak_factory_(this) {
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
29c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
30c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen MurdochPushMessagingMessageFilter::~PushMessagingMessageFilter() {}
31c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool PushMessagingMessageFilter::OnMessageReceived(
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const IPC::Message& message) {
34c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool handled = true;
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  IPC_BEGIN_MESSAGE_MAP(PushMessagingMessageFilter, message)
36c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    IPC_MESSAGE_HANDLER(PushMessagingHostMsg_Register, OnRegister)
37c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    IPC_MESSAGE_UNHANDLED(handled = false)
38c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  IPC_END_MESSAGE_MAP()
39c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return handled;
40c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
41c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid PushMessagingMessageFilter::OnRegister(int render_frame_id,
43c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                            int callbacks_id,
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                            const std::string& sender_id,
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                            bool user_gesture,
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                            int service_worker_provider_id) {
47c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
48f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // TODO(mvanouwerkerk): Validate arguments?
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ServiceWorkerProviderHost* service_worker_host =
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      service_worker_context_->context()->GetProviderHost(
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          render_process_id_, service_worker_provider_id);
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (!service_worker_host || !service_worker_host->active_version()) {
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    Send(new PushMessagingMsg_RegisterError(
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        render_frame_id,
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        callbacks_id,
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_NO_SERVICE_WORKER));
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return;
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  BrowserThread::PostTask(
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      BrowserThread::UI,
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      FROM_HERE,
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      base::Bind(&PushMessagingMessageFilter::DoRegister,
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 weak_factory_.GetWeakPtr(),
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 render_frame_id,
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 callbacks_id,
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 sender_id,
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 user_gesture,
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 service_worker_host->active_version()->scope().GetOrigin(),
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                 service_worker_host->active_version()->registration_id()));
70f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
71c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid PushMessagingMessageFilter::DoRegister(
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    int render_frame_id,
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    int callbacks_id,
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::string& sender_id,
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    bool user_gesture,
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const GURL& origin,
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    int64 service_worker_registration_id) {
79f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
80f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (!service()) {
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    Send(new PushMessagingMsg_RegisterError(
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        render_frame_id,
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        callbacks_id,
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        PUSH_MESSAGING_STATUS_REGISTRATION_FAILED_SERVICE_NOT_AVAILABLE));
85f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return;
86f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  service()->Register(origin,
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                      service_worker_registration_id,
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                      sender_id,
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                      render_process_id_,
91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                      render_frame_id,
92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                      user_gesture,
93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                      base::Bind(&PushMessagingMessageFilter::DidRegister,
94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 weak_factory_.GetWeakPtr(),
95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 render_frame_id,
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 callbacks_id));
97c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
98c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
99116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid PushMessagingMessageFilter::DidRegister(
100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    int render_frame_id,
101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    int callbacks_id,
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const GURL& push_endpoint,
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::string& push_registration_id,
104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    PushMessagingStatus status) {
105c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (status == PUSH_MESSAGING_STATUS_OK) {
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    Send(new PushMessagingMsg_RegisterSuccess(
108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        render_frame_id, callbacks_id, push_endpoint, push_registration_id));
109c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  } else {
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    Send(new PushMessagingMsg_RegisterError(
111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        render_frame_id, callbacks_id, status));
112c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
113c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
114c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
115f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)PushMessagingService* PushMessagingMessageFilter::service() {
116f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (!service_) {
117116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    RenderProcessHost* process_host =
118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        RenderProcessHost::FromID(render_process_id_);
119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (!process_host)
120f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return NULL;
121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    service_ = process_host->GetBrowserContext()->GetPushMessagingService();
122f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
123f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return service_;
124f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
125f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
126c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}  // namespace content
127