Commit 97f2c905 authored by nhiroki's avatar nhiroki Committed by Commit bot

Worker: Introduce WorkerReportingProxy::willEvaluateWorkerScript()

This introduces WorkerReportingProxy::willEvaluateWorkerScript() and removes
WorkerReportingProxy::didLoadWorkerScript().

Before this CL, didLoadWorkerScript() was called during WorkerThread
initialization. This timing was late, but it was intended because one of
observers (ServiceWorker) wanted to access WorkerGlobalScope in the notification
that is created during WorkerThread initialization in order to record size of
worker scripts.

After this CL, recording the size is done in willEvaluateWorkerScript() that
would be more appropriate timing for the operation. willEvaluateWorkerScript()
is also useful for unit tests. It enables them to wait until the timing that
they really wait for.

BUG=640843

Review-Url: https://codereview.chromium.org/2355723005
Cr-Commit-Position: refs/heads/master@{#420546}
parent 95d3bea4
...@@ -213,11 +213,11 @@ void WorkerGlobalScope::importScripts(const Vector<String>& urls, ExceptionState ...@@ -213,11 +213,11 @@ void WorkerGlobalScope::importScripts(const Vector<String>& urls, ExceptionState
} }
InspectorInstrumentation::scriptImported(&executionContext, scriptLoader->identifier(), scriptLoader->script()); InspectorInstrumentation::scriptImported(&executionContext, scriptLoader->identifier(), scriptLoader->script());
thread()->workerReportingProxy().didLoadWorkerScript(scriptLoader->script().length(), scriptLoader->cachedMetadata() ? scriptLoader->cachedMetadata()->size() : 0);
ErrorEvent* errorEvent = nullptr; ErrorEvent* errorEvent = nullptr;
std::unique_ptr<Vector<char>> cachedMetaData(scriptLoader->releaseCachedMetadata()); std::unique_ptr<Vector<char>> cachedMetaData(scriptLoader->releaseCachedMetadata());
CachedMetadataHandler* handler(createWorkerScriptCachedMetadataHandler(completeURL, cachedMetaData.get())); CachedMetadataHandler* handler(createWorkerScriptCachedMetadataHandler(completeURL, cachedMetaData.get()));
thread()->workerReportingProxy().willEvaluateImportedScript(scriptLoader->script().length(), scriptLoader->cachedMetadata() ? scriptLoader->cachedMetadata()->size() : 0);
m_scriptController->evaluate(ScriptSourceCode(scriptLoader->script(), scriptLoader->responseURL()), &errorEvent, handler, m_v8CacheOptions); m_scriptController->evaluate(ScriptSourceCode(scriptLoader->script(), scriptLoader->responseURL()), &errorEvent, handler, m_v8CacheOptions);
if (errorEvent) { if (errorEvent) {
m_scriptController->rethrowExceptionFromImportedScript(errorEvent, exceptionState); m_scriptController->rethrowExceptionFromImportedScript(errorEvent, exceptionState);
......
...@@ -52,9 +52,6 @@ public: ...@@ -52,9 +52,6 @@ public:
virtual void reportConsoleMessage(MessageSource, MessageLevel, const String& message, SourceLocation*) = 0; virtual void reportConsoleMessage(MessageSource, MessageLevel, const String& message, SourceLocation*) = 0;
virtual void postMessageToPageInspector(const String&) = 0; virtual void postMessageToPageInspector(const String&) = 0;
// Invoked when the worker script or imported script is loaded.
virtual void didLoadWorkerScript(size_t scriptSize, size_t cachedMetadataSize) { }
// Invoked when the new WorkerGlobalScope is created. This is called after // Invoked when the new WorkerGlobalScope is created. This is called after
// didLoadWorkerScript(). // didLoadWorkerScript().
virtual void didCreateWorkerGlobalScope(WorkerOrWorkletGlobalScope*) { } virtual void didCreateWorkerGlobalScope(WorkerOrWorkletGlobalScope*) { }
...@@ -63,9 +60,16 @@ public: ...@@ -63,9 +60,16 @@ public:
// didCreateWorkerGlobalScope(). // didCreateWorkerGlobalScope().
virtual void didInitializeWorkerContext() { } virtual void didInitializeWorkerContext() { }
// Invoked when the worker script is about to be evaluated. This is called
// after didInitializeWorkerContext().
virtual void willEvaluateWorkerScript(size_t scriptSize, size_t cachedMetadataSize) { }
// Invoked when an imported script is about to be evaluated. This is called
// after willEvaluateWorkerScript().
virtual void willEvaluateImportedScript(size_t scriptSize, size_t cachedMetadataSize) { }
// Invoked when the worker script is evaluated. |success| is true if the // Invoked when the worker script is evaluated. |success| is true if the
// evaluation completed with no uncaught exception. This is called after // evaluation completed with no uncaught exception.
// didInitializeWorkerContext().
virtual void didEvaluateWorkerScript(bool success) = 0; virtual void didEvaluateWorkerScript(bool success) = 0;
// Invoked when close() is invoked on the worker context. // Invoked when close() is invoked on the worker context.
......
...@@ -520,9 +520,6 @@ void WorkerThread::initializeOnWorkerThread(std::unique_ptr<WorkerThreadStartupD ...@@ -520,9 +520,6 @@ void WorkerThread::initializeOnWorkerThread(std::unique_ptr<WorkerThreadStartupD
m_workerReportingProxy.didCreateWorkerGlobalScope(globalScope()); m_workerReportingProxy.didCreateWorkerGlobalScope(globalScope());
m_workerInspectorController = WorkerInspectorController::create(this); m_workerInspectorController = WorkerInspectorController::create(this);
if (globalScope()->isWorkerGlobalScope())
m_workerReportingProxy.didLoadWorkerScript(sourceCode.length(), cachedMetaData.get() ? cachedMetaData->size() : 0);
globalScope()->scriptController()->initializeContextIfNeeded(); globalScope()->scriptController()->initializeContextIfNeeded();
// If Origin Trials have been registered before the V8 context was ready, // If Origin Trials have been registered before the V8 context was ready,
...@@ -553,6 +550,7 @@ void WorkerThread::initializeOnWorkerThread(std::unique_ptr<WorkerThreadStartupD ...@@ -553,6 +550,7 @@ void WorkerThread::initializeOnWorkerThread(std::unique_ptr<WorkerThreadStartupD
if (globalScope()->isWorkerGlobalScope()) { if (globalScope()->isWorkerGlobalScope()) {
WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(globalScope()); WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(globalScope());
CachedMetadataHandler* handler = workerGlobalScope->createWorkerScriptCachedMetadataHandler(scriptURL, cachedMetaData.get()); CachedMetadataHandler* handler = workerGlobalScope->createWorkerScriptCachedMetadataHandler(scriptURL, cachedMetaData.get());
m_workerReportingProxy.willEvaluateWorkerScript(sourceCode.length(), cachedMetaData.get() ? cachedMetaData->size() : 0);
bool success = workerGlobalScope->scriptController()->evaluate(ScriptSourceCode(sourceCode, scriptURL), nullptr, handler, v8CacheOptions); bool success = workerGlobalScope->scriptController()->evaluate(ScriptSourceCode(sourceCode, scriptURL), nullptr, handler, v8CacheOptions);
m_workerReportingProxy.didEvaluateWorkerScript(success); m_workerReportingProxy.didEvaluateWorkerScript(success);
} }
......
...@@ -74,9 +74,9 @@ public: ...@@ -74,9 +74,9 @@ public:
protected: protected:
void expectReportingCalls() void expectReportingCalls()
{ {
EXPECT_CALL(*m_reportingProxy, didLoadWorkerScriptMock(_, _)).Times(1);
EXPECT_CALL(*m_reportingProxy, didCreateWorkerGlobalScope(_)).Times(1); EXPECT_CALL(*m_reportingProxy, didCreateWorkerGlobalScope(_)).Times(1);
EXPECT_CALL(*m_reportingProxy, didInitializeWorkerContext()).Times(1); EXPECT_CALL(*m_reportingProxy, didInitializeWorkerContext()).Times(1);
EXPECT_CALL(*m_reportingProxy, willEvaluateWorkerScriptMock(_, _)).Times(1);
EXPECT_CALL(*m_reportingProxy, didEvaluateWorkerScript(true)).Times(1); EXPECT_CALL(*m_reportingProxy, didEvaluateWorkerScript(true)).Times(1);
EXPECT_CALL(*m_reportingProxy, willDestroyWorkerGlobalScope()).Times(1); EXPECT_CALL(*m_reportingProxy, willDestroyWorkerGlobalScope()).Times(1);
EXPECT_CALL(*m_reportingProxy, didTerminateWorkerThread()).Times(1); EXPECT_CALL(*m_reportingProxy, didTerminateWorkerThread()).Times(1);
...@@ -85,9 +85,9 @@ protected: ...@@ -85,9 +85,9 @@ protected:
void expectReportingCallsForWorkerPossiblyTerminatedBeforeInitialization() void expectReportingCallsForWorkerPossiblyTerminatedBeforeInitialization()
{ {
EXPECT_CALL(*m_reportingProxy, didLoadWorkerScriptMock(_, _)).Times(AtMost(1));
EXPECT_CALL(*m_reportingProxy, didCreateWorkerGlobalScope(_)).Times(AtMost(1)); EXPECT_CALL(*m_reportingProxy, didCreateWorkerGlobalScope(_)).Times(AtMost(1));
EXPECT_CALL(*m_reportingProxy, didInitializeWorkerContext()).Times(AtMost(1)); EXPECT_CALL(*m_reportingProxy, didInitializeWorkerContext()).Times(AtMost(1));
EXPECT_CALL(*m_reportingProxy, willEvaluateWorkerScriptMock(_, _)).Times(AtMost(1));
EXPECT_CALL(*m_reportingProxy, didEvaluateWorkerScript(_)).Times(AtMost(1)); EXPECT_CALL(*m_reportingProxy, didEvaluateWorkerScript(_)).Times(AtMost(1));
EXPECT_CALL(*m_reportingProxy, willDestroyWorkerGlobalScope()).Times(AtMost(1)); EXPECT_CALL(*m_reportingProxy, willDestroyWorkerGlobalScope()).Times(AtMost(1));
EXPECT_CALL(*m_reportingProxy, didTerminateWorkerThread()).Times(1); EXPECT_CALL(*m_reportingProxy, didTerminateWorkerThread()).Times(1);
...@@ -96,9 +96,9 @@ protected: ...@@ -96,9 +96,9 @@ protected:
void expectReportingCallsForWorkerForciblyTerminated() void expectReportingCallsForWorkerForciblyTerminated()
{ {
EXPECT_CALL(*m_reportingProxy, didLoadWorkerScriptMock(_, _)).Times(1);
EXPECT_CALL(*m_reportingProxy, didCreateWorkerGlobalScope(_)).Times(1); EXPECT_CALL(*m_reportingProxy, didCreateWorkerGlobalScope(_)).Times(1);
EXPECT_CALL(*m_reportingProxy, didInitializeWorkerContext()).Times(1); EXPECT_CALL(*m_reportingProxy, didInitializeWorkerContext()).Times(1);
EXPECT_CALL(*m_reportingProxy, willEvaluateWorkerScriptMock(_, _)).Times(1);
EXPECT_CALL(*m_reportingProxy, didEvaluateWorkerScript(false)).Times(1); EXPECT_CALL(*m_reportingProxy, didEvaluateWorkerScript(false)).Times(1);
EXPECT_CALL(*m_reportingProxy, willDestroyWorkerGlobalScope()).Times(1); EXPECT_CALL(*m_reportingProxy, willDestroyWorkerGlobalScope()).Times(1);
EXPECT_CALL(*m_reportingProxy, didTerminateWorkerThread()).Times(1); EXPECT_CALL(*m_reportingProxy, didTerminateWorkerThread()).Times(1);
...@@ -208,7 +208,7 @@ TEST_F(WorkerThreadTest, AsyncTerminate_WhileTaskIsRunning) ...@@ -208,7 +208,7 @@ TEST_F(WorkerThreadTest, AsyncTerminate_WhileTaskIsRunning)
expectReportingCallsForWorkerForciblyTerminated(); expectReportingCallsForWorkerForciblyTerminated();
startWithSourceCodeNotToFinish(); startWithSourceCodeNotToFinish();
m_reportingProxy->waitUntilScriptLoaded(); m_reportingProxy->waitUntilScriptEvaluation();
// terminate() schedules a force termination task. // terminate() schedules a force termination task.
m_workerThread->terminate(); m_workerThread->terminate();
...@@ -230,7 +230,7 @@ TEST_F(WorkerThreadTest, SyncTerminate_WhileTaskIsRunning) ...@@ -230,7 +230,7 @@ TEST_F(WorkerThreadTest, SyncTerminate_WhileTaskIsRunning)
{ {
expectReportingCallsForWorkerForciblyTerminated(); expectReportingCallsForWorkerForciblyTerminated();
startWithSourceCodeNotToFinish(); startWithSourceCodeNotToFinish();
m_reportingProxy->waitUntilScriptLoaded(); m_reportingProxy->waitUntilScriptEvaluation();
// terminateAndWait() synchronously terminates the worker execution. // terminateAndWait() synchronously terminates the worker execution.
m_workerThread->terminateAndWait(); m_workerThread->terminateAndWait();
...@@ -244,7 +244,7 @@ TEST_F(WorkerThreadTest, AsyncTerminateAndThenSyncTerminate_WhileTaskIsRunning) ...@@ -244,7 +244,7 @@ TEST_F(WorkerThreadTest, AsyncTerminateAndThenSyncTerminate_WhileTaskIsRunning)
expectReportingCallsForWorkerForciblyTerminated(); expectReportingCallsForWorkerForciblyTerminated();
startWithSourceCodeNotToFinish(); startWithSourceCodeNotToFinish();
m_reportingProxy->waitUntilScriptLoaded(); m_reportingProxy->waitUntilScriptEvaluation();
// terminate() schedules a force termination task. // terminate() schedules a force termination task.
m_workerThread->terminate(); m_workerThread->terminate();
...@@ -259,7 +259,6 @@ TEST_F(WorkerThreadTest, AsyncTerminateAndThenSyncTerminate_WhileTaskIsRunning) ...@@ -259,7 +259,6 @@ TEST_F(WorkerThreadTest, AsyncTerminateAndThenSyncTerminate_WhileTaskIsRunning)
TEST_F(WorkerThreadTest, Terminate_WhileDebuggerTaskIsRunningOnInitialization) TEST_F(WorkerThreadTest, Terminate_WhileDebuggerTaskIsRunningOnInitialization)
{ {
EXPECT_CALL(*m_reportingProxy, didLoadWorkerScriptMock(_, _)).Times(1);
EXPECT_CALL(*m_reportingProxy, didCreateWorkerGlobalScope(_)).Times(1); EXPECT_CALL(*m_reportingProxy, didCreateWorkerGlobalScope(_)).Times(1);
EXPECT_CALL(*m_reportingProxy, willDestroyWorkerGlobalScope()).Times(1); EXPECT_CALL(*m_reportingProxy, willDestroyWorkerGlobalScope()).Times(1);
EXPECT_CALL(*m_reportingProxy, didTerminateWorkerThread()).Times(1); EXPECT_CALL(*m_reportingProxy, didTerminateWorkerThread()).Times(1);
......
...@@ -58,9 +58,9 @@ public: ...@@ -58,9 +58,9 @@ public:
MOCK_METHOD4(reportConsoleMessage, void(MessageSource, MessageLevel, const String& message, SourceLocation*)); MOCK_METHOD4(reportConsoleMessage, void(MessageSource, MessageLevel, const String& message, SourceLocation*));
MOCK_METHOD1(postMessageToPageInspector, void(const String&)); MOCK_METHOD1(postMessageToPageInspector, void(const String&));
MOCK_METHOD2(didLoadWorkerScriptMock, void(size_t scriptSize, size_t cachedMetadataSize));
MOCK_METHOD1(didCreateWorkerGlobalScope, void(WorkerOrWorkletGlobalScope*)); MOCK_METHOD1(didCreateWorkerGlobalScope, void(WorkerOrWorkletGlobalScope*));
MOCK_METHOD0(didInitializeWorkerContext, void()); MOCK_METHOD0(didInitializeWorkerContext, void());
MOCK_METHOD2(willEvaluateWorkerScriptMock, void(size_t scriptSize, size_t cachedMetadataSize));
MOCK_METHOD1(didEvaluateWorkerScript, void(bool success)); MOCK_METHOD1(didEvaluateWorkerScript, void(bool success));
MOCK_METHOD0(didCloseWorkerGlobalScope, void()); MOCK_METHOD0(didCloseWorkerGlobalScope, void());
MOCK_METHOD0(willDestroyWorkerGlobalScope, void()); MOCK_METHOD0(willDestroyWorkerGlobalScope, void());
...@@ -71,19 +71,19 @@ public: ...@@ -71,19 +71,19 @@ public:
reportExceptionMock(errorMessage, location.get(), exceptionId); reportExceptionMock(errorMessage, location.get(), exceptionId);
} }
void didLoadWorkerScript(size_t scriptSize, size_t cachedMetadataSize) override void willEvaluateWorkerScript(size_t scriptSize, size_t cachedMetadataSize) override
{ {
m_scriptLoadedEvent.signal(); m_scriptEvaluationEvent.signal();
didLoadWorkerScriptMock(scriptSize, cachedMetadataSize); willEvaluateWorkerScriptMock(scriptSize, cachedMetadataSize);
} }
void waitUntilScriptLoaded() void waitUntilScriptEvaluation()
{ {
m_scriptLoadedEvent.wait(); m_scriptEvaluationEvent.wait();
} }
private: private:
WaitableEvent m_scriptLoadedEvent; WaitableEvent m_scriptEvaluationEvent;
}; };
class MockWorkerThreadLifecycleObserver final : public GarbageCollectedFinalized<MockWorkerThreadLifecycleObserver>, public WorkerThreadLifecycleObserver { class MockWorkerThreadLifecycleObserver final : public GarbageCollectedFinalized<MockWorkerThreadLifecycleObserver>, public WorkerThreadLifecycleObserver {
......
...@@ -99,7 +99,7 @@ ServiceWorkerGlobalScope::~ServiceWorkerGlobalScope() ...@@ -99,7 +99,7 @@ ServiceWorkerGlobalScope::~ServiceWorkerGlobalScope()
{ {
} }
void ServiceWorkerGlobalScope::didLoadWorkerScript(size_t scriptSize, size_t cachedMetadataSize) void ServiceWorkerGlobalScope::countScript(size_t scriptSize, size_t cachedMetadataSize)
{ {
++m_scriptCount; ++m_scriptCount;
m_scriptTotalSize += scriptSize; m_scriptTotalSize += scriptSize;
......
...@@ -61,7 +61,11 @@ public: ...@@ -61,7 +61,11 @@ public:
~ServiceWorkerGlobalScope() override; ~ServiceWorkerGlobalScope() override;
bool isServiceWorkerGlobalScope() const override { return true; } bool isServiceWorkerGlobalScope() const override { return true; }
void didLoadWorkerScript(size_t scriptSize, size_t cachedMetadataSize); // Counts an evaluated script and its size. Called for each of the main
// worker script and imported scripts.
void countScript(size_t scriptSize, size_t cachedMetadataSize);
// Called when the main worker script is evaluated.
void didEvaluateWorkerScript(); void didEvaluateWorkerScript();
// ServiceWorkerGlobalScope.idl // ServiceWorkerGlobalScope.idl
......
...@@ -254,12 +254,6 @@ void ServiceWorkerGlobalScopeProxy::postMessageToPageInspector(const String& mes ...@@ -254,12 +254,6 @@ void ServiceWorkerGlobalScopeProxy::postMessageToPageInspector(const String& mes
document().postInspectorTask(BLINK_FROM_HERE, createCrossThreadTask(&WebEmbeddedWorkerImpl::postMessageToPageInspector, crossThreadUnretained(m_embeddedWorker), message)); document().postInspectorTask(BLINK_FROM_HERE, createCrossThreadTask(&WebEmbeddedWorkerImpl::postMessageToPageInspector, crossThreadUnretained(m_embeddedWorker), message));
} }
void ServiceWorkerGlobalScopeProxy::didLoadWorkerScript(size_t scriptSize, size_t cachedMetadataSize)
{
DCHECK(m_workerGlobalScope);
m_workerGlobalScope->didLoadWorkerScript(scriptSize, cachedMetadataSize);
}
void ServiceWorkerGlobalScopeProxy::didCreateWorkerGlobalScope(WorkerOrWorkletGlobalScope* workerGlobalScope) void ServiceWorkerGlobalScopeProxy::didCreateWorkerGlobalScope(WorkerOrWorkletGlobalScope* workerGlobalScope)
{ {
DCHECK(!m_workerGlobalScope); DCHECK(!m_workerGlobalScope);
...@@ -273,6 +267,18 @@ void ServiceWorkerGlobalScopeProxy::didInitializeWorkerContext() ...@@ -273,6 +267,18 @@ void ServiceWorkerGlobalScopeProxy::didInitializeWorkerContext()
client().didInitializeWorkerContext(workerGlobalScope()->scriptController()->context()); client().didInitializeWorkerContext(workerGlobalScope()->scriptController()->context());
} }
void ServiceWorkerGlobalScopeProxy::willEvaluateWorkerScript(size_t scriptSize, size_t cachedMetadataSize)
{
DCHECK(m_workerGlobalScope);
m_workerGlobalScope->countScript(scriptSize, cachedMetadataSize);
}
void ServiceWorkerGlobalScopeProxy::willEvaluateImportedScript(size_t scriptSize, size_t cachedMetadataSize)
{
DCHECK(m_workerGlobalScope);
m_workerGlobalScope->countScript(scriptSize, cachedMetadataSize);
}
void ServiceWorkerGlobalScopeProxy::didEvaluateWorkerScript(bool success) void ServiceWorkerGlobalScopeProxy::didEvaluateWorkerScript(bool success)
{ {
DCHECK(m_workerGlobalScope); DCHECK(m_workerGlobalScope);
......
...@@ -87,9 +87,10 @@ public: ...@@ -87,9 +87,10 @@ public:
void reportException(const String& errorMessage, std::unique_ptr<SourceLocation>, int exceptionId) override; void reportException(const String& errorMessage, std::unique_ptr<SourceLocation>, int exceptionId) override;
void reportConsoleMessage(MessageSource, MessageLevel, const String& message, SourceLocation*) override; void reportConsoleMessage(MessageSource, MessageLevel, const String& message, SourceLocation*) override;
void postMessageToPageInspector(const String&) override; void postMessageToPageInspector(const String&) override;
void didLoadWorkerScript(size_t scriptSize, size_t cachedMetadataSize) override;
void didCreateWorkerGlobalScope(WorkerOrWorkletGlobalScope*) override; void didCreateWorkerGlobalScope(WorkerOrWorkletGlobalScope*) override;
void didInitializeWorkerContext() override; void didInitializeWorkerContext() override;
void willEvaluateWorkerScript(size_t scriptSize, size_t cachedMetadataSize) override;
void willEvaluateImportedScript(size_t scriptSize, size_t cachedMetadataSize) override;
void didEvaluateWorkerScript(bool success) override; void didEvaluateWorkerScript(bool success) override;
void didCloseWorkerGlobalScope() override; void didCloseWorkerGlobalScope() override;
void willDestroyWorkerGlobalScope() override; void willDestroyWorkerGlobalScope() override;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment