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 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,
70 SharedWorkerMessageFilter* filter);
71 void AllowDatabase(int worker_route_id,
73 const base::string16& name,
74 const base::string16& display_name,
75 unsigned long estimated_size,
77 SharedWorkerMessageFilter* filter);
78 void AllowFileSystem(int worker_route_id,
80 IPC::Message* reply_msg,
81 SharedWorkerMessageFilter* filter);
82 void AllowIndexedDB(int worker_route_id,
84 const base::string16& name,
86 SharedWorkerMessageFilter* filter);
88 void OnSharedWorkerMessageFilterClosing(
89 SharedWorkerMessageFilter* filter);
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();
96 void NotifyWorkerDestroyed(int worker_process_id, int worker_route_id);
99 class SharedWorkerPendingInstance;
100 class SharedWorkerReserver;
102 friend struct DefaultSingletonTraits<SharedWorkerServiceImpl>;
103 friend class SharedWorkerServiceImplTest;
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>
112 typedef base::ScopedPtrHashMap<int, SharedWorkerPendingInstance>
115 SharedWorkerServiceImpl();
116 virtual ~SharedWorkerServiceImpl();
118 void ResetForTesting();
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,
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,
133 bool pause_on_start);
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,
142 SharedWorkerHost* FindSharedWorkerHost(
143 SharedWorkerMessageFilter* filter,
144 int worker_route_id);
146 SharedWorkerHost* FindSharedWorkerHost(const SharedWorkerInstance& instance);
147 SharedWorkerPendingInstance* FindPendingInstance(
148 const SharedWorkerInstance& instance);
150 // Returns the IDs of the renderer processes which are executing
151 // SharedWorkers connected to other renderer processes.
152 const std::set<int> GetRenderersWithWorkerDependency();
154 void ChangeUpdateWorkerDependencyFuncForTesting(
155 UpdateWorkerDependencyFunc new_func);
156 void ChangeTryIncrementWorkerRefCountFuncForTesting(bool (*new_func)(int));
158 std::set<int> last_worker_depended_renderers_;
159 // Function ptr to update worker dependency, tests may override this.
160 UpdateWorkerDependencyFunc update_worker_dependency_;
162 // Function ptr to increment worker ref count, tests may override this.
163 static bool (*s_try_increment_worker_ref_count_)(int);
165 WorkerHostMap worker_hosts_;
166 PendingInstaneMap pending_instances_;
167 int next_pending_instance_id_;
169 ObserverList<WorkerServiceObserver> observers_;
171 DISALLOW_COPY_AND_ASSIGN(SharedWorkerServiceImpl);
174 } // namespace content
176 #endif // CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_SERVICE_IMPL_H_