Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/power_save_blocker_android.h" | |
| 6 | |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/android/jni_weak_ref.h" | |
| 9 #include "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "content/browser/android/content_view_core_impl.h" | |
| 10 #include "content/browser/power_save_blocker_impl.h" | 7 #include "content/browser/power_save_blocker_impl.h" |
| 8 #include "content/browser/web_contents/web_contents_impl.h" | |
| 11 #include "content/public/browser/android/content_view_core.h" | 9 #include "content/public/browser/android/content_view_core.h" |
| 12 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 13 #include "jni/PowerSaveBlocker_jni.h" | 11 #include "content/public/browser/web_contents_observer.h" |
| 14 #include "ui/android/view_android.h" | 12 #include "ui/android/view_android.h" |
| 15 | 13 |
| 16 using base::android::AttachCurrentThread; | |
| 17 using base::android::ScopedJavaLocalRef; | |
| 18 using gfx::NativeView; | |
| 19 | |
| 20 namespace content { | 14 namespace content { |
| 21 | 15 |
| 22 class PowerSaveBlockerImpl::Delegate | 16 class PowerSaveBlockerImpl::Delegate |
| 23 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { | 17 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate>, |
| 18 public WebContentsObserver { | |
| 24 public: | 19 public: |
| 25 explicit Delegate(NativeView view_android) { | 20 explicit Delegate(WebContents* web_contents); |
| 26 j_view_android_ = JavaObjectWeakGlobalRef( | |
| 27 AttachCurrentThread(), view_android->GetJavaObject().obj()); | |
| 28 } | |
| 29 | 21 |
| 30 // Does the actual work to apply or remove the desired power save block. | 22 // Does the actual work to apply or remove the desired power save block. |
| 31 void ApplyBlock(); | 23 void ApplyBlock(); |
| 32 void RemoveBlock(); | 24 void RemoveBlock(); |
| 33 | 25 |
| 34 private: | 26 private: |
| 35 friend class base::RefCountedThreadSafe<Delegate>; | 27 friend class base::RefCountedThreadSafe<Delegate>; |
| 36 ~Delegate() {} | 28 ~Delegate() override; |
| 37 | 29 |
| 38 JavaObjectWeakGlobalRef j_view_android_; | 30 ContentViewCoreImpl* GetContentViewCore(); |
| 39 | 31 |
| 40 DISALLOW_COPY_AND_ASSIGN(Delegate); | 32 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 41 }; | 33 }; |
| 42 | 34 |
| 35 PowerSaveBlockerImpl::Delegate::Delegate(WebContents* web_contents) | |
| 36 : WebContentsObserver(web_contents) { | |
| 37 } | |
| 38 | |
| 39 PowerSaveBlockerImpl::Delegate::~Delegate() { | |
| 40 } | |
| 41 | |
| 43 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { | 42 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { |
| 44 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 43 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 45 JNIEnv* env = AttachCurrentThread(); | 44 ContentViewCoreImpl* content_view_core_impl = GetContentViewCore(); |
| 46 ScopedJavaLocalRef<jobject> j_object = j_view_android_.get(env); | 45 if (!content_view_core_impl) |
| 47 if (j_object.obj()) | 46 return; |
| 48 Java_PowerSaveBlocker_applyBlock(env, j_object.obj()); | 47 content_view_core_impl->IncrementKeepScreenOnCount(); |
| 49 } | 48 } |
| 50 | 49 |
| 51 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { | 50 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { |
| 52 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 51 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 53 JNIEnv* env = AttachCurrentThread(); | 52 ContentViewCoreImpl* content_view_core_impl = GetContentViewCore(); |
| 54 ScopedJavaLocalRef<jobject> j_object = j_view_android_.get(env); | 53 if (!content_view_core_impl) |
| 55 if (j_object.obj()) | 54 return; |
| 56 Java_PowerSaveBlocker_removeBlock(env, j_object.obj()); | 55 content_view_core_impl->DecrementKeepScreenOnCount(); |
| 56 } | |
| 57 | |
| 58 ContentViewCoreImpl* PowerSaveBlockerImpl::Delegate::GetContentViewCore() { | |
| 59 if (!web_contents()) | |
|
Ted C
2015/04/06 20:56:11
it took me a second to see why this needed to be a
boliu
2015/04/06 21:04:06
No in general, but yes in practice on android.
Po
| |
| 60 return nullptr; | |
| 61 return ContentViewCoreImpl::FromWebContents(web_contents()); | |
| 57 } | 62 } |
| 58 | 63 |
| 59 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, | 64 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, |
| 60 Reason reason, | 65 Reason reason, |
| 61 const std::string& description) { | 66 const std::string& description) { |
| 62 // Don't support kPowerSaveBlockPreventAppSuspension | 67 // Don't support kPowerSaveBlockPreventAppSuspension |
| 63 } | 68 } |
| 64 | 69 |
| 65 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | 70 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
| 66 if (delegate_.get()) { | 71 if (delegate_.get()) { |
| 67 BrowserThread::PostTask( | 72 BrowserThread::PostTask( |
| 68 BrowserThread::UI, FROM_HERE, | 73 BrowserThread::UI, FROM_HERE, |
| 69 base::Bind(&Delegate::RemoveBlock, delegate_)); | 74 base::Bind(&Delegate::RemoveBlock, delegate_)); |
| 70 } | 75 } |
| 71 } | 76 } |
| 72 | 77 |
| 73 void PowerSaveBlockerImpl::InitDisplaySleepBlocker(NativeView view_android) { | 78 void PowerSaveBlockerImpl::InitDisplaySleepBlocker(WebContents* web_contents) { |
| 74 if (!view_android) | 79 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 80 if (!web_contents) | |
| 75 return; | 81 return; |
| 76 | 82 |
| 77 delegate_ = new Delegate(view_android); | 83 delegate_ = new Delegate(web_contents); |
| 78 // This may be called on any thread. | 84 delegate_->ApplyBlock(); |
| 79 BrowserThread::PostTask( | |
| 80 BrowserThread::UI, FROM_HERE, | |
| 81 base::Bind(&Delegate::ApplyBlock, delegate_)); | |
| 82 } | |
| 83 | |
| 84 bool RegisterPowerSaveBlocker(JNIEnv* env) { | |
| 85 return RegisterNativesImpl(env); | |
| 86 } | 85 } |
| 87 | 86 |
| 88 } // namespace content | 87 } // namespace content |
| OLD | NEW |