summaryrefslogtreecommitdiff
path: root/chromium/content/browser/service_worker/service_worker_dispatcher_host.h
blob: ede12c7f0193c8bfd4f7fd1af2b32a4f2022391f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_
#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_

#include <stdint.h>

#include "base/macros.h"
#include "content/common/content_export.h"
#include "content/common/service_worker/service_worker_provider.mojom.h"
#include "content/common/service_worker/service_worker_types.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host_observer.h"
#include "mojo/public/cpp/bindings/associated_binding_set.h"

namespace content {

class ServiceWorkerContextWrapper;

namespace service_worker_dispatcher_host_unittest {
class ServiceWorkerDispatcherHostTest;
class TestingServiceWorkerDispatcherHost;
FORWARD_DECLARE_TEST(ServiceWorkerDispatcherHostTest,
                     ProviderCreatedAndDestroyed);
FORWARD_DECLARE_TEST(ServiceWorkerDispatcherHostTest, CleanupOnRendererCrash);
}  // namespace service_worker_dispatcher_host_unittest

// ServiceWorkerDispatcherHost is a browser-side endpoint for the renderer to
// notify the browser a service worker provider is created.
// Unless otherwise noted, all methods are called on the IO thread.
//
// In order to keep ordering with navigation IPCs to avoid potential races,
// currently mojom::ServiceWorkerDispatcherHost interface is associated with the
// legacy IPC channel.
// TODO(leonhsl): Remove this class once we can understand how to move
// OnProviderCreated() to an isolated message pipe.
class CONTENT_EXPORT ServiceWorkerDispatcherHost
    : public mojom::ServiceWorkerDispatcherHost,
      public RenderProcessHostObserver {
 public:
  // Called on the UI thread.
  ServiceWorkerDispatcherHost(
      scoped_refptr<ServiceWorkerContextWrapper> context_wrapper,
      int render_process_id);

  void AddBinding(mojom::ServiceWorkerDispatcherHostAssociatedRequest request);

  // Called on the UI thread.
  void RenderProcessExited(RenderProcessHost* host,
                           const ChildProcessTerminationInfo& info) override;

 protected:
  ~ServiceWorkerDispatcherHost() override;

 private:
  friend class BrowserThread;
  friend class base::DeleteHelper<ServiceWorkerDispatcherHost>;
  friend class service_worker_dispatcher_host_unittest::
      ServiceWorkerDispatcherHostTest;
  friend class service_worker_dispatcher_host_unittest::
      TestingServiceWorkerDispatcherHost;
  FRIEND_TEST_ALL_PREFIXES(
      service_worker_dispatcher_host_unittest::ServiceWorkerDispatcherHostTest,
      ProviderCreatedAndDestroyed);
  FRIEND_TEST_ALL_PREFIXES(
      service_worker_dispatcher_host_unittest::ServiceWorkerDispatcherHostTest,
      CleanupOnRendererCrash);

  // mojom::ServiceWorkerDispatcherHost implementation
  void OnProviderCreated(mojom::ServiceWorkerProviderHostInfoPtr info) override;

  void RemoveAllProviderHostsForProcess();

  const int render_process_id_;
  // Only accessed on the IO thread.
  scoped_refptr<ServiceWorkerContextWrapper> context_wrapper_;
  mojo::AssociatedBindingSet<mojom::ServiceWorkerDispatcherHost> bindings_;

  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcherHost);
};

}  // namespace content

#endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_