Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(427)

Side by Side Diff: content/browser/wake_lock/wake_lock_service_context.cc

Issue 1393203004: Reland of Wake Lock API implementation (Chromium part) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix presubmit errors Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_service_context.h"
6
7 #include "base/bind.h"
8 #include "content/browser/power_save_blocker_impl.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/power_save_blocker.h"
11 #include "content/public/browser/render_frame_host.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/common/service_registry.h"
14
15 namespace content {
16
17 WakeLockServiceContext::WakeLockServiceContext(WebContents* web_contents)
18 : WebContentsObserver(web_contents), weak_factory_(this) {}
19
20 WakeLockServiceContext::~WakeLockServiceContext() {}
21
22 void WakeLockServiceContext::CreateService(
23 int render_process_id,
24 int render_frame_id,
25 mojo::InterfaceRequest<WakeLockService> request) {
26 new WakeLockServiceImpl(weak_factory_.GetWeakPtr(), render_process_id,
27 render_frame_id, request.Pass());
28 }
29
30 void WakeLockServiceContext::RenderFrameDeleted(
31 RenderFrameHost* render_frame_host) {
32 CancelWakeLock(render_frame_host->GetProcess()->GetID(),
33 render_frame_host->GetRoutingID());
34 }
35
36 void WakeLockServiceContext::RequestWakeLock(int render_process_id,
37 int render_frame_id) {
38 DCHECK_CURRENTLY_ON(BrowserThread::UI);
39 if (!RenderFrameHost::FromID(render_process_id, render_frame_id))
40 return;
41
42 frames_requesting_lock_.insert(
43 std::pair<int, int>(render_process_id, render_frame_id));
44 UpdateWakeLock();
45 }
46
47 void WakeLockServiceContext::CancelWakeLock(int render_process_id,
48 int render_frame_id) {
49 DCHECK_CURRENTLY_ON(BrowserThread::UI);
50 frames_requesting_lock_.erase(
51 std::pair<int, int>(render_process_id, render_frame_id));
52 UpdateWakeLock();
53 }
54
55 bool WakeLockServiceContext::HasWakeLockForTests() const {
56 return wake_lock_;
57 }
58
59 void WakeLockServiceContext::CreateWakeLock() {
60 DCHECK(!wake_lock_);
61 wake_lock_ = PowerSaveBlocker::Create(
62 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep,
63 PowerSaveBlocker::kReasonOther, "Wake Lock API");
64
65 #if defined(OS_ANDROID)
66 // On Android, additionaly associate the blocker with this WebContents.
67 DCHECK(web_contents());
68
69 static_cast<PowerSaveBlockerImpl*>(wake_lock_.get())
70 ->InitDisplaySleepBlocker(web_contents());
71 #endif
72 }
73
74 void WakeLockServiceContext::RemoveWakeLock() {
75 DCHECK(wake_lock_);
76 wake_lock_.reset();
77 }
78
79 void WakeLockServiceContext::UpdateWakeLock() {
80 if (!frames_requesting_lock_.empty()) {
81 if (!wake_lock_)
82 CreateWakeLock();
83 } else {
84 if (wake_lock_)
85 RemoveWakeLock();
86 }
87 }
88
89 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/wake_lock/wake_lock_service_context.h ('k') | content/browser/wake_lock/wake_lock_service_context_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698