blob: d7a352769369d797fbff34dd609044499ac89f05 [file] [log] [blame]
// Copyright 2021 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.
#ifndef CHROMECAST_CAST_CORE_GRPC_RESOURCE_DATA_SOURCE_H_
#define CHROMECAST_CAST_CORE_GRPC_RESOURCE_DATA_SOURCE_H_
#include "base/macros.h"
#include "base/task/sequenced_task_runner_forward.h"
#include "content/public/browser/url_data_source.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/cast_core/public/src/proto/v2/core_application_service.grpc.pb.h"
namespace chromecast {
// This class is responsible for making data requests for resources required by
// Cast WebUI. This implementation uses gRPC for requesting resources from
// CastCore.
class GrpcResourceDataSource : public content::URLDataSource {
public:
GrpcResourceDataSource(
const std::string host,
bool for_webui,
cast::v2::CoreApplicationService::Stub core_app_service_stub);
~GrpcResourceDataSource() override;
void OverrideContentSecurityPolicyChildSrc(const std::string& data);
void DisableDenyXFrameOptions();
private:
friend class GrpcResourceDataSourceTest;
// content::URLDataSource implementation.
std::string GetSource() override;
// Starts a gRPC request to fetch resources.
void StartDataRequest(
const GURL& url,
const content::WebContents::Getter& wc_getter,
content::URLDataSource::GotDataCallback callback) override;
std::string GetMimeType(const std::string& path) override;
// Determines whether the Url request is allowed.
bool ShouldServiceRequest(const GURL& url,
content::BrowserContext* browser_context,
int render_process_id) override;
// Checks origin of the data request url.
std::string GetAccessControlAllowOriginForOrigin(
const std::string& origin) override;
bool ShouldDenyXFrameOptions() override;
void ReadResourceFile(base::StringPiece path,
content::URLDataSource::GotDataCallback callback);
scoped_refptr<base::SequencedTaskRunner> task_runner_;
const std::string host_;
// This is set to true when GrpcResourceDataSource is initialized by
// GrpcWebUIController and false otherwise. In practice, for all the
// chrome://home/* urls this is set to true and false for chrome-resource://*
const bool for_webui_;
cast::v2::CoreApplicationService::Stub core_app_service_stub_;
absl::optional<std::string> frame_src_;
bool deny_xframe_options_ = true;
base::WeakPtrFactory<GrpcResourceDataSource> weak_ptr_factory_{this};
};
} // namespace chromecast
#endif // CHROMECAST_CAST_CORE_GRPC_RESOURCE_DATA_SOURCE_H_