1 // Copyright 2013 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_EMBEDDED_WORKER_REGISTRY_H_ 6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_ 7 8 #include <map> 9 #include <set> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/weak_ptr.h" 16 #include "base/strings/string16.h" 17 #include "content/common/content_export.h" 18 #include "content/common/service_worker/service_worker_status_code.h" 19 20 struct EmbeddedWorkerMsg_StartWorker_Params; 21 class GURL; 22 23 namespace IPC { 24 class Message; 25 class Sender; 26 } 27 28 namespace content { 29 30 class EmbeddedWorkerInstance; 31 class ServiceWorkerContextCore; 32 33 // Acts as a thin stub between MessageFilter and each EmbeddedWorkerInstance, 34 // which sends/receives messages to/from each EmbeddedWorker in child process. 35 // 36 // Hangs off ServiceWorkerContextCore (its reference is also held by each 37 // EmbeddedWorkerInstance). Operated only on IO thread. 38 class CONTENT_EXPORT EmbeddedWorkerRegistry NON_EXPORTED_BASE(base::RefCounted<EmbeddedWorkerRegistry>)39 : public NON_EXPORTED_BASE(base::RefCounted<EmbeddedWorkerRegistry>) { 40 public: 41 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; 42 43 explicit EmbeddedWorkerRegistry( 44 base::WeakPtr<ServiceWorkerContextCore> context); 45 46 bool OnMessageReceived(const IPC::Message& message); 47 48 // Creates and removes a new worker instance entry for bookkeeping. 49 // This doesn't actually start or stop the worker. 50 scoped_ptr<EmbeddedWorkerInstance> CreateWorker(); 51 52 // Called from EmbeddedWorkerInstance, relayed to the child process. 53 void SendStartWorker(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, 54 const StatusCallback& callback, 55 int process_id); 56 ServiceWorkerStatusCode StopWorker(int process_id, 57 int embedded_worker_id); 58 59 // Stop all active workers, even if they're handling events. 60 void Shutdown(); 61 62 // Called back from EmbeddedWorker in the child process, relayed via 63 // ServiceWorkerDispatcherHost. 64 void OnWorkerScriptLoaded(int process_id, int embedded_worker_id); 65 void OnWorkerScriptLoadFailed(int process_id, int embedded_worker_id); 66 void OnWorkerStarted(int process_id, int thread_id, int embedded_worker_id); 67 void OnWorkerStopped(int process_id, int embedded_worker_id); 68 void OnReportException(int embedded_worker_id, 69 const base::string16& error_message, 70 int line_number, 71 int column_number, 72 const GURL& source_url); 73 void OnReportConsoleMessage(int embedded_worker_id, 74 int source_identifier, 75 int message_level, 76 const base::string16& message, 77 int line_number, 78 const GURL& source_url); 79 80 // Keeps a map from process_id to sender information. 81 void AddChildProcessSender(int process_id, IPC::Sender* sender); 82 void RemoveChildProcessSender(int process_id); 83 84 // Returns an embedded worker instance for given |embedded_worker_id|. 85 EmbeddedWorkerInstance* GetWorker(int embedded_worker_id); 86 87 private: 88 friend class base::RefCounted<EmbeddedWorkerRegistry>; 89 friend class EmbeddedWorkerInstance; 90 91 typedef std::map<int, EmbeddedWorkerInstance*> WorkerInstanceMap; 92 typedef std::map<int, IPC::Sender*> ProcessToSenderMap; 93 94 ~EmbeddedWorkerRegistry(); 95 96 ServiceWorkerStatusCode Send(int process_id, IPC::Message* message); 97 98 // RemoveWorker is called when EmbeddedWorkerInstance is destructed. 99 // |process_id| could be invalid (i.e. -1) if it's not running. 100 void RemoveWorker(int process_id, int embedded_worker_id); 101 102 base::WeakPtr<ServiceWorkerContextCore> context_; 103 104 WorkerInstanceMap worker_map_; 105 ProcessToSenderMap process_sender_map_; 106 107 // Map from process_id to embedded_worker_id. 108 // This map only contains running workers. 109 std::map<int, std::set<int> > worker_process_map_; 110 111 int next_embedded_worker_id_; 112 113 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerRegistry); 114 }; 115 116 } // namespace content 117 118 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_ 119