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 ServiceWorkerContextCore
;
33 // Acts as a thin stub between MessageFilter and each EmbeddedWorkerInstance,
34 // which sends/receives messages to/from each EmbeddedWorker in child process.
36 // Hangs off ServiceWorkerContextCore (its reference is also held by each
37 // EmbeddedWorkerInstance). Operated only on IO thread.
38 class CONTENT_EXPORT EmbeddedWorkerRegistry
39 : public NON_EXPORTED_BASE(base::RefCounted
<EmbeddedWorkerRegistry
>) {
41 typedef base::Callback
<void(ServiceWorkerStatusCode
)> StatusCallback
;
43 explicit EmbeddedWorkerRegistry(
44 base::WeakPtr
<ServiceWorkerContextCore
> context
);
46 bool OnMessageReceived(const IPC::Message
& message
);
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();
52 // Called from EmbeddedWorkerInstance, relayed to the child process.
53 void StartWorker(const std::vector
<int>& process_ids
,
54 int embedded_worker_id
,
55 int64 service_worker_version_id
,
57 const GURL
& script_url
,
58 const StatusCallback
& callback
);
59 ServiceWorkerStatusCode
StopWorker(int process_id
,
60 int embedded_worker_id
);
62 // Stop all active workers, even if they're handling events.
65 // Called back from EmbeddedWorker in the child process, relayed via
66 // ServiceWorkerDispatcherHost.
67 void OnWorkerScriptLoaded(int process_id
, int embedded_worker_id
);
68 void OnWorkerScriptLoadFailed(int process_id
, int embedded_worker_id
);
69 void OnWorkerStarted(int process_id
, int thread_id
, int embedded_worker_id
);
70 void OnWorkerStopped(int process_id
, int embedded_worker_id
);
71 void OnReportException(int embedded_worker_id
,
72 const base::string16
& error_message
,
75 const GURL
& source_url
);
76 void OnReportConsoleMessage(int embedded_worker_id
,
77 int source_identifier
,
79 const base::string16
& message
,
81 const GURL
& source_url
);
83 // Keeps a map from process_id to sender information.
84 void AddChildProcessSender(int process_id
, IPC::Sender
* sender
);
85 void RemoveChildProcessSender(int process_id
);
87 // Returns an embedded worker instance for given |embedded_worker_id|.
88 EmbeddedWorkerInstance
* GetWorker(int embedded_worker_id
);
91 friend class base::RefCounted
<EmbeddedWorkerRegistry
>;
92 friend class EmbeddedWorkerInstance
;
94 typedef std::map
<int, EmbeddedWorkerInstance
*> WorkerInstanceMap
;
95 typedef std::map
<int, IPC::Sender
*> ProcessToSenderMap
;
97 ~EmbeddedWorkerRegistry();
99 void StartWorkerWithProcessId(
100 int embedded_worker_id
,
101 scoped_ptr
<EmbeddedWorkerMsg_StartWorker_Params
> params
,
102 const StatusCallback
& callback
,
103 ServiceWorkerStatusCode status
,
106 ServiceWorkerStatusCode
Send(int process_id
, IPC::Message
* message
);
108 // RemoveWorker is called when EmbeddedWorkerInstance is destructed.
109 // |process_id| could be invalid (i.e. -1) if it's not running.
110 void RemoveWorker(int process_id
, int embedded_worker_id
);
112 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
114 WorkerInstanceMap worker_map_
;
115 ProcessToSenderMap process_sender_map_
;
117 // Map from process_id to embedded_worker_id.
118 // This map only contains running workers.
119 std::map
<int, std::set
<int> > worker_process_map_
;
121 int next_embedded_worker_id_
;
123 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerRegistry
);
126 } // namespace content
128 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_