Skip to content

Commit

Permalink
Remove CR_DEFINE_STATIC_LOCAL.
Browse files Browse the repository at this point in the history
BUG=893317

Cq-Include-Trybots: luci.chromium.try:ios-simulator-cronet;luci.chromium.try:ios-simulator-full-configs;luci.chromium.try:win_optional_gpu_tests_rel
Change-Id: I8920fd240a55a32ee5dd27e049cc528193644dcf
Reviewed-on: https://chromium-review.googlesource.com/c/1273809
Commit-Queue: Avi Drissman <avi@chromium.org>
Reviewed-by: Sami Kyöstilä <skyostil@chromium.org>
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: Mitsuru Oshima <oshima@chromium.org>
Reviewed-by: Wei Li <weili@chromium.org>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Ken Rockot <rockot@chromium.org>
Reviewed-by: Bo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#599267}
  • Loading branch information
Avi Drissman authored and Commit Bot committed Oct 12, 2018
1 parent e26bef0 commit 94ea9d3
Show file tree
Hide file tree
Showing 22 changed files with 140 additions and 160 deletions.
9 changes: 5 additions & 4 deletions android_webview/browser/aw_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "base/location.h"
#include "base/macros.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/no_destructor.h"
#include "base/pickle.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string16.h"
Expand Down Expand Up @@ -116,13 +117,13 @@ namespace {
bool g_should_download_favicons = false;

std::string* g_locale() {
CR_DEFINE_STATIC_LOCAL(std::string, locale, ());
return &locale;
static base::NoDestructor<std::string> locale;
return locale.get();
}

std::string* g_locale_list() {
CR_DEFINE_STATIC_LOCAL(std::string, locale_list, ());
return &locale_list;
static base::NoDestructor<std::string> locale_list;
return locale_list.get();
}

const void* const kAwContentsUserDataKey = &kAwContentsUserDataKey;
Expand Down
18 changes: 9 additions & 9 deletions android_webview/browser/aw_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/macros.h"
#include "base/no_destructor.h"
#include "base/supports_user_data.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
Expand All @@ -36,15 +37,14 @@ namespace {

void PopulateFixedRendererPreferences(RendererPreferences* prefs) {
// TODO(boliu): Deduplicate with chrome/ code.
CR_DEFINE_STATIC_LOCAL(
const gfx::FontRenderParams, params,
(gfx::GetFontRenderParams(gfx::FontRenderParamsQuery(), NULL)));
prefs->should_antialias_text = params.antialiasing;
prefs->use_subpixel_positioning = params.subpixel_positioning;
prefs->hinting = params.hinting;
prefs->use_autohinter = params.autohinter;
prefs->use_bitmaps = params.use_bitmaps;
prefs->subpixel_rendering = params.subpixel_rendering;
static const base::NoDestructor<gfx::FontRenderParams> params(
gfx::GetFontRenderParams(gfx::FontRenderParamsQuery(), nullptr));
prefs->should_antialias_text = params->antialiasing;
prefs->use_subpixel_positioning = params->subpixel_positioning;
prefs->hinting = params->hinting;
prefs->use_autohinter = params->autohinter;
prefs->use_bitmaps = params->use_bitmaps;
prefs->subpixel_rendering = params->subpixel_rendering;
}

void PopulateFixedWebPreferences(WebPreferences* web_prefs) {
Expand Down
5 changes: 3 additions & 2 deletions ash/public/cpp/app_list/app_list_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "ash/public/cpp/app_list/app_list_features.h"
#include "base/macros.h"
#include "base/no_destructor.h"
#include "ui/gfx/color_palette.h"

namespace app_list {
Expand Down Expand Up @@ -101,8 +102,8 @@ AppListConfig::~AppListConfig() = default;

// static
const AppListConfig& AppListConfig::instance() {
CR_DEFINE_STATIC_LOCAL(AppListConfig, instance, ());
return instance;
static const base::NoDestructor<AppListConfig> instance;
return *instance;
}

int AppListConfig::GetPreferredIconDimension(
Expand Down
23 changes: 0 additions & 23 deletions base/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,4 @@ template<typename T>
inline void ignore_result(const T&) {
}

namespace base {

// Use these to declare and define a static local variable (static T;) so that
// it is leaked so that its destructors are not called at exit. This is
// thread-safe.
//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DEPRECATED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Please don't use this macro. Use a function-local static of type
// base::NoDestructor<T> instead:
//
// Factory& Factory::GetInstance() {
// static base::NoDestructor<Factory> instance;
// return *instance;
// }
//
// Removal of this macro is tracked in https://crbug.com/893317.
//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \
static type& name = *new type arguments

} // base

#endif // BASE_MACROS_H_
3 changes: 1 addition & 2 deletions base/memory/shared_memory_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,7 @@ bool SharedMemory::FilePathForMemoryName(const std::string& mem_name,
#else
static const char kShmem[] = "org.chromium.Chromium.shmem.";
#endif
CR_DEFINE_STATIC_LOCAL(const std::string, name_base, (kShmem));
*path = temp_dir.AppendASCII(name_base + mem_name);
*path = temp_dir.AppendASCII(kShmem + mem_name);
return true;
}

Expand Down
1 change: 0 additions & 1 deletion base/no_destructor.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ namespace base {
// indirection and a malloc. Also note that since C++11 static local variable
// initialization is thread-safe and so is this pattern. Code should prefer to
// use NoDestructor<T> over:
// - The CR_DEFINE_STATIC_LOCAL() helper macro.
// - A function scoped static T* or T& that is dynamically initialized.
// - A global base::LazyInstance<T>.
//
Expand Down
15 changes: 7 additions & 8 deletions base/test/test_suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/process/memory.h"
Expand Down Expand Up @@ -135,18 +136,16 @@ class CheckForLeakedGlobals : public testing::EmptyTestEventListener {
DISALLOW_COPY_AND_ASSIGN(CheckForLeakedGlobals);
};

std::string GetProfileName() {
static const char kDefaultProfileName[] = "test-profile-{pid}";
CR_DEFINE_STATIC_LOCAL(std::string, profile_name, ());
if (profile_name.empty()) {
const std::string& GetProfileName() {
static const base::NoDestructor<std::string> profile_name([]() {
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kProfilingFile))
profile_name = command_line.GetSwitchValueASCII(switches::kProfilingFile);
return command_line.GetSwitchValueASCII(switches::kProfilingFile);
else
profile_name = std::string(kDefaultProfileName);
}
return profile_name;
return std::string("test-profile-{pid}");
}());
return *profile_name;
}

void InitializeLogging() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <memory>

#include "base/no_destructor.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "extensions/common/image_util.h"
Expand All @@ -20,11 +21,11 @@ namespace errors = manifest_errors;
namespace {

const AppIconColorInfo& GetAppIconColorInfo(const Extension* extension) {
CR_DEFINE_STATIC_LOCAL(const AppIconColorInfo, fallback, ());
static const base::NoDestructor<AppIconColorInfo> fallback;

AppIconColorInfo* info = static_cast<AppIconColorInfo*>(
extension->GetManifestData(keys::kAppIconColor));
return info ? *info : fallback;
return info ? *info : *fallback;
}

} // namespace
Expand Down
5 changes: 3 additions & 2 deletions chrome/common/net/net_resource_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>

#include "base/i18n/rtl.h"
#include "base/no_destructor.h"
#include "base/strings/string_piece.h"
#include "base/values.h"
#include "chrome/grit/chromium_strings.h"
Expand Down Expand Up @@ -57,10 +58,10 @@ struct LazyDirectoryListerCacher {
namespace chrome_common_net {

base::StringPiece NetResourceProvider(int key) {
CR_DEFINE_STATIC_LOCAL(LazyDirectoryListerCacher, lazy_dir_lister, ());
static base::NoDestructor<LazyDirectoryListerCacher> lazy_dir_lister;

if (IDR_DIR_HEADER_HTML == key)
return base::StringPiece(lazy_dir_lister.html_data);
return base::StringPiece(lazy_dir_lister->html_data);

return ui::ResourceBundle::GetSharedInstance().GetRawDataResource(key);
}
Expand Down
15 changes: 9 additions & 6 deletions chrome/common/profiling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/macros.h"
#include "base/no_destructor.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_util.h"
#include "base/threading/thread.h"
Expand All @@ -21,23 +22,25 @@
namespace {

std::string GetProfileName() {
static const char kDefaultProfileName[] = "chrome-profile-{type}-{pid}";
CR_DEFINE_STATIC_LOCAL(std::string, profile_name, ());
static base::NoDestructor<std::string> profile_name([]() {
std::string profile_name;

if (profile_name.empty()) {
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kProfilingFile))
profile_name = command_line.GetSwitchValueASCII(switches::kProfilingFile);
else
profile_name = std::string(kDefaultProfileName);
profile_name = std::string("chrome-profile-{type}-{pid}");
std::string process_type =
command_line.GetSwitchValueASCII(switches::kProcessType);
std::string type = process_type.empty() ?
std::string("browser") : std::string(process_type);
base::ReplaceSubstringsAfterOffset(&profile_name, 0, "{type}", type);
}
return profile_name;

return profile_name;
}());

return *profile_name;
}

void FlushProfilingData(base::Thread* thread) {
Expand Down
5 changes: 3 additions & 2 deletions chrome/renderer/chrome_content_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/macros.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics_action.h"
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
Expand Down Expand Up @@ -747,8 +748,8 @@ ChromeContentRendererClient::GetPluginInfoHost() {
~PluginInfoHostHolder() {}
chrome::mojom::PluginInfoHostAssociatedPtr plugin_info_host;
};
CR_DEFINE_STATIC_LOCAL(PluginInfoHostHolder, holder, ());
return holder.plugin_info_host;
static base::NoDestructor<PluginInfoHostHolder> holder;
return holder->plugin_info_host;
}

// static
Expand Down
7 changes: 4 additions & 3 deletions chrome/renderer/net_benchmarking_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "chrome/renderer/net_benchmarking_extension.h"

#include "base/no_destructor.h"
#include "chrome/common/net_benchmarking.mojom.h"
#include "content/public/common/service_names.mojom.h"
#include "content/public/renderer/render_thread.h"
Expand Down Expand Up @@ -74,9 +75,9 @@ class NetBenchmarkingWrapper : public v8::Extension {
}

static chrome::mojom::NetBenchmarking& GetNetBenchmarking() {
CR_DEFINE_STATIC_LOCAL(chrome::mojom::NetBenchmarkingPtr, net_benchmarking,
(ConnectToBrowser()));
return *net_benchmarking;
static base::NoDestructor<chrome::mojom::NetBenchmarkingPtr>
net_benchmarking(ConnectToBrowser());
return **net_benchmarking;
}

static chrome::mojom::NetBenchmarkingPtr ConnectToBrowser() {
Expand Down
15 changes: 5 additions & 10 deletions device/vr/openvr/openvr_render_loop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,13 @@ void OpenVRRenderLoop::OnSessionStart() {
openvr_->GetCompositor()->SuspendRendering(false);

// Measure the VrViewerType we are presenting with.
using ViewerMap = std::map<std::string, VrViewerType>;
CR_DEFINE_STATIC_LOCAL(ViewerMap, viewer_types,
({
{"Oculus Rift CV1", VrViewerType::OPENVR_RIFT_CV1},
{"Vive MV", VrViewerType::OPENVR_VIVE},
}));
VrViewerType type = VrViewerType::OPENVR_UNKNOWN;
std::string model =
GetOpenVRString(openvr_->GetSystem(), vr::Prop_ModelNumber_String);
auto it = viewer_types.find(model);
if (it != viewer_types.end())
type = it->second;
VrViewerType type = VrViewerType::OPENVR_UNKNOWN;
if (model == "Oculus Rift CV1")
type = VrViewerType::OPENVR_RIFT_CV1;
else if (model == "Vive MV")
type = VrViewerType::OPENVR_VIVE;

base::UmaHistogramSparse("VRViewerType", static_cast<int>(type));
}
Expand Down
4 changes: 2 additions & 2 deletions extensions/browser/api/web_request/web_request_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -885,8 +885,8 @@ ExtensionWebRequestEventRouter::RequestFilter::~RequestFilter() {

// static
ExtensionWebRequestEventRouter* ExtensionWebRequestEventRouter::GetInstance() {
CR_DEFINE_STATIC_LOCAL(ExtensionWebRequestEventRouter, instance, ());
return &instance;
static base::NoDestructor<ExtensionWebRequestEventRouter> instance;
return instance.get();
}

ExtensionWebRequestEventRouter::ExtensionWebRequestEventRouter()
Expand Down
2 changes: 2 additions & 0 deletions extensions/browser/api/web_request/web_request_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "content/public/browser/content_browser_client.h"
Expand Down Expand Up @@ -469,6 +470,7 @@ class ExtensionWebRequestEventRouter {

private:
friend class WebRequestAPI;
friend class base::NoDestructor<ExtensionWebRequestEventRouter>;
FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest,
BlockingEventPrecedenceRedirect);
FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest,
Expand Down
5 changes: 3 additions & 2 deletions extensions/common/extension_l10n_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/files/file_util.h"
#include "base/json/json_file_value_serializer.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
Expand Down Expand Up @@ -101,8 +102,8 @@ bool LocalizeManifestListValue(const std::string& key,
}

std::string& GetProcessLocale() {
CR_DEFINE_STATIC_LOCAL(std::string, locale, ());
return locale;
static base::NoDestructor<std::string> locale;
return *locale;
}

} // namespace
Expand Down
29 changes: 12 additions & 17 deletions extensions/common/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,6 @@ std::vector<base::FilePath> FindPrivateKeyFiles(

bool CheckForIllegalFilenames(const base::FilePath& extension_path,
std::string* error) {
// Reserved underscore names.
static const base::FilePath::CharType* const reserved_names[] = {
kLocaleFolder, kPlatformSpecificFolder, FILE_PATH_LITERAL("__MACOSX"),
};
CR_DEFINE_STATIC_LOCAL(
std::set<base::FilePath::StringType>,
reserved_underscore_names,
(reserved_names, reserved_names + arraysize(reserved_names)));

// Enumerate all files and directories in the extension root.
// There is a problem when using pattern "_*" with FileEnumerator, so we have
// to cheat with find_first_of and match all.
Expand All @@ -363,17 +354,21 @@ bool CheckForIllegalFilenames(const base::FilePath& extension_path,
while (!(file = all_files.Next()).empty()) {
base::FilePath::StringType filename = file.BaseName().value();

// Skip all that don't start with "_".
// Skip all filenames that don't start with "_".
if (filename.find_first_of(FILE_PATH_LITERAL("_")) != 0)
continue;
if (reserved_underscore_names.find(filename) ==
reserved_underscore_names.end()) {
*error = base::StringPrintf(
"Cannot load extension with file or directory name %s. "
"Filenames starting with \"_\" are reserved for use by the system.",
file.BaseName().AsUTF8Unsafe().c_str());
return false;

// Some filenames are special and allowed to start with "_".
if (filename == kLocaleFolder || filename == kPlatformSpecificFolder ||
filename == FILE_PATH_LITERAL("__MACOSX")) {
continue;
}

*error = base::StringPrintf(
"Cannot load extension with file or directory name %s. "
"Filenames starting with \"_\" are reserved for use by the system.",
file.BaseName().AsUTF8Unsafe().c_str());
return false;
}

return true;
Expand Down
Loading

0 comments on commit 94ea9d3

Please sign in to comment.