OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "content/browser/wake_lock/wake_lock_dispatcher_host.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "content/browser/power_save_blocker_impl.h" |
| 9 #include "content/browser/web_contents/web_contents_impl.h" |
| 10 #include "content/browser/web_contents/web_contents_view.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/power_save_blocker.h" |
| 13 #include "content/public/browser/render_frame_host.h" |
| 14 #include "content/public/common/service_registry.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 WakeLockDispatcherHost::WakeLockDispatcherHost(WebContents* web_contents) |
| 19 : WebContentsObserver(web_contents), |
| 20 weak_factory_(this) { |
| 21 } |
| 22 |
| 23 WakeLockDispatcherHost::~WakeLockDispatcherHost() { |
| 24 } |
| 25 |
| 26 void WakeLockDispatcherHost::RenderFrameCreated( |
| 27 RenderFrameHost* render_frame_host) { |
| 28 if (auto registry = render_frame_host->GetServiceRegistry()) { |
| 29 registry->AddService<WakeLockService>( |
| 30 base::Bind(&WakeLockServiceImpl::CreateService, |
| 31 weak_factory_.GetWeakPtr(), |
| 32 base::Unretained(render_frame_host))); |
| 33 } |
| 34 } |
| 35 |
| 36 void WakeLockDispatcherHost::RenderFrameDeleted( |
| 37 RenderFrameHost* render_frame_host) { |
| 38 wake_lock_state_.RemoveFrame(render_frame_host); |
| 39 UpdateBlocker(); |
| 40 } |
| 41 |
| 42 void WakeLockDispatcherHost::RenderFrameHostChanged( |
| 43 RenderFrameHost* old_host, |
| 44 RenderFrameHost* new_host) { |
| 45 wake_lock_state_.RemoveFrame(old_host); |
| 46 UpdateBlocker(); |
| 47 } |
| 48 |
| 49 void WakeLockDispatcherHost::RequestWakeLock( |
| 50 RenderFrameHost* render_frame_host) { |
| 51 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 52 wake_lock_state_.AddFrame(render_frame_host); |
| 53 UpdateBlocker(); |
| 54 } |
| 55 |
| 56 void WakeLockDispatcherHost::CancelWakeLock( |
| 57 RenderFrameHost* render_frame_host) { |
| 58 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 59 wake_lock_state_.RemoveFrame(render_frame_host); |
| 60 UpdateBlocker(); |
| 61 } |
| 62 |
| 63 void WakeLockDispatcherHost::CreateBlocker() { |
| 64 DCHECK(!blocker_); |
| 65 blocker_ = PowerSaveBlocker::Create( |
| 66 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, |
| 67 PowerSaveBlocker::kReasonOther, |
| 68 "WakeLock API"); |
| 69 // On Android, additionaly associate the blocker with this WebContents' |
| 70 // NativeView. |
| 71 #if defined(OS_ANDROID) |
| 72 WebContentsImpl* web_contents_impl = |
| 73 static_cast<WebContentsImpl*>(web_contents()); |
| 74 DCHECK(web_contents_impl); |
| 75 static_cast<PowerSaveBlockerImpl*>(blocker_.get())->InitDisplaySleepBlocker( |
| 76 web_contents_impl->GetView()->GetNativeView()); |
| 77 #endif |
| 78 } |
| 79 |
| 80 void WakeLockDispatcherHost::RemoveBlocker() { |
| 81 blocker_.reset(); |
| 82 } |
| 83 |
| 84 void WakeLockDispatcherHost::UpdateBlocker() { |
| 85 if (wake_lock_state_.GetLockState()) { |
| 86 if (!blocker_) { |
| 87 CreateBlocker(); |
| 88 } |
| 89 } else { |
| 90 RemoveBlocker(); |
| 91 } |
| 92 NotifyUpdate(); |
| 93 } |
| 94 |
| 95 void WakeLockDispatcherHost::NotifyUpdate() { |
| 96 FOR_EACH_OBSERVER(Observer, observer_list_, OnUpdate()); |
| 97 } |
| 98 |
| 99 } // namespace content |
OLD | NEW |