blob: e05b0d1e77d43ee4f976746e7129ffe1950330f1 [file] [log] [blame]
// 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 "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "content/public/browser/url_loader_request_interceptor.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "services/network/public/mojom/url_loader.mojom.h"
namespace previews {
using FallbackCallback = base::OnceCallback<void()>;
// This class attempts to fetch a LitePage from the LitePage server, and if
// successful, calls a success callback. Otherwise, it calls fallback in the
// case of a failure and redirect in the case of a redirect served from the lite
// pages service. For now, it is only partially implemented.
class PreviewsLitePageServingURLLoader : public network::mojom::URLLoader {
// Creates a network service URLLoader, binds to the URL Loader, and stores
// the various callbacks.
PreviewsLitePageServingURLLoader(const network::ResourceRequest& request,
FallbackCallback fallback_callback);
~PreviewsLitePageServingURLLoader() override;
// network::mojom::URLLoader:
void FollowRedirect(const std::vector<std::string>& removed_headers,
const net::HttpRequestHeaders& modified_headers,
const base::Optional<GURL>& new_url) override;
void ProceedWithResponse() override;
void SetPriority(net::RequestPriority priority,
int32_t intra_priority_value) override;
void PauseReadingBodyFromNet() override;
void ResumeReadingBodyFromNet() override;
// Calls |fallback_callback_| and cleans up.
void Fallback();
// When an error occurs or the LitePage is not suitable, this callback resumes
// default behavior.
FallbackCallback fallback_callback_;
base::WeakPtrFactory<PreviewsLitePageServingURLLoader> weak_ptr_factory_;
} // namespace previews