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/singleton.h"
13 #include "base/observer_list.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "content/public/browser/worker_service.h"
18 struct ViewHostMsg_CreateWorker_Params
;
26 class SharedWorkerInstance
;
27 class SharedWorkerHost
;
28 class SharedWorkerMessageFilter
;
29 class ResourceContext
;
30 class WorkerServiceObserver
;
31 class WorkerStoragePartitionId
;
33 // If "enable-embedded-shared-worker" is set this class will be used instead of
35 // TODO(horo): implement this class.
36 class CONTENT_EXPORT SharedWorkerServiceImpl
37 : public NON_EXPORTED_BASE(WorkerService
) {
39 // Returns the SharedWorkerServiceImpl singleton.
40 static SharedWorkerServiceImpl
* GetInstance();
42 // WorkerService implementation:
43 bool TerminateWorker(int process_id
, int route_id
) override
;
44 std::vector
<WorkerInfo
> GetWorkers() override
;
45 void AddObserver(WorkerServiceObserver
* observer
) override
;
46 void RemoveObserver(WorkerServiceObserver
* observer
) override
;
48 // These methods correspond to worker related IPCs.
49 void CreateWorker(const ViewHostMsg_CreateWorker_Params
& params
,
51 SharedWorkerMessageFilter
* filter
,
52 ResourceContext
* resource_context
,
53 const WorkerStoragePartitionId
& partition_id
,
55 void ForwardToWorker(const IPC::Message
& message
,
56 SharedWorkerMessageFilter
* filter
);
57 void DocumentDetached(unsigned long long document_id
,
58 SharedWorkerMessageFilter
* filter
);
59 void WorkerContextClosed(int worker_route_id
,
60 SharedWorkerMessageFilter
* filter
);
61 void WorkerContextDestroyed(int worker_route_id
,
62 SharedWorkerMessageFilter
* filter
);
63 void WorkerReadyForInspection(int worker_route_id
,
64 SharedWorkerMessageFilter
* filter
);
65 void WorkerScriptLoaded(int worker_route_id
,
66 SharedWorkerMessageFilter
* filter
);
67 void WorkerScriptLoadFailed(int worker_route_id
,
68 SharedWorkerMessageFilter
* filter
);
69 void WorkerConnected(int message_port_id
,
71 SharedWorkerMessageFilter
* filter
);
72 void AllowDatabase(int worker_route_id
,
74 const base::string16
& name
,
75 const base::string16
& display_name
,
76 unsigned long estimated_size
,
78 SharedWorkerMessageFilter
* filter
);
79 void AllowFileSystem(int worker_route_id
,
81 IPC::Message
* reply_msg
,
82 SharedWorkerMessageFilter
* filter
);
83 void AllowIndexedDB(int worker_route_id
,
85 const base::string16
& name
,
87 SharedWorkerMessageFilter
* filter
);
89 void OnSharedWorkerMessageFilterClosing(
90 SharedWorkerMessageFilter
* filter
);
92 // Checks the worker dependency of renderer processes and calls
93 // IncrementWorkerRefCount and DecrementWorkerRefCount of
94 // RenderProcessHostImpl on UI thread if necessary.
95 void CheckWorkerDependency();
97 void NotifyWorkerDestroyed(int worker_process_id
, int worker_route_id
);
100 class SharedWorkerPendingInstance
;
101 class SharedWorkerReserver
;
103 friend struct base::DefaultSingletonTraits
<SharedWorkerServiceImpl
>;
104 friend class SharedWorkerServiceImplTest
;
106 typedef void (*UpdateWorkerDependencyFunc
)(const std::vector
<int>&,
107 const std::vector
<int>&);
108 typedef bool (*TryIncrementWorkerRefCountFunc
)(bool);
109 // Pair of render_process_id and worker_route_id.
110 typedef std::pair
<int, int> ProcessRouteIdPair
;
111 typedef base::ScopedPtrHashMap
<ProcessRouteIdPair
,
112 scoped_ptr
<SharedWorkerHost
>> WorkerHostMap
;
113 typedef base::ScopedPtrHashMap
<int, scoped_ptr
<SharedWorkerPendingInstance
>>
116 SharedWorkerServiceImpl();
117 ~SharedWorkerServiceImpl() override
;
119 void ResetForTesting();
121 // Reserves the render process to create Shared Worker. This reservation
122 // procedure will be executed on UI thread and
123 // RenderProcessReservedCallback() or RenderProcessReserveFailedCallback()
124 // will be called on IO thread.
125 void ReserveRenderProcessToCreateWorker(
126 scoped_ptr
<SharedWorkerPendingInstance
> pending_instance
,
129 // Called after the render process is reserved to create Shared Worker in it.
130 void RenderProcessReservedCallback(int pending_instance_id
,
131 int worker_process_id
,
134 bool pause_on_start
);
136 // Called after the fast shutdown is detected while reserving the render
137 // process to create Shared Worker in it.
138 void RenderProcessReserveFailedCallback(int pending_instance_id
,
139 int worker_process_id
,
143 SharedWorkerHost
* FindSharedWorkerHost(
144 SharedWorkerMessageFilter
* filter
,
145 int worker_route_id
);
147 SharedWorkerHost
* FindSharedWorkerHost(const SharedWorkerInstance
& instance
);
148 SharedWorkerPendingInstance
* FindPendingInstance(
149 const SharedWorkerInstance
& instance
);
151 // Returns the IDs of the renderer processes which are executing
152 // SharedWorkers connected to other renderer processes.
153 const std::set
<int> GetRenderersWithWorkerDependency();
155 void ChangeUpdateWorkerDependencyFuncForTesting(
156 UpdateWorkerDependencyFunc new_func
);
157 void ChangeTryIncrementWorkerRefCountFuncForTesting(bool (*new_func
)(int));
159 std::set
<int> last_worker_depended_renderers_
;
160 // Function ptr to update worker dependency, tests may override this.
161 UpdateWorkerDependencyFunc update_worker_dependency_
;
163 // Function ptr to increment worker ref count, tests may override this.
164 static bool (*s_try_increment_worker_ref_count_
)(int);
166 WorkerHostMap worker_hosts_
;
167 PendingInstaneMap pending_instances_
;
168 int next_pending_instance_id_
;
170 base::ObserverList
<WorkerServiceObserver
> observers_
;
172 DISALLOW_COPY_AND_ASSIGN(SharedWorkerServiceImpl
);
175 } // namespace content
177 #endif // CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_SERVICE_IMPL_H_