Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / power_save_blocker_android.cc
blob1e91625e19c295a5544f1adfe7f0cffdbac33b16
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.
5 #include "content/browser/power_save_blocker_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_weak_ref.h"
9 #include "base/logging.h"
10 #include "content/browser/power_save_blocker_impl.h"
11 #include "content/public/browser/android/content_view_core.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "jni/PowerSaveBlocker_jni.h"
14 #include "ui/android/view_android.h"
16 using base::android::AttachCurrentThread;
17 using base::android::ScopedJavaLocalRef;
18 using gfx::NativeView;
20 namespace content {
22 class PowerSaveBlockerImpl::Delegate
23 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> {
24 public:
25 explicit Delegate(NativeView view_android) {
26 j_view_android_ = JavaObjectWeakGlobalRef(
27 AttachCurrentThread(), view_android->GetJavaObject().obj());
30 // Does the actual work to apply or remove the desired power save block.
31 void ApplyBlock();
32 void RemoveBlock();
34 private:
35 friend class base::RefCountedThreadSafe<Delegate>;
36 ~Delegate() {}
38 JavaObjectWeakGlobalRef j_view_android_;
40 DISALLOW_COPY_AND_ASSIGN(Delegate);
43 void PowerSaveBlockerImpl::Delegate::ApplyBlock() {
44 DCHECK_CURRENTLY_ON(BrowserThread::UI);
45 JNIEnv* env = AttachCurrentThread();
46 ScopedJavaLocalRef<jobject> j_object = j_view_android_.get(env);
47 if (j_object.obj())
48 Java_PowerSaveBlocker_applyBlock(env, j_object.obj());
51 void PowerSaveBlockerImpl::Delegate::RemoveBlock() {
52 DCHECK_CURRENTLY_ON(BrowserThread::UI);
53 JNIEnv* env = AttachCurrentThread();
54 ScopedJavaLocalRef<jobject> j_object = j_view_android_.get(env);
55 if (j_object.obj())
56 Java_PowerSaveBlocker_removeBlock(env, j_object.obj());
59 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type,
60 Reason reason,
61 const std::string& description) {
62 // Don't support kPowerSaveBlockPreventAppSuspension
65 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() {
66 if (delegate_.get()) {
67 BrowserThread::PostTask(
68 BrowserThread::UI, FROM_HERE,
69 base::Bind(&Delegate::RemoveBlock, delegate_));
73 void PowerSaveBlockerImpl::InitDisplaySleepBlocker(NativeView view_android) {
74 if (!view_android)
75 return;
77 delegate_ = new Delegate(view_android);
78 // This may be called on any thread.
79 BrowserThread::PostTask(
80 BrowserThread::UI, FROM_HERE,
81 base::Bind(&Delegate::ApplyBlock, delegate_));
84 bool RegisterPowerSaveBlocker(JNIEnv* env) {
85 return RegisterNativesImpl(env);
88 } // namespace content