Chromium Code Reviews| Index: content/browser/power_save_blocker_android.cc |
| diff --git a/content/browser/power_save_blocker_android.cc b/content/browser/power_save_blocker_android.cc |
| index 1e91625e19c295a5544f1adfe7f0cffdbac33b16..2bd0c509904741ff855b82d4447ea4b0903d2b74 100644 |
| --- a/content/browser/power_save_blocker_android.cc |
| +++ b/content/browser/power_save_blocker_android.cc |
| @@ -2,30 +2,22 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "content/browser/power_save_blocker_android.h" |
| - |
| -#include "base/android/jni_android.h" |
| -#include "base/android/jni_weak_ref.h" |
| #include "base/logging.h" |
| +#include "content/browser/android/content_view_core_impl.h" |
| #include "content/browser/power_save_blocker_impl.h" |
| +#include "content/browser/web_contents/web_contents_impl.h" |
| #include "content/public/browser/android/content_view_core.h" |
| #include "content/public/browser/browser_thread.h" |
| -#include "jni/PowerSaveBlocker_jni.h" |
| +#include "content/public/browser/web_contents_observer.h" |
| #include "ui/android/view_android.h" |
| -using base::android::AttachCurrentThread; |
| -using base::android::ScopedJavaLocalRef; |
| -using gfx::NativeView; |
| - |
| namespace content { |
| class PowerSaveBlockerImpl::Delegate |
| - : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { |
| + : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate>, |
| + public WebContentsObserver { |
| public: |
| - explicit Delegate(NativeView view_android) { |
| - j_view_android_ = JavaObjectWeakGlobalRef( |
| - AttachCurrentThread(), view_android->GetJavaObject().obj()); |
| - } |
| + explicit Delegate(WebContents* web_contents); |
| // Does the actual work to apply or remove the desired power save block. |
| void ApplyBlock(); |
| @@ -33,27 +25,40 @@ class PowerSaveBlockerImpl::Delegate |
| private: |
| friend class base::RefCountedThreadSafe<Delegate>; |
| - ~Delegate() {} |
| + ~Delegate() override; |
| - JavaObjectWeakGlobalRef j_view_android_; |
| + ContentViewCoreImpl* GetContentViewCore(); |
| DISALLOW_COPY_AND_ASSIGN(Delegate); |
| }; |
| +PowerSaveBlockerImpl::Delegate::Delegate(WebContents* web_contents) |
| + : WebContentsObserver(web_contents) { |
| +} |
| + |
| +PowerSaveBlockerImpl::Delegate::~Delegate() { |
| +} |
| + |
| void PowerSaveBlockerImpl::Delegate::ApplyBlock() { |
| DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| - JNIEnv* env = AttachCurrentThread(); |
| - ScopedJavaLocalRef<jobject> j_object = j_view_android_.get(env); |
| - if (j_object.obj()) |
| - Java_PowerSaveBlocker_applyBlock(env, j_object.obj()); |
| + ContentViewCoreImpl* content_view_core_impl = GetContentViewCore(); |
| + if (!content_view_core_impl) |
| + return; |
| + content_view_core_impl->IncrementKeepScreenOnCount(); |
| } |
| void PowerSaveBlockerImpl::Delegate::RemoveBlock() { |
| DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| - JNIEnv* env = AttachCurrentThread(); |
| - ScopedJavaLocalRef<jobject> j_object = j_view_android_.get(env); |
| - if (j_object.obj()) |
| - Java_PowerSaveBlocker_removeBlock(env, j_object.obj()); |
| + ContentViewCoreImpl* content_view_core_impl = GetContentViewCore(); |
| + if (!content_view_core_impl) |
| + return; |
| + content_view_core_impl->DecrementKeepScreenOnCount(); |
| +} |
| + |
| +ContentViewCoreImpl* PowerSaveBlockerImpl::Delegate::GetContentViewCore() { |
| + 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
|
| + return nullptr; |
| + return ContentViewCoreImpl::FromWebContents(web_contents()); |
| } |
| PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, |
| @@ -70,19 +75,13 @@ PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
| } |
| } |
| -void PowerSaveBlockerImpl::InitDisplaySleepBlocker(NativeView view_android) { |
| - if (!view_android) |
| +void PowerSaveBlockerImpl::InitDisplaySleepBlocker(WebContents* web_contents) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + if (!web_contents) |
| return; |
| - delegate_ = new Delegate(view_android); |
| - // This may be called on any thread. |
| - BrowserThread::PostTask( |
| - BrowserThread::UI, FROM_HERE, |
| - base::Bind(&Delegate::ApplyBlock, delegate_)); |
| -} |
| - |
| -bool RegisterPowerSaveBlocker(JNIEnv* env) { |
| - return RegisterNativesImpl(env); |
| + delegate_ = new Delegate(web_contents); |
| + delegate_->ApplyBlock(); |
| } |
| } // namespace content |