Index: content/browser/service_worker/embedded_worker_instance.cc |
diff --git a/content/browser/service_worker/embedded_worker_instance.cc b/content/browser/service_worker/embedded_worker_instance.cc |
index 07b14f65e0ba636920690ff3678875929e977cc0..af5e4ceee4ce6436771cba803a51109ce5e13428 100644 |
--- a/content/browser/service_worker/embedded_worker_instance.cc |
+++ b/content/browser/service_worker/embedded_worker_instance.cc |
@@ -9,6 +9,7 @@ |
#include "base/bind_helpers.h" |
#include "base/debug/trace_event.h" |
+#include "base/threading/non_thread_safe.h" |
#include "content/browser/devtools/service_worker_devtools_manager.h" |
#include "content/browser/service_worker/embedded_worker_registry.h" |
#include "content/browser/service_worker/service_worker_context_core.h" |
@@ -31,70 +32,34 @@ struct SecondGreater { |
} |
}; |
-void NotifyWorkerReadyForInspection(int worker_process_id, |
- int worker_route_id) { |
- if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
- BrowserThread::PostTask(BrowserThread::UI, |
- FROM_HERE, |
- base::Bind(NotifyWorkerReadyForInspection, |
- worker_process_id, |
- worker_route_id)); |
- return; |
- } |
+void NotifyWorkerReadyForInspectionOnUI(int worker_process_id, |
+ int worker_route_id) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
ServiceWorkerDevToolsManager::GetInstance()->WorkerReadyForInspection( |
worker_process_id, worker_route_id); |
} |
-void NotifyWorkerDestroyed(int worker_process_id, int worker_route_id) { |
- if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
- BrowserThread::PostTask( |
- BrowserThread::UI, |
- FROM_HERE, |
- base::Bind(NotifyWorkerDestroyed, worker_process_id, worker_route_id)); |
- return; |
- } |
+void NotifyWorkerDestroyedOnUI(int worker_process_id, int worker_route_id) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
ServiceWorkerDevToolsManager::GetInstance()->WorkerDestroyed( |
worker_process_id, worker_route_id); |
} |
-void NotifyWorkerStopIgnored(int worker_process_id, int worker_route_id) { |
- if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
- BrowserThread::PostTask(BrowserThread::UI, |
- FROM_HERE, |
- base::Bind(NotifyWorkerStopIgnored, |
- worker_process_id, |
- worker_route_id)); |
- return; |
- } |
+void NotifyWorkerStopIgnoredOnUI(int worker_process_id, int worker_route_id) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
ServiceWorkerDevToolsManager::GetInstance()->WorkerStopIgnored( |
worker_process_id, worker_route_id); |
} |
- |
-void RegisterToWorkerDevToolsManager( |
+void RegisterToWorkerDevToolsManagerOnUI( |
int process_id, |
const ServiceWorkerContextCore* service_worker_context, |
- base::WeakPtr<ServiceWorkerContextCore> service_worker_context_weak, |
+ const base::WeakPtr<ServiceWorkerContextCore>& service_worker_context_weak, |
int64 service_worker_version_id, |
const GURL& url, |
const base::Callback<void(int worker_devtools_agent_route_id, |
bool wait_for_debugger)>& callback) { |
- if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
- BrowserThread::PostTask(BrowserThread::UI, |
- FROM_HERE, |
- base::Bind(RegisterToWorkerDevToolsManager, |
- process_id, |
- service_worker_context, |
- service_worker_context_weak, |
- service_worker_version_id, |
- url, |
- callback)); |
- return; |
- } |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
int worker_devtools_agent_route_id = MSG_ROUTING_NONE; |
bool wait_for_debugger = false; |
if (RenderProcessHost* rph = RenderProcessHost::FromID(process_id)) { |
@@ -118,11 +83,49 @@ void RegisterToWorkerDevToolsManager( |
} // namespace |
+// Lives on IO thread, proxies notifications to DevToolsManager that lives on |
+// UI thread. Owned by EmbeddedWorkerInstance. |
+class EmbeddedWorkerInstance::DevToolsProxy : public base::NonThreadSafe { |
+ public: |
+ DevToolsProxy(int process_id, int agent_route_id) |
+ : process_id_(process_id), |
+ agent_route_id_(agent_route_id) {} |
+ |
+ ~DevToolsProxy() { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(NotifyWorkerDestroyedOnUI, |
+ process_id_, agent_route_id_)); |
+ } |
+ |
+ void NotifyWorkerReadyForInspection() { |
+ DCHECK(CalledOnValidThread()); |
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
+ base::Bind(NotifyWorkerReadyForInspectionOnUI, |
+ process_id_, agent_route_id_)); |
+ } |
+ |
+ void NotifyWorkerStopIgnored() { |
+ DCHECK(CalledOnValidThread()); |
+ BrowserThread::PostTask(BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(NotifyWorkerStopIgnoredOnUI, |
+ process_id_, agent_route_id_)); |
+ } |
+ |
+ int agent_route_id() const { return agent_route_id_; } |
+ |
+ private: |
+ const int process_id_; |
+ const int agent_route_id_; |
+ DISALLOW_COPY_AND_ASSIGN(DevToolsProxy); |
+}; |
+ |
EmbeddedWorkerInstance::~EmbeddedWorkerInstance() { |
if (status_ == STARTING || status_ == RUNNING) |
Stop(); |
- if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
- NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); |
+ devtools_proxy_.reset(); |
if (context_ && process_id_ != -1) |
context_->process_manager()->ReleaseWorkerProcess(embedded_worker_id_); |
registry_->RemoveWorker(process_id_, embedded_worker_id_); |
@@ -175,7 +178,8 @@ ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { |
void EmbeddedWorkerInstance::StopIfIdle() { |
if (devtools_attached_) { |
- NotifyWorkerStopIgnored(process_id_, worker_devtools_agent_route_id_); |
+ if (devtools_proxy_) |
+ devtools_proxy_->NotifyWorkerStopIgnored(); |
return; |
} |
Stop(); |
@@ -207,7 +211,6 @@ EmbeddedWorkerInstance::EmbeddedWorkerInstance( |
status_(STOPPED), |
process_id_(-1), |
thread_id_(kInvalidEmbeddedWorkerThreadId), |
- worker_devtools_agent_route_id_(MSG_ROUTING_NONE), |
devtools_attached_(false), |
weak_factory_(this) { |
} |
@@ -254,16 +257,22 @@ void EmbeddedWorkerInstance::ProcessAllocated( |
const int64 service_worker_version_id = params->service_worker_version_id; |
process_id_ = process_id; |
GURL script_url(params->script_url); |
- RegisterToWorkerDevToolsManager( |
- process_id, |
- context_.get(), |
- context_, |
- service_worker_version_id, |
- script_url, |
- base::Bind(&EmbeddedWorkerInstance::SendStartWorker, |
- weak_factory_.GetWeakPtr(), |
- base::Passed(¶ms), |
- callback)); |
+ |
+ // Register this worker to DevToolsManager on UI thread, then continue to |
+ // call SendStartWorker on IO thread. |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(RegisterToWorkerDevToolsManagerOnUI, |
+ process_id_, |
+ context_.get(), |
+ context_, |
+ service_worker_version_id, |
+ script_url, |
+ base::Bind(&EmbeddedWorkerInstance::SendStartWorker, |
+ weak_factory_.GetWeakPtr(), |
+ base::Passed(¶ms), |
+ callback))); |
} |
void EmbeddedWorkerInstance::SendStartWorker( |
@@ -271,7 +280,11 @@ void EmbeddedWorkerInstance::SendStartWorker( |
const StatusCallback& callback, |
int worker_devtools_agent_route_id, |
bool wait_for_debugger) { |
- worker_devtools_agent_route_id_ = worker_devtools_agent_route_id; |
+ if (worker_devtools_agent_route_id != MSG_ROUTING_NONE) { |
+ DCHECK(!devtools_proxy_); |
+ devtools_proxy_.reset(new DevToolsProxy(process_id_, |
+ worker_devtools_agent_route_id)); |
+ } |
params->worker_devtools_agent_route_id = worker_devtools_agent_route_id; |
params->wait_for_debugger = wait_for_debugger; |
ServiceWorkerStatusCode status = |
@@ -285,9 +298,8 @@ void EmbeddedWorkerInstance::SendStartWorker( |
} |
void EmbeddedWorkerInstance::OnReadyForInspection() { |
- if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
- NotifyWorkerReadyForInspection(process_id_, |
- worker_devtools_agent_route_id_); |
+ if (devtools_proxy_) |
+ devtools_proxy_->NotifyWorkerReadyForInspection(); |
} |
void EmbeddedWorkerInstance::OnScriptLoaded(int thread_id) { |
@@ -314,15 +326,13 @@ void EmbeddedWorkerInstance::OnStarted() { |
} |
void EmbeddedWorkerInstance::OnStopped() { |
- if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
- NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); |
+ devtools_proxy_.reset(); |
if (context_) |
context_->process_manager()->ReleaseWorkerProcess(embedded_worker_id_); |
Status old_status = status_; |
status_ = STOPPED; |
process_id_ = -1; |
thread_id_ = -1; |
- worker_devtools_agent_route_id_ = MSG_ROUTING_NONE; |
start_callback_.Reset(); |
FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped(old_status)); |
} |
@@ -368,6 +378,12 @@ void EmbeddedWorkerInstance::OnReportConsoleMessage( |
source_identifier, message_level, message, line_number, source_url)); |
} |
+int EmbeddedWorkerInstance::worker_devtools_agent_route_id() const { |
+ if (devtools_proxy_) |
+ return devtools_proxy_->agent_route_id(); |
+ return MSG_ROUTING_NONE; |
+} |
+ |
MessagePortMessageFilter* EmbeddedWorkerInstance::message_port_message_filter() |
const { |
return registry_->MessagePortMessageFilterForProcess(process_id_); |