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