Skip to content

Commit

Permalink
Move StrongAlias and PassKey to base/types/ due to widespread value.
Browse files Browse the repository at this point in the history
Bug: none
Change-Id: Idb6a5aef372726171389074bf216ef9000e715f8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2533285
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: Darin Fisher <darin@chromium.org>
Reviewed-by: Łukasz Anforowicz <lukasza@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828887}
  • Loading branch information
pkasting authored and Commit Bot committed Nov 18, 2020
1 parent fafd307 commit 796cde2
Show file tree
Hide file tree
Showing 215 changed files with 652 additions and 660 deletions.
5 changes: 5 additions & 0 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,8 @@ component("base") {
"trace_event/trace_id_helper.h",
"traits_bag.h",
"tuple.h",
"types/pass_key.h",
"types/strong_alias.h",
"unguessable_token.cc",
"unguessable_token.h",
"updateable_sequenced_task_runner.h",
Expand Down Expand Up @@ -3009,6 +3011,8 @@ test("base_unittests") {
"tools_sanity_unittest.cc",
"traits_bag_unittest.cc",
"tuple_unittest.cc",
"types/pass_key_unittest.cc",
"types/strong_alias_unittest.cc",
"unguessable_token_unittest.cc",
"value_iterators_unittest.cc",
"values_unittest.cc",
Expand Down Expand Up @@ -3502,6 +3506,7 @@ if (enable_nocompile_tests) {
"task/task_traits_unittest.nc",
"thread_annotations_unittest.nc",
"traits_bag_unittest.nc",
"types/pass_key_unittest.nc",
]

deps = [
Expand Down
5 changes: 5 additions & 0 deletions base/types/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include_rules = [
"-base",
"+base/types",
"-third_party",
]
2 changes: 2 additions & 0 deletions base/types/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lukasza@chromium.org
mpawlowski@opera.com
18 changes: 9 additions & 9 deletions base/util/type_safety/pass_key.h → base/types/pass_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_UTIL_TYPE_SAFETY_PASS_KEY_H_
#define BASE_UTIL_TYPE_SAFETY_PASS_KEY_H_
#ifndef BASE_TYPES_PASS_KEY_H_
#define BASE_TYPES_PASS_KEY_H_

namespace util {
namespace base {

// util::PassKey can be used to restrict access to functions to an authorized
// base::PassKey can be used to restrict access to functions to an authorized
// caller. The primary use case is restricting the construction of an object in
// situations where the constructor needs to be public, which may be the case
// if the object must be constructed through a helper function, such as
Expand All @@ -17,12 +17,12 @@ namespace util {
//
// class Foo {
// public:
// Foo(util::PassKey<Manager>);
// Foo(base::PassKey<Manager>);
// };
//
// class Manager {
// public:
// using PassKey = util::PassKey<Manager>;
// using PassKey = base::PassKey<Manager>;
// Manager() : foo_(blink::MakeGarbageCollected<Foo>(PassKey())) {}
// void Trace(blink::Visitor* visitor) const { visitor->Trace(foo_); }
// Foo* GetFooSingleton() { foo_; }
Expand All @@ -32,7 +32,7 @@ namespace util {
// };
//
// In the above example, the 'Foo' constructor requires an instance of
// util::PassKey<Manager>. Only Manager is allowed to create such instances,
// base::PassKey<Manager>. Only Manager is allowed to create such instances,
// making the constructor unusable elsewhere.
template <typename T>
class PassKey {
Expand All @@ -43,6 +43,6 @@ class PassKey {
friend T;
};

} // namespace util
} // namespace base

#endif // BASE_UTIL_TYPE_SAFETY_PASS_KEY_H_
#endif // BASE_TYPES_PASS_KEY_H_
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/util/type_safety/pass_key.h"
#include "base/types/pass_key.h"

#include <utility>

#include "testing/gtest/include/gtest/gtest.h"

namespace util {
namespace base {
namespace {

class Manager;

// May not be created without a PassKey.
class Restricted {
public:
Restricted(util::PassKey<Manager>) {}
Restricted(base::PassKey<Manager>) {}
};

class Manager {
public:
enum class ExplicitConstruction { kTag };
enum class UniformInitialization { kTag };

Manager(ExplicitConstruction) : restricted_(util::PassKey<Manager>()) {}
Manager(ExplicitConstruction) : restricted_(base::PassKey<Manager>()) {}
Manager(UniformInitialization) : restricted_({}) {}

private:
Expand All @@ -43,4 +43,4 @@ TEST(PassKeyTest, UniformInitialization) {
}

} // namespace
} // namespace util
} // namespace base
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@
// This is a no-compile test suite.
// http://dev.chromium.org/developers/testing/no-compile-tests

#include "base/util/type_safety/pass_key.h"
#include "base/types/pass_key.h"

namespace util {
namespace base {

class Manager;

// May not be created without a PassKey.
class Restricted {
public:
Restricted(util::PassKey<Manager>) {}
Restricted(base::PassKey<Manager>) {}
};

int Secret(util::PassKey<Manager>) {
int Secret(base::PassKey<Manager>) {
return 1;
}

#if defined(NCTEST_UNAUTHORIZED_PASS_KEY_IN_INITIALIZER) // [r"fatal error: calling a private constructor of class 'util::PassKey<util::Manager>'"]
#if defined(NCTEST_UNAUTHORIZED_PASS_KEY_IN_INITIALIZER) // [r"fatal error: calling a private constructor of class 'base::PassKey<base::Manager>'"]

class NotAManager {
public:
NotAManager() : restricted_(util::PassKey<Manager>()) {}
NotAManager() : restricted_(base::PassKey<Manager>()) {}

private:
Restricted restricted_;
Expand All @@ -35,7 +35,7 @@ void WillNotCompile() {
NotAManager not_a_manager;
}

#elif defined(NCTEST_UNAUTHORIZED_UNIFORM_INITIALIZED_PASS_KEY_IN_INITIALIZER) // [r"fatal error: calling a private constructor of class 'util::PassKey<util::Manager>'"]
#elif defined(NCTEST_UNAUTHORIZED_UNIFORM_INITIALIZED_PASS_KEY_IN_INITIALIZER) // [r"fatal error: calling a private constructor of class 'base::PassKey<base::Manager>'"]

class NotAManager {
public:
Expand All @@ -49,25 +49,25 @@ void WillNotCompile() {
NotAManager not_a_manager;
}

#elif defined(NCTEST_UNAUTHORIZED_PASS_KEY_IN_FUNCTION) // [r"fatal error: calling a private constructor of class 'util::PassKey<util::Manager>'"]
#elif defined(NCTEST_UNAUTHORIZED_PASS_KEY_IN_FUNCTION) // [r"fatal error: calling a private constructor of class 'base::PassKey<base::Manager>'"]

int WillNotCompile() {
return Secret(util::PassKey<Manager>());
return Secret(base::PassKey<Manager>());
}

#elif defined(NCTEST_UNAUTHORIZED_UNIFORM_INITIALIZATION_WITH_DEDUCED_PASS_KEY_TYPE) // [r"fatal error: calling a private constructor of class 'util::PassKey<util::Manager>'"]
#elif defined(NCTEST_UNAUTHORIZED_UNIFORM_INITIALIZATION_WITH_DEDUCED_PASS_KEY_TYPE) // [r"fatal error: calling a private constructor of class 'base::PassKey<base::Manager>'"]

int WillNotCompile() {
return Secret({});
}

#elif defined(NCTEST_UNAUTHORIZED_UNIFORM_INITIALIZATION) // [r"fatal error: calling a private constructor of class 'util::PassKey<util::Manager>'"]
#elif defined(NCTEST_UNAUTHORIZED_UNIFORM_INITIALIZATION) // [r"fatal error: calling a private constructor of class 'base::PassKey<base::Manager>'"]

int WillNotCompile() {
util::PassKey<Manager> key {};
base::PassKey<Manager> key {};
return Secret(key);
}

#endif

} // namespace util
} // namespace base
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_UTIL_TYPE_SAFETY_STRONG_ALIAS_H_
#define BASE_UTIL_TYPE_SAFETY_STRONG_ALIAS_H_
#ifndef BASE_TYPES_STRONG_ALIAS_H_
#define BASE_TYPES_STRONG_ALIAS_H_

#include <ostream>
#include <utility>

namespace util {
namespace base {

// A type-safe alternative for a typedef or a 'using' directive.
//
Expand Down Expand Up @@ -119,6 +119,6 @@ std::ostream& operator<<(std::ostream& stream,
return stream << alias.value();
}

} // namespace util
} // namespace base

#endif // BASE_UTIL_TYPE_SAFETY_STRONG_ALIAS_H_
#endif // BASE_TYPES_STRONG_ALIAS_H_
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/util/type_safety/strong_alias.h"
#include "base/types/strong_alias.h"

#include <cstdint>
#include <map>
Expand All @@ -15,7 +15,7 @@

#include "testing/gtest/include/gtest/gtest.h"

namespace util {
namespace base {

namespace {

Expand Down Expand Up @@ -317,4 +317,4 @@ TEST(StrongAliasTest, EnsureConstexpr) {
static_assert(kOne >= kZero, "");
}

} // namespace util
} // namespace base
19 changes: 0 additions & 19 deletions base/util/type_safety/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/nocompile.gni")

# Change this target's type to component if it starts to contain more than
# just headers. Header-only targets cannot be compiled to libraries, so it must
# remain a source_set for now.
source_set("type_safety") {
sources = [
"id_type.h",
"pass_key.h",
"strong_alias.h",
"token_type.h",
]

Expand All @@ -22,8 +18,6 @@ source_set("tests") {
testonly = true
sources = [
"id_type_unittest.cc",
"pass_key_unittest.cc",
"strong_alias_unittest.cc",
"token_type_unittest.cc",
]

Expand All @@ -32,16 +26,3 @@ source_set("tests") {
"//testing/gtest",
]
}

if (enable_nocompile_tests) {
nocompile_test("type_safety_nocompile_tests") {
sources = [ "pass_key_unittest.nc" ]

deps = [
":type_safety",
"//base:base_unittests_tasktraits",
"//base/test:run_all_unittests",
"//testing/gtest",
]
}
}
1 change: 1 addition & 0 deletions base/util/type_safety/DEPS
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include_rules = [
"-base",
"+base/types",
"+base/unguessable_token.h",
"+base/util/type_safety",
"-third_party",
Expand Down
9 changes: 5 additions & 4 deletions base/util/type_safety/id_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <cstdint>
#include <type_traits>

#include "base/util/type_safety/strong_alias.h"
#include "base/types/strong_alias.h"

namespace util {

Expand Down Expand Up @@ -51,7 +51,7 @@ template <typename TypeMarker,
typename WrappedType,
WrappedType kInvalidValue,
WrappedType kFirstGeneratedId = kInvalidValue + 1>
class IdType : public StrongAlias<TypeMarker, WrappedType> {
class IdType : public base::StrongAlias<TypeMarker, WrappedType> {
public:
static_assert(
std::is_unsigned<WrappedType>::value || kInvalidValue <= 0,
Expand All @@ -67,7 +67,7 @@ class IdType : public StrongAlias<TypeMarker, WrappedType> {
"invalid value so that the monotonically increasing "
"GenerateNextId method will never return the invalid value.");

using StrongAlias<TypeMarker, WrappedType>::StrongAlias;
using base::StrongAlias<TypeMarker, WrappedType>::StrongAlias;

// This class can be used to generate unique IdTypes. It keeps an internal
// counter that is continually increased by one every time an ID is generated.
Expand All @@ -88,7 +88,8 @@ class IdType : public StrongAlias<TypeMarker, WrappedType> {

// Default-construct in the null state.
constexpr IdType()
: StrongAlias<TypeMarker, WrappedType>::StrongAlias(kInvalidValue) {}
: base::StrongAlias<TypeMarker, WrappedType>::StrongAlias(kInvalidValue) {
}

constexpr bool is_null() const { return this->value() == kInvalidValue; }
constexpr explicit operator bool() const { return !is_null(); }
Expand Down
6 changes: 3 additions & 3 deletions base/util/type_safety/token_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#ifndef BASE_UTIL_TYPE_SAFETY_TOKEN_TYPE_H_
#define BASE_UTIL_TYPE_SAFETY_TOKEN_TYPE_H_

#include "base/types/strong_alias.h"
#include "base/unguessable_token.h"
#include "base/util/type_safety/strong_alias.h"

namespace util {

Expand All @@ -15,9 +15,9 @@ namespace util {
// not expose the concept of null tokens. If you need to indicate a null token,
// please use base::Optional<TokenType<...>>.
template <typename TypeMarker>
class TokenType : public StrongAlias<TypeMarker, base::UnguessableToken> {
class TokenType : public base::StrongAlias<TypeMarker, base::UnguessableToken> {
private:
using Super = StrongAlias<TypeMarker, base::UnguessableToken>;
using Super = base::StrongAlias<TypeMarker, base::UnguessableToken>;

public:
TokenType() : Super(base::UnguessableToken::Create()) {}
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/chrome_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5780,7 +5780,7 @@ content::XrIntegrationClient*
ChromeContentBrowserClient::GetXrIntegrationClient() {
if (!xr_integration_client_)
xr_integration_client_ = std::make_unique<vr::ChromeXrIntegrationClient>(
util::PassKey<ChromeContentBrowserClient>());
base::PassKey<ChromeContentBrowserClient>());
return xr_integration_client_.get();
}
#endif // BUILDFLAG(ENABLE_VR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "ui/base/idle/idle.h"

namespace chromeos {
class CrosSettings;
namespace system {
class StatisticsProvider;
}
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/lifetime/application_lifetime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "base/logging.h"
#include "base/process/process.h"
#include "base/process/process_handle.h"
#include "base/util/type_safety/strong_alias.h"
#include "base/types/strong_alias.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
Expand Down Expand Up @@ -122,7 +122,7 @@ bool g_send_stop_request_to_session_manager = false;

#if !defined(OS_ANDROID)
using IgnoreUnloadHandlers =
util::StrongAlias<class IgnoreUnloadHandlersTag, bool>;
base::StrongAlias<class IgnoreUnloadHandlersTag, bool>;

void AttemptRestartInternal(IgnoreUnloadHandlers ignore_unload_handlers) {
// TODO(beng): Can this use ProfileManager::GetLoadedProfiles instead?
Expand Down
Loading

0 comments on commit 796cde2

Please sign in to comment.