| // Copyright 2019 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. |
| |
| #include "third_party/blink/renderer/modules/cookie_store/global_cookie_store.h" |
| |
| #include <utility> |
| |
| #include "services/network/public/mojom/restricted_cookie_manager.mojom-blink.h" |
| #include "third_party/blink/public/common/browser_interface_broker_proxy.h" |
| #include "third_party/blink/renderer/core/execution_context/execution_context.h" |
| #include "third_party/blink/renderer/core/frame/local_dom_window.h" |
| #include "third_party/blink/renderer/core/frame/local_frame.h" |
| #include "third_party/blink/renderer/core/workers/worker_global_scope.h" |
| #include "third_party/blink/renderer/modules/cookie_store/cookie_store.h" |
| #include "third_party/blink/renderer/modules/service_worker/service_worker_global_scope.h" |
| #include "third_party/blink/renderer/platform/heap/handle.h" |
| #include "third_party/blink/renderer/platform/supplementable.h" |
| |
| namespace blink { |
| |
| namespace { |
| |
| template <typename T> |
| class GlobalCookieStoreImpl final |
| : public GarbageCollected<GlobalCookieStoreImpl<T>>, |
| public Supplement<T> { |
| public: |
| static const char kSupplementName[]; |
| |
| static GlobalCookieStoreImpl& From(T& supplementable) { |
| GlobalCookieStoreImpl* supplement = |
| Supplement<T>::template From<GlobalCookieStoreImpl>(supplementable); |
| if (!supplement) { |
| supplement = MakeGarbageCollected<GlobalCookieStoreImpl>(supplementable); |
| Supplement<T>::ProvideTo(supplementable, supplement); |
| } |
| return *supplement; |
| } |
| |
| explicit GlobalCookieStoreImpl(T& supplementable) |
| : Supplement<T>(supplementable) {} |
| |
| CookieStore* GetCookieStore(T& scope) { |
| if (!cookie_store_) { |
| ExecutionContext* execution_context = scope.GetExecutionContext(); |
| if (&execution_context->GetBrowserInterfaceBroker() == |
| &GetEmptyBrowserInterfaceBroker()) { |
| return nullptr; |
| } |
| |
| mojo::Remote<network::mojom::blink::RestrictedCookieManager> backend; |
| execution_context->GetBrowserInterfaceBroker().GetInterface( |
| backend.BindNewPipeAndPassReceiver( |
| execution_context->GetTaskRunner(TaskType::kDOMManipulation))); |
| cookie_store_ = MakeGarbageCollected<CookieStore>(execution_context, |
| std::move(backend)); |
| } |
| return cookie_store_; |
| } |
| |
| void Trace(Visitor* visitor) const override { |
| visitor->Trace(cookie_store_); |
| Supplement<T>::Trace(visitor); |
| } |
| |
| private: |
| Member<CookieStore> cookie_store_; |
| }; |
| |
| // static |
| template <typename T> |
| const char GlobalCookieStoreImpl<T>::kSupplementName[] = |
| "GlobalCookieStoreImpl"; |
| |
| } // namespace |
| |
| // static |
| CookieStore* GlobalCookieStore::cookieStore(LocalDOMWindow& window) { |
| return GlobalCookieStoreImpl<LocalDOMWindow>::From(window).GetCookieStore( |
| window); |
| } |
| |
| // static |
| CookieStore* GlobalCookieStore::cookieStore(ServiceWorkerGlobalScope& worker) { |
| // ServiceWorkerGlobalScope is Supplementable<WorkerGlobalScope>, not |
| // Supplementable<ServiceWorkerGlobalScope>. |
| return GlobalCookieStoreImpl<WorkerGlobalScope>::From(worker).GetCookieStore( |
| worker); |
| } |
| |
| } // namespace blink |