Skip to content

Commit

Permalink
Remove LazyInstance usage from media/
Browse files Browse the repository at this point in the history
We have thread safe statics now, there's no reason to use lazy
instance anymore.

BUG=686866
TEST=compiles.
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:linux_optional_gpu_tests_rel;master.tryserver.chromium.mac:mac_optional_gpu_tests_rel;master.tryserver.chromium.win:win_optional_gpu_tests_rel

Review-Url: https://codereview.chromium.org/2668813002
Cr-Commit-Position: refs/heads/master@{#449830}
  • Loading branch information
dalecurtis authored and Commit bot committed Feb 11, 2017
1 parent c4e83bd commit c3af509
Show file tree
Hide file tree
Showing 40 changed files with 321 additions and 401 deletions.
11 changes: 2 additions & 9 deletions content/browser/media/capture/audio_mirroring_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,13 @@

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/lazy_instance.h"

namespace content {

namespace {

base::LazyInstance<AudioMirroringManager>::Leaky g_audio_mirroring_manager =
LAZY_INSTANCE_INITIALIZER;

} // namespace

// static
AudioMirroringManager* AudioMirroringManager::GetInstance() {
return g_audio_mirroring_manager.Pointer();
static AudioMirroringManager* manager = new AudioMirroringManager();
return manager;
}

AudioMirroringManager::AudioMirroringManager() {
Expand Down
6 changes: 2 additions & 4 deletions content/browser/media/cdm_registry_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@

namespace content {

static base::LazyInstance<CdmRegistryImpl>::Leaky g_cdm_registry =
LAZY_INSTANCE_INITIALIZER;

// static
CdmRegistry* CdmRegistry::GetInstance() {
return CdmRegistryImpl::GetInstance();
}

// static
CdmRegistryImpl* CdmRegistryImpl::GetInstance() {
return g_cdm_registry.Pointer();
static CdmRegistryImpl* registry = new CdmRegistryImpl();
return registry;
}

CdmRegistryImpl::CdmRegistryImpl() {}
Expand Down
3 changes: 1 addition & 2 deletions content/browser/media/cdm_registry_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <vector>

#include "base/lazy_instance.h"
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "content/common/content_export.h"
#include "content/public/browser/cdm_registry.h"
Expand All @@ -27,7 +27,6 @@ class CONTENT_EXPORT CdmRegistryImpl : NON_EXPORTED_BASE(public CdmRegistry) {
const std::vector<CdmInfo>& GetAllRegisteredCdms() override;

private:
friend struct base::DefaultLazyInstanceTraits<CdmRegistryImpl>;
friend class CdmRegistryImplTest;

CdmRegistryImpl();
Expand Down
6 changes: 2 additions & 4 deletions content/browser/media/media_internals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@

namespace {

static base::LazyInstance<content::MediaInternals>::Leaky g_media_internals =
LAZY_INSTANCE_INITIALIZER;

base::string16 SerializeUpdate(const std::string& function,
const base::Value* value) {
return content::WebUI::GetJavascriptCall(
Expand Down Expand Up @@ -596,7 +593,8 @@ void MediaInternals::MediaInternalsUMAHandler::OnProcessTerminated(
}

MediaInternals* MediaInternals::GetInstance() {
return g_media_internals.Pointer();
static content::MediaInternals* internals = new content::MediaInternals();
return internals;
}

MediaInternals::MediaInternals()
Expand Down
2 changes: 0 additions & 2 deletions content/browser/media/media_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include "base/callback_forward.h"
#include "base/compiler_specific.h"
#include "base/lazy_instance.h"
#include "base/macros.h"
#include "base/strings/string16.h"
#include "base/synchronization/lock.h"
Expand Down Expand Up @@ -96,7 +95,6 @@ class CONTENT_EXPORT MediaInternals

friend class AudioLogImpl;
friend class MediaInternalsTest;
friend struct base::DefaultLazyInstanceTraits<MediaInternals>;

MediaInternals();

Expand Down
11 changes: 6 additions & 5 deletions content/browser/media/media_web_contents_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <memory>

#include "base/lazy_instance.h"
#include "build/build_config.h"
#include "content/browser/media/audible_metrics.h"
#include "content/browser/media/audio_stream_monitor.h"
Expand All @@ -21,8 +20,10 @@ namespace content {

namespace {

static base::LazyInstance<AudibleMetrics>::Leaky g_audible_metrics =
LAZY_INSTANCE_INITIALIZER;
AudibleMetrics* GetAudibleMetrics() {
static AudibleMetrics* metrics = new AudibleMetrics();
return metrics;
}

} // anonymous namespace

Expand All @@ -33,7 +34,7 @@ MediaWebContentsObserver::MediaWebContentsObserver(WebContents* web_contents)
MediaWebContentsObserver::~MediaWebContentsObserver() {}

void MediaWebContentsObserver::WebContentsDestroyed() {
g_audible_metrics.Get().UpdateAudibleWebContentsState(web_contents(), false);
GetAudibleMetrics()->UpdateAudibleWebContentsState(web_contents(), false);
}

void MediaWebContentsObserver::RenderFrameDeleted(
Expand All @@ -53,7 +54,7 @@ void MediaWebContentsObserver::MaybeUpdateAudibleState() {
audio_power_save_blocker_.reset();
}

g_audible_metrics.Get().UpdateAudibleWebContentsState(
GetAudibleMetrics()->UpdateAudibleWebContentsState(
web_contents(), audio_stream_monitor->IsCurrentlyAudible());
}

Expand Down
10 changes: 6 additions & 4 deletions content/renderer/media/recorder/video_track_recorder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ class CodecEnumerator {
DISALLOW_COPY_AND_ASSIGN(CodecEnumerator);
};

static base::LazyInstance<CodecEnumerator>::Leaky g_codec_enumerator =
LAZY_INSTANCE_INITIALIZER;
CodecEnumerator* GetCodecEnumerator() {
static CodecEnumerator* enumerator = new CodecEnumerator();
return enumerator;
}

CodecEnumerator::CodecEnumerator() {
#if defined(OS_CHROMEOS)
Expand Down Expand Up @@ -1118,7 +1120,7 @@ void H264Encoder::ConfigureEncoderOnEncodingTaskRunner(const gfx::Size& size) {

// static
VideoTrackRecorder::CodecId VideoTrackRecorder::GetPreferredCodecId() {
return g_codec_enumerator.Get().GetPreferredCodecId();
return GetCodecEnumerator()->GetPreferredCodecId();
}

VideoTrackRecorder::VideoTrackRecorder(
Expand Down Expand Up @@ -1190,7 +1192,7 @@ void VideoTrackRecorder::InitializeEncoder(

const gfx::Size& input_size = frame->visible_rect().size();
const auto& vea_supported_profile =
g_codec_enumerator.Get().CodecIdToVEAProfile(codec);
GetCodecEnumerator()->CodecIdToVEAProfile(codec);
if (vea_supported_profile != media::VIDEO_CODEC_PROFILE_UNKNOWN &&
input_size.width() >= kVEAEncoderMinResolutionWidth &&
input_size.height() >= kVEAEncoderMinResolutionHeight) {
Expand Down
17 changes: 5 additions & 12 deletions content/renderer/media/render_media_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@

#include "content/renderer/media/render_media_client.h"

#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/time/default_tick_clock.h"
#include "content/public/common/content_client.h"
#include "content/public/renderer/content_renderer_client.h"

namespace content {

static base::LazyInstance<RenderMediaClient>::Leaky g_render_media_client =
LAZY_INSTANCE_INITIALIZER;

void RenderMediaClient::Initialize() {
g_render_media_client.Get();
GetInstance();
}

RenderMediaClient::RenderMediaClient()
Expand Down Expand Up @@ -120,13 +116,10 @@ void RenderMediaClient::SetTickClockForTesting(
tick_clock_.swap(tick_clock);
}

// This functions is for testing purpose only. The declaration in the
// header file is guarded by "#if defined(UNIT_TEST)" so that it can be used
// by tests but not non-test code. However, this .cc file is compiled as part of
// "content" where "UNIT_TEST" is not defined. So we need to specify
// "CONTENT_EXPORT" here again so that it is visible to tests.
CONTENT_EXPORT RenderMediaClient* GetRenderMediaClientInstanceForTesting() {
return g_render_media_client.Pointer();
// static
RenderMediaClient* RenderMediaClient::GetInstance() {
static RenderMediaClient* client = new RenderMediaClient();
return client;
}

} // namespace content
12 changes: 3 additions & 9 deletions content/renderer/media/render_media_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#include <memory>

#include "base/lazy_instance.h"
#include "base/macros.h"
#include "base/threading/thread_checker.h"
#include "base/time/tick_clock.h"
#include "base/time/time.h"
Expand Down Expand Up @@ -39,11 +37,13 @@ class CONTENT_EXPORT RenderMediaClient : public media::MediaClient {
void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock);

private:
friend struct base::DefaultLazyInstanceTraits<RenderMediaClient>;
friend class RenderMediaClientTest;

RenderMediaClient();
~RenderMediaClient() override;

static RenderMediaClient* GetInstance();

// Makes sure all methods are called from the same thread.
base::ThreadChecker thread_checker_;

Expand All @@ -61,12 +61,6 @@ class CONTENT_EXPORT RenderMediaClient : public media::MediaClient {
DISALLOW_COPY_AND_ASSIGN(RenderMediaClient);
};

#if defined(UNIT_TEST)
// Helper function to access the RenderMediaClient instance. Used only by unit
// tests.
CONTENT_EXPORT RenderMediaClient* GetRenderMediaClientInstanceForTesting();
#endif

} // namespace content

#endif // CONTENT_RENDERER_MEDIA_RENDER_MEDIA_CLIENT_H_
2 changes: 1 addition & 1 deletion content/renderer/media/render_media_client_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ bool ContainsWidevine(
class RenderMediaClientTest : public testing::Test {
protected:
RenderMediaClientTest()
: render_media_client_(GetRenderMediaClientInstanceForTesting()) {
: render_media_client_(RenderMediaClient::GetInstance()) {
SetContentClient(&test_content_client_);
SetRendererClientForTesting(&test_content_renderer_client_);
}
Expand Down
17 changes: 9 additions & 8 deletions content/renderer/media/rtc_peer_connection_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <vector>

#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
Expand Down Expand Up @@ -913,8 +912,11 @@ void ConvertConstraintsToWebrtcOfferOptions(
&output->ice_restart);
}

base::LazyInstance<std::set<RTCPeerConnectionHandler*> >::Leaky
g_peer_connection_handlers = LAZY_INSTANCE_INITIALIZER;
std::set<RTCPeerConnectionHandler*>* GetPeerConnectionHandlers() {
static std::set<RTCPeerConnectionHandler*>* handlers =
new std::set<RTCPeerConnectionHandler*>();
return handlers;
}

} // namespace

Expand Down Expand Up @@ -1095,16 +1097,16 @@ RTCPeerConnectionHandler::RTCPeerConnectionHandler(
is_closed_(false),
dependency_factory_(dependency_factory),
weak_factory_(this) {
CHECK(client_),
g_peer_connection_handlers.Get().insert(this);
CHECK(client_);
GetPeerConnectionHandlers()->insert(this);
}

RTCPeerConnectionHandler::~RTCPeerConnectionHandler() {
DCHECK(thread_checker_.CalledOnValidThread());

stop();

g_peer_connection_handlers.Get().erase(this);
GetPeerConnectionHandlers()->erase(this);
if (peer_connection_tracker_)
peer_connection_tracker_->UnregisterPeerConnection(this);

Expand All @@ -1117,8 +1119,7 @@ void RTCPeerConnectionHandler::DestructAllHandlers() {
// Copy g_peer_connection_handlers since releasePeerConnectionHandler will
// remove an item.
std::set<RTCPeerConnectionHandler*> handlers(
g_peer_connection_handlers.Get().begin(),
g_peer_connection_handlers.Get().end());
GetPeerConnectionHandlers()->begin(), GetPeerConnectionHandlers()->end());
for (auto* handler : handlers)
handler->client_->releasePeerConnectionHandler();
}
Expand Down
11 changes: 6 additions & 5 deletions content/renderer/media/webrtc/rtc_stats.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <set>
#include <string>

#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/time/time.h"
#include "third_party/webrtc/api/stats/rtcstats_objects.h"
Expand Down Expand Up @@ -48,11 +47,13 @@ class RTCStatsWhitelist {
std::set<std::string> whitelisted_stats_types_;
};

base::LazyInstance<RTCStatsWhitelist>::Leaky
g_whitelisted_stats = LAZY_INSTANCE_INITIALIZER;
RTCStatsWhitelist* GetStatsWhitelist() {
static RTCStatsWhitelist* whitelist = new RTCStatsWhitelist();
return whitelist;
}

bool IsWhitelistedStats(const webrtc::RTCStats& stats) {
return g_whitelisted_stats.Get().IsWhitelisted(stats);
return GetStatsWhitelist()->IsWhitelisted(stats);
}

} // namespace
Expand Down Expand Up @@ -276,7 +277,7 @@ blink::WebVector<blink::WebString> RTCStatsMember::valueSequenceString() const {
}

void WhitelistStatsForTesting(const char* type) {
g_whitelisted_stats.Get().WhitelistStatsForTesting(type);
GetStatsWhitelist()->WhitelistStatsForTesting(type);
}

} // namespace content
19 changes: 19 additions & 0 deletions media/PRESUBMIT.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,30 @@ def _CheckPassByValue(input_api, output_api):
return []


def _CheckForUseOfLazyInstance(input_api, output_api):
"""Check that base::LazyInstance is not used."""

problems = []

lazy_instance_re = re.compile(r'(^|\W)base::LazyInstance<')

for f in input_api.AffectedSourceFiles(_FilterFile):
for line_number, line in f.ChangedContents():
if lazy_instance_re.search(line):
problems.append('%s:%d' % (f, line_number))

if problems:
return [output_api.PresubmitError(
'base::LazyInstance is deprecated; use a thread safe static.', problems)]
return []


def _CheckChange(input_api, output_api):
results = []
results.extend(_CheckForUseOfWrongClock(input_api, output_api))
results.extend(_CheckPassByValue(input_api, output_api))
results.extend(_CheckForHistogramOffByOne(input_api, output_api))
results.extend(_CheckForUseOfLazyInstance(input_api, output_api))
return results


Expand Down
Loading

0 comments on commit c3af509

Please sign in to comment.