Add ICU message format support
[chromium-blink-merge.git] / content / browser / power_save_blocker_android.cc
blob20ea63ccc58e1c53a10bc036aff8f9008a3e50cc
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 "base/android/jni_android.h"
6 #include "base/logging.h"
7 #include "content/browser/android/content_view_core_impl.h"
8 #include "content/browser/power_save_blocker_impl.h"
9 #include "content/browser/web_contents/web_contents_impl.h"
10 #include "content/public/browser/android/content_view_core.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "jni/PowerSaveBlocker_jni.h"
14 #include "ui/android/view_android.h"
16 namespace content {
18 using base::android::AttachCurrentThread;
20 class PowerSaveBlockerImpl::Delegate
21 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate>,
22 public WebContentsObserver {
23 public:
24 explicit Delegate(WebContents* web_contents);
26 // Does the actual work to apply or remove the desired power save block.
27 void ApplyBlock();
28 void RemoveBlock();
30 private:
31 friend class base::RefCountedThreadSafe<Delegate>;
32 ~Delegate() override;
34 base::android::ScopedJavaLocalRef<jobject> GetContentViewCore();
36 base::android::ScopedJavaGlobalRef<jobject> java_power_save_blocker_;
38 DISALLOW_COPY_AND_ASSIGN(Delegate);
41 PowerSaveBlockerImpl::Delegate::Delegate(WebContents* web_contents)
42 : WebContentsObserver(web_contents) {
43 JNIEnv* env = AttachCurrentThread();
44 java_power_save_blocker_.Reset(Java_PowerSaveBlocker_create(env));
47 PowerSaveBlockerImpl::Delegate::~Delegate() {
50 void PowerSaveBlockerImpl::Delegate::ApplyBlock() {
51 DCHECK_CURRENTLY_ON(BrowserThread::UI);
52 base::android::ScopedJavaLocalRef<jobject> java_content_view_core =
53 GetContentViewCore();
54 if (java_content_view_core.is_null())
55 return;
57 ScopedJavaLocalRef<jobject> obj(java_power_save_blocker_);
58 JNIEnv* env = AttachCurrentThread();
59 Java_PowerSaveBlocker_applyBlock(env, obj.obj(),
60 java_content_view_core.obj());
63 void PowerSaveBlockerImpl::Delegate::RemoveBlock() {
64 DCHECK_CURRENTLY_ON(BrowserThread::UI);
65 base::android::ScopedJavaLocalRef<jobject> java_content_view_core =
66 GetContentViewCore();
67 if (java_content_view_core.is_null())
68 return;
70 ScopedJavaLocalRef<jobject> obj(java_power_save_blocker_);
71 JNIEnv* env = AttachCurrentThread();
72 Java_PowerSaveBlocker_removeBlock(env, obj.obj(),
73 java_content_view_core.obj());
76 base::android::ScopedJavaLocalRef<jobject>
77 PowerSaveBlockerImpl::Delegate::GetContentViewCore() {
78 if (!web_contents())
79 return base::android::ScopedJavaLocalRef<jobject>();
81 ContentViewCoreImpl* content_view_core_impl =
82 ContentViewCoreImpl::FromWebContents(web_contents());
83 if (!content_view_core_impl)
84 return base::android::ScopedJavaLocalRef<jobject>();
86 return content_view_core_impl->GetJavaObject();
89 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type,
90 Reason reason,
91 const std::string& description) {
92 // Don't support kPowerSaveBlockPreventAppSuspension
95 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() {
96 if (delegate_.get()) {
97 BrowserThread::PostTask(
98 BrowserThread::UI, FROM_HERE,
99 base::Bind(&Delegate::RemoveBlock, delegate_));
103 void PowerSaveBlockerImpl::InitDisplaySleepBlocker(WebContents* web_contents) {
104 DCHECK_CURRENTLY_ON(BrowserThread::UI);
105 if (!web_contents)
106 return;
108 delegate_ = new Delegate(web_contents);
109 delegate_->ApplyBlock();
112 bool RegisterPowerSaveBlocker(JNIEnv* env) {
113 return RegisterNativesImpl(env);
116 } // namespace content