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.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "content/common/content_export.h"
25 class EmbeddedWorkerInstance;
26 class ServiceWorkerContextCore;
28 // Acts as a thin stub between MessageFilter and each EmbeddedWorkerInstance,
29 // which sends/receives messages to/from each EmbeddedWorker in child process.
31 // Hangs off ServiceWorkerContextCore (its reference is also held by each
32 // EmbeddedWorkerInstance). Operated only on IO thread.
33 class CONTENT_EXPORT EmbeddedWorkerRegistry
34 : public NON_EXPORTED_BASE(base::RefCounted<EmbeddedWorkerRegistry>) {
36 explicit EmbeddedWorkerRegistry(
37 base::WeakPtr<ServiceWorkerContextCore> context);
39 // Creates and removes a new worker instance entry for bookkeeping.
40 // This doesn't actually start or stop the worker.
41 scoped_ptr<EmbeddedWorkerInstance> CreateWorker();
43 // Called from EmbeddedWorkerInstance, relayed to the child process.
44 bool StartWorker(int process_id,
45 int embedded_worker_id,
46 int64 service_worker_version_id,
47 const GURL& script_url);
48 bool StopWorker(int process_id,
49 int embedded_worker_id);
51 // Called back from EmbeddedWorker in the child process, relayed via
52 // ServiceWorkerDispatcherHost.
53 void OnWorkerStarted(int process_id, int thread_id, int embedded_worker_id);
54 void OnWorkerStopped(int process_id, int embedded_worker_id);
55 void OnSendMessageToBrowser(int embedded_worker_id,
56 const IPC::Message& message);
58 // Keeps a map from process_id to sender information.
59 void AddChildProcessSender(int process_id, IPC::Sender* sender);
60 void RemoveChildProcessSender(int process_id);
62 // Returns an embedded worker instance for given |embedded_worker_id|.
63 EmbeddedWorkerInstance* GetWorker(int embedded_worker_id);
66 friend class base::RefCounted<EmbeddedWorkerRegistry>;
67 friend class EmbeddedWorkerInstance;
69 typedef std::map<int, EmbeddedWorkerInstance*> WorkerInstanceMap;
70 typedef std::map<int, IPC::Sender*> ProcessToSenderMap;
72 ~EmbeddedWorkerRegistry();
73 bool Send(int process_id, IPC::Message* message);
75 // RemoveWorker is called when EmbeddedWorkerInstance is destructed.
76 // |process_id| could be invalid (i.e. -1) if it's not running.
77 void RemoveWorker(int process_id, int embedded_worker_id);
79 base::WeakPtr<ServiceWorkerContextCore> context_;
81 WorkerInstanceMap worker_map_;
82 ProcessToSenderMap process_sender_map_;
84 // Map from process_id to embedded_worker_id.
85 // This map only contains running workers.
86 std::map<int, int> worker_process_map_;
88 int next_embedded_worker_id_;
90 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerRegistry);
93 } // namespace content
95 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_