OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "base/android/jni_android.h" | |
6 #include "base/bind.h" | |
7 #include "base/location.h" | |
8 #include "base/logging.h" | |
9 #include "base/macros.h" | |
10 #include "device/power_save_blocker/power_save_blocker_impl.h" | |
11 #include "jni/PowerSaveBlocker_jni.h" | |
12 #include "ui/android/view_android.h" | |
13 | |
14 namespace device { | |
15 | |
16 using base::android::AttachCurrentThread; | |
17 | |
18 class PowerSaveBlockerImpl::Delegate | |
19 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { | |
20 public: | |
21 Delegate(base::WeakPtr<ui::ViewAndroid> view_android, | |
22 scoped_refptr<base::SequencedTaskRunner> ui_task_runner); | |
23 | |
24 // Does the actual work to apply or remove the desired power save block. | |
25 void ApplyBlock(); | |
26 void RemoveBlock(); | |
27 | |
28 private: | |
29 friend class base::RefCountedThreadSafe<Delegate>; | |
30 ~Delegate(); | |
31 | |
32 base::WeakPtr<ui::ViewAndroid> view_android_; | |
33 | |
34 base::android::ScopedJavaGlobalRef<jobject> java_power_save_blocker_; | |
35 | |
36 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
39 }; | |
40 | |
41 PowerSaveBlockerImpl::Delegate::Delegate( | |
42 base::WeakPtr<ui::ViewAndroid> view_android, | |
43 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) | |
44 : view_android_(view_android), ui_task_runner_(ui_task_runner) { | |
45 JNIEnv* env = AttachCurrentThread(); | |
46 java_power_save_blocker_.Reset(Java_PowerSaveBlocker_create(env)); | |
47 } | |
48 | |
49 PowerSaveBlockerImpl::Delegate::~Delegate() {} | |
50 | |
51 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { | |
52 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
53 | |
54 ScopedJavaLocalRef<jobject> obj(java_power_save_blocker_); | |
55 JNIEnv* env = AttachCurrentThread(); | |
56 if (view_android_) { | |
57 Java_PowerSaveBlocker_applyBlock( | |
58 env, obj.obj(), view_android_->GetViewAndroidDelegate().obj()); | |
59 } | |
60 } | |
61 | |
62 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { | |
63 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
64 ScopedJavaLocalRef<jobject> obj(java_power_save_blocker_); | |
65 JNIEnv* env = AttachCurrentThread(); | |
66 if (view_android_) { | |
67 Java_PowerSaveBlocker_removeBlock( | |
68 env, obj.obj(), view_android_->GetViewAndroidDelegate().obj()); | |
69 } | |
70 } | |
71 | |
72 PowerSaveBlockerImpl::PowerSaveBlockerImpl( | |
73 PowerSaveBlockerType type, | |
74 Reason reason, | |
75 const std::string& description, | |
76 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | |
77 scoped_refptr<base::SingleThreadTaskRunner> blocking_task_runner) | |
78 : ui_task_runner_(ui_task_runner), | |
79 blocking_task_runner_(blocking_task_runner) { | |
80 // Don't support kPowerSaveBlockPreventAppSuspension | |
81 } | |
82 | |
83 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | |
84 if (delegate_.get()) { | |
85 ui_task_runner_->PostTask(FROM_HERE, | |
86 base::Bind(&Delegate::RemoveBlock, delegate_)); | |
87 } | |
88 } | |
89 | |
90 void PowerSaveBlockerImpl::InitDisplaySleepBlocker( | |
91 const base::WeakPtr<ui::ViewAndroid>& view_android) { | |
92 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
93 if (!view_android) | |
94 return; | |
95 | |
96 delegate_ = new Delegate(view_android, ui_task_runner_); | |
97 delegate_->ApplyBlock(); | |
98 } | |
99 | |
100 bool RegisterPowerSaveBlocker(JNIEnv* env) { | |
101 return RegisterNativesImpl(env); | |
102 } | |
103 | |
104 } // namespace device | |
OLD | NEW |