| Index: third_party/WebKit/Source/modules/serviceworkers/ServiceWorkerGlobalScope.cpp
|
| diff --git a/third_party/WebKit/Source/modules/serviceworkers/ServiceWorkerGlobalScope.cpp b/third_party/WebKit/Source/modules/serviceworkers/ServiceWorkerGlobalScope.cpp
|
| index 144f0b649f54ede118ad8d47fb2d97f6b80cdeed..247054c6813a8bf5365e7fd749094866238759d8 100644
|
| --- a/third_party/WebKit/Source/modules/serviceworkers/ServiceWorkerGlobalScope.cpp
|
| +++ b/third_party/WebKit/Source/modules/serviceworkers/ServiceWorkerGlobalScope.cpp
|
| @@ -31,6 +31,7 @@
|
| #include "modules/serviceworkers/ServiceWorkerGlobalScope.h"
|
|
|
| #include "bindings/core/v8/CallbackPromiseAdapter.h"
|
| +#include "bindings/core/v8/ScriptSourceCode.h"
|
| #include "bindings/core/v8/ScriptPromise.h"
|
| #include "bindings/core/v8/ScriptPromiseResolver.h"
|
| #include "bindings/core/v8/ScriptState.h"
|
| @@ -38,6 +39,7 @@
|
| #include "bindings/core/v8/V8ThrowException.h"
|
| #include "core/dom/ExceptionCode.h"
|
| #include "core/events/Event.h"
|
| +#include "core/fetch/CachedMetadata.h"
|
| #include "core/fetch/MemoryCache.h"
|
| #include "core/fetch/ResourceLoaderOptions.h"
|
| #include "core/inspector/ConsoleMessage.h"
|
| @@ -46,11 +48,14 @@
|
| #include "core/loader/ThreadableLoader.h"
|
| #include "core/origin_trials/OriginTrialContext.h"
|
| #include "core/workers/WorkerClients.h"
|
| +#include "core/workers/WorkerReportingProxy.h"
|
| #include "core/workers/WorkerThreadStartupData.h"
|
| #include "modules/EventTargetModules.h"
|
| #include "modules/cachestorage/CacheStorage.h"
|
| #include "modules/cachestorage/InspectorCacheStorageAgent.h"
|
| #include "modules/fetch/GlobalFetch.h"
|
| +#include "modules/serviceworkers/IsolatedWorkerReportingProxy.h"
|
| +#include "modules/serviceworkers/IsolatedWorkerThread.h"
|
| #include "modules/serviceworkers/ServiceWorkerClients.h"
|
| #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h"
|
| #include "modules/serviceworkers/ServiceWorkerRegistration.h"
|
| @@ -59,6 +64,7 @@
|
| #include "modules/serviceworkers/WaitUntilObserver.h"
|
| #include "platform/Histogram.h"
|
| #include "platform/network/ResourceRequest.h"
|
| +#include "platform/TraceEvent.h"
|
| #include "platform/weborigin/KURL.h"
|
| #include "public/platform/Platform.h"
|
| #include "public/platform/WebURL.h"
|
| @@ -67,6 +73,93 @@
|
| #include <memory>
|
|
|
| namespace blink {
|
| +namespace {
|
| +
|
| + class IsolatedWorkerScriptCachedMetadataHandler : public CachedMetadataHandler {
|
| + public:
|
| + static IsolatedWorkerScriptCachedMetadataHandler* create(WorkerGlobalScope* workerGlobalScope, const KURL& scriptURL, const Vector<char>* metaData)
|
| + {
|
| + return new IsolatedWorkerScriptCachedMetadataHandler(workerGlobalScope, scriptURL, metaData);
|
| + }
|
| + ~IsolatedWorkerScriptCachedMetadataHandler() override;
|
| + DECLARE_VIRTUAL_TRACE();
|
| + void setCachedMetadata(unsigned dataTypeID, const char*, size_t, CacheType) override;
|
| + void clearCachedMetadata(CacheType) override;
|
| + PassRefPtr<CachedMetadata> cachedMetadata(unsigned dataTypeID) const override;
|
| + String encoding() const override;
|
| +
|
| + private:
|
| + IsolatedWorkerScriptCachedMetadataHandler(WorkerGlobalScope*, const KURL& scriptURL, const Vector<char>* metaData);
|
| +
|
| + Member<WorkerGlobalScope> m_workerGlobalScope;
|
| + KURL m_scriptURL;
|
| + RefPtr<CachedMetadata> m_cachedMetadata;
|
| + };
|
| +
|
| + IsolatedWorkerScriptCachedMetadataHandler::IsolatedWorkerScriptCachedMetadataHandler(WorkerGlobalScope* workerGlobalScope, const KURL& scriptURL, const Vector<char>* metaData)
|
| + : m_workerGlobalScope(workerGlobalScope)
|
| + , m_scriptURL(scriptURL)
|
| + {
|
| + if (metaData)
|
| + m_cachedMetadata = CachedMetadata::deserialize(metaData->data(), metaData->size());
|
| + }
|
| +
|
| + IsolatedWorkerScriptCachedMetadataHandler::~IsolatedWorkerScriptCachedMetadataHandler()
|
| + {
|
| + }
|
| +
|
| + DEFINE_TRACE(IsolatedWorkerScriptCachedMetadataHandler)
|
| + {
|
| + visitor->trace(m_workerGlobalScope);
|
| + CachedMetadataHandler::trace(visitor);
|
| + }
|
| +
|
| + void IsolatedWorkerScriptCachedMetadataHandler::setCachedMetadata(unsigned dataTypeID, const char* data, size_t size, CacheType type)
|
| + {
|
| + if (type != SendToPlatform)
|
| + return;
|
| + //TODO
|
| + LOG(ERROR) << "IsolatedWorkerScriptCachedMetadataHandler::setCachedMetadata size: " << size;
|
| + }
|
| +
|
| + void IsolatedWorkerScriptCachedMetadataHandler::clearCachedMetadata(CacheType type)
|
| + {
|
| + if (type != SendToPlatform)
|
| + return;
|
| + m_cachedMetadata = nullptr;
|
| + //TODO
|
| + LOG(ERROR) << "IsolatedWorkerScriptCachedMetadataHandler::setCachedMetadata";
|
| + }
|
| +
|
| + PassRefPtr<CachedMetadata> IsolatedWorkerScriptCachedMetadataHandler::cachedMetadata(unsigned dataTypeID) const
|
| + {
|
| + if (!m_cachedMetadata || m_cachedMetadata->dataTypeID() != dataTypeID)
|
| + return nullptr;
|
| + return m_cachedMetadata.get();
|
| + }
|
| +
|
| + String IsolatedWorkerScriptCachedMetadataHandler::encoding() const
|
| + {
|
| + return emptyString();
|
| + }
|
| +
|
| +} // namespace
|
| +
|
| +ServiceWorkerGlobalScope* ServiceWorkerGlobalScope::create(IsolatedWorkerThread* thread, std::unique_ptr<WorkerThreadStartupData> startupData)
|
| +{
|
| + // Note: startupData is finalized on return. After the relevant parts has been
|
| + // passed along to the created 'context'.
|
| + ServiceWorkerGlobalScope* context = new ServiceWorkerGlobalScope(startupData->m_scriptURL, startupData->m_userAgent, thread, monotonicallyIncreasingTime(), std::move(startupData->m_starterOriginPrivilegeData));
|
| +
|
| + context->setV8CacheOptions(startupData->m_v8CacheOptions);
|
| + context->applyContentSecurityPolicyFromVector(*startupData->m_contentSecurityPolicyHeaders);
|
| + if (!startupData->m_referrerPolicy.isNull())
|
| + context->parseAndSetReferrerPolicy(startupData->m_referrerPolicy);
|
| + context->setAddressSpace(startupData->m_addressSpace);
|
| + OriginTrialContext::addTokens(context, startupData->m_originTrialTokens.get());
|
| +
|
| + return context;
|
| +}
|
|
|
| ServiceWorkerGlobalScope* ServiceWorkerGlobalScope::create(ServiceWorkerThread* thread, std::unique_ptr<WorkerThreadStartupData> startupData)
|
| {
|
| @@ -84,8 +177,21 @@ ServiceWorkerGlobalScope* ServiceWorkerGlobalScope::create(ServiceWorkerThread*
|
| return context;
|
| }
|
|
|
| +ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(const KURL& url, const String& userAgent, IsolatedWorkerThread* thread, double timeOrigin, std::unique_ptr<SecurityOrigin::PrivilegeData> starterOriginPrivilegeData)
|
| + : WorkerGlobalScope(url, userAgent, thread, timeOrigin, std::move(starterOriginPrivilegeData), WorkerClients::create())
|
| + , m_isIsolatedWorkerGlobalScope(true)
|
| + , m_didEvaluateScript(false)
|
| + , m_hadErrorInTopLevelEventHandler(false)
|
| + , m_eventNestingLevel(0)
|
| + , m_scriptCount(0)
|
| + , m_scriptTotalSize(0)
|
| + , m_scriptCachedMetadataTotalSize(0)
|
| +{
|
| +}
|
| +
|
| ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(const KURL& url, const String& userAgent, ServiceWorkerThread* thread, double timeOrigin, std::unique_ptr<SecurityOrigin::PrivilegeData> starterOriginPrivilegeData, WorkerClients* workerClients)
|
| : WorkerGlobalScope(url, userAgent, thread, timeOrigin, std::move(starterOriginPrivilegeData), workerClients)
|
| + , m_isIsolatedWorkerGlobalScope(false)
|
| , m_didEvaluateScript(false)
|
| , m_hadErrorInTopLevelEventHandler(false)
|
| , m_eventNestingLevel(0)
|
| @@ -204,17 +310,57 @@ DEFINE_TRACE(ServiceWorkerGlobalScope)
|
|
|
| void ServiceWorkerGlobalScope::importScripts(const Vector<String>& urls, ExceptionState& exceptionState)
|
| {
|
| - // Bust the MemoryCache to ensure script requests reach the browser-side
|
| - // and get added to and retrieved from the ServiceWorker's script cache.
|
| - // FIXME: Revisit in light of the solution to crbug/388375.
|
| - for (Vector<String>::const_iterator it = urls.begin(); it != urls.end(); ++it)
|
| - getExecutionContext()->removeURLFromMemoryCache(completeURL(*it));
|
| - WorkerGlobalScope::importScripts(urls, exceptionState);
|
| + if (!m_isIsolatedWorkerGlobalScope) {
|
| + // Bust the MemoryCache to ensure script requests reach the browser-side
|
| + // and get added to and retrieved from the ServiceWorker's script cache.
|
| + // FIXME: Revisit in light of the solution to crbug/388375.
|
| + for (Vector<String>::const_iterator it = urls.begin(); it != urls.end(); ++it)
|
| + getExecutionContext()->removeURLFromMemoryCache(completeURL(*it));
|
| + WorkerGlobalScope::importScripts(urls, exceptionState);
|
| + return;
|
| + }
|
| +
|
| + TRACE_EVENT0("ServiceWorker", "IsolatedWorkerGlobalScope::importScripts");
|
| +
|
| + ExecutionContext& executionContext = *this->getExecutionContext();
|
| + Vector<KURL> completedURLs;
|
| + for (const String& urlString : urls) {
|
| + const KURL& url = executionContext.completeURL(urlString);
|
| + if (!url.isValid()) {
|
| + exceptionState.throwDOMException(SyntaxError, "The URL '" + urlString + "' is invalid.");
|
| + return;
|
| + }
|
| + completedURLs.append(url);
|
| + }
|
| +
|
| + for (const auto& scriptURL : completedURLs) {
|
| + std::unique_ptr<String> alternativeCode(thread()->workerReportingProxy().takeAlternativeCode(scriptURL));
|
| + if (!alternativeCode) {
|
| + return;
|
| + }
|
| + ErrorEvent* errorEvent = nullptr;
|
| + CachedMetadataHandler* handler(createWorkerScriptCachedMetadataHandler(scriptURL, nullptr));
|
| + {
|
| + TRACE_EVENT0("ServiceWorker", "IsolatedWorkerGlobalScope::importScripts evaluate");
|
| + scriptController()->evaluate(ScriptSourceCode(*alternativeCode, scriptURL), &errorEvent, handler, V8CacheOptionsAlways);
|
| + }
|
| + if (errorEvent) {
|
| + scriptController()->rethrowExceptionFromImportedScript(errorEvent, exceptionState);
|
| + return;
|
| + }
|
| + }
|
| }
|
|
|
| CachedMetadataHandler* ServiceWorkerGlobalScope::createWorkerScriptCachedMetadataHandler(const KURL& scriptURL, const Vector<char>* metaData)
|
| {
|
| - return ServiceWorkerScriptCachedMetadataHandler::create(this, scriptURL, metaData);
|
| + if (!m_isIsolatedWorkerGlobalScope)
|
| + return ServiceWorkerScriptCachedMetadataHandler::create(this, scriptURL, metaData);
|
| + TRACE_EVENT0("ServiceWorker", "IsolatedWorkerGlobalScope::createWorkerScriptCachedMetadataHandler");
|
| + IsolatedWorkerReportingProxy* workerReportingProxy = static_cast<IsolatedWorkerReportingProxy*>(&thread()->workerReportingProxy());
|
| + std::unique_ptr<WTF::Vector<char>> cachedMetadata(workerReportingProxy->takeAlternativeCachedMetadata(scriptURL));
|
| + if (cachedMetadata && !cachedMetadata->isEmpty())
|
| + return IsolatedWorkerScriptCachedMetadataHandler::create(this, scriptURL, cachedMetadata.get());
|
| + return nullptr;
|
| }
|
|
|
| void ServiceWorkerGlobalScope::exceptionThrown(ErrorEvent* event)
|
|
|