0

Use DefaultConstructTraits for network::CorsErrorStatus.

This allows the default constructor for CorsErrorStatus to be restricted
so it is only available to Mojo.

Fixed: 1408442
Change-Id: Ia26e1689a66167d6d5ed485735509d4fd2048f70
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4129453
Reviewed-by: Takashi Toyoshima <toyoshim@chromium.org>
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1129442}
This commit is contained in:
Daniel Cheng
2023-04-12 20:07:13 +00:00
committed by Chromium LUCI CQ
parent 99e7595655
commit 5a6511254e
8 changed files with 25 additions and 18 deletions

@ -10,6 +10,7 @@
#include "mojo/public/cpp/bindings/array_traits.h"
#include "mojo/public/cpp/bindings/enum_traits.h"
#include "mojo/public/cpp/bindings/lib/buffer.h"
#include "mojo/public/cpp/bindings/lib/default_construct_tag_internal.h"
#include "mojo/public/cpp/bindings/lib/message_fragment.h"
#include "mojo/public/cpp/bindings/lib/template_util.h"
#include "mojo/public/cpp/bindings/map_traits.h"
@ -63,8 +64,14 @@ bool Deserialize(DataType&& input, InputUserType* output, Args&&... args) {
*output = absl::nullopt;
return true;
}
if (!*output)
output->emplace();
if (!*output) {
if constexpr (std::is_constructible_v<typename InputUserType::value_type,
::mojo::DefaultConstruct::Tag>) {
output->emplace(mojo::internal::DefaultConstructTag());
} else {
output->emplace();
}
}
return Deserialize<MojomType>(std::forward<DataType>(input),
&output->value(),
std::forward<Args>(args)...);

@ -579,8 +579,9 @@ TEST_F(UrlLoaderTest, DidFailWithErrorNetworkAccessDenied) {
}
TEST_F(UrlLoaderTest, DidFailWithWebSecurityViolationError) {
blink::WebURLError error(network::CorsErrorStatus(),
blink::WebURLError::HasCopyInCache::kFalse, GURL());
blink::WebURLError error(
network::CorsErrorStatus(network::mojom::CorsError::kDisallowedByMode),
blink::WebURLError::HasCopyInCache::kFalse, GURL());
ASSERT_TRUE(error.is_web_security_violation());
int32_t result = DidFailWithError(error);

@ -2004,7 +2004,7 @@ TEST_F(CorsURLLoaderTest, DevToolsObserverOnCorsErrorCallback) {
EXPECT_TRUE(devtools_observer.cors_error_params());
const network::MockDevToolsObserver::OnCorsErrorParams& params =
*devtools_observer.cors_error_params();
EXPECT_EQ(mojom::CorsError::kDisallowedByMode, params.status.cors_error);
EXPECT_EQ(mojom::CorsError::kDisallowedByMode, params.status->cors_error);
EXPECT_EQ(initiator_origin, params.initiator_origin);
EXPECT_EQ(url, params.url);
}

@ -13,7 +13,7 @@
namespace network {
CorsErrorStatus::CorsErrorStatus() = default;
CorsErrorStatus::CorsErrorStatus(mojo::DefaultConstruct::Tag) {}
CorsErrorStatus::CorsErrorStatus(const CorsErrorStatus&) = default;
CorsErrorStatus& CorsErrorStatus::operator=(const CorsErrorStatus&) = default;

@ -10,6 +10,7 @@
#include "base/component_export.h"
#include "base/unguessable_token.h"
#include "mojo/public/cpp/bindings/default_construct_tag.h"
#include "services/network/public/mojom/cors.mojom-shared.h"
#include "services/network/public/mojom/ip_address_space.mojom-shared.h"
@ -17,12 +18,6 @@ namespace network {
// Type-mapped to `network::mojom::CorsErrorStatus`.
struct COMPONENT_EXPORT(NETWORK_CPP_BASE) CorsErrorStatus {
// This constructor is used by generated IPC serialization code.
// Should not use this explicitly.
// TODO(toyoshim, yhirano): Exploring a way to make this private, and allows
// only serialization code for mojo can access.
CorsErrorStatus();
// Instances of this type are copyable and efficiently movable.
CorsErrorStatus(const CorsErrorStatus&);
CorsErrorStatus& operator=(const CorsErrorStatus&);
@ -43,6 +38,9 @@ struct COMPONENT_EXPORT(NETWORK_CPP_BASE) CorsErrorStatus {
bool operator==(const CorsErrorStatus& rhs) const;
bool operator!=(const CorsErrorStatus& rhs) const { return !(*this == rhs); }
// This constructor is used by generated IPC serialization code.
explicit CorsErrorStatus(mojo::DefaultConstruct::Tag);
// NOTE: This value is meaningless and should be overridden immediately either
// by a constructor or by Mojo deserialization code.
mojom::CorsError cors_error = mojom::CorsError::kMaxValue;

@ -13,14 +13,13 @@ namespace network {
namespace {
TEST(CorsMojomTraitsTest, CorsErrorStatusMojoRoundTrip) {
CorsErrorStatus original;
original.cors_error = mojom::CorsError::kInsecurePrivateNetwork;
CorsErrorStatus original(mojom::CorsError::kInsecurePrivateNetwork,
mojom::IPAddressSpace::kLoopback,
mojom::IPAddressSpace::kLocal);
original.failed_parameter = "bleep";
original.target_address_space = mojom::IPAddressSpace::kLocal;
original.resource_address_space = mojom::IPAddressSpace::kLoopback;
original.has_authorization_covered_by_wildcard_on_preflight = true;
CorsErrorStatus copy;
CorsErrorStatus copy(mojom::CorsError::kInvalidResponse);
EXPECT_TRUE(mojo::test::SerializeAndDeserialize<mojom::CorsErrorStatus>(
original, copy));
EXPECT_EQ(original, copy);

@ -664,6 +664,7 @@ mojom("url_loader_base") {
{
mojom = "network.mojom.CorsErrorStatus"
cpp = "::network::CorsErrorStatus"
default_constructible = false
},
]
traits_headers =

@ -17,6 +17,7 @@
#include "services/network/public/mojom/http_raw_headers.mojom-forward.h"
#include "services/network/public/mojom/ip_address_space.mojom-forward.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace network {
@ -179,7 +180,7 @@ class MockDevToolsObserver : public mojom::DevToolsObserver {
absl::optional<::url::Origin> initiator_origin;
mojom::ClientSecurityStatePtr client_security_state;
GURL url;
network::CorsErrorStatus status;
absl::optional<network::CorsErrorStatus> status;
bool is_warning = false;
};