Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_registry.h
blobafb9dcb5dfa062ef8cac8d33c4c6d74b58437dec
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/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;
22 class GURL;
24 namespace IPC {
25 class Message;
26 class Sender;
29 namespace content {
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>) {
42 public:
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,
63 int process_id);
64 ServiceWorkerStatusCode StopWorker(int process_id,
65 int embedded_worker_id);
67 // Stop all active workers, even if they're handling events.
68 void Shutdown();
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, int embedded_worker_id);
74 void OnWorkerThreadStarted(int process_id,
75 int thread_id,
76 int embedded_worker_id);
77 void OnWorkerScriptLoadFailed(int process_id, int embedded_worker_id);
78 void OnWorkerScriptEvaluated(int process_id,
79 int embedded_worker_id,
80 bool success);
81 void OnWorkerStarted(int process_id, int embedded_worker_id);
82 void OnWorkerStopped(int process_id, int embedded_worker_id);
83 void OnReportException(int embedded_worker_id,
84 const base::string16& error_message,
85 int line_number,
86 int column_number,
87 const GURL& source_url);
88 void OnReportConsoleMessage(int embedded_worker_id,
89 int source_identifier,
90 int message_level,
91 const base::string16& message,
92 int line_number,
93 const GURL& source_url);
95 // Keeps a map from process_id to sender information.
96 void AddChildProcessSender(
97 int process_id,
98 IPC::Sender* sender,
99 MessagePortMessageFilter* message_port_message_filter);
100 void RemoveChildProcessSender(int process_id);
102 // Returns an embedded worker instance for given |embedded_worker_id|.
103 EmbeddedWorkerInstance* GetWorker(int embedded_worker_id);
105 // Returns true if |embedded_worker_id| is managed by this registry.
106 bool CanHandle(int embedded_worker_id) const;
108 MessagePortMessageFilter* MessagePortMessageFilterForProcess(int process_id);
110 private:
111 friend class base::RefCounted<EmbeddedWorkerRegistry>;
112 friend class EmbeddedWorkerInstance;
113 friend class EmbeddedWorkerInstanceTest;
114 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest,
115 RemoveWorkerInSharedProcess);
117 typedef std::map<int, EmbeddedWorkerInstance*> WorkerInstanceMap;
118 typedef std::map<int, IPC::Sender*> ProcessToSenderMap;
119 typedef std::map<int, MessagePortMessageFilter*>
120 ProcessToMessagePortMessageFilterMap;
122 EmbeddedWorkerRegistry(
123 const base::WeakPtr<ServiceWorkerContextCore>& context,
124 int initial_embedded_worker_id);
125 ~EmbeddedWorkerRegistry();
127 ServiceWorkerStatusCode Send(int process_id, IPC::Message* message);
129 // RemoveWorker is called when EmbeddedWorkerInstance is destructed.
130 // |process_id| could be invalid (i.e. -1) if it's not running.
131 void RemoveWorker(int process_id, int embedded_worker_id);
133 base::WeakPtr<ServiceWorkerContextCore> context_;
135 WorkerInstanceMap worker_map_;
136 ProcessToSenderMap process_sender_map_;
137 ProcessToMessagePortMessageFilterMap process_message_port_message_filter_map_;
139 // Map from process_id to embedded_worker_id.
140 // This map only contains starting and running workers.
141 std::map<int, std::set<int> > worker_process_map_;
143 int next_embedded_worker_id_;
144 const int initial_embedded_worker_id_;
146 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerRegistry);
149 } // namespace content
151 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_