OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_STASHED_PORT_MANAGER_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_STASHED_PORT_MANAGER_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <set> |
| 10 |
| 11 #include "content/browser/service_worker/service_worker_version.h" |
| 12 #include "content/public/browser/message_port_delegate.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 class ServiceWorkerContextWrapper; |
| 17 |
| 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); |
| 40 |
| 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(); |
| 45 |
| 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); |
| 52 |
| 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; |
| 60 |
| 61 // ServiceWorkerVersion::Listener implementation: |
| 62 void OnRunningStateChanged(ServiceWorkerVersion* service_worker) override; |
| 63 |
| 64 private: |
| 65 friend class base::RefCountedThreadSafe<StashedPortManager>; |
| 66 ~StashedPortManager() override; |
| 67 |
| 68 void InitOnIO(); |
| 69 void ShutdownOnIO(); |
| 70 |
| 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); |
| 78 |
| 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); |
| 87 |
| 88 // Internal bookkeeping for each stashed port. |
| 89 struct StashedPort; |
| 90 |
| 91 // Maps message_port_id to StashedPort instances. |
| 92 std::map<int, StashedPort> ports_; |
| 93 |
| 94 // Set of all service worker versions that are currently being observed. |
| 95 std::set<ServiceWorkerVersion*> observed_service_workers_; |
| 96 |
| 97 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
| 98 }; |
| 99 |
| 100 } // namespace content |
| 101 |
| 102 #endif // CONTENT_BROWSER_SERVICE_WORKER_STASHED_PORT_MANAGER_H_ |
OLD | NEW |