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/gtest_prod_util.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/strings/string16.h"
18 #include "content/common/content_export.h"
19 #include "content/common/service_worker/service_worker_status_code.h"
21 struct EmbeddedWorkerMsg_StartWorker_Params
;
31 class EmbeddedWorkerInstance
;
32 class MessagePortMessageFilter
;
33 class ServiceWorkerContextCore
;
35 // Acts as a thin stub between MessageFilter and each EmbeddedWorkerInstance,
36 // which sends/receives messages to/from each EmbeddedWorker in child process.
38 // Hangs off ServiceWorkerContextCore (its reference is also held by each
39 // EmbeddedWorkerInstance). Operated only on IO thread.
40 class CONTENT_EXPORT EmbeddedWorkerRegistry
41 : public NON_EXPORTED_BASE(base::RefCounted
<EmbeddedWorkerRegistry
>) {
43 typedef base::Callback
<void(ServiceWorkerStatusCode
)> StatusCallback
;
45 static scoped_refptr
<EmbeddedWorkerRegistry
> Create(
46 const base::WeakPtr
<ServiceWorkerContextCore
>& contxet
);
48 // Used for DeleteAndStartOver. Creates a new registry which takes over
49 // |next_embedded_worker_id_| and |process_sender_map_| from |old_registry|.
50 static scoped_refptr
<EmbeddedWorkerRegistry
> Create(
51 const base::WeakPtr
<ServiceWorkerContextCore
>& context
,
52 EmbeddedWorkerRegistry
* old_registry
);
54 bool OnMessageReceived(const IPC::Message
& message
, int process_id
);
56 // Creates and removes a new worker instance entry for bookkeeping.
57 // This doesn't actually start or stop the worker.
58 scoped_ptr
<EmbeddedWorkerInstance
> CreateWorker();
60 // Called from EmbeddedWorkerInstance, relayed to the child process.
61 ServiceWorkerStatusCode
SendStartWorker(
62 scoped_ptr
<EmbeddedWorkerMsg_StartWorker_Params
> params
,
64 ServiceWorkerStatusCode
StopWorker(int process_id
,
65 int embedded_worker_id
);
67 // Stop all active workers, even if they're handling events.
70 // Called back from EmbeddedWorker in the child process, relayed via
71 // ServiceWorkerDispatcherHost.
72 void OnWorkerReadyForInspection(int process_id
, int embedded_worker_id
);
73 void OnWorkerScriptLoaded(int process_id
,
75 int embedded_worker_id
);
76 void OnWorkerScriptLoadFailed(int process_id
, int embedded_worker_id
);
77 void OnWorkerScriptEvaluated(int process_id
,
78 int embedded_worker_id
,
80 void OnWorkerStarted(int process_id
, int embedded_worker_id
);
81 void OnWorkerStopped(int process_id
, int embedded_worker_id
);
82 void OnReportException(int embedded_worker_id
,
83 const base::string16
& error_message
,
86 const GURL
& source_url
);
87 void OnReportConsoleMessage(int embedded_worker_id
,
88 int source_identifier
,
90 const base::string16
& message
,
92 const GURL
& source_url
);
94 // Keeps a map from process_id to sender information.
95 void AddChildProcessSender(
98 MessagePortMessageFilter
* message_port_message_filter
);
99 void RemoveChildProcessSender(int process_id
);
101 // Returns an embedded worker instance for given |embedded_worker_id|.
102 EmbeddedWorkerInstance
* GetWorker(int embedded_worker_id
);
104 // Returns true if |embedded_worker_id| is managed by this registry.
105 bool CanHandle(int embedded_worker_id
) const;
107 MessagePortMessageFilter
* MessagePortMessageFilterForProcess(int process_id
);
110 friend class base::RefCounted
<EmbeddedWorkerRegistry
>;
111 friend class EmbeddedWorkerInstance
;
112 friend class EmbeddedWorkerInstanceTest
;
113 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest
,
114 RemoveWorkerInSharedProcess
);
116 typedef std::map
<int, EmbeddedWorkerInstance
*> WorkerInstanceMap
;
117 typedef std::map
<int, IPC::Sender
*> ProcessToSenderMap
;
118 typedef std::map
<int, MessagePortMessageFilter
*>
119 ProcessToMessagePortMessageFilterMap
;
121 EmbeddedWorkerRegistry(
122 const base::WeakPtr
<ServiceWorkerContextCore
>& context
,
123 int initial_embedded_worker_id
);
124 ~EmbeddedWorkerRegistry();
126 ServiceWorkerStatusCode
Send(int process_id
, IPC::Message
* message
);
128 // RemoveWorker is called when EmbeddedWorkerInstance is destructed.
129 // |process_id| could be invalid (i.e. -1) if it's not running.
130 void RemoveWorker(int process_id
, int embedded_worker_id
);
132 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
134 WorkerInstanceMap worker_map_
;
135 ProcessToSenderMap process_sender_map_
;
136 ProcessToMessagePortMessageFilterMap process_message_port_message_filter_map_
;
138 // Map from process_id to embedded_worker_id.
139 // This map only contains starting and running workers.
140 std::map
<int, std::set
<int> > worker_process_map_
;
142 int next_embedded_worker_id_
;
143 const int initial_embedded_worker_id_
;
145 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerRegistry
);
148 } // namespace content
150 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_