blob: fc2e8e82efdd0eeb55056f2ab0ad2d9dc4f005f5 [file] [log] [blame]
// Copyright 2022 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "cobalt/loader/fetch_interceptor_coordinator.h"
#include <memory>
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/memory/singleton.h"
#include "url/gurl.h"
namespace cobalt {
namespace loader {
// static
FetchInterceptorCoordinator* FetchInterceptorCoordinator::GetInstance() {
return base::Singleton<
FetchInterceptorCoordinator,
base::LeakySingletonTraits<FetchInterceptorCoordinator>>::get();
}
FetchInterceptorCoordinator::FetchInterceptorCoordinator()
: fetch_interceptor_(nullptr) {}
void FetchInterceptorCoordinator::TryIntercept(
const GURL& url,
std::unique_ptr<base::OnceCallback<void(std::unique_ptr<std::string>)>>
callback,
std::unique_ptr<base::OnceCallback<void(const net::LoadTimingInfo&)>>
report_load_timing_info,
std::unique_ptr<base::OnceClosure> fallback) {
// https://www.w3.org/TR/2022/CRD-service-workers-20220712/#handle-fetch
// Steps 17 to 23
// TODO: add should skip event handling
// (https://w3c.github.io/ServiceWorker/#should-skip-event).
// TODO: determine |shouldSoftUpdate| and if true run soft update in parallel
// (https://w3c.github.io/ServiceWorker/#soft-update).
if (!fetch_interceptor_) {
std::move(*fallback).Run();
return;
}
// TODO: test interception once registered service workers are persisted.
// Consider moving the interception out of the ServiceWorkerGlobalScope
// to avoid a race condition. Fetches should be able to be intercepted
// by an active, registered, service worker.
fetch_interceptor_->StartFetch(url, std::move(callback),
std::move(report_load_timing_info),
std::move(fallback));
}
} // namespace loader
} // namespace cobalt