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_
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"
20 struct EmbeddedWorkerMsg_StartWorker_Params
;
30 class EmbeddedWorkerInstance
;
31 class MessagePortMessageFilter
;
32 class ServiceWorkerContextCore
;
34 // Acts as a thin stub between MessageFilter and each EmbeddedWorkerInstance,
35 // which sends/receives messages to/from each EmbeddedWorker in child process.
37 // Hangs off ServiceWorkerContextCore (its reference is also held by each
38 // EmbeddedWorkerInstance). Operated only on IO thread.
39 class CONTENT_EXPORT EmbeddedWorkerRegistry
40 : public NON_EXPORTED_BASE(base::RefCounted
<EmbeddedWorkerRegistry
>) {
42 typedef base::Callback
<void(ServiceWorkerStatusCode
)> StatusCallback
;
44 static scoped_refptr
<EmbeddedWorkerRegistry
> Create(
45 const base::WeakPtr
<ServiceWorkerContextCore
>& contxet
);
47 // Used for DeleteAndStartOver. Creates a new registry which takes over
48 // |next_embedded_worker_id_| and |process_sender_map_| from |old_registry|.
49 static scoped_refptr
<EmbeddedWorkerRegistry
> Create(
50 const base::WeakPtr
<ServiceWorkerContextCore
>& context
,
51 EmbeddedWorkerRegistry
* old_registry
);
53 bool OnMessageReceived(const IPC::Message
& message
, int process_id
);
55 // Creates and removes a new worker instance entry for bookkeeping.
56 // This doesn't actually start or stop the worker.
57 scoped_ptr
<EmbeddedWorkerInstance
> CreateWorker();
59 // Called from EmbeddedWorkerInstance, relayed to the child process.
60 ServiceWorkerStatusCode
SendStartWorker(
61 scoped_ptr
<EmbeddedWorkerMsg_StartWorker_Params
> params
,
63 ServiceWorkerStatusCode
StopWorker(int process_id
,
64 int embedded_worker_id
);
66 // Stop all active workers, even if they're handling events.
69 // Called back from EmbeddedWorker in the child process, relayed via
70 // ServiceWorkerDispatcherHost.
71 void OnWorkerReadyForInspection(int process_id
, int embedded_worker_id
);
72 void OnWorkerScriptLoaded(int process_id
,
74 int embedded_worker_id
);
75 void OnWorkerScriptLoadFailed(int process_id
, int embedded_worker_id
);
76 void OnWorkerScriptEvaluated(int process_id
,
77 int embedded_worker_id
,
79 void OnWorkerStarted(int process_id
, int embedded_worker_id
);
80 void OnWorkerStopped(int process_id
, int embedded_worker_id
);
81 void OnReportException(int embedded_worker_id
,
82 const base::string16
& error_message
,
85 const GURL
& source_url
);
86 void OnReportConsoleMessage(int embedded_worker_id
,
87 int source_identifier
,
89 const base::string16
& message
,
91 const GURL
& source_url
);
93 // Keeps a map from process_id to sender information.
94 void AddChildProcessSender(
97 MessagePortMessageFilter
* message_port_message_filter
);
98 void RemoveChildProcessSender(int process_id
);
100 // Returns an embedded worker instance for given |embedded_worker_id|.
101 EmbeddedWorkerInstance
* GetWorker(int embedded_worker_id
);
103 // Returns true if |embedded_worker_id| is managed by this registry.
104 bool CanHandle(int embedded_worker_id
) const;
106 MessagePortMessageFilter
* MessagePortMessageFilterForProcess(int process_id
);
109 friend class base::RefCounted
<EmbeddedWorkerRegistry
>;
110 friend class EmbeddedWorkerInstance
;
112 typedef std::map
<int, EmbeddedWorkerInstance
*> WorkerInstanceMap
;
113 typedef std::map
<int, IPC::Sender
*> ProcessToSenderMap
;
114 typedef std::map
<int, MessagePortMessageFilter
*>
115 ProcessToMessagePortMessageFilterMap
;
117 EmbeddedWorkerRegistry(
118 const base::WeakPtr
<ServiceWorkerContextCore
>& context
,
119 int initial_embedded_worker_id
);
120 ~EmbeddedWorkerRegistry();
122 ServiceWorkerStatusCode
Send(int process_id
, IPC::Message
* message
);
124 // RemoveWorker is called when EmbeddedWorkerInstance is destructed.
125 // |process_id| could be invalid (i.e. -1) if it's not running.
126 void RemoveWorker(int process_id
, int embedded_worker_id
);
128 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
130 WorkerInstanceMap worker_map_
;
131 ProcessToSenderMap process_sender_map_
;
132 ProcessToMessagePortMessageFilterMap process_message_port_message_filter_map_
;
134 // Map from process_id to embedded_worker_id.
135 // This map only contains starting and running workers.
136 std::map
<int, std::set
<int> > worker_process_map_
;
138 int next_embedded_worker_id_
;
139 const int initial_embedded_worker_id_
;
141 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerRegistry
);
144 } // namespace content
146 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_