Chromium Code Reviews| 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 port. Stashed message ports are | |
|
scheib
2015/05/06 22:19:46
port -> ports
Marijn Kruisselbrink
2015/05/12 06:57:33
Done.
| |
| 19 // 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 void TransferMessagePort(int message_port_id, | |
|
scheib
2015/05/06 22:19:46
Doc what this does too.
Marijn Kruisselbrink
2015/05/12 06:57:33
Done.
| |
| 72 ServiceWorkerStatusCode service_worker_status, | |
| 73 const scoped_refptr<ServiceWorkerRegistration>& | |
| 74 service_worker_registration); | |
| 75 void TransferredPorts( | |
| 76 const scoped_refptr<ServiceWorkerVersion>& service_worker, | |
| 77 const std::vector<TransferredMessagePort>& ports, | |
| 78 ServiceWorkerStatusCode service_worker_status, | |
| 79 const std::vector<int>& route_ids); | |
| 80 | |
| 81 // Internal bookkeeping for each stashed port. | |
| 82 struct StashedPort; | |
| 83 | |
| 84 // Maps message_port_id to StashedPort instances. | |
| 85 std::map<int, StashedPort> ports_; | |
| 86 | |
| 87 // Set of all service worker versions that are currently being observed. | |
| 88 std::set<ServiceWorkerVersion*> observing_service_workers_; | |
| 89 | |
| 90 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | |
| 91 }; | |
| 92 | |
| 93 } // namespace content | |
| 94 | |
| 95 #endif // CONTENT_BROWSER_SERVICE_WORKER_STASHED_PORT_MANAGER_H_ | |
| OLD | NEW |