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.
5 #ifndef CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_SERVICE_IMPL_H_
6 #define CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_SERVICE_IMPL_H_
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"
19 struct ViewHostMsg_CreateWorker_Params
;
27 class SharedWorkerInstance
;
28 class SharedWorkerHost
;
29 class SharedWorkerMessageFilter
;
30 class ResourceContext
;
31 class WorkerServiceObserver
;
32 class WorkerStoragePartitionId
;
34 // If "enable-embedded-shared-worker" is set this class will be used instead of
36 // TODO(horo): implement this class.
37 class CONTENT_EXPORT SharedWorkerServiceImpl
38 : public NON_EXPORTED_BASE(WorkerService
) {
40 // Returns the SharedWorkerServiceImpl singleton.
41 static SharedWorkerServiceImpl
* GetInstance();
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
;
49 // These methods correspond to worker related IPCs.
50 void CreateWorker(const ViewHostMsg_CreateWorker_Params
& params
,
52 SharedWorkerMessageFilter
* filter
,
53 ResourceContext
* resource_context
,
54 const WorkerStoragePartitionId
& partition_id
,
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 WorkerReadyForInspection(int worker_route_id
,
65 SharedWorkerMessageFilter
* filter
);
66 void WorkerScriptLoaded(int worker_route_id
,
67 SharedWorkerMessageFilter
* filter
);
68 void WorkerScriptLoadFailed(int worker_route_id
,
69 SharedWorkerMessageFilter
* filter
);
70 void WorkerConnected(int message_port_id
,
72 SharedWorkerMessageFilter
* filter
);
73 void AllowDatabase(int worker_route_id
,
75 const base::string16
& name
,
76 const base::string16
& display_name
,
77 unsigned long estimated_size
,
79 SharedWorkerMessageFilter
* filter
);
80 void AllowFileSystem(int worker_route_id
,
82 IPC::Message
* reply_msg
,
83 SharedWorkerMessageFilter
* filter
);
84 void AllowIndexedDB(int worker_route_id
,
86 const base::string16
& name
,
88 SharedWorkerMessageFilter
* filter
);
90 void OnSharedWorkerMessageFilterClosing(
91 SharedWorkerMessageFilter
* filter
);
93 // Checks the worker dependency of renderer processes and calls
94 // IncrementWorkerRefCount and DecrementWorkerRefCount of
95 // RenderProcessHostImpl on UI thread if necessary.
96 void CheckWorkerDependency();
98 void NotifyWorkerDestroyed(int worker_process_id
, int worker_route_id
);
101 class SharedWorkerPendingInstance
;
102 class SharedWorkerReserver
;
104 friend struct DefaultSingletonTraits
<SharedWorkerServiceImpl
>;
105 friend class SharedWorkerServiceImplTest
;
107 typedef void (*UpdateWorkerDependencyFunc
)(const std::vector
<int>&,
108 const std::vector
<int>&);
109 typedef bool (*TryIncrementWorkerRefCountFunc
)(bool);
110 // Pair of render_process_id and worker_route_id.
111 typedef std::pair
<int, int> ProcessRouteIdPair
;
112 typedef base::ScopedPtrHashMap
<ProcessRouteIdPair
, SharedWorkerHost
>
114 typedef base::ScopedPtrHashMap
<int, SharedWorkerPendingInstance
>
117 SharedWorkerServiceImpl();
118 virtual ~SharedWorkerServiceImpl();
120 void ResetForTesting();
122 // Reserves the render process to create Shared Worker. This reservation
123 // procedure will be executed on UI thread and
124 // RenderProcessReservedCallback() or RenderProcessReserveFailedCallback()
125 // will be called on IO thread.
126 void ReserveRenderProcessToCreateWorker(
127 scoped_ptr
<SharedWorkerPendingInstance
> pending_instance
,
130 // Called after the render process is reserved to create Shared Worker in it.
131 void RenderProcessReservedCallback(int pending_instance_id
,
132 int worker_process_id
,
135 bool pause_on_start
);
137 // Called after the fast shutdown is detected while reserving the render
138 // process to create Shared Worker in it.
139 void RenderProcessReserveFailedCallback(int pending_instance_id
,
140 int worker_process_id
,
144 SharedWorkerHost
* FindSharedWorkerHost(
145 SharedWorkerMessageFilter
* filter
,
146 int worker_route_id
);
148 SharedWorkerHost
* FindSharedWorkerHost(const SharedWorkerInstance
& instance
);
149 SharedWorkerPendingInstance
* FindPendingInstance(
150 const SharedWorkerInstance
& instance
);
152 // Returns the IDs of the renderer processes which are executing
153 // SharedWorkers connected to other renderer processes.
154 const std::set
<int> GetRenderersWithWorkerDependency();
156 void ChangeUpdateWorkerDependencyFuncForTesting(
157 UpdateWorkerDependencyFunc new_func
);
158 void ChangeTryIncrementWorkerRefCountFuncForTesting(bool (*new_func
)(int));
160 std::set
<int> last_worker_depended_renderers_
;
161 // Function ptr to update worker dependency, tests may override this.
162 UpdateWorkerDependencyFunc update_worker_dependency_
;
164 // Function ptr to increment worker ref count, tests may override this.
165 static bool (*s_try_increment_worker_ref_count_
)(int);
167 WorkerHostMap worker_hosts_
;
168 PendingInstaneMap pending_instances_
;
169 int next_pending_instance_id_
;
171 ObserverList
<WorkerServiceObserver
> observers_
;
173 DISALLOW_COPY_AND_ASSIGN(SharedWorkerServiceImpl
);
176 } // namespace content
178 #endif // CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_SERVICE_IMPL_H_