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#ifndef CONTENT_BROWSER_PUSH_MESSAGING_ROUTER_H_
6#define CONTENT_BROWSER_PUSH_MESSAGING_ROUTER_H_
7
8#include "base/memory/ref_counted.h"
9#include "base/memory/weak_ptr.h"
10#include "content/common/service_worker/service_worker_status_code.h"
11#include "content/public/common/push_messaging_status.h"
12#include "url/gurl.h"
13
14namespace content {
15
16class BrowserContext;
17class ServiceWorkerContextWrapper;
18class ServiceWorkerRegistration;
19
20class PushMessagingRouter {
21 public:
22  typedef base::Callback<void(PushMessagingStatus /* push_messaging_status */)>
23      DeliverMessageCallback;
24
25  // Delivers a push message with |data| to the Service Worker identified by
26  // |origin| and |service_worker_registration_id|. Must be called on the UI
27  // thread.
28  static void DeliverMessage(
29      BrowserContext* browser_context,
30      const GURL& origin,
31      int64 service_worker_registration_id,
32      const std::string& data,
33      const DeliverMessageCallback& deliver_message_callback);
34
35 private:
36  // Attempts to find a Service Worker registration so that a push event can be
37  // dispatched. Must be called on the IO thread.
38  static void FindServiceWorkerRegistration(
39      const GURL& origin,
40      int64 service_worker_registration_id,
41      const std::string& data,
42      const DeliverMessageCallback& deliver_message_callback,
43      scoped_refptr<ServiceWorkerContextWrapper> service_worker_context);
44
45  // If a registration was successfully retrieved, dispatches a push event with
46  // |data| on the Service Worker identified by |service_worker_registration|.
47  // Must be called on the IO thread.
48  static void FindServiceWorkerRegistrationCallback(
49      const std::string& data,
50      const DeliverMessageCallback& deliver_message_callback,
51      ServiceWorkerStatusCode service_worker_status,
52      const scoped_refptr<ServiceWorkerRegistration>&
53          service_worker_registration);
54
55  // Gets called asynchronously after the Service Worker has dispatched the push
56  // event. Must be called on the IO thread.
57  static void DeliverMessageEnd(
58      const DeliverMessageCallback& deliver_message_callback,
59      const scoped_refptr<ServiceWorkerRegistration>&
60          service_worker_registration,
61      ServiceWorkerStatusCode service_worker_status);
62
63  DISALLOW_IMPLICIT_CONSTRUCTORS(PushMessagingRouter);
64};
65
66}  // namespace content
67
68#endif  // CONTENT_BROWSER_PUSH_MESSAGING_ROUTER_H_
69