Commit 3e7fa0cf authored by Victor Costan's avatar Victor Costan Committed by Commit Bot

Cookies API: Wire cookieStore to ServiceWorker.

Bug: 729800
Change-Id: Ic4753af825aa89e26c06da2ff944f03667669697
Reviewed-on: https://chromium-review.googlesource.com/818451
Commit-Queue: Victor Costan <pwnall@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarJoshua Bell <jsbell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#524512}
parent a29ee211
...@@ -16,11 +16,13 @@ ...@@ -16,11 +16,13 @@
#include "content/browser/renderer_host/render_process_host_impl.h" #include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/storage_partition_impl.h" #include "content/browser/storage_partition_impl.h"
#include "content/browser/websockets/websocket_manager.h" #include "content/browser/websockets/websocket_manager.h"
#include "content/network/restricted_cookie_manager.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h" #include "content/public/browser/content_browser_client.h"
#include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h" #include "content/public/browser/render_process_host.h"
#include "content/public/common/content_switches.h"
#include "services/device/public/interfaces/constants.mojom.h" #include "services/device/public/interfaces/constants.mojom.h"
#include "services/device/public/interfaces/vibration_manager.mojom.h" #include "services/device/public/interfaces/vibration_manager.mojom.h"
#include "services/service_manager/public/cpp/binder_registry.h" #include "services/service_manager/public/cpp/binder_registry.h"
...@@ -85,6 +87,24 @@ void ForwardServiceRequest(const char* service_name, ...@@ -85,6 +87,24 @@ void ForwardServiceRequest(const char* service_name,
connector->BindInterface(service_name, std::move(request)); connector->BindInterface(service_name, std::move(request));
} }
void GetRestrictedCookieManager(
network::mojom::RestrictedCookieManagerRequest request,
RenderProcessHost* render_process_host,
const url::Origin& origin) {
if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableExperimentalWebPlatformFeatures)) {
return;
}
StoragePartition* storage_partition =
render_process_host->GetStoragePartition();
mojom::NetworkContext* network_context =
storage_partition->GetNetworkContext();
uint32_t render_process_id = render_process_host->GetID();
network_context->GetRestrictedCookieManager(
std::move(request), render_process_id, MSG_ROUTING_NONE);
}
// Register renderer-exposed interfaces. Each registered interface binder is // Register renderer-exposed interfaces. Each registered interface binder is
// exposed to all renderer-hosted execution context types (document/frame, // exposed to all renderer-hosted execution context types (document/frame,
// dedicated worker, shared worker and service worker) where the appropriate // dedicated worker, shared worker and service worker) where the appropriate
...@@ -142,6 +162,8 @@ void RendererInterfaceBinders::InitializeParameterizedBinderRegistry() { ...@@ -142,6 +162,8 @@ void RendererInterfaceBinders::InitializeParameterizedBinderRegistry() {
})); }));
parameterized_binder_registry_.AddInterface( parameterized_binder_registry_.AddInterface(
base::BindRepeating(&BackgroundFetchServiceImpl::Create)); base::BindRepeating(&BackgroundFetchServiceImpl::Create));
parameterized_binder_registry_.AddInterface(
base::BindRepeating(GetRestrictedCookieManager));
} }
RendererInterfaceBinders& GetRendererInterfaceBinders() { RendererInterfaceBinders& GetRendererInterfaceBinders() {
......
...@@ -201,6 +201,7 @@ ...@@ -201,6 +201,7 @@
"blink::mojom::NotificationService", "blink::mojom::NotificationService",
"blink::mojom::PermissionService", "blink::mojom::PermissionService",
"blink::mojom::WebSocket", "blink::mojom::WebSocket",
"network::mojom::RestrictedCookieManager",
"payments::mojom::PaymentManager", "payments::mojom::PaymentManager",
"shape_detection::mojom::BarcodeDetection", "shape_detection::mojom::BarcodeDetection",
"shape_detection::mojom::FaceDetectionProvider", "shape_detection::mojom::FaceDetectionProvider",
......
<!doctype html>
<meta charset="utf-8">
<title>Async Cookies: cookieStore.get() sees cookieStore.set() cookie</title>
<link rel="help" href="https://github.com/WICG/cookie-store">
<link rel="author" href="pwnall@chromium.org" title="Victor Costan">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
'use strict'; 'use strict';
// Workaround because add_cleanup doesn't support async functions yet. // Workaround because add_cleanup doesn't support async functions yet.
...@@ -27,5 +19,3 @@ promise_test(async testCase => { ...@@ -27,5 +19,3 @@ promise_test(async testCase => {
await async_cleanup(() => cookieStore.delete('cookie-name')); await async_cleanup(() => cookieStore.delete('cookie-name'));
}, 'cookieStore.get returns the cookie written by cookieStore.set'); }, 'cookieStore.get returns the cookie written by cookieStore.set');
</script>
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
};
importScripts("/resources/testharness.js");
importScripts("get_set.tentative.window.js");
done();
<!doctype html>
<meta charset="utf-8">
<title>Async Cookies: cookieStore.get() sees cookieStore.set() cookie in ServiceWorker</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
'use strict';
(async () => {
const scope = 'does/not/exist';
let registration = await navigator.serviceWorker.getRegistration(scope);
if (registration)
await registration.unregister();
registration = await navigator.serviceWorker.register(
'serviceworker_cookiestore_basic.js', {scope});
fetch_tests_from_worker(registration.installing);
})();
</script>
...@@ -134,6 +134,14 @@ interface CloseEvent : Event ...@@ -134,6 +134,14 @@ interface CloseEvent : Event
getter reason getter reason
getter wasClean getter wasClean
method constructor method constructor
interface CookieStore
attribute @@toStringTag
method constructor
method delete
method get
method getAll
method has
method set
interface CountQueuingStrategy interface CountQueuingStrategy
method constructor method constructor
method size method size
...@@ -2574,6 +2582,7 @@ interface WritableStream ...@@ -2574,6 +2582,7 @@ interface WritableStream
attribute console attribute console
attribute internals attribute internals
getter clients getter clients
getter cookieStore
getter onabortpayment getter onabortpayment
getter onactivate getter onactivate
getter onbackgroundfetchabort getter onbackgroundfetchabort
...@@ -2593,6 +2602,7 @@ interface WritableStream ...@@ -2593,6 +2602,7 @@ interface WritableStream
method fetch method fetch
method gc method gc
method skipWaiting method skipWaiting
setter cookieStore
setter onabortpayment setter onabortpayment
setter onactivate setter onactivate
setter onbackgroundfetchabort setter onbackgroundfetchabort
......
...@@ -177,8 +177,13 @@ void ExtractCookieURLs(ScriptState* script_state, ...@@ -177,8 +177,13 @@ void ExtractCookieURLs(ScriptState* script_state,
Document* document = ToDocument(execution_context); Document* document = ToDocument(execution_context);
cookie_url = document->CookieURL(); cookie_url = document->CookieURL();
site_for_cookies = document->SiteForCookies(); site_for_cookies = document->SiteForCookies();
} else if (execution_context->IsServiceWorkerGlobalScope()) {
ServiceWorkerGlobalScope* scope =
ToServiceWorkerGlobalScope(execution_context);
// TODO(crbug.com/729800): Correct values?
cookie_url = scope->Url();
site_for_cookies = scope->Url();
} else { } else {
// TODO(crbug.com/729800): Add branch for service workers.
NOTIMPLEMENTED(); NOTIMPLEMENTED();
} }
} }
...@@ -388,6 +393,10 @@ ScriptPromise CookieStore::DoWrite(ScriptState* script_state, ...@@ -388,6 +393,10 @@ ScriptPromise CookieStore::DoWrite(ScriptState* script_state,
// static // static
void CookieStore::OnSetCanonicalCookieResult(ScriptPromiseResolver* resolver, void CookieStore::OnSetCanonicalCookieResult(ScriptPromiseResolver* resolver,
bool backend_success) { bool backend_success) {
ScriptState* script_state = resolver->GetScriptState();
if (!script_state->ContextIsValid())
return;
if (!backend_success) { if (!backend_success) {
resolver->Reject(DOMException::Create( resolver->Reject(DOMException::Create(
kUnknownError, "An unknown error occured while writing the cookie.")); kUnknownError, "An unknown error occured while writing the cookie."));
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
// https://github.com/WICG/async-cookies-api/blob/gh-pages/explainer.md // https://github.com/WICG/async-cookies-api/blob/gh-pages/explainer.md
[ [
Exposed=Window, Exposed=(ServiceWorker,Window),
RuntimeEnabled=AsyncCookies RuntimeEnabled=AsyncCookies
] interface CookieStore { ] interface CookieStore {
// https://github.com/WICG/async-cookies-api/blob/gh-pages/explainer.md#reading // https://github.com/WICG/async-cookies-api/blob/gh-pages/explainer.md#reading
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "core/frame/LocalFrame.h" #include "core/frame/LocalFrame.h"
#include "core/workers/WorkerThread.h" #include "core/workers/WorkerThread.h"
#include "modules/cookie_store/CookieStore.h" #include "modules/cookie_store/CookieStore.h"
#include "modules/serviceworkers/ServiceWorkerGlobalScope.h"
#include "platform/Supplementable.h" #include "platform/Supplementable.h"
#include "platform/heap/Handle.h" #include "platform/heap/Handle.h"
#include "services/network/public/interfaces/restricted_cookie_manager.mojom-blink.h" #include "services/network/public/interfaces/restricted_cookie_manager.mojom-blink.h"
...@@ -36,21 +37,10 @@ class GlobalCookieStoreImpl final ...@@ -36,21 +37,10 @@ class GlobalCookieStoreImpl final
return *supplement; return *supplement;
} }
CookieStore* GetCookieStore(); CookieStore* GetCookieStore(T& scope) {
virtual void Trace(blink::Visitor* visitor) {
visitor->Trace(cookie_store_);
Supplement<T>::Trace(visitor);
}
private:
explicit GlobalCookieStoreImpl(T& supplementable)
: Supplement<T>(supplementable) {}
static const char* GetName() { return "CookieStore"; }
CookieStore* GetCookieStore(ExecutionContext* execution_context) {
if (!cookie_store_) { if (!cookie_store_) {
ExecutionContext* execution_context = scope.GetExecutionContext();
network::mojom::blink::RestrictedCookieManagerPtr cookie_manager_ptr; network::mojom::blink::RestrictedCookieManagerPtr cookie_manager_ptr;
service_manager::InterfaceProvider* interface_provider = service_manager::InterfaceProvider* interface_provider =
execution_context->GetInterfaceProvider(); execution_context->GetInterfaceProvider();
...@@ -63,21 +53,32 @@ class GlobalCookieStoreImpl final ...@@ -63,21 +53,32 @@ class GlobalCookieStoreImpl final
return cookie_store_; return cookie_store_;
} }
Member<CookieStore> cookie_store_; virtual void Trace(blink::Visitor* visitor) {
}; visitor->Trace(cookie_store_);
Supplement<T>::Trace(visitor);
}
template <> private:
CookieStore* GlobalCookieStoreImpl<LocalDOMWindow>::GetCookieStore() { explicit GlobalCookieStoreImpl(T& supplementable)
LocalDOMWindow* window = GetSupplementable(); : Supplement<T>(supplementable) {}
return GetCookieStore(window->document());
}
// TODO(crbug.com/729800): Add ServiceWorkerGlobalScope overload. static const char* GetName() { return "CookieStore"; }
Member<CookieStore> cookie_store_;
};
} // namespace } // namespace
CookieStore* GlobalCookieStore::cookieStore(LocalDOMWindow& window) { CookieStore* GlobalCookieStore::cookieStore(LocalDOMWindow& window) {
return GlobalCookieStoreImpl<LocalDOMWindow>::From(window).GetCookieStore(); return GlobalCookieStoreImpl<LocalDOMWindow>::From(window).GetCookieStore(
window);
}
CookieStore* GlobalCookieStore::cookieStore(ServiceWorkerGlobalScope& worker) {
// ServiceWorkerGlobalScope is Supplementable<WorkerGlobalScope>, not
// Supplementable<ServiceWorkerGlobalScope>.
return GlobalCookieStoreImpl<WorkerGlobalScope>::From(worker).GetCookieStore(
worker);
} }
} // namespace blink } // namespace blink
...@@ -11,6 +11,7 @@ namespace blink { ...@@ -11,6 +11,7 @@ namespace blink {
class CookieStore; class CookieStore;
class LocalDOMWindow; class LocalDOMWindow;
class ServiceWorkerGlobalScope;
// Exposes a CookieStore as the "cookieStore" attribute on the global scope. // Exposes a CookieStore as the "cookieStore" attribute on the global scope.
// //
...@@ -20,8 +21,7 @@ class GlobalCookieStore { ...@@ -20,8 +21,7 @@ class GlobalCookieStore {
public: public:
static CookieStore* cookieStore(LocalDOMWindow&); static CookieStore* cookieStore(LocalDOMWindow&);
static CookieStore* cookieStore(ServiceWorkerGlobalScope&);
// TODO(crbug.com/729800): Expose to ServiceWorkerGlobalScope.
}; };
} // namespace blink } // namespace blink
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// https://github.com/WICG/async-cookies-api/blob/gh-pages/explainer.md
[
RuntimeEnabled=AsyncCookies,
ImplementedAs=GlobalCookieStore
] partial interface ServiceWorkerGlobalScope {
[Replaceable, SameObject] readonly attribute CookieStore cookieStore;
};
...@@ -654,6 +654,7 @@ modules_dependency_idl_files = ...@@ -654,6 +654,7 @@ modules_dependency_idl_files =
"canvas/htmlcanvas/HTMLCanvasElementModule.idl", "canvas/htmlcanvas/HTMLCanvasElementModule.idl",
"canvas/canvas2d/CanvasPath.idl", "canvas/canvas2d/CanvasPath.idl",
"clipboard/NavigatorClipboard.idl", "clipboard/NavigatorClipboard.idl",
"cookie_store/ServiceWorkerGlobalScopeCookieStore.idl",
"cookie_store/WindowCookieStore.idl", "cookie_store/WindowCookieStore.idl",
"credentialmanager/CredentialUserData.idl", "credentialmanager/CredentialUserData.idl",
"credentialmanager/NavigatorCredentials.idl", "credentialmanager/NavigatorCredentials.idl",
......
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