| 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 "base/android/jni_android.h" | 5 #include "base/android/jni_android.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "content/browser/android/content_view_core_impl.h" | 8 #include "content/browser/android/content_view_core_impl.h" |
| 9 #include "content/browser/power_save_blocker_impl.h" | 9 #include "content/browser/power_save_blocker_impl.h" |
| 10 #include "content/browser/web_contents/web_contents_impl.h" | 10 #include "content/browser/web_contents/web_contents_impl.h" |
| 11 #include "content/public/browser/android/content_view_core.h" | 11 #include "content/public/browser/android/content_view_core.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/web_contents_observer.h" | 13 #include "content/public/browser/web_contents_observer.h" |
| 13 #include "jni/PowerSaveBlocker_jni.h" | 14 #include "jni/PowerSaveBlocker_jni.h" |
| 14 #include "ui/android/view_android.h" | 15 #include "ui/android/view_android.h" |
| 15 | 16 |
| 16 namespace content { | 17 namespace content { |
| 17 | 18 |
| 18 using base::android::AttachCurrentThread; | 19 using base::android::AttachCurrentThread; |
| 19 | 20 |
| 20 class PowerSaveBlockerImpl::Delegate | 21 class PowerSaveBlockerImpl::Delegate |
| 21 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate>, | 22 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate>, |
| 22 public WebContentsObserver { | 23 public WebContentsObserver { |
| 23 public: | 24 public: |
| 24 Delegate(WebContents* web_contents, | 25 explicit Delegate(WebContents* web_contents); |
| 25 scoped_refptr<base::SequencedTaskRunner> ui_task_runner); | |
| 26 | 26 |
| 27 // Does the actual work to apply or remove the desired power save block. | 27 // Does the actual work to apply or remove the desired power save block. |
| 28 void ApplyBlock(); | 28 void ApplyBlock(); |
| 29 void RemoveBlock(); | 29 void RemoveBlock(); |
| 30 | 30 |
| 31 private: | 31 private: |
| 32 friend class base::RefCountedThreadSafe<Delegate>; | 32 friend class base::RefCountedThreadSafe<Delegate>; |
| 33 ~Delegate() override; | 33 ~Delegate() override; |
| 34 | 34 |
| 35 base::android::ScopedJavaLocalRef<jobject> GetContentViewCore(); | 35 base::android::ScopedJavaLocalRef<jobject> GetContentViewCore(); |
| 36 | 36 |
| 37 base::android::ScopedJavaGlobalRef<jobject> java_power_save_blocker_; | 37 base::android::ScopedJavaGlobalRef<jobject> java_power_save_blocker_; |
| 38 | 38 |
| 39 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; | |
| 40 | |
| 41 DISALLOW_COPY_AND_ASSIGN(Delegate); | 39 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 42 }; | 40 }; |
| 43 | 41 |
| 44 PowerSaveBlockerImpl::Delegate::Delegate( | 42 PowerSaveBlockerImpl::Delegate::Delegate(WebContents* web_contents) |
| 45 WebContents* web_contents, | 43 : WebContentsObserver(web_contents) { |
| 46 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) | |
| 47 : WebContentsObserver(web_contents), ui_task_runner_(ui_task_runner) { | |
| 48 JNIEnv* env = AttachCurrentThread(); | 44 JNIEnv* env = AttachCurrentThread(); |
| 49 java_power_save_blocker_.Reset(Java_PowerSaveBlocker_create(env)); | 45 java_power_save_blocker_.Reset(Java_PowerSaveBlocker_create(env)); |
| 50 } | 46 } |
| 51 | 47 |
| 52 PowerSaveBlockerImpl::Delegate::~Delegate() { | 48 PowerSaveBlockerImpl::Delegate::~Delegate() { |
| 53 } | 49 } |
| 54 | 50 |
| 55 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { | 51 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { |
| 56 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 52 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 57 base::android::ScopedJavaLocalRef<jobject> java_content_view_core = | 53 base::android::ScopedJavaLocalRef<jobject> java_content_view_core = |
| 58 GetContentViewCore(); | 54 GetContentViewCore(); |
| 59 if (java_content_view_core.is_null()) | 55 if (java_content_view_core.is_null()) |
| 60 return; | 56 return; |
| 61 | 57 |
| 62 ScopedJavaLocalRef<jobject> obj(java_power_save_blocker_); | 58 ScopedJavaLocalRef<jobject> obj(java_power_save_blocker_); |
| 63 JNIEnv* env = AttachCurrentThread(); | 59 JNIEnv* env = AttachCurrentThread(); |
| 64 Java_PowerSaveBlocker_applyBlock(env, obj.obj(), | 60 Java_PowerSaveBlocker_applyBlock(env, obj.obj(), |
| 65 java_content_view_core.obj()); | 61 java_content_view_core.obj()); |
| 66 } | 62 } |
| 67 | 63 |
| 68 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { | 64 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { |
| 69 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 65 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 70 base::android::ScopedJavaLocalRef<jobject> java_content_view_core = | 66 base::android::ScopedJavaLocalRef<jobject> java_content_view_core = |
| 71 GetContentViewCore(); | 67 GetContentViewCore(); |
| 72 if (java_content_view_core.is_null()) | 68 if (java_content_view_core.is_null()) |
| 73 return; | 69 return; |
| 74 | 70 |
| 75 ScopedJavaLocalRef<jobject> obj(java_power_save_blocker_); | 71 ScopedJavaLocalRef<jobject> obj(java_power_save_blocker_); |
| 76 JNIEnv* env = AttachCurrentThread(); | 72 JNIEnv* env = AttachCurrentThread(); |
| 77 Java_PowerSaveBlocker_removeBlock(env, obj.obj(), | 73 Java_PowerSaveBlocker_removeBlock(env, obj.obj(), |
| 78 java_content_view_core.obj()); | 74 java_content_view_core.obj()); |
| 79 } | 75 } |
| 80 | 76 |
| 81 base::android::ScopedJavaLocalRef<jobject> | 77 base::android::ScopedJavaLocalRef<jobject> |
| 82 PowerSaveBlockerImpl::Delegate::GetContentViewCore() { | 78 PowerSaveBlockerImpl::Delegate::GetContentViewCore() { |
| 83 if (!web_contents()) | 79 if (!web_contents()) |
| 84 return base::android::ScopedJavaLocalRef<jobject>(); | 80 return base::android::ScopedJavaLocalRef<jobject>(); |
| 85 | 81 |
| 86 ContentViewCoreImpl* content_view_core_impl = | 82 ContentViewCoreImpl* content_view_core_impl = |
| 87 ContentViewCoreImpl::FromWebContents(web_contents()); | 83 ContentViewCoreImpl::FromWebContents(web_contents()); |
| 88 if (!content_view_core_impl) | 84 if (!content_view_core_impl) |
| 89 return base::android::ScopedJavaLocalRef<jobject>(); | 85 return base::android::ScopedJavaLocalRef<jobject>(); |
| 90 | 86 |
| 91 return content_view_core_impl->GetJavaObject(); | 87 return content_view_core_impl->GetJavaObject(); |
| 92 } | 88 } |
| 93 | 89 |
| 94 PowerSaveBlockerImpl::PowerSaveBlockerImpl( | 90 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, |
| 95 PowerSaveBlockerType type, | 91 Reason reason, |
| 96 Reason reason, | 92 const std::string& description) { |
| 97 const std::string& description, | |
| 98 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | |
| 99 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) | |
| 100 : ui_task_runner_(ui_task_runner), | |
| 101 blocking_task_runner_(blocking_task_runner) { | |
| 102 // Don't support kPowerSaveBlockPreventAppSuspension | 93 // Don't support kPowerSaveBlockPreventAppSuspension |
| 103 } | 94 } |
| 104 | 95 |
| 105 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | 96 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
| 106 if (delegate_.get()) { | 97 if (delegate_.get()) { |
| 107 ui_task_runner_->PostTask(FROM_HERE, | 98 BrowserThread::PostTask( |
| 108 base::Bind(&Delegate::RemoveBlock, delegate_)); | 99 BrowserThread::UI, FROM_HERE, |
| 100 base::Bind(&Delegate::RemoveBlock, delegate_)); |
| 109 } | 101 } |
| 110 } | 102 } |
| 111 | 103 |
| 112 void PowerSaveBlockerImpl::InitDisplaySleepBlocker(WebContents* web_contents) { | 104 void PowerSaveBlockerImpl::InitDisplaySleepBlocker(WebContents* web_contents) { |
| 113 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 105 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 114 if (!web_contents) | 106 if (!web_contents) |
| 115 return; | 107 return; |
| 116 | 108 |
| 117 delegate_ = new Delegate(web_contents, ui_task_runner_); | 109 delegate_ = new Delegate(web_contents); |
| 118 delegate_->ApplyBlock(); | 110 delegate_->ApplyBlock(); |
| 119 } | 111 } |
| 120 | 112 |
| 121 bool RegisterPowerSaveBlocker(JNIEnv* env) { | 113 bool RegisterPowerSaveBlocker(JNIEnv* env) { |
| 122 return RegisterNativesImpl(env); | 114 return RegisterNativesImpl(env); |
| 123 } | 115 } |
| 124 | 116 |
| 125 } // namespace content | 117 } // namespace content |
| OLD | NEW |