Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / shared_worker / shared_worker_service_impl.h
1 // Copyright 2014 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_SHARED_WORKER_SHARED_WORKER_SERVICE_IMPL_H_
6 #define CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_SERVICE_IMPL_H_
7
8 #include <set>
9
10 #include "base/compiler_specific.h"
11 #include "base/containers/scoped_ptr_hash_map.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/singleton.h"
14 #include "base/observer_list.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "content/public/browser/worker_service.h"
18
19 struct ViewHostMsg_CreateWorker_Params;
20
21 namespace IPC {
22 class Message;
23 }
24
25 namespace content {
26
27 class SharedWorkerInstance;
28 class SharedWorkerHost;
29 class SharedWorkerMessageFilter;
30 class ResourceContext;
31 class WorkerServiceObserver;
32 class WorkerStoragePartitionId;
33
34 // If "enable-embedded-shared-worker" is set this class will be used instead of
35 // WorkerServiceImpl.
36 // TODO(horo): implement this class.
37 class CONTENT_EXPORT SharedWorkerServiceImpl
38     : public NON_EXPORTED_BASE(WorkerService) {
39  public:
40   // Returns the SharedWorkerServiceImpl singleton.
41   static SharedWorkerServiceImpl* GetInstance();
42
43   // WorkerService implementation:
44   virtual bool TerminateWorker(int process_id, int route_id) OVERRIDE;
45   virtual std::vector<WorkerInfo> GetWorkers() OVERRIDE;
46   virtual void AddObserver(WorkerServiceObserver* observer) OVERRIDE;
47   virtual void RemoveObserver(WorkerServiceObserver* observer) OVERRIDE;
48
49   // These methods correspond to worker related IPCs.
50   void CreateWorker(const ViewHostMsg_CreateWorker_Params& params,
51                     int route_id,
52                     SharedWorkerMessageFilter* filter,
53                     ResourceContext* resource_context,
54                     const WorkerStoragePartitionId& partition_id,
55                     bool* url_mismatch);
56   void ForwardToWorker(const IPC::Message& message,
57                        SharedWorkerMessageFilter* filter);
58   void DocumentDetached(unsigned long long document_id,
59                         SharedWorkerMessageFilter* filter);
60   void WorkerContextClosed(int worker_route_id,
61                            SharedWorkerMessageFilter* filter);
62   void WorkerContextDestroyed(int worker_route_id,
63                               SharedWorkerMessageFilter* filter);
64   void WorkerScriptLoaded(int worker_route_id,
65                           SharedWorkerMessageFilter* filter);
66   void WorkerScriptLoadFailed(int worker_route_id,
67                               SharedWorkerMessageFilter* filter);
68   void WorkerConnected(int message_port_id,
69                        int worker_route_id,
70                        SharedWorkerMessageFilter* filter);
71   void AllowDatabase(int worker_route_id,
72                      const GURL& url,
73                      const base::string16& name,
74                      const base::string16& display_name,
75                      unsigned long estimated_size,
76                      bool* result,
77                      SharedWorkerMessageFilter* filter);
78   void AllowFileSystem(int worker_route_id,
79                        const GURL& url,
80                        IPC::Message* reply_msg,
81                        SharedWorkerMessageFilter* filter);
82   void AllowIndexedDB(int worker_route_id,
83                       const GURL& url,
84                       const base::string16& name,
85                       bool* result,
86                       SharedWorkerMessageFilter* filter);
87
88   void OnSharedWorkerMessageFilterClosing(
89       SharedWorkerMessageFilter* filter);
90
91   // Checks the worker dependency of renderer processes and calls
92   // IncrementWorkerRefCount and DecrementWorkerRefCount of
93   // RenderProcessHostImpl on UI thread if necessary.
94   void CheckWorkerDependency();
95
96   void NotifyWorkerDestroyed(int worker_process_id, int worker_route_id);
97
98  private:
99   class SharedWorkerPendingInstance;
100   class SharedWorkerReserver;
101
102   friend struct DefaultSingletonTraits<SharedWorkerServiceImpl>;
103   friend class SharedWorkerServiceImplTest;
104
105   typedef void (*UpdateWorkerDependencyFunc)(const std::vector<int>&,
106                                              const std::vector<int>&);
107   typedef bool (*TryIncrementWorkerRefCountFunc)(bool);
108   // Pair of render_process_id and worker_route_id.
109   typedef std::pair<int, int> ProcessRouteIdPair;
110   typedef base::ScopedPtrHashMap<ProcessRouteIdPair, SharedWorkerHost>
111       WorkerHostMap;
112   typedef base::ScopedPtrHashMap<int, SharedWorkerPendingInstance>
113       PendingInstaneMap;
114
115   SharedWorkerServiceImpl();
116   virtual ~SharedWorkerServiceImpl();
117
118   void ResetForTesting();
119
120   // Reserves the render process to create Shared Worker. This reservation
121   // procedure will be executed on UI thread and
122   // RenderProcessReservedCallback() or RenderProcessReserveFailedCallback()
123   // will be called on IO thread.
124   void ReserveRenderProcessToCreateWorker(
125       scoped_ptr<SharedWorkerPendingInstance> pending_instance,
126       bool* url_mismatch);
127
128   // Called after the render process is reserved to create Shared Worker in it.
129   void RenderProcessReservedCallback(int pending_instance_id,
130                                      int worker_process_id,
131                                      int worker_route_id,
132                                      bool is_new_worker,
133                                      bool pause_on_start);
134
135   // Called after the fast shutdown is detected while reserving the render
136   // process to create Shared Worker in it.
137   void RenderProcessReserveFailedCallback(int pending_instance_id,
138                                           int worker_process_id,
139                                           int worker_route_id,
140                                           bool is_new_worker);
141
142   SharedWorkerHost* FindSharedWorkerHost(
143       SharedWorkerMessageFilter* filter,
144       int worker_route_id);
145
146   SharedWorkerHost* FindSharedWorkerHost(const SharedWorkerInstance& instance);
147   SharedWorkerPendingInstance* FindPendingInstance(
148       const SharedWorkerInstance& instance);
149
150   // Returns the IDs of the renderer processes which are executing
151   // SharedWorkers connected to other renderer processes.
152   const std::set<int> GetRenderersWithWorkerDependency();
153
154   void ChangeUpdateWorkerDependencyFuncForTesting(
155       UpdateWorkerDependencyFunc new_func);
156   void ChangeTryIncrementWorkerRefCountFuncForTesting(bool (*new_func)(int));
157
158   std::set<int> last_worker_depended_renderers_;
159   // Function ptr to update worker dependency, tests may override this.
160   UpdateWorkerDependencyFunc update_worker_dependency_;
161
162   // Function ptr to increment worker ref count, tests may override this.
163   static bool (*s_try_increment_worker_ref_count_)(int);
164
165   WorkerHostMap worker_hosts_;
166   PendingInstaneMap pending_instances_;
167   int next_pending_instance_id_;
168
169   ObserverList<WorkerServiceObserver> observers_;
170
171   DISALLOW_COPY_AND_ASSIGN(SharedWorkerServiceImpl);
172 };
173
174 }  // namespace content
175
176 #endif  // CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_SERVICE_IMPL_H_