[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / content / browser / shared_worker / shared_worker_service_impl.h
blobb09d58345a9275d4429e5e37a226b6832dbc4fd6
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_
8 #include <set>
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;
21 namespace IPC {
22 class Message;
25 namespace content {
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
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();
43 // WorkerService implementation:
44 bool TerminateWorker(int process_id, int route_id) override;
45 std::vector<WorkerInfo> GetWorkers() override;
46 void AddObserver(WorkerServiceObserver* observer) override;
47 void RemoveObserver(WorkerServiceObserver* observer) override;
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 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,
71 int worker_route_id,
72 SharedWorkerMessageFilter* filter);
73 void AllowDatabase(int worker_route_id,
74 const GURL& url,
75 const base::string16& name,
76 const base::string16& display_name,
77 unsigned long estimated_size,
78 bool* result,
79 SharedWorkerMessageFilter* filter);
80 void AllowFileSystem(int worker_route_id,
81 const GURL& url,
82 IPC::Message* reply_msg,
83 SharedWorkerMessageFilter* filter);
84 void AllowIndexedDB(int worker_route_id,
85 const GURL& url,
86 const base::string16& name,
87 bool* result,
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);
100 private:
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>
113 WorkerHostMap;
114 typedef base::ScopedPtrHashMap<int, SharedWorkerPendingInstance>
115 PendingInstaneMap;
117 SharedWorkerServiceImpl();
118 ~SharedWorkerServiceImpl() override;
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,
128 bool* url_mismatch);
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,
133 int worker_route_id,
134 bool is_new_worker,
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,
141 int worker_route_id,
142 bool is_new_worker);
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_