lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 1 | // Copyright 2016 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 | #include "extensions/renderer/worker_thread_dispatcher.h" |
| 6 | |
Istiaque Ahmed | ee72f495 | 2019-03-06 21:18:36 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 9 | #include "base/bind.h" |
Devlin Cronin | a3fe3d60 | 2017-11-22 04:47:43 | [diff] [blame] | 10 | #include "base/feature_list.h" |
Brett Wilson | 27e878c | 2017-09-08 21:32:21 | [diff] [blame] | 11 | #include "base/lazy_instance.h" |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 12 | #include "base/threading/platform_thread.h" |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 13 | #include "base/threading/thread_local.h" |
| 14 | #include "base/values.h" |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 15 | #include "content/public/renderer/render_thread.h" |
John Abd-El-Malek | 383fc8f6 | 2017-10-23 00:08:42 | [diff] [blame] | 16 | #include "content/public/renderer/worker_thread.h" |
lazyboy | e784724 | 2017-06-07 23:29:18 | [diff] [blame] | 17 | #include "extensions/common/constants.h" |
Devlin Cronin | a3fe3d60 | 2017-11-22 04:47:43 | [diff] [blame] | 18 | #include "extensions/common/extension_features.h" |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 19 | #include "extensions/common/extension_messages.h" |
lazyboy | e784724 | 2017-06-07 23:29:18 | [diff] [blame] | 20 | #include "extensions/renderer/dispatcher.h" |
Istiaque Ahmed | 96f50609 | 2019-07-08 23:40:57 | [diff] [blame] | 21 | #include "extensions/renderer/extension_interaction_provider.h" |
Devlin Cronin | 0a24022 | 2017-12-01 23:17:51 | [diff] [blame] | 22 | #include "extensions/renderer/extensions_renderer_client.h" |
Devlin Cronin | 095dc13 | 2019-03-29 19:43:30 | [diff] [blame] | 23 | #include "extensions/renderer/native_extension_bindings_system.h" |
Devlin Cronin | 613ebffa | 2019-04-17 21:04:17 | [diff] [blame] | 24 | #include "extensions/renderer/native_renderer_messaging_service.h" |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 25 | #include "extensions/renderer/service_worker_data.h" |
Istiaque Ahmed | ee72f495 | 2019-03-06 21:18:36 | [diff] [blame] | 26 | #include "extensions/renderer/worker_script_context_set.h" |
Istiaque Ahmed | 3dd60423 | 2019-08-02 19:22:21 | [diff] [blame] | 27 | #include "extensions/renderer/worker_thread_util.h" |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 28 | |
| 29 | namespace extensions { |
| 30 | |
| 31 | namespace { |
| 32 | |
Jens Widell | c71ec40a | 2018-01-04 09:55:17 | [diff] [blame] | 33 | base::LazyInstance<WorkerThreadDispatcher>::DestructorAtExit |
| 34 | g_worker_thread_dispatcher_instance = LAZY_INSTANCE_INITIALIZER; |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 35 | base::LazyInstance<base::ThreadLocalPointer<extensions::ServiceWorkerData>>:: |
| 36 | DestructorAtExit g_data_tls = LAZY_INSTANCE_INITIALIZER; |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 37 | |
Istiaque Ahmed | be7c3d7 | 2019-09-25 21:25:21 | [diff] [blame] | 38 | ServiceWorkerData* GetServiceWorkerDataChecked() { |
| 39 | ServiceWorkerData* data = WorkerThreadDispatcher::GetServiceWorkerData(); |
| 40 | DCHECK(data); |
| 41 | return data; |
| 42 | } |
| 43 | |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 44 | } // namespace |
| 45 | |
| 46 | WorkerThreadDispatcher::WorkerThreadDispatcher() {} |
| 47 | WorkerThreadDispatcher::~WorkerThreadDispatcher() {} |
| 48 | |
| 49 | WorkerThreadDispatcher* WorkerThreadDispatcher::Get() { |
Jens Widell | c71ec40a | 2018-01-04 09:55:17 | [diff] [blame] | 50 | return g_worker_thread_dispatcher_instance.Pointer(); |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void WorkerThreadDispatcher::Init(content::RenderThread* render_thread) { |
| 54 | DCHECK(render_thread); |
| 55 | DCHECK_EQ(content::RenderThread::Get(), render_thread); |
| 56 | DCHECK(!message_filter_); |
| 57 | message_filter_ = render_thread->GetSyncMessageFilter(); |
| 58 | render_thread->AddObserver(this); |
| 59 | } |
| 60 | |
rdevlin.cronin | 9f33889 | 2016-11-21 19:37:00 | [diff] [blame] | 61 | // static |
Devlin Cronin | 095dc13 | 2019-03-29 19:43:30 | [diff] [blame] | 62 | NativeExtensionBindingsSystem* WorkerThreadDispatcher::GetBindingsSystem() { |
Istiaque Ahmed | be7c3d7 | 2019-09-25 21:25:21 | [diff] [blame] | 63 | return GetServiceWorkerDataChecked()->bindings_system(); |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // static |
rdevlin.cronin | 9f33889 | 2016-11-21 19:37:00 | [diff] [blame] | 67 | V8SchemaRegistry* WorkerThreadDispatcher::GetV8SchemaRegistry() { |
Istiaque Ahmed | be7c3d7 | 2019-09-25 21:25:21 | [diff] [blame] | 68 | return GetServiceWorkerDataChecked()->v8_schema_registry(); |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 69 | } |
| 70 | |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 71 | // static |
Devlin Cronin | 4f02ff0b | 2017-07-19 17:46:10 | [diff] [blame] | 72 | ScriptContext* WorkerThreadDispatcher::GetScriptContext() { |
Istiaque Ahmed | be7c3d7 | 2019-09-25 21:25:21 | [diff] [blame] | 73 | return GetServiceWorkerDataChecked()->context(); |
Devlin Cronin | 4f02ff0b | 2017-07-19 17:46:10 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // static |
Istiaque Ahmed | 91d6987c | 2019-06-25 00:09:33 | [diff] [blame] | 77 | ServiceWorkerData* WorkerThreadDispatcher::GetServiceWorkerData() { |
Istiaque Ahmed | be7c3d7 | 2019-09-25 21:25:21 | [diff] [blame] | 78 | return g_data_tls.Pointer()->Get(); |
Istiaque Ahmed | 91d6987c | 2019-06-25 00:09:33 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // static |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 82 | bool WorkerThreadDispatcher::HandlesMessageOnWorkerThread( |
| 83 | const IPC::Message& message) { |
lazyboy | e784724 | 2017-06-07 23:29:18 | [diff] [blame] | 84 | return message.type() == ExtensionMsg_ResponseWorker::ID || |
Istiaque Ahmed | ee72f495 | 2019-03-06 21:18:36 | [diff] [blame] | 85 | message.type() == ExtensionMsg_DispatchEvent::ID || |
| 86 | message.type() == ExtensionMsg_DispatchOnConnect::ID || |
| 87 | message.type() == ExtensionMsg_DeliverMessage::ID || |
| 88 | message.type() == ExtensionMsg_DispatchOnDisconnect::ID || |
| 89 | message.type() == ExtensionMsg_ValidateMessagePort::ID; |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // static |
| 93 | void WorkerThreadDispatcher::ForwardIPC(int worker_thread_id, |
| 94 | const IPC::Message& message) { |
| 95 | WorkerThreadDispatcher::Get()->OnMessageReceivedOnWorkerThread( |
| 96 | worker_thread_id, message); |
| 97 | } |
| 98 | |
Istiaque Ahmed | 3dd60423 | 2019-08-02 19:22:21 | [diff] [blame] | 99 | // static |
| 100 | void WorkerThreadDispatcher::UpdateBindingsOnWorkerThread( |
| 101 | const ExtensionId& extension_id) { |
| 102 | DCHECK(worker_thread_util::IsWorkerThread()); |
| 103 | DCHECK(!extension_id.empty()); |
| 104 | GetBindingsSystem()->UpdateBindings(extension_id, |
| 105 | true /* permissions_changed */, |
| 106 | Dispatcher::GetWorkerScriptContextSet()); |
| 107 | } |
| 108 | |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 109 | bool WorkerThreadDispatcher::OnControlMessageReceived( |
| 110 | const IPC::Message& message) { |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 111 | if (HandlesMessageOnWorkerThread(message)) { |
Kinuko Yasuda | b56fc92a | 2019-07-01 11:39:13 | [diff] [blame] | 112 | int worker_thread_id = content::WorkerThread::kInvalidWorkerThreadId; |
lazyboy | e784724 | 2017-06-07 23:29:18 | [diff] [blame] | 113 | // TODO(lazyboy): Route |message| directly to the child thread using routed |
| 114 | // IPC. Probably using mojo? |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 115 | bool found = base::PickleIterator(message).ReadInt(&worker_thread_id); |
lazyboy | e784724 | 2017-06-07 23:29:18 | [diff] [blame] | 116 | CHECK(found); |
Istiaque Ahmed | 461c4ed | 2017-09-14 17:23:45 | [diff] [blame] | 117 | if (worker_thread_id == kMainThreadId) |
lazyboy | e784724 | 2017-06-07 23:29:18 | [diff] [blame] | 118 | return false; |
Istiaque Ahmed | b488bf4 | 2019-09-26 04:30:58 | [diff] [blame] | 119 | return PostTaskToWorkerThread( |
| 120 | worker_thread_id, base::BindOnce(&WorkerThreadDispatcher::ForwardIPC, |
| 121 | worker_thread_id, message)); |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
Istiaque Ahmed | 3dd60423 | 2019-08-02 19:22:21 | [diff] [blame] | 126 | bool WorkerThreadDispatcher::UpdateBindingsForWorkers( |
| 127 | const ExtensionId& extension_id) { |
| 128 | bool success = true; |
| 129 | base::AutoLock lock(task_runner_map_lock_); |
| 130 | for (const auto& task_runner_info : task_runner_map_) { |
| 131 | const int worker_thread_id = task_runner_info.first; |
| 132 | base::TaskRunner* runner = task_runner_map_[worker_thread_id]; |
| 133 | bool posted = runner->PostTask( |
| 134 | FROM_HERE, |
| 135 | base::BindOnce(&WorkerThreadDispatcher::UpdateBindingsOnWorkerThread, |
| 136 | extension_id)); |
| 137 | success &= posted; |
| 138 | } |
| 139 | return success; |
| 140 | } |
| 141 | |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 142 | void WorkerThreadDispatcher::OnMessageReceivedOnWorkerThread( |
| 143 | int worker_thread_id, |
| 144 | const IPC::Message& message) { |
| 145 | CHECK_EQ(content::WorkerThread::GetCurrentId(), worker_thread_id); |
Istiaque Ahmed | b488bf4 | 2019-09-26 04:30:58 | [diff] [blame] | 146 | |
| 147 | // If the worker state was already destroyed via |
| 148 | // Dispatcher::WillDestroyServiceWorkerContextOnWorkerThread, then |
| 149 | // drop this IPC. See https://crbug.com/1008143 for details. |
| 150 | if (!GetServiceWorkerData()) |
| 151 | return; |
| 152 | |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 153 | bool handled = true; |
| 154 | IPC_BEGIN_MESSAGE_MAP(WorkerThreadDispatcher, message) |
| 155 | IPC_MESSAGE_HANDLER(ExtensionMsg_ResponseWorker, OnResponseWorker) |
lazyboy | e784724 | 2017-06-07 23:29:18 | [diff] [blame] | 156 | IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchEvent, OnDispatchEvent) |
Istiaque Ahmed | ee72f495 | 2019-03-06 21:18:36 | [diff] [blame] | 157 | IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnConnect, OnDispatchOnConnect) |
| 158 | IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnDeliverMessage) |
| 159 | IPC_MESSAGE_HANDLER(ExtensionMsg_DispatchOnDisconnect, |
| 160 | OnDispatchOnDisconnect) |
| 161 | IPC_MESSAGE_HANDLER(ExtensionMsg_ValidateMessagePort, OnValidateMessagePort) |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 162 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 163 | IPC_END_MESSAGE_MAP() |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 164 | CHECK(handled); |
| 165 | } |
| 166 | |
Istiaque Ahmed | b488bf4 | 2019-09-26 04:30:58 | [diff] [blame] | 167 | bool WorkerThreadDispatcher::PostTaskToWorkerThread(int worker_thread_id, |
| 168 | base::OnceClosure task) { |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 169 | base::AutoLock lock(task_runner_map_lock_); |
David Bertoni | 1dc1b7a | 2019-06-13 02:44:59 | [diff] [blame] | 170 | auto it = task_runner_map_.find(worker_thread_id); |
Istiaque Ahmed | b488bf4 | 2019-09-26 04:30:58 | [diff] [blame] | 171 | if (it == task_runner_map_.end()) |
| 172 | return false; |
| 173 | |
| 174 | bool task_posted = it->second->PostTask(FROM_HERE, std::move(task)); |
| 175 | DCHECK(task_posted) << "Could not PostTask IPC to worker thread."; |
| 176 | return task_posted; |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | bool WorkerThreadDispatcher::Send(IPC::Message* message) { |
| 180 | return message_filter_->Send(message); |
| 181 | } |
| 182 | |
| 183 | void WorkerThreadDispatcher::OnResponseWorker(int worker_thread_id, |
| 184 | int request_id, |
| 185 | bool succeeded, |
| 186 | const base::ListValue& response, |
| 187 | const std::string& error) { |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 188 | ServiceWorkerData* data = g_data_tls.Pointer()->Get(); |
Devlin Cronin | 4607e32 | 2017-07-17 16:17:18 | [diff] [blame] | 189 | data->bindings_system()->HandleResponse(request_id, succeeded, response, |
| 190 | error); |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 191 | } |
| 192 | |
lazyboy | e784724 | 2017-06-07 23:29:18 | [diff] [blame] | 193 | void WorkerThreadDispatcher::OnDispatchEvent( |
| 194 | const ExtensionMsg_DispatchEvent_Params& params, |
| 195 | const base::ListValue& event_args) { |
| 196 | ServiceWorkerData* data = g_data_tls.Pointer()->Get(); |
| 197 | DCHECK(data); |
Istiaque Ahmed | 96f50609 | 2019-07-08 23:40:57 | [diff] [blame] | 198 | |
| 199 | ScriptContext* script_context = data->context(); |
| 200 | // Note |scoped_extension_interaction| requires a HandleScope. |
| 201 | v8::Isolate* isolate = script_context->isolate(); |
| 202 | v8::HandleScope handle_scope(isolate); |
| 203 | std::unique_ptr<InteractionProvider::Scope> scoped_extension_interaction; |
| 204 | if (params.is_user_gesture) { |
| 205 | scoped_extension_interaction = |
| 206 | ExtensionInteractionProvider::Scope::ForWorker( |
| 207 | script_context->v8_context()); |
| 208 | } |
lazyboy | e784724 | 2017-06-07 23:29:18 | [diff] [blame] | 209 | data->bindings_system()->DispatchEventInContext( |
| 210 | params.event_name, &event_args, ¶ms.filtering_info, data->context()); |
Istiaque Ahmed | 741f69e | 2019-10-15 19:43:36 | [diff] [blame] | 211 | const int worker_thread_id = content::WorkerThread::GetCurrentId(); |
| 212 | Send(new ExtensionHostMsg_EventAckWorker(data->context()->GetExtensionID(), |
| 213 | data->service_worker_version_id(), |
| 214 | worker_thread_id, params.event_id)); |
lazyboy | e784724 | 2017-06-07 23:29:18 | [diff] [blame] | 215 | } |
| 216 | |
Istiaque Ahmed | ee72f495 | 2019-03-06 21:18:36 | [diff] [blame] | 217 | void WorkerThreadDispatcher::OnDispatchOnConnect( |
| 218 | int worker_thread_id, |
| 219 | const PortId& target_port_id, |
| 220 | const std::string& channel_name, |
| 221 | const ExtensionMsg_TabConnectionInfo& source, |
| 222 | const ExtensionMsg_ExternalConnectionInfo& info) { |
| 223 | DCHECK_EQ(worker_thread_id, content::WorkerThread::GetCurrentId()); |
| 224 | WorkerThreadDispatcher::GetBindingsSystem() |
Devlin Cronin | 613ebffa | 2019-04-17 21:04:17 | [diff] [blame] | 225 | ->messaging_service() |
Istiaque Ahmed | ee72f495 | 2019-03-06 21:18:36 | [diff] [blame] | 226 | ->DispatchOnConnect(Dispatcher::GetWorkerScriptContextSet(), |
| 227 | target_port_id, channel_name, source, info, |
| 228 | // Render frames do not matter. |
| 229 | nullptr); |
| 230 | } |
| 231 | |
| 232 | void WorkerThreadDispatcher::OnValidateMessagePort(int worker_thread_id, |
| 233 | const PortId& id) { |
| 234 | DCHECK_EQ(content::WorkerThread::GetCurrentId(), worker_thread_id); |
| 235 | WorkerThreadDispatcher::GetBindingsSystem() |
Devlin Cronin | 613ebffa | 2019-04-17 21:04:17 | [diff] [blame] | 236 | ->messaging_service() |
Istiaque Ahmed | ee72f495 | 2019-03-06 21:18:36 | [diff] [blame] | 237 | ->ValidateMessagePort(Dispatcher::GetWorkerScriptContextSet(), id, |
| 238 | // Render frames do not matter. |
| 239 | nullptr); |
| 240 | } |
| 241 | |
| 242 | void WorkerThreadDispatcher::OnDeliverMessage(int worker_thread_id, |
| 243 | const PortId& target_port_id, |
| 244 | const Message& message) { |
| 245 | WorkerThreadDispatcher::GetBindingsSystem() |
Devlin Cronin | 613ebffa | 2019-04-17 21:04:17 | [diff] [blame] | 246 | ->messaging_service() |
Istiaque Ahmed | ee72f495 | 2019-03-06 21:18:36 | [diff] [blame] | 247 | ->DeliverMessage(Dispatcher::GetWorkerScriptContextSet(), target_port_id, |
| 248 | message, |
| 249 | // Render frames do not matter. |
| 250 | nullptr); |
| 251 | } |
| 252 | |
| 253 | void WorkerThreadDispatcher::OnDispatchOnDisconnect( |
| 254 | int worker_thread_id, |
| 255 | const PortId& port_id, |
| 256 | const std::string& error_message) { |
| 257 | WorkerThreadDispatcher::GetBindingsSystem() |
Devlin Cronin | 613ebffa | 2019-04-17 21:04:17 | [diff] [blame] | 258 | ->messaging_service() |
Istiaque Ahmed | ee72f495 | 2019-03-06 21:18:36 | [diff] [blame] | 259 | ->DispatchOnDisconnect(Dispatcher::GetWorkerScriptContextSet(), port_id, |
| 260 | error_message, |
| 261 | // Render frames do not matter. |
| 262 | nullptr); |
| 263 | } |
| 264 | |
rdevlin.cronin | 9f33889 | 2016-11-21 19:37:00 | [diff] [blame] | 265 | void WorkerThreadDispatcher::AddWorkerData( |
| 266 | int64_t service_worker_version_id, |
Istiaque Ahmed | 9987ca89 | 2020-01-09 22:47:17 | [diff] [blame] | 267 | ActivationSequence activation_sequence, |
Istiaque Ahmed | 242a410 | 2019-06-25 01:47:57 | [diff] [blame] | 268 | ScriptContext* script_context, |
Devlin Cronin | 095dc13 | 2019-03-29 19:43:30 | [diff] [blame] | 269 | std::unique_ptr<NativeExtensionBindingsSystem> bindings_system) { |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 270 | ServiceWorkerData* data = g_data_tls.Pointer()->Get(); |
| 271 | if (!data) { |
Istiaque Ahmed | c92b1ea | 2019-12-31 00:32:49 | [diff] [blame] | 272 | ServiceWorkerData* new_data = |
| 273 | new ServiceWorkerData(service_worker_version_id, activation_sequence, |
| 274 | script_context, std::move(bindings_system)); |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 275 | g_data_tls.Pointer()->Set(new_data); |
| 276 | } |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 277 | |
Kinuko Yasuda | b56fc92a | 2019-07-01 11:39:13 | [diff] [blame] | 278 | int worker_thread_id = content::WorkerThread::GetCurrentId(); |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 279 | { |
| 280 | base::AutoLock lock(task_runner_map_lock_); |
| 281 | auto* task_runner = base::ThreadTaskRunnerHandle::Get().get(); |
| 282 | CHECK(task_runner); |
| 283 | task_runner_map_[worker_thread_id] = task_runner; |
| 284 | } |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 285 | } |
| 286 | |
Istiaque Ahmed | d4b67ee | 2019-03-02 10:53:20 | [diff] [blame] | 287 | void WorkerThreadDispatcher::DidInitializeContext( |
| 288 | int64_t service_worker_version_id) { |
| 289 | ServiceWorkerData* data = g_data_tls.Pointer()->Get(); |
| 290 | DCHECK_EQ(service_worker_version_id, data->service_worker_version_id()); |
| 291 | const int thread_id = content::WorkerThread::GetCurrentId(); |
| 292 | DCHECK_NE(thread_id, kMainThreadId); |
| 293 | Send(new ExtensionHostMsg_DidInitializeServiceWorkerContext( |
| 294 | data->context()->GetExtensionID(), service_worker_version_id, thread_id)); |
| 295 | } |
| 296 | |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 297 | void WorkerThreadDispatcher::DidStartContext( |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 298 | const GURL& service_worker_scope, |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 299 | int64_t service_worker_version_id) { |
| 300 | ServiceWorkerData* data = g_data_tls.Pointer()->Get(); |
| 301 | DCHECK_EQ(service_worker_version_id, data->service_worker_version_id()); |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 302 | const int thread_id = content::WorkerThread::GetCurrentId(); |
| 303 | DCHECK_NE(thread_id, kMainThreadId); |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 304 | Send(new ExtensionHostMsg_DidStartServiceWorkerContext( |
Istiaque Ahmed | c92b1ea | 2019-12-31 00:32:49 | [diff] [blame] | 305 | data->context()->GetExtensionID(), data->activation_sequence(), |
| 306 | service_worker_scope, service_worker_version_id, thread_id)); |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 307 | } |
| 308 | |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 309 | void WorkerThreadDispatcher::DidStopContext(const GURL& service_worker_scope, |
| 310 | int64_t service_worker_version_id) { |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 311 | ServiceWorkerData* data = g_data_tls.Pointer()->Get(); |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 312 | const int thread_id = content::WorkerThread::GetCurrentId(); |
| 313 | DCHECK_NE(thread_id, kMainThreadId); |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 314 | DCHECK_EQ(service_worker_version_id, data->service_worker_version_id()); |
| 315 | Send(new ExtensionHostMsg_DidStopServiceWorkerContext( |
Istiaque Ahmed | c92b1ea | 2019-12-31 00:32:49 | [diff] [blame] | 316 | data->context()->GetExtensionID(), data->activation_sequence(), |
| 317 | service_worker_scope, service_worker_version_id, thread_id)); |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 318 | } |
| 319 | |
lazyboy | 4c82177a | 2016-10-18 00:04:09 | [diff] [blame] | 320 | void WorkerThreadDispatcher::RemoveWorkerData( |
| 321 | int64_t service_worker_version_id) { |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 322 | ServiceWorkerData* data = g_data_tls.Pointer()->Get(); |
| 323 | if (data) { |
lazyboy | 4c82177a | 2016-10-18 00:04:09 | [diff] [blame] | 324 | DCHECK_EQ(service_worker_version_id, data->service_worker_version_id()); |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 325 | delete data; |
| 326 | g_data_tls.Pointer()->Set(nullptr); |
| 327 | } |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 328 | |
Kinuko Yasuda | b56fc92a | 2019-07-01 11:39:13 | [diff] [blame] | 329 | int worker_thread_id = content::WorkerThread::GetCurrentId(); |
lazyboy | 8874f2d | 2017-05-08 15:07:08 | [diff] [blame] | 330 | { |
| 331 | base::AutoLock lock(task_runner_map_lock_); |
| 332 | task_runner_map_.erase(worker_thread_id); |
| 333 | } |
lazyboy | ee4adef | 2016-05-24 00:55:16 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | } // namespace extensions |