Roll ANGLE e754fb8..6ffeb74
[chromium-blink-merge.git] / content / browser / service_worker / stashed_port_manager.h
blobdcfc81f022084dd6a476f1ae06265a527b3069ab
1 // Copyright 2015 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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_STASHED_PORT_MANAGER_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_STASHED_PORT_MANAGER_H_
8 #include <map>
9 #include <set>
11 #include "content/browser/service_worker/service_worker_version.h"
12 #include "content/public/browser/message_port_delegate.h"
14 namespace content {
16 class ServiceWorkerContextWrapper;
18 // This class keeps track of all stashed message ports. Stashed message ports
19 // are ports that will survive a service worker being shut down. This class
20 // registers itself with the MessagePortService as delegate for all these ports
21 // and is responsible for starting the right service worker when messages are
22 // sent to a stashed port.
23 // This class is created on the UI thread, but all its methods should only be
24 // called from the IO thread.
25 // TODO(mek): Cleanup ports when service worker registration is unregistered.
26 // TODO(mek): special handle channels where both ports are stashed. These need
27 // to be persisted to disk and restored later.
28 // TODO(mek): Make sure messages are delivered to the correct version of a
29 // service worker, once it's properly specced what the actual desired behavior
30 // is.
31 // TODO(mek): Handle stashing an already stashed port.
32 // TODO(mek): Make sure transfering a stashed port unregisters it from here.
33 class StashedPortManager
34 : public MessagePortDelegate,
35 public ServiceWorkerVersion::Listener,
36 public base::RefCountedThreadSafe<StashedPortManager> {
37 public:
38 explicit StashedPortManager(
39 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context);
41 // Init and Shutdown are for use on the UI thread when the storagepartition is
42 // being setup and torn down.
43 void Init();
44 void Shutdown();
46 // Add a new port to be stashed. This updates the MessagePortService with the
47 // new delegate for the port, and makes sure messages are queued when the
48 // |service_worker| isn't running.
49 void AddPort(ServiceWorkerVersion* service_worker,
50 int message_port_id,
51 const base::string16& name);
53 // MessagePortDelegate implementation.
54 void SendMessage(
55 int route_id,
56 const MessagePortMessage& message,
57 const std::vector<TransferredMessagePort>& sent_message_ports) override;
58 void MessageWasHeld(int route_id) override;
59 void SendMessagesAreQueued(int route_id) override;
61 // ServiceWorkerVersion::Listener implementation:
62 void OnRunningStateChanged(ServiceWorkerVersion* service_worker) override;
64 private:
65 friend class base::RefCountedThreadSafe<StashedPortManager>;
66 ~StashedPortManager() override;
68 void InitOnIO();
69 void ShutdownOnIO();
71 // Helper method that transfers a message port to a specific service worker.
72 // This is called when messages are sent to a port held by a service worker
73 // that isn't currently running.
74 void TransferMessagePort(int message_port_id,
75 ServiceWorkerStatusCode service_worker_status,
76 const scoped_refptr<ServiceWorkerRegistration>&
77 service_worker_registration);
79 // Helper method called when transfering of ports either succeeded or failed.
80 // Updates internal bookkeeping with the new status and route ids of these
81 // ports.
82 void OnPortsTransferred(
83 const scoped_refptr<ServiceWorkerVersion>& service_worker,
84 const std::vector<TransferredMessagePort>& ports,
85 ServiceWorkerStatusCode service_worker_status,
86 const std::vector<int>& route_ids);
88 // Internal bookkeeping for each stashed port.
89 struct StashedPort;
91 // Maps message_port_id to StashedPort instances.
92 std::map<int, StashedPort> ports_;
94 // Set of all service worker versions that are currently being observed.
95 std::set<ServiceWorkerVersion*> observed_service_workers_;
97 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
100 } // namespace content
102 #endif // CONTENT_BROWSER_SERVICE_WORKER_STASHED_PORT_MANAGER_H_