1 // Copyright 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "platform/impl/scoped_wake_lock_linux.h" 6 7 #include "platform/api/task_runner.h" 8 #include "platform/impl/platform_client_posix.h" 9 #include "util/osp_logging.h" 10 11 namespace openscreen { 12 13 int ScopedWakeLockLinux::reference_count_ = 0; 14 Create(TaskRunner * task_runner)15SerialDeletePtr<ScopedWakeLock> ScopedWakeLock::Create( 16 TaskRunner* task_runner) { 17 return SerialDeletePtr<ScopedWakeLock>(task_runner, 18 new ScopedWakeLockLinux()); 19 } 20 21 namespace { 22 GetTaskRunner()23TaskRunner* GetTaskRunner() { 24 auto* const platform_client = PlatformClientPosix::GetInstance(); 25 OSP_DCHECK(platform_client); 26 auto* const task_runner = platform_client->GetTaskRunner(); 27 OSP_DCHECK(task_runner); 28 return task_runner; 29 } 30 31 } // namespace 32 ScopedWakeLockLinux()33ScopedWakeLockLinux::ScopedWakeLockLinux() : ScopedWakeLock() { 34 OSP_DCHECK(GetTaskRunner()->IsRunningOnTaskRunner()); 35 if (reference_count_++ == 0) { 36 AcquireWakeLock(); 37 } 38 } 39 ~ScopedWakeLockLinux()40ScopedWakeLockLinux::~ScopedWakeLockLinux() { 41 GetTaskRunner()->PostTask([] { 42 if (--reference_count_ == 0) { 43 ReleaseWakeLock(); 44 } 45 }); 46 } 47 48 // static AcquireWakeLock()49void ScopedWakeLockLinux::AcquireWakeLock() { 50 OSP_VLOG << "Acquired wake lock: currently a noop"; 51 } 52 53 // static ReleaseWakeLock()54void ScopedWakeLockLinux::ReleaseWakeLock() { 55 OSP_VLOG << "Released wake lock: currently a noop"; 56 } 57 58 } // namespace openscreen 59