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 "content/browser/power_save_blocker_impl.h" |
| 11 #include "jni/PowerSaveBlocker_jni.h" |
| 12 #include "ui/android/view_android.h" |
| 13 |
| 14 namespace content { |
| 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 |
| 52 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { |
| 53 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 54 |
| 55 ScopedJavaLocalRef<jobject> obj(java_power_save_blocker_); |
| 56 JNIEnv* env = AttachCurrentThread(); |
| 57 if (view_android_) { |
| 58 Java_PowerSaveBlocker_applyBlock( |
| 59 env, obj.obj(), view_android_->GetViewAndroidDelegate().obj()); |
| 60 } |
| 61 } |
| 62 |
| 63 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { |
| 64 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 65 ScopedJavaLocalRef<jobject> obj(java_power_save_blocker_); |
| 66 JNIEnv* env = AttachCurrentThread(); |
| 67 if (view_android_) { |
| 68 Java_PowerSaveBlocker_removeBlock( |
| 69 env, obj.obj(), view_android_->GetViewAndroidDelegate().obj()); |
| 70 } |
| 71 } |
| 72 |
| 73 PowerSaveBlockerImpl::PowerSaveBlockerImpl( |
| 74 PowerSaveBlockerType type, |
| 75 Reason reason, |
| 76 const std::string& description, |
| 77 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 78 scoped_refptr<base::SingleThreadTaskRunner> blocking_task_runner) |
| 79 : ui_task_runner_(ui_task_runner), |
| 80 blocking_task_runner_(blocking_task_runner) { |
| 81 // Don't support kPowerSaveBlockPreventAppSuspension |
| 82 } |
| 83 |
| 84 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
| 85 if (delegate_.get()) { |
| 86 ui_task_runner_->PostTask(FROM_HERE, |
| 87 base::Bind(&Delegate::RemoveBlock, delegate_)); |
| 88 } |
| 89 } |
| 90 |
| 91 void PowerSaveBlockerImpl::InitDisplaySleepBlocker( |
| 92 const base::WeakPtr<ui::ViewAndroid>& view_android) { |
| 93 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 94 if (!view_android) |
| 95 return; |
| 96 |
| 97 delegate_ = new Delegate(view_android, ui_task_runner_); |
| 98 delegate_->ApplyBlock(); |
| 99 } |
| 100 |
| 101 bool RegisterPowerSaveBlocker(JNIEnv* env) { |
| 102 return RegisterNativesImpl(env); |
| 103 } |
| 104 |
| 105 } // namespace content |
OLD | NEW |