ServiceWorker: Stop exposing ServiceWorkerContextCore
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_registry.h
blobf17f1c36392e3185d8fd1a0e4aa5f6e60f99fc98
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_
8 #include <map>
9 #include <set>
10 #include <vector>
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;
21 class GURL;
23 namespace IPC {
24 class Message;
25 class Sender;
28 namespace content {
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>) {
41 public:
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,
62 int process_id);
63 ServiceWorkerStatusCode StopWorker(int process_id,
64 int embedded_worker_id);
66 // Stop all active workers, even if they're handling events.
67 void Shutdown();
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,
73 int thread_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,
78 bool success);
79 void OnWorkerStarted(int process_id, int embedded_worker_id);
80 void OnWorkerStopped(int process_id, int embedded_worker_id);
81 void OnPausedAfterDownload(int process_id, int embedded_worker_id);
82 void OnReportException(int embedded_worker_id,
83 const base::string16& error_message,
84 int line_number,
85 int column_number,
86 const GURL& source_url);
87 void OnReportConsoleMessage(int embedded_worker_id,
88 int source_identifier,
89 int message_level,
90 const base::string16& message,
91 int line_number,
92 const GURL& source_url);
94 // Keeps a map from process_id to sender information.
95 void AddChildProcessSender(
96 int process_id,
97 IPC::Sender* sender,
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);
109 private:
110 friend class base::RefCounted<EmbeddedWorkerRegistry>;
111 friend class EmbeddedWorkerInstance;
113 typedef std::map<int, EmbeddedWorkerInstance*> WorkerInstanceMap;
114 typedef std::map<int, IPC::Sender*> ProcessToSenderMap;
115 typedef std::map<int, MessagePortMessageFilter*>
116 ProcessToMessagePortMessageFilterMap;
118 EmbeddedWorkerRegistry(
119 const base::WeakPtr<ServiceWorkerContextCore>& context,
120 int initial_embedded_worker_id);
121 ~EmbeddedWorkerRegistry();
123 ServiceWorkerStatusCode Send(int process_id, IPC::Message* message);
125 // RemoveWorker is called when EmbeddedWorkerInstance is destructed.
126 // |process_id| could be invalid (i.e. -1) if it's not running.
127 void RemoveWorker(int process_id, int embedded_worker_id);
129 base::WeakPtr<ServiceWorkerContextCore> context_;
131 WorkerInstanceMap worker_map_;
132 ProcessToSenderMap process_sender_map_;
133 ProcessToMessagePortMessageFilterMap process_message_port_message_filter_map_;
135 // Map from process_id to embedded_worker_id.
136 // This map only contains starting and running workers.
137 std::map<int, std::set<int> > worker_process_map_;
139 int next_embedded_worker_id_;
140 const int initial_embedded_worker_id_;
142 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerRegistry);
145 } // namespace content
147 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_