Fix layout of Autofill suggestions in RTL.
[chromium-blink-merge.git] / content / browser / wake_lock / wake_lock_service_context.cc
blob6b9a36daf9486dd2f76d95548ddd5feeef8b90da
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.
5 #include "content/browser/wake_lock/wake_lock_service_context.h"
7 #include "base/bind.h"
8 #include "content/browser/frame_host/frame_tree_node.h"
9 #include "content/browser/frame_host/render_frame_host_impl.h"
10 #include "content/browser/power_save_blocker_impl.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/power_save_blocker.h"
13 #include "content/public/common/service_registry.h"
15 namespace content {
17 WakeLockServiceContext::WakeLockServiceContext(WebContents* web_contents)
18 : WebContentsObserver(web_contents),
19 weak_factory_(this) {
22 WakeLockServiceContext::~WakeLockServiceContext() {
25 void WakeLockServiceContext::CreateService(
26 int frame_id,
27 mojo::InterfaceRequest<WakeLockService> request) {
28 new WakeLockServiceImpl(weak_factory_.GetWeakPtr(),
29 frame_id,
30 request.Pass());
33 void WakeLockServiceContext::RenderFrameDeleted(
34 RenderFrameHost* render_frame_host) {
35 RenderFrameHostImpl* rfh_impl =
36 static_cast<RenderFrameHostImpl*>(render_frame_host);
37 CancelWakeLock(rfh_impl->frame_tree_node()->frame_tree_node_id());
40 void WakeLockServiceContext::RequestWakeLock(int frame_id) {
41 DCHECK_CURRENTLY_ON(BrowserThread::UI);
42 frames_requesting_lock_.insert(frame_id);
43 UpdateWakeLock();
46 void WakeLockServiceContext::CancelWakeLock(int frame_id) {
47 DCHECK_CURRENTLY_ON(BrowserThread::UI);
48 frames_requesting_lock_.erase(frame_id);
49 UpdateWakeLock();
52 bool WakeLockServiceContext::HasWakeLockForTests() const {
53 return wake_lock_;
56 void WakeLockServiceContext::CreateWakeLock() {
57 DCHECK(!wake_lock_);
58 wake_lock_ = PowerSaveBlocker::Create(
59 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep,
60 PowerSaveBlocker::kReasonOther,
61 "Wake Lock API");
63 // On Android, additionaly associate the blocker with this WebContents.
64 #if defined(OS_ANDROID)
65 DCHECK(web_contents());
67 static_cast<PowerSaveBlockerImpl*>(wake_lock_.get())->InitDisplaySleepBlocker(
68 web_contents());
69 #endif
72 void WakeLockServiceContext::RemoveWakeLock() {
73 DCHECK(wake_lock_);
74 wake_lock_.reset();
77 void WakeLockServiceContext::UpdateWakeLock() {
78 if (!frames_requesting_lock_.empty()) {
79 if (!wake_lock_)
80 CreateWakeLock();
81 } else {
82 if (wake_lock_)
83 RemoveWakeLock();
87 } // namespace content