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