Refactor and cleanup codes. No functional changes.

This commit is contained in:
KongQun Yang
2019-01-23 15:16:31 -08:00
parent 84f66d2320
commit 93265ab9d1
207 changed files with 14893 additions and 3332 deletions

43
util/BUILD Normal file
View File

@@ -0,0 +1,43 @@
################################################################################
# Copyright 2017 Google LLC.
#
# This software is licensed under the terms defined in the Widevine Master
# License Agreement. For a copy of this agreement, please contact
# widevine-licensing@google.com.
################################################################################
package(
default_visibility = ["//visibility:public"],
)
filegroup(
name = "binary_release_files",
srcs = [
"error_space.h",
],
)
cc_library(
name = "error_space",
hdrs = ["error_space.h"],
)
cc_library(
name = "proto_status",
hdrs = ["proto_status.h"],
deps = [
"@protobuf_repo//:protobuf",
"//util:error_space",
],
)
cc_test(
name = "error_space_test",
srcs = ["error_space_test.cc"],
deps = [
":error_space",
"//testing:gunit_main",
],
)

19
util/endian/BUILD Normal file
View File

@@ -0,0 +1,19 @@
################################################################################
# Copyright 2017 Google LLC.
#
# This software is licensed under the terms defined in the Widevine Master
# License Agreement. For a copy of this agreement, please contact
# widevine-licensing@google.com.
################################################################################
package(
default_visibility = ["//visibility:public"],
)
cc_library(
name = "endian",
hdrs = [
"endian.h",
],
)

49
util/endian/endian.h Normal file
View File

@@ -0,0 +1,49 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 2017 Google LLC.
//
// This software is licensed under the terms defined in the Widevine Master
// License Agreement. For a copy of this agreement, please contact
// widevine-licensing@google.com.
////////////////////////////////////////////////////////////////////////////////
#ifndef UTIL_ENDIAN_ENDIAN_H_
#define UTIL_ENDIAN_ENDIAN_H_
#include <netinet/in.h>
#include <cstdint>
namespace widevine {
// Utilities to convert numbers between the current hosts's native byte
// order and big-endian byte order (same as network byte order)
class BigEndian {
public:
static uint32_t Load32(const char* data) {
return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
}
static uint64_t Load64(const char* data) {
return ((static_cast<uint64_t>(Load32(data)) << 32) | Load32(data + 4));
}
static void Store32(void* p, uint32_t data) {
for (int i = 0; i < 4; ++i)
(reinterpret_cast<uint8_t*>(p))[i] = (data >> ((3 - i) * 8)) & 0xFF;
}
}; // BigEndian
inline uint64_t gntohll(uint64_t n) {
uint64_t retval;
#if __BYTE_ORDER == __BIG_ENDIAN
retval = n;
#else
retval = ((uint64_t) ntohl(n & 0xFFFFFFFFLLU)) << 32;
retval |= ntohl((n & 0xFFFFFFFF00000000LLU) >> 32);
#endif
return(retval);
}
} // namespace widevine
#endif // UTIL_ENDIAN_ENDIAN_H_

83
util/error_space.h Normal file
View File

@@ -0,0 +1,83 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 2017 Google LLC.
//
// This software is licensed under the terms defined in the Widevine Master
// License Agreement. For a copy of this agreement, please contact
// widevine-licensing@google.com.
////////////////////////////////////////////////////////////////////////////////
#ifndef UTIL_ERROR_SPACE_H_
#define UTIL_ERROR_SPACE_H_
#include <string>
namespace widevine {
namespace util {
class ErrorSpace {
public:
std::string SpaceName() const { return space_name_func_(this); }
std::string String(int code) const { return code_to_string_func_(this, code); }
protected:
// typedef instead of using statements for SWIG compatibility.
typedef std::string (*SpaceNameFunc)(const ErrorSpace* space);
typedef std::string (*CodeToStringFunc)(const ErrorSpace* space, int code);
constexpr ErrorSpace(SpaceNameFunc space_name_func,
CodeToStringFunc code_to_string_func)
: space_name_func_(space_name_func),
code_to_string_func_(code_to_string_func) {}
private:
const SpaceNameFunc space_name_func_;
const CodeToStringFunc code_to_string_func_;
};
// Manages creation of error space subclasses.
template <typename T>
class ErrorSpaceImpl : public ErrorSpace {
public:
constexpr ErrorSpaceImpl()
: ErrorSpace(&ErrorSpaceImpl::SpaceNameImpl,
&ErrorSpaceImpl::CodeToStringImpl) {}
// Returns the canonical instance of the `T` error space.
static constexpr const T* Get();
private:
// These functions adapt the stateful implementation that takes a space
// pointer to stateless static methods, so that clients of ErrorSpaceImpl are
// safe to have constexpr global instances.
static std::string SpaceNameImpl(const ErrorSpace* /*space*/) {
return T::space_name();
}
static std::string CodeToStringImpl(const ErrorSpace* /*space*/, int code) {
return T::code_to_string(code);
}
};
namespace internal {
// Provides a global constexpr instance of the error space `T`.
// We need the indirection because ErrorSpaceImpl can't declare constexpr
// instances of T since it is not yet fully declared.
template <typename T>
struct ErrorSpaceInstance {
static constexpr T value = {};
};
template <typename T>
constexpr T ErrorSpaceInstance<T>::value;
} // namespace internal
template <typename T>
constexpr const T* ErrorSpaceImpl<T>::Get() {
return &internal::ErrorSpaceInstance<T>::value;
}
} // namespace util
} // namespace widevine
#endif // UTIL_ERROR_SPACE_H_

33
util/error_space_test.cc Normal file
View File

@@ -0,0 +1,33 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 2017 Google LLC.
//
// This software is licensed under the terms defined in the Widevine Master
// License Agreement. For a copy of this agreement, please contact
// widevine-licensing@google.com.
////////////////////////////////////////////////////////////////////////////////
#include "util/error_space.h"
#include "testing/gunit.h"
namespace widevine {
namespace util {
namespace {
class Space1 : public util::ErrorSpaceImpl<Space1> {
public:
static std::string space_name() { return "Space1"; }
static std::string code_to_string(int code) {
return "Test" + std::to_string(code);
}
};
TEST(ErrorSpaceTest, Basic) {
const ErrorSpace* space1 = Space1::Get();
EXPECT_EQ("Space1", space1->SpaceName());
EXPECT_EQ(Space1::code_to_string(23), space1->String(23));
}
} // namespace
} // namespace util
} // namespace widevine

19
util/gtl/BUILD Normal file
View File

@@ -0,0 +1,19 @@
################################################################################
# Copyright 2017 Google LLC.
#
# This software is licensed under the terms defined in the Widevine Master
# License Agreement. For a copy of this agreement, please contact
# widevine-licensing@google.com.
################################################################################
package(
default_visibility = ["//visibility:public"],
)
cc_library(
name = "map_util",
hdrs = [
"map_util.h",
],
)

58
util/gtl/map_util.h Normal file
View File

@@ -0,0 +1,58 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 2017 Google LLC.
//
// This software is licensed under the terms defined in the Widevine Master
// License Agreement. For a copy of this agreement, please contact
// widevine-licensing@google.com.
////////////////////////////////////////////////////////////////////////////////
#ifndef UTIL_GTL_MAP_UTIL_H_
#define UTIL_GTL_MAP_UTIL_H_
namespace widevine {
namespace gtl {
template <class Collection>
const typename Collection::value_type::second_type& FindWithDefault(
const Collection& collection,
const typename Collection::value_type::first_type& key,
const typename Collection::value_type::second_type& value) {
typename Collection::const_iterator it = collection.find(key);
if (it == collection.end()) {
return value;
}
return it->second;
}
template <class Collection>
typename Collection::value_type::second_type* FindOrNull(
const Collection& collection,
const typename Collection::value_type::first_type& key) {
typename Collection::iterator it = collection.find(key);
if (it == collection.end()) {
return nullptr;
}
return &it->second;
}
template <class Collection>
typename Collection::value_type::second_type* FindOrNull(
Collection& collection, // NOLINT
const typename Collection::value_type::first_type& key) {
typename Collection::iterator it = collection.find(key);
if (it == collection.end()) {
return nullptr;
}
return &it->second;
}
template <class C, class K>
bool ContainsKey(const C& collection, const K& key) {
typename C::const_iterator it = collection.find(key);
return it != collection.end();
}
} // namespace gtl
} // namespace widevine
#endif // UTIL_GTL_MAP_UTIL_H_

39
util/proto_status.h Normal file
View File

@@ -0,0 +1,39 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 2017 Google LLC.
//
// This software is licensed under the terms defined in the Widevine Master
// License Agreement. For a copy of this agreement, please contact
// widevine-licensing@google.com.
////////////////////////////////////////////////////////////////////////////////
#ifndef UTIL_PROTO_STATUS_H_
#define UTIL_PROTO_STATUS_H_
#include "google/protobuf/descriptor.h"
#include "google/protobuf/generated_enum_reflection.h"
#include "util/error_space.h"
namespace widevine {
namespace util {
template <typename T>
class ProtoEnumErrorSpace
: public util::ErrorSpaceImpl<ProtoEnumErrorSpace<T>> {
public:
static std::string space_name() {
return google::protobuf::GetEnumDescriptor<T>()->full_name();
}
static std::string code_to_string(int code) {
const google::protobuf::EnumValueDescriptor* v =
google::protobuf::GetEnumDescriptor<T>()->FindValueByNumber(code);
if (v) return v->name();
return std::to_string(code);
}
};
} // namespace util
} // namespace widevine
#endif // UTIL_PROTO_STATUS_H_

64
util/proto_status_test.cc Normal file
View File

@@ -0,0 +1,64 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 2007 Google LLC.
//
// This software is licensed under the terms defined in the Widevine Master
// License Agreement. For a copy of this agreement, please contact
// widevine-licensing@google.com.
////////////////////////////////////////////////////////////////////////////////
#include "util/proto_status.h"
#include "testing/gunit.h"
#include "common/status.h"
#include "protos/public/errors.pb.h"
namespace widevine {
namespace util {
TEST(StatusTest, PROVIDER_ID_MISMATCH_Status) {
Status status(ProtoEnumErrorSpace<Errors>::Get(), PROVIDER_ID_MISMATCH,
"provider_id_mismatch");
EXPECT_EQ("Errors::PROVIDER_ID_MISMATCH: provider_id_mismatch",
status.ToString());
}
TEST(StatusTest, PROVIDER_ID_MISMATCH_Status2) {
Status status(static_cast<error::StatusCode>(PROVIDER_ID_MISMATCH),
"provider_id_mismatch");
EXPECT_EQ("Errors::157: provider_id_mismatch", status.ToString());
}
TEST(StatusTest, Same) {
Status status1(ProtoEnumErrorSpace<Errors>::Get(), PROVIDER_ID_MISMATCH,
"provider_id_mismatch");
Status status2(ProtoEnumErrorSpace<Errors>::Get(), PROVIDER_ID_MISMATCH,
"provider_id_mismatch");
EXPECT_EQ(status1, status2);
}
TEST(StatusTest, ErrorMessageMismatch) {
Status status1(ProtoEnumErrorSpace<Errors>::Get(), PROVIDER_ID_MISMATCH,
"provider_id_mismatch");
Status status2(ProtoEnumErrorSpace<Errors>::Get(), PROVIDER_ID_MISMATCH,
"this is a provider_id_mismatch error");
EXPECT_NE(status1, status2);
}
TEST(StatusTest, NotTheSameStatus) {
Status status1(ProtoEnumErrorSpace<Errors>::Get(), PROVIDER_ID_MISMATCH,
"provider_id_mismatch");
Status status2(ProtoEnumErrorSpace<Errors>::Get(), MISSING_CLIENT_ID,
"missing client id");
EXPECT_NE(status1, status2);
}
TEST(StatusTest, NotTheSameErrorSpace) {
Status status1(ProtoEnumErrorSpace<Errors>::Get(), PROVIDER_ID_MISMATCH,
"provider_id_mismatch");
Status status2(static_cast<error::StatusCode>(PROVIDER_ID_MISMATCH),
"this is a provider_id_mismatch error");
EXPECT_NE(status1, status2);
}
} // namespace util
} // namespace widevine

31
util/random/BUILD Normal file
View File

@@ -0,0 +1,31 @@
################################################################################
# Copyright 2017 Google LLC.
#
# This software is licensed under the terms defined in the Widevine Master
# License Agreement. For a copy of this agreement, please contact
# widevine-licensing@google.com.
################################################################################
package(
default_visibility = ["//visibility:public"],
)
cc_library(
name = "global_id",
hdrs = [
"global_id.h",
],
deps = [
"//external:openssl",
],
)
cc_test(
name = "global_id_test",
srcs = ["global_id_test.cc"],
deps = [
":global_id",
"//base",
"//testing:gunit_main",
],
)

30
util/random/global_id.h Normal file
View File

@@ -0,0 +1,30 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 2017 Google LLC.
//
// This software is licensed under the terms defined in the Widevine Master
// License Agreement. For a copy of this agreement, please contact
// widevine-licensing@google.com.
////////////////////////////////////////////////////////////////////////////////
#ifndef UTIL_RANDOM_GLOBAL_ID_H_
#define UTIL_RANDOM_GLOBAL_ID_H_
#include "openssl/rand.h"
namespace widevine {
namespace util {
namespace random {
static uint64_t NewGlobalID() {
static __thread uint64_t val = 0;
if (val == 0) {
RAND_bytes(reinterpret_cast<uint8_t*>(&val), sizeof(val));
}
return val++;
}
} // namespace random
} // namespace util
} // namespace widevine
#endif // UTIL_RANDOM_GLOBAL_ID_H_

View File

@@ -0,0 +1,27 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 2017 Google LLC.
//
// This software is licensed under the terms defined in the Widevine Master
// License Agreement. For a copy of this agreement, please contact
// widevine-licensing@google.com.
////////////////////////////////////////////////////////////////////////////////
#include "util/random/global_id.h"
#include "glog/logging.h"
#include "testing/gunit.h"
namespace widevine {
namespace util {
namespace random {
TEST(GlobalIDTest, NewGlobalID) {
uint64_t id = NewGlobalID();
LOG(INFO) << "NewGlobalID " << id;
EXPECT_EQ(id + 1, NewGlobalID());
EXPECT_EQ(id + 2, NewGlobalID());
}
} // namespace random
} // namespace util
} // namespace widevine