Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / embedded_worker_registry.h
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.
4
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_
7
8 #include <map>
9
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "content/common/content_export.h"
15
16 class GURL;
17
18 namespace IPC {
19 class Message;
20 class Sender;
21 }
22
23 namespace content {
24
25 class EmbeddedWorkerInstance;
26 class ServiceWorkerContextCore;
27
28 // Acts as a thin stub between MessageFilter and each EmbeddedWorkerInstance,
29 // which sends/receives messages to/from each EmbeddedWorker in child process.
30 //
31 // Hangs off ServiceWorkerContextCore (its reference is also held by each
32 // EmbeddedWorkerInstance).  Operated only on IO thread.
33 class CONTENT_EXPORT EmbeddedWorkerRegistry
34     : public NON_EXPORTED_BASE(base::RefCounted<EmbeddedWorkerRegistry>) {
35  public:
36   explicit EmbeddedWorkerRegistry(
37       base::WeakPtr<ServiceWorkerContextCore> context);
38
39   // Creates and removes a new worker instance entry for bookkeeping.
40   // This doesn't actually start or stop the worker.
41   scoped_ptr<EmbeddedWorkerInstance> CreateWorker();
42
43   // Called from EmbeddedWorkerInstance, relayed to the child process.
44   bool StartWorker(int process_id,
45                    int embedded_worker_id,
46                    int64 service_worker_version_id,
47                    const GURL& script_url);
48   bool StopWorker(int process_id,
49                   int embedded_worker_id);
50
51   // Called back from EmbeddedWorker in the child process, relayed via
52   // ServiceWorkerDispatcherHost.
53   void OnWorkerStarted(int process_id, int thread_id, int embedded_worker_id);
54   void OnWorkerStopped(int process_id, int embedded_worker_id);
55   void OnSendMessageToBrowser(int embedded_worker_id,
56                               const IPC::Message& message);
57
58   // Keeps a map from process_id to sender information.
59   void AddChildProcessSender(int process_id, IPC::Sender* sender);
60   void RemoveChildProcessSender(int process_id);
61
62   // Returns an embedded worker instance for given |embedded_worker_id|.
63   EmbeddedWorkerInstance* GetWorker(int embedded_worker_id);
64
65  private:
66   friend class base::RefCounted<EmbeddedWorkerRegistry>;
67   friend class EmbeddedWorkerInstance;
68
69   typedef std::map<int, EmbeddedWorkerInstance*> WorkerInstanceMap;
70   typedef std::map<int, IPC::Sender*> ProcessToSenderMap;
71
72   ~EmbeddedWorkerRegistry();
73   bool Send(int process_id, IPC::Message* message);
74
75   // RemoveWorker is called when EmbeddedWorkerInstance is destructed.
76   // |process_id| could be invalid (i.e. -1) if it's not running.
77   void RemoveWorker(int process_id, int embedded_worker_id);
78
79   base::WeakPtr<ServiceWorkerContextCore> context_;
80
81   WorkerInstanceMap worker_map_;
82   ProcessToSenderMap process_sender_map_;
83
84   // Map from process_id to embedded_worker_id.
85   // This map only contains running workers.
86   std::map<int, int> worker_process_map_;
87
88   int next_embedded_worker_id_;
89
90   DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerRegistry);
91 };
92
93 }  // namespace content
94
95 #endif  // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_REGISTRY_H_